Kubernetes configMap - 只有一个文件

时间:2017-06-02 09:11:00

标签: kubernetes

我从文件中创建了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 个答案:

答案 0 :(得分:12)

好的,这有点棘手。最终工作的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 

答案 1 :(得分:8)

是。在volumeMounts设置subPath: ssportal.confmountPath: /etc/apache2/ssportal.conf。您也可以删除items: ...

在此处阅读更多内容:https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath

答案 2 :(得分:7)

这也适合我。在configmap中有多个文件的情况下很有用。

            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