一、接著上文
本文的內(nèi)容是在k8s容器中,如何使用configmap對(duì).env文件進(jìn)行掛載,實(shí)現(xiàn)環(huán)境的差異化配置。
二、源碼結(jié)構(gòu)
項(xiàng)目ChatGPT-Next-Web使用了.env文件來配置不同環(huán)境下的值:
所以,我們同理新增兩個(gè)配置文件,見下:
三、k8s使用configmap掛載.env文件
生產(chǎn)環(huán)境的.env文件對(duì)應(yīng)生產(chǎn)環(huán)境的數(shù)據(jù)庫(kù)連接等信息,不可能寫在上面的源碼中。
所以,我們?cè)谏a(chǎn)k8s里新增.env文件,掛載到容器的對(duì)應(yīng)文件。
1、新增configmap配置
把.env.production文件配置在configmap,下一步將在deployment.yaml中引用它。
- 配置項(xiàng)名稱ai-assist-web-conf
- 文件的path路徑是.env.production
2、deployment.yaml引用configmap,掛載.env.production文件
增加以下內(nèi)容:
################ 修改前 #######################
volumeMounts:
- mountPath: /etc/localtime
name: volume-localtime
volumes:
- hostPath:
path: /etc/localtime
type: ''
name: volume-localtime
############### 修改后 #####################
volumeMounts:
- mountPath: /etc/localtime
name: volume-localtime
# 注意容器里,該配置文件的路徑是/opt
- mountPath: /opt/.env.production
name: configmap-volume
subPath: .env.production
volumes:
- hostPath:
path: /etc/localtime
type: ''
name: volume-localtime
- name: configmap-volume
configMap:
name: ai-assist-web-conf
items:
- key: .env.production
path: .env.production
3、完整的deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ai-assist-web
name: ai-assist-web
namespace: web-service
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: ai-assist-web
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: ai-assist-web
spec:
containers:
- image: xxx-harbor-registry-vpc.cn-hangzhou.cr.aliyuncs.com/xxx/ai-assist-web:1.0.5
imagePullPolicy: Always
name: ai-assist-web
ports:
- containerPort: 5173
env:
- name: TZ
value: Asia/Shanghai
- name: PORT
value: '5173'
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 200m
memory: 1.8Gi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/localtime
name: volume-localtime
- mountPath: /opt/.env.production
name: configmap-volume
subPath: .env.production
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /etc/localtime
type: ''
name: volume-localtime
- name: configmap-volume
configMap:
name: ai-assist-web-conf
items:
- key: .env.production
path: .env.production
四、驗(yàn)證掛載
進(jìn)入容器,查看.env.production文件的內(nèi)容。文章來源:http://www.zghlxwxcb.cn/news/detail-848581.html
切到目錄/opt文章來源地址http://www.zghlxwxcb.cn/news/detail-848581.html
- 查看.env.production文件內(nèi)容,確認(rèn)與configmap掛載一致
到了這里,關(guān)于開源項(xiàng)目ChatGPT-Next-Web的容器化部署(四)-- k8s容器部署使用configmap配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!