如何在不清除文件夹的情况下挂载具有ConfigMap的键/值的内容的文件?

问题描述

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

spec:
    containers:
          volumeMounts:
          - mountPath: /path/toaSubPath/
            name: somename

在这个deploymentconfig中有可能吗? 如果是这样,我该怎么办?

解决方法

是的,我正在使用它来安装默认配置。只需使用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”将其挂载为在ConfigMap中配置的文件,如下所示。 该演示演示了如何仅挂载配置为ConfigMap的test.txt文件为“ /etc/test.txt”。

// Create test pod and deploymentconfig.
$ oc run test --image registry.redhat.io/rhel7 -- tail -f /dev/null
deploymentconfig.apps.openshift.io/test created

// Create test.txt
$ cat <<EOF > test.txt
Test file
EOF

// Create testmap ConfigMap using above test.txt file.
$ oc create configmap testmap --from-file=test.txt

// Modify volumes and volumeMounts for mounting only test.txt file to "/etc/test.txt".
$ oc edit dc/test
:
containers:
- name: test
  :
  volumeMounts:
  - mountPath: /etc/test.txt
    name: testtxt
    subPath: test.txt
:
terminationGracePeriodSeconds: 30
volumes:
- configMap:
    defaultMode: 420
    items:
    - key: test.txt
      path: test.txt
    name: testmap
  name: testtxt
:

// You can verify test.txt after redeploying test pod after modification.
$ oc rsh dc/test cat /etc/test.txt
Test file

// you can also verify test.txt file is only mounted to /etc directory as one specified file by subPath,not all directory.
$ oc rsh dc/test ls -l /etc/
total 888
:
drwxr-xr-x.  4 root root          151 Aug  3 09:13 systemd
drwxr-xr-x.  2 root root            6 Aug 15  2017 terminfo
-rw-r--r--.  1 root 1000110000     10 Aug 14 16:02 test.txt
:
drwxr-xr-x.  1 root root            6 Aug  3 09:37 yum.repos.d
,

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

root@ng:/usr/share/nginx/html# ls -la
total 16
drwxr-xr-x 2 root root 4096 Aug 14 00:36 .
drwxr-xr-x 3 root root 4096 Aug 14 00:36 ..
-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html
-rw-r--r-- 1 root root  612 Aug 11 14:50 index.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

pod部署后 kubectl apply -f <your_pod_yaml>

root@my:/usr/share/nginx/html# ls -la
total 24
drwxr-xr-x 1 root root 4096 Aug 17 12:26 .
drwxr-xr-x 1 root root 4096 Aug 14 00:36 ..
-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html
-rw-r--r-- 1 root root  157 Aug 17 12:26 index.html
-rw-r--r-- 1 root root   83 Aug 17 12:26 ui.properties.txt

验证index.html

root@my:/usr/share/nginx/html# curl localhost

enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

验证ui.properties.txt

root@my:/usr/share/nginx/html# cat ui.properties.txt 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...