使用 Kubernetes 搭建 Lobe Chat 并实现自动化更新

使用 Kubernetes 搭建 Lobe Chat 并实现自动化更新

使用 Kubernetes (k8s) 部署 Lobe Chat 应用,并设置自动化更新机制。这种方法可以让你轻松地管理和维护 Lobe Chat 服务,确保它始终运行最新版本。

前提条件

  • 已安装并配置好 Kubernetes 集群
  • API 密钥

步骤 1: 创建配置文件

首先,创建两个 YAML 文件来配置我们的部署。

lobe.yaml

这个文件包含了 Lobe Chat 的主要部署配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
apiVersion: v1
kind: ConfigMap
metadata:
name: lobe-chat-config
data:
AZURE_API_KEY: "your_azure_api_key"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: lobe-chat-deployment
spec:
replicas: 3 # 将副本数量设置为3
selector:
matchLabels:
app: lobe-chat
template:
metadata:
labels:
app: lobe-chat
spec:
containers:
- name: lobe-chat
image: lobehub/lobe-chat:latest
ports:
- containerPort: 3210
envFrom:
- configMapRef:
name: lobe-chat-config
imagePullPolicy: Always # 确保每次部署时都会拉取最新的镜像
---
apiVersion: v1
kind: Service
metadata:
name: lobe-chat-service
spec:
selector:
app: lobe-chat
ports:
- protocol: TCP
port: 3210
targetPort: 3210
type: LoadBalancer # 使用 LoadBalancer 类型实现负载均衡

确保将示例中的 API 密钥和访问代码替换为你自己的值。

lobe-update.yaml

这个文件设置了自动更新机制:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 创建一个新的 ServiceAccount,名为 lobe-chat-updater
# 该 ServiceAccount 将被 CronJob 使用,以获得操作 Kubernetes API 的权限
apiVersion: v1
kind: ServiceAccount
metadata:
name: lobe-chat-updater
namespace: default

---
# 定义一个 Role,名为 lobe-chat-updater-role
# 该 Role 允许持有者对 "deployments" 资源执行 get、list、watch、update 和 patch 操作
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: lobe-chat-updater-role
rules:
- apiGroups: ["apps"] # "apps" API 组包括了 "deployments" 资源
resources: ["deployments"] # 目标资源类型为 "deployments"
verbs: ["get", "list", "watch", "update", "patch"] # 允许执行的操作

---
# 定义一个 RoleBinding,将上面定义的 Role 绑定到 lobe-chat-updater ServiceAccount
# 这使得 lobe-chat-updater 可以使用 lobe-chat-updater-role 中定义的权限
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: lobe-chat-updater-binding
namespace: default
subjects:
- kind: ServiceAccount
name: lobe-chat-updater # 绑定的 ServiceAccount
namespace: default
roleRef:
kind: Role
name: lobe-chat-updater-role # 绑定的 Role
apiGroup: rbac.authorization.k8s.io

---
# 定义一个 CronJob,名为 lobe-chat-update
# 该 CronJob 将定期执行滚动更新,确保部署始终使用最新的镜像
apiVersion: batch/v1
kind: CronJob
metadata:
name: lobe-chat-update
namespace: default
spec:
# 使用 Cron 表达式定义任务的执行时间
# 这里是每天凌晨 3 点执行
schedule: "0 3 * * *"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
# 定义任务的模板
jobTemplate:
spec:
ttlSecondsAfterFinished: 3600 # Job 完成1小时后自动删除
template:
spec:
# 指定要使用的 ServiceAccount,这里是 lobe-chat-updater
serviceAccountName: lobe-chat-updater

# 设置容器的重启策略,这里是 OnFailure,表示任务失败时重启容器
restartPolicy: OnFailure

# 定义任务中要运行的容器
containers:
- name: lobe-chat-updater
image: bitnami/kubectl:1.23.0 # 使用 bitnami/kubectl 镜像,该镜像包含 kubectl 工具
imagePullPolicy: IfNotPresent # 使用固定版本的镜像

# 定义容器启动时要执行的命令
# 这里使用 kubectl 来触发 lobe-chat-deployment 的滚动更新
command:
- /bin/sh
- -c
- |
kubectl rollout restart deployment/lobe-chat-deployment

# 为容器设置资源请求和限制,确保它不会消耗过多的集群资源
resources:
limits:
cpu: "100m" # 限制最多使用 100 毫核的 CPU
memory: "128Mi" # 限制最多使用 128 MiB 的内存
requests:
cpu: "50m" # 容器启动时请求 50 毫核的 CPU
memory: "64Mi" # 容器启动时请求 64 MiB 的内存

步骤 2: 部署 Lobe Chat

使用以下命令应用 lobe.yaml 配置:

1
kubectl apply -f lobe.yaml

这将创建一个 ConfigMap、一个包含 3 个副本的 Deployment 和一个 LoadBalancer 类型的 Service。

步骤 3: 设置自动更新

应用 lobe-update.yaml 配置:

1
kubectl apply -f lobe-update.yaml

这将创建一个 CronJob,每天凌晨 3 点自动更新 Lobe Chat 部署。

解释

  1. ConfigMap: 存储所有必要的环境变量和 API 密钥。
  2. Deployment: 定义 Lobe Chat 应用的部署配置,包括副本数、容器镜像等。
  3. Service: 创建一个 LoadBalancer 类型的服务,使 Lobe Chat 可以从集群外部访问。
  4. ServiceAccount, Role, RoleBinding: 为自动更新任务创建必要的权限。
  5. CronJob: 定期执行滚动更新,确保始终使用最新的 Lobe Chat 镜像。

外部访问

查看Service

1
2
3
4
# kubectl get svc                                                                                                                                                      
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 443/TCP 14d
lobe-chat-service LoadBalancer 10.43.154.41 172.27.177.79,172.31.18.43,172.31.30.122 3210:32156/TCP 14d

因为多个网络接口,导致有多个外部 IP 地址

通过http://your_ip:3210即可访问到服务


使用 Kubernetes 搭建 Lobe Chat 并实现自动化更新
http://example.com/2024/09/21/使用-Kubernetes-搭建-Lobe-Chat-并实现自动化更新/
作者
Sanli Ma
发布于
2024年9月21日
许可协议