Kubernetes 如何在不清除文件夹的情况下装载包含ConfigMap的key/value内容的文件?

Kubernetes 如何在不清除文件夹的情况下装载包含ConfigMap的key/value内容的文件?,kubernetes,openshift,openshift-3,configmap,Kubernetes,Openshift,Openshift 3,Configmap,我创建了一个deploymentconfig.yml来部署一个应用程序,并希望挂载一个文件,其中的内容存储在ConfigMap中。 装载时,将替换所装载文件夹中的文件。 但目标是只添加/删除/覆盖该文件夹中的特定文件 spec: containers: volumeMounts: - mountPath: /path/toaSubPath/ name: somename 在这个deploymentconfig中可以这样做吗

我创建了一个deploymentconfig.yml来部署一个应用程序,并希望挂载一个文件,其中的内容存储在ConfigMap中。 装载时,将替换所装载文件夹中的文件。 但目标是只添加/删除/覆盖该文件夹中的特定文件

spec:
    containers:
          volumeMounts:
          - mountPath: /path/toaSubPath/
            name: somename
在这个deploymentconfig中可以这样做吗?
如果是这样,我该怎么做?

您可以使用下面的“子路径”将其装载为ConfigMap中配置的文件。 此演示演示如何仅装载配置为“/etc/test.txt”的test.txt文件

//创建测试盒和部署配置。
$oc run test--image registry.redhat.io/rhel7--tail-f/dev/null
deploymentconfig.apps.openshift.io/test已创建
//创建test.txt

$cat您可以使用下面的“子路径”装载为ConfigMap中配置的文件。 此演示演示如何仅装载配置为“/etc/test.txt”的test.txt文件

//创建测试盒和部署配置。
$oc run test--image registry.redhat.io/rhel7--tail-f/dev/null
deploymentconfig.apps.openshift.io/test已创建
//创建test.txt

$cat是的,我正在使用它装载默认配置。只需使用subPath和subPath中的文件名。在下面的示例中找到它,它就像一个符咒

spec:
  selector:
     matchLabels:
       name: some_name
  template:
    spec:
      containers:
      - args:
        - bash
        - entrypoint.sh
        image: xyz
        imagePullPolicy: IfNotPresent
        name: some_name
        volumeMounts:
        - mountPath: /full/path/to/be/mounted/default.json
          name: config
          subPath: default.json
      volumes:
      - configMap:
          defaultMode: 420
          name: config

是的,我正在使用它装载默认配置。只需使用subPath和subPath中的文件名。在下面的示例中找到它,它就像一个符咒

spec:
  selector:
     matchLabels:
       name: some_name
  template:
    spec:
      containers:
      - args:
        - bash
        - entrypoint.sh
        image: xyz
        imagePullPolicy: IfNotPresent
        name: some_name
        volumeMounts:
        - mountPath: /full/path/to/be/mounted/default.json
          name: config
          subPath: default.json
      volumes:
      - configMap:
          defaultMode: 420
          name: config

在应用所需配置之前,这是默认的nginx配置/usr/share/nginx/html

wget https://kubernetes.io/examples/configmap/game.properties 
wget https://kubernetes.io/examples/configmap/ui.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    containers:
    - name: nginx
      image: nginx
      volumeMounts:
      - mountPath: /usr/share/nginx/html/index.html
        name: data
        subPath: game                    # make a reference to existing CM Key
      - mountPath: /usr/share/nginx/html/ui.properties.txt
        name: data
        subPath: ui.properties.txt        # make a reference to existing CM Key
    volumes:
    - name: data
      configMap:
        name: game
        items:
        - key: game.properties
          path: game
        - key: ui.properties
          path: ui.properties.txt
自定义配置示例

wget https://kubernetes.io/examples/configmap/game.properties 
wget https://kubernetes.io/examples/configmap/ui.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    containers:
    - name: nginx
      image: nginx
      volumeMounts:
      - mountPath: /usr/share/nginx/html/index.html
        name: data
        subPath: game                    # make a reference to existing CM Key
      - mountPath: /usr/share/nginx/html/ui.properties.txt
        name: data
        subPath: ui.properties.txt        # make a reference to existing CM Key
    volumes:
    - name: data
      configMap:
        name: game
        items:
        - key: game.properties
          path: game
        - key: ui.properties
          path: ui.properties.txt

吊舱部署后
kubectl应用-f

验证index.html

验证ui.properties.txt


从ConfigMap获取不同文件时,请确保在我们应用所需配置之前引用了正确的密钥,这是默认的nginx配置/usr/share/nginx/html

wget https://kubernetes.io/examples/configmap/game.properties 
wget https://kubernetes.io/examples/configmap/ui.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    containers:
    - name: nginx
      image: nginx
      volumeMounts:
      - mountPath: /usr/share/nginx/html/index.html
        name: data
        subPath: game                    # make a reference to existing CM Key
      - mountPath: /usr/share/nginx/html/ui.properties.txt
        name: data
        subPath: ui.properties.txt        # make a reference to existing CM Key
    volumes:
    - name: data
      configMap:
        name: game
        items:
        - key: game.properties
          path: game
        - key: ui.properties
          path: ui.properties.txt
自定义配置示例

wget https://kubernetes.io/examples/configmap/game.properties 
wget https://kubernetes.io/examples/configmap/ui.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    containers:
    - name: nginx
      image: nginx
      volumeMounts:
      - mountPath: /usr/share/nginx/html/index.html
        name: data
        subPath: game                    # make a reference to existing CM Key
      - mountPath: /usr/share/nginx/html/ui.properties.txt
        name: data
        subPath: ui.properties.txt        # make a reference to existing CM Key
    volumes:
    - name: data
      configMap:
        name: game
        items:
        - key: game.properties
          path: game
        - key: ui.properties
          path: ui.properties.txt

吊舱部署后
kubectl应用-f

验证index.html

验证ui.properties.txt


从ConfigMap中获取不同的文件时,请确保引用了正确的密钥

如果要装载default.json,只需使用>kubctl create cm config--从文件default.json创建ConfigMap如果要装载default.json,只需使用>kubctl create cm config--从文件default.json创建ConfigMap即可
root@my:/usr/share/nginx/html# cat ui.properties.txt 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice