赞
踩
本文的内容是在k8s容器中,如何使用configmap对.env文件进行挂载,实现环境的差异化配置。
项目ChatGPT-Next-Web使用了.env文件来配置不同环境下的值:
所以,我们同理新增两个配置文件,见下:
生产环境的.env文件对应生产环境的数据库连接等信息,不可能写在上面的源码中。
所以,我们在生产k8s里新增.env文件,挂载到容器的对应文件。
把.env.production文件配置在configmap,下一步将在deployment.yaml中引用它。
增加以下内容:
################ 修改前 ####################### 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
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
进入容器,查看.env.production文件的内容。
切到目录/opt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。