如何在kubernetes中使用卷装置合并两个配置映射

时间:2018-03-14 20:26:58

标签: deployment kubernetes

我有两个不同的配置地图 test-configmap common-config 。我试图将它们安装在同一个位置,但是一个配置图覆盖了另一个。然后我读了subPath并且没有用。

deploy.yaml

apiVersion: apps/v1beta1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: testing
spec:
  replicas: 1
  template:
    metadata:
      name: testing
      labels:
        app: testing
    spec:
      containers:
      - name: testing-container
        image: testing
        imagePullPolicy: IfNotPresent
      ports:
      - containerPort: __PORT__
      volumeMounts:
      - name: commonconfig-volume
        mountPath: /usr/src/app/config/test.config
        subPath: test.config
    volumes:
      - name: commonconfig-volume
        configMap:
          name: test-configmap
      - name: commonconfig-volume
        configMap:
          name: common-config

错误:

The Deployment "testing" is invalid: spec.template.spec.volumes[1].name: Duplicate value: "commonconfig-volume"

我不确定合并两个配置图是否可以实现。如果是,那我该怎么办呢。

5 个答案:

答案 0 :(得分:7)

您无法将两个ConfigMaps安装到同一位置。

但是,为每个配置映射中的每个项目提及subPathkey都可以让您从同一位置的两个配置文件中获取项目。您必须手动为每个文件编写挂载点:

apiVersion: v1
kind: Pod
metadata:
  name: config-single-file-volume-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "cat /etc/special-key" ]
      volumeMounts:
      - name: config-volume-1
        mountPath: /etc/special-key1
        subPath: path/to/special-key1
      - name: config-volume-2
        mountPath: /etc/special-key2
        subPath: path/to/special-key2
  volumes:
    - name: config-volume-1
      configMap:
        name: test-configmap1
        items:
        - key: data-1
          path: path/to/special-key1
    - name: config-volume-2
      configMap:
        name: test-configmap2
        items:
        - key: data-2
          path: path/to/special-key2
restartPolicy: Never

另一种方法是将它们安装在同一目录下但不同的子路径下,这样您就不必手动指定项目。但是,每个configmap中的密钥将放在两个不同的目录中:

apiVersion: v1
kind: Pod
metadata:
  name: config-single-file-volume-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: [ "/bin/sh", "-c", "cat /etc/special-key" ]
      volumeMounts:
      - name: config-volume-1
        mountPath: /etc/special-keys
        subPath: cm1
      - name: config-volume-2
        mountPath: /etc/special-keys
        subPath: cm2
  volumes:
    - name: config-volume-1
      configMap:
        name: test-configmap1
    - name: config-volume-2
      configMap:
        name: test-configmap2
restartPolicy: Never

cm1cm2将是两个目录,其中包含分别来自test-configmap1test-configmap2中的键的文件。

答案 1 :(得分:3)

您必须使用特殊的projected卷来实现。部署示例:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: testing
spec:
  replicas: 1
  selector:
    matchLabels:
      app: testing
  template:
    metadata:
      name: testing
      labels:
        app: testing
    spec:
      containers:
      - name: testing-container
        image: testing
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: __PORT__
        volumeMounts:
        - name: commonconfig-volume
          mountPath: /usr/src/app/config
      volumes:
        - name: commonconfig-volume
          projected:
            sources:
            - configMap:
                name: test-configmap
            - configMap:
                name: common-config

您可以与secret一样使用configMap

答案 2 :(得分:0)

一种方法是将它们安装在不同的点但是在相同的emptyDir卷中,将相同的卷安装到init container中并在init容器中包含一个短脚本以合并这两个文件您在脚本开头安装的任何工具。使用this answer中的技术可以轻松地将脚本包含在pod清单中。

答案 3 :(得分:0)

关于如何安装多个configmap的另一个示例。您想同时替换主要的/etc/nginx/nginx.conf和/etc/nginx/conn.f中的文件吗?这也会删除conf.d中的default.conf文件。

 containers:
    - name: nginx-proxy
      image: nginx:1.16-alpine
      imagePullPolicy: Always
      ports:
        - containerPort: 443
        - containerPort: 80
      volumeMounts:
        - name: nginx-main-conf-file
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        - name: nginx-site-conf-file
          mountPath: /etc/nginx/conf.d
  volumes:
    - name: nginx-main-conf-file
      configMap:
        name: nginx-main-conf
    - name: nginx-site-conf-file
      configMap:
        name: nginx-site-conf

还有一个非常重要点。如果您的yaml文件中有任何注释掉的行(#东西),那么这将不起作用。这是一个错误。在kubectl v1.14中测试过

答案 4 :(得分:0)

注意:如果您在configmap中的数据量较少,这种方法很好。

假设您有两个configmaps

test-confimap

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-configmap
data:
  test-1.conf: |
    test-property-1=test-value-1

公共配置图

apiVersion: v1
kind: ConfigMap
metadata:
  name: common-configmap
data:
  common-1.conf: |
    common-property-1=common-value-1

除了拥有不同的配置映射,您还可以在单​​个配置映射中拥有相同的数据,如下所示。

apiVersion: v1
kind: ConfigMap
metadata:
  name: single-configmap
data:
  common-1.conf: |
    property-1=value-1
  test-1.conf: |
    property-1=value-1

现在从上面的configmap创建一个卷,并使用如下的mounhPath挂载到容器

configmap中的音量

volumes:
- configMap:
    defaultMode: 420
    name: single-cofigmap
  name: my-single-config

volumeMount

volumeMounts:
- mountPath: /path/to/config/folder/
  name: my-single-config

现在,您可以在容器内/path/to/config/folder/的位置看到两个文件。告诉您的应用程序使用所需的应用程序。