Kubernetes configMap - 只有一个文件

Mis*_*sko 14 kubernetes

我有一个configMap从文件创建:

kubectl create configmap ssportal-apache-conf --from-file=ssportal.conf=ssportal.conf
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

是否可以在现有配置目录上仅覆盖一个文件?

Mis*_*sko 15

好的,这有点棘手.最终工作的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
Run Code Online (Sandbox Code Playgroud)

configMap创作步骤:

echo "# comment" > test.conf
kubectl create configmap sstest --from-file=test.conf=test.conf 
Run Code Online (Sandbox Code Playgroud)

  • 另外,顺便提一下,`kubectl create configmap sstest --from-file=test.conf` 足以创建 configmap。 (3认同)

Jan*_*art 8

是.在volumeMountssubPath: ssportal.confmountPath: /etc/apache2/ssportal.conf.您也可以删除它items: ....

在这里阅读更多:https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath


小智 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
Run Code Online (Sandbox Code Playgroud)