如何在不清除文件夹的情况下挂载包含 ConfigMap key/value 内容的文件?
How can I mount a file with the contents of key/value of a ConfigMap without clearing the folder?
我创建了一个 deploymentconfig.yml 来部署一个应用程序,并且想要装载一个文件,该文件的内容存储在 ConfigMap 中。
挂载时,挂载文件夹中的文件被替换。
不过,我们的目标是仅 add/remove/overwrite 该文件夹中的特定文件。
spec:
containers:
volumeMounts:
- mountPath: /path/toaSubPath/
name: somename
在这个部署配置中这可能吗?
如果可以,我该怎么做?
您可以使用“subPath”作为配置映射中配置的文件进行挂载,如下所示。
该演示展示了如何仅将配置为“/etc/test.txt”的 test.txt 文件挂载到 ConfigMap 中。
// 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
是的,我正在使用它来安装默认配置。只需使用 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:
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 获取不同文件时,请确保引用正确的密钥
我创建了一个 deploymentconfig.yml 来部署一个应用程序,并且想要装载一个文件,该文件的内容存储在 ConfigMap 中。 挂载时,挂载文件夹中的文件被替换。 不过,我们的目标是仅 add/remove/overwrite 该文件夹中的特定文件。
spec:
containers:
volumeMounts:
- mountPath: /path/toaSubPath/
name: somename
在这个部署配置中这可能吗? 如果可以,我该怎么做?
您可以使用“subPath”作为配置映射中配置的文件进行挂载,如下所示。 该演示展示了如何仅将配置为“/etc/test.txt”的 test.txt 文件挂载到 ConfigMap 中。
// 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
是的,我正在使用它来安装默认配置。只需使用 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:
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 获取不同文件时,请确保引用正确的密钥