一、接着上文

本文的内容是在k8s容器中,如何使用configmap对.env文件进行挂载,实现环境的差异化配置。

二、源码结构

项目ChatGPT-Next-Web使用了.env文件来配置不同环境下的值:

在这里插入图片描述
所以,我们同理新增两个配置文件,见下:

在这里插入图片描述

三、k8s使用configmap挂载.env文件

生产环境的.env文件对应生产环境的数据库连接等信息,不可能写在上面的源码中。
所以,我们在生产k8s里新增.env文件,挂载到容器的对应文件。

1、新增configmap配置

在这里插入图片描述
把.env.production文件配置在configmap,下一步将在deployment.yaml中引用它。

  • 配置项名称ai-assist-web-conf
  • 文件的path路径是.env.production
    在这里插入图片描述

2、deployment.yaml引用configmap,挂载.env.production文件

增加以下内容:

################ 修改前 #######################
		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

四、验证挂载

进入容器,查看.env.production文件的内容。

切到目录/opt
在这里插入图片描述

  • 查看.env.production文件内容,确认与configmap挂载一致
    在这里插入图片描述
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐