在 Kubernetes 上挂载配置文件

在 Kubernetes 上挂载配置文件

我正在努力在主应用程序目录 (/publish) 中创建两个 JSON 配置文件,同时保持该目录中的所有其他文件不变。我尝试了以下操作:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: url_to_image
        ports:
        - containerPort: 1234
        volumeMounts:
        - name: config-files
          mountPath: "/publish"
      volumes:
      - name: config-files
        configMap:
          name: myapp
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  ports:
    - port: 80
      targetPort: 1234
      protocol: TCP
  type: NodePort
  selector:
    app: myapp
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: myapp
  labels:
    app: myapp
data:
  appsettings.json: |
    { some JSON }
  sharedsettings.json: |
    { more JSON }

两个配置文件(appsettings.json 和 sharedsettings.json)已正确创建,但不幸的是,/publish 中的所有其他文件在此过程中都被删除。

最好的方法是什么?

答案1

好的,找到了一个解决方案,但不太优雅。还有其他更好的解决方案吗?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: url_to_image
        ports:
        - containerPort: 1234
        volumeMounts:
        - name: appsettings
          mountPath: "/publish/appsettings.json"
          subPath: whatever
        volumeMounts:
        - name: sharedsettings
          mountPath: "/publish/sharedsettings.json"
          subPath: whatever
      volumes:
      - name: appsettings
        configMap:
          name: myapp
          items:
          - key: appsettings.json
            path: whatever
      - name: sharedsettings
        configMap:
          name: myapp
          items:
          - key: sharedsettings.json
            path: whatever
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  ports:
    - port: 80
      targetPort: 1234
      protocol: TCP
  type: NodePort
  selector:
    app: myapp
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: myapp
  labels:
    app: myapp
data:
  appsettings.json: |
    { some JSON }
  sharedsettings.json: |
    { more JSON }

相关内容