Kubernetes配置映射 - 仅限一个文件

29

我有一个从文件创建出来的configMap

kubectl create configmap ssportal-apache-conf --from-file=ssportal.conf=ssportal.conf

然后我需要将这个文件挂载到部署中:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ssportal
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: ssportal
    spec:
      containers:
        - name: ssportal
          image: eu.gcr.io/my-project/ssportal:0.0.0
          ports:
          - containerPort: 80
          volumeMounts:
            - name: apache2-config-volume
              mountPath: /etc/apache2/
      volumes:
        - name: apache2-config-volume
          configMap:
            name: ssportal-apache-conf
            items:
              - key: ssportal.conf
                path: sites-enabled/ssportal.conf

但这实际上从容器中移除了现有的/etc/apache2/目录,并用一个唯一文件/etc/apache2/sites-enabled/ssportal.conf替换它。

是否可能只覆盖现有配置目录中的一个文件?

3个回答

34

好的,这有点棘手。最终可用的YAML规范是:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: ssportal
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: ssportal
    spec:
      containers:
        - name: ssportal
          image: eu.gcr.io/my-project/ssportal:0.0.0
          command: ["sleep","120d"]
          ports:
          - containerPort: 80
          volumeMounts:
            - name: test
              mountPath: /etc/apache2/conf-enabled/test.conf
              subPath: test.conf
      volumes:
        - name: test
          configMap:
            name: sstest

关于 configMap 的创建步骤:

echo "# comment" > test.conf
kubectl create configmap sstest --from-file=test.conf=test.conf 

3
另外,顺带提一下,kubectl create configmap sstest --from-file=test.conf 就足以创建配置映射。 - c0degeas

17

这对我也有用。如果配置映射中有多个文件,则非常有用。

            volumeMounts:
            - name: test
              mountPath: /etc/apache2/conf-enabled/test.conf
              subPath: test.conf
      volumes:
        - name: test
          configMap:
            name: test
            - key: test.conf
              path: test.conf

如果原始容器在 /etc/apache2/conf-enabled/ 中有更多的文件,它们不会受到影响吗? - Daniel Hernández

10

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接