# 文件名: dify-core.yaml
# 命名空间: beidian (请提前创建: kubectl create ns beidian)
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: dify-config
  namespace: beidian
data:
  # 数据库 (MySQL)
  DB_TYPE: "mysql"
  DB_USERNAME: "zskj_admin"
  DB_PASSWORD: "Zskj@163501"
  DB_HOST: "rm-bp101jv1148wwp8m2.mysql.rds.aliyuncs.com"
  DB_PORT: "3306"
  DB_DATABASE: "beidian"
  # Redis
  REDIS_HOST: "r-bp1amz9x3w13x1gr5l.redis.rds.aliyuncs.com"
  REDIS_PORT: "6379"
  REDIS_PASSWORD: "zskj_admin:Zskj@163501"
  REDIS_DB: "10"
  # 对象存储 (阿里云 OSS)
  STORAGE_TYPE: "aliyun-oss"
  ALIYUN_OSS_BUCKET_NAME: "bedian"
  ALIYUN_OSS_ACCESS_KEY: "LTAI5tAV9oMsQrcUJn8Td6bz"
  ALIYUN_OSS_SECRET_KEY: "s2kxHLDpwLFcqd5S24a6MdOZ3JoLKj"
  ALIYUN_OSS_ENDPOINT: "https://bedian.oss-cn-hangzhou.aliyuncs.com"
  ALIYUN_OSS_REGION: "cn-hangzhou"
  ALIYUN_OSS_AUTH_VERSION: "v4"
  # 基础配置
  SECRET_KEY: "sk-9f73s3slT8c5cT8c5cT8c5c"   # 请用 openssl rand -base64 42 替换
  INIT_PASSWORD: "123456"
  LOG_LEVEL: "INFO"
  CONSOLE_API_URL: "http://<替换为节点IP>:30080"
  APP_API_URL: "http://<替换为节点IP>:30080"
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: dify-nginx-conf
  namespace: beidian
data:
  nginx.conf: |
    events {
        worker_connections 1024;
    }
    http {
        client_max_body_size 100M;
        server {
            listen 80;
            server_name _;
            location /console/api {
                proxy_pass http://dify-api:5001;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
            }
            location /api {
                proxy_pass http://dify-api:5001;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
            }
            location /files {
                proxy_pass http://dify-api:5001;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
            }
            location / {
                proxy_pass http://dify-web:3000;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_read_timeout 3600s;
                proxy_send_timeout 3600s;
            }
        }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-api
  namespace: beidian
  labels:
    app: dify-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dify-api
  template:
    metadata:
      labels:
        app: dify-api
    spec:
      containers:
      - name: api
        image: langgenius/dify-api:1.13.3
        env:
        - name: MODE
          value: "api"
        envFrom:
        - configMapRef:
            name: dify-config
        ports:
        - containerPort: 5001
        livenessProbe:
          httpGet:
            path: /health
            port: 5001
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 5001
          initialDelaySeconds: 5
          periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: dify-api
  namespace: beidian
spec:
  selector:
    app: dify-api
  ports:
  - port: 5001
    targetPort: 5001
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-worker
  namespace: beidian
  labels:
    app: dify-worker
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dify-worker
  template:
    metadata:
      labels:
        app: dify-worker
    spec:
      containers:
      - name: worker
        image: langgenius/dify-api:1.13.3
        env:
        - name: MODE
          value: "worker"
        envFrom:
        - configMapRef:
            name: dify-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-worker-beat
  namespace: beidian
  labels:
    app: dify-worker-beat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dify-worker-beat
  template:
    metadata:
      labels:
        app: dify-worker-beat
    spec:
      containers:
      - name: beat
        image: langgenius/dify-api:1.13.3
        env:
        - name: MODE
          value: "beat"
        envFrom:
        - configMapRef:
            name: dify-config
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-web
  namespace: beidian
  labels:
    app: dify-web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dify-web
  template:
    metadata:
      labels:
        app: dify-web
    spec:
      containers:
      - name: web
        image: langgenius/dify-web:1.13.3
        env:
        - name: CONSOLE_API_URL
          valueFrom:
            configMapKeyRef:
              name: dify-config
              key: CONSOLE_API_URL
        - name: APP_API_URL
          valueFrom:
            configMapKeyRef:
              name: dify-config
              key: APP_API_URL
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: dify-web
  namespace: beidian
spec:
  selector:
    app: dify-web
  ports:
  - port: 3000
    targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dify-nginx
  namespace: beidian
  labels:
    app: dify-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dify-nginx
  template:
    metadata:
      labels:
        app: dify-nginx
    spec:
      containers:
      - name: nginx
        image: nginx:stable
        volumeMounts:
        - name: nginx-conf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-conf
        configMap:
          name: dify-nginx-conf
---
apiVersion: v1
kind: Service
metadata:
  name: dify-nginx
  namespace: beidian
spec:
  type: NodePort
  selector:
    app: dify-nginx
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30080
