一、概述
二、准备工作
前提条件
硬件
软件
镜像推送
下载镜像
docker pull postgres:15-alpine docker pull redis:6-alpine docker pull semitechnologies/weaviate:1.19.0 docker pull nginx:latest docker pull langgenius/dify-plugin-daemon:0.0.6-local docker pull langgenius/dify-sandbox:0.2.11 docker pull ubuntu/squid:latest docker pull langgenius/dify-api:1.1.3 docker pull langgenius/dify-web:1.1.3
推送镜像
这里使用shell脚本完成
#!/bin/bash images="postgres:15-alpine redis:6-alpine semitechnologies/weaviate:1.19.0 nginx:latest langgenius/dify-plugin-daemon:0.0.6-local langgenius/dify-sandbox:0.2.11 ubuntu/squid:latest langgenius/dify-api:1.1.3 langgenius/dify-web:1.1.3" # 遍历镜像列表,并推送到harbor for line in $images; do #echo "$line" docker tag $line harbor.qq.com:8083/dify/$line docker push harbor.qq.com:8083/dify/$line done
注意修改harbor的的地址为实际访问地址
登录到harbor,查看镜像
创建命名空间以及密钥
kubectl create namespace dify
kubectl create secret docker-registry harbor-key --docker-server=harbor.qq.com:8083 --docker-username=devops --docker-password=1sB5r9UShgK5 --namespace=dify
注意:修改harbor地址,用户名,密码。
下载部署yaml
yaml已经写好了,推送到我个人的github,地址:https://github.com/987334176/dify-k8s
下载项目后,进入文件夹1.1.3
目录结构
env --> 全局环境变量 pvc --> 所有组件,统一使用一个pvc来进行持久化存储 databases --> 数据库相关:postgresql,redis,weaviate middleware --> 中间件相关:plugin-daemon,sandox,ssf-proxy,nginx services --> 服务相关:api,web,worker
三、创建全局环境变量
这里有500个多个变量,直接一键运行
kubectl apply -f env/env.yaml
四、创建pv和pvc
创建storageClass,因为pv和pvc必须是同一个storageClass才能绑定成功
kubectl apply -f pvc/storageClass.yaml
注意:修改NFS 服务端的共享路径,provisioner必须指定为nfs-client
nfs-client是一个外部的动态存储供给器(Provisioner),用于在 Kubernetes 集群中动态创建和管理基于 NFS(Network File System)的 PersistentVolumes(PV)。它是 Kubernetes 社区提供的一个解决方案,用于支持 NFS 存储的动态供给。
创建pv,pvc。注意:这里的pv是自建的NFS,请根据实际情况修改
kubectl apply -f pvc/pv.yaml kubectl apply -f pvc/pvc.yaml
查看pvc状态,注意:请确保pvc状态为Bound
# kubectl -n dify get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE dify-pvc Bound dify 10Gi RWX nfs-storage <unset> 12m
服务器已经挂载好了NFS,进入到NFS根目录
cd /mnt/nfs_share
创建dify相关持久化文件,并设置权限
mkdir -p dify/volumes/db/data mkdir -p dify/volumes/redis/data mkdir -p dify/volumes/weaviate mkdir -p dify/volumes/plugin_daemon mkdir -p dify/volumes/app/storage chmod 777 -R dify
五、数据库相关
postgresql
修改文件postgres-StatefulSet.yaml
vi databases/postgresql/postgres-StatefulSet.yaml
修改红色部分,增加了密钥,改了镜像地址
spec: imagePullSecrets: - name: harbor-key terminationGracePeriodSeconds: 10 containers: - name: postgres image: harbor.qq.com:8083/dify/postgres:15-alpine
发布应用
kubectl apply -f databases/postgresql/postgres-StatefulSet.yaml kubectl apply -f databases/postgresql/postgres-Service.yaml
查看postgresql日志
# kubectl -n dify logs -f postgres-0 ... UTC [72] WARNING: no usable system locales were found performing post-bootstrap initialization ...
第一次会出现,pod到这里就终止了,不用管
再次查看日志。
# kubectl -n dify logs -f postgres-0 2025-04-05 16:29:11.415 UTC [1] LOG: database system is ready to accept connections 2025-04-05 16:29:13.707 UTC [46] FATAL: database "postgres" does not exist 2025-04-05 16:29:18.721 UTC [53] FATAL: database "postgres" does not exist ...
提示数据库postgres不存在,因为在全局configMap里面,POSTGRES_DB的值为dify,所以默认的postgres不会创建。
这个是不影响dify运行的,但是postgres的健康检查命令,必须要这个数据库存在,否则会一直发日志。
手动创建postgres
kubectl -n dify exec -it postgres-0 -- createdb postgres
再次查看日志,就不会再出现了。
进入容器,查看dify数据库是否创建完成
# kubectl -n dify exec -it postgres-0 -- /bin/bash postgres-0:/# pg_isready -U $PGUSER -d $POSTGRES_DB /var/run/postgresql:5432 - accepting connections
出现accepting connections,说明连接成功了
111