【问题标题】:Kubernetes configMap - only one fileKubernetes configMap - 只有一个文件
【发布时间】:2018-03-07 23:25:03
【问题描述】:

我有一个从文件创建的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

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

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

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

    【讨论】:

    • 另外,在旁注中,kubectl create configmap sstest --from-file=test.conf 足以创建 configmap。
    【解决方案2】:

    这也适用于我。如果您在 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
    

    【讨论】:

    • 如果原容器在/etc/apache2/conf-enabled/中有更多文件,不受影响?
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2021-10-14
      • 2017-08-15
      • 2020-03-23
      • 2019-11-26
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多