关于 kubernet configmap mountPath 和 subPath
about kubernet configmap mountPath with subPath
pod yaml
containers:
- name: kiada
image: :kiada-0.1
volumeMounts:
- name: my-test
subPath: my-app.conf
mountPath: /html/my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
配置图
➜ v5-kubernetes git:(master) ✗ k get cm kiada-config -oyaml
apiVersion: v1
data:
key: value\n
status-message: This status message is set in the kiada-config config map2\n
kind: ConfigMap
metadata:
creationTimestamp: "2022-05-18T03:01:15Z"
name: kiada-config
namespace: default
resourceVersion: "135185128"
uid: 8c8875ce-47f5-49d4-8bc7-d8dbc2d7f7ba
pod 有我的-app.conf
root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/
total 12
drwxrwxrwx 3 root root 4096 May 21 02:29 .
drwxr-xr-x 1 root root 4096 May 21 02:29 ..
drwxr-xr-x 2 root root 4096 May 21 02:29 ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root 31 May 21 02:29 ..data -> ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root 10 May 21 02:29 key -> ..data/key
lrwxrwxrwx 1 root root 21 May 21 02:29 status-message -> ..data/status-message
root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/
如果我在 pod yaml 中添加子路径
spec:
containers:
- name: kiada
image: kiada-0.1
volumeMounts:
- name: my-test
subPath: my-app.conf
mountPath: /html/my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
结果
root@kiada2-c89749c8-x9qwq:/# ls -al html/my-app.conf/
total 8
drwxrwxrwx 2 root root 4096 May 21 02:36 .
drwxr-xr-x 1 root root 4096 May 21 02:36 ..
为什么我使用子路径,配置映射键不存在,怎么了?
你的配置映射中没有文件我建议检查一下:https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume
配置映射:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
POD 部署
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
当 pod 运行时,命令 ls /etc/config/
产生以下输出:
SPECIAL_LEVEL
SPECIAL_TYPE
如果你想用不同的文件名注入 configmap 你可以使用 items
items:
- key: SPECIAL_LEVEL
path: keys
为了在 Pod 的文件系统中生成一个名为 my-app.config
的文件,其中包含您的应用程序配置,必须确保此文件 存在于您的配置映射 中:
apiVersion: v1
kind: ConfigMap
metadata:
name: kiada-config
data:
my-app.conf: |
key: value
status-message: This status message is set in the kiada-config config map2
然后,您可以像这样将它安装到您的 Pod 中:
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/
name: my-test
volumes:
- name: my-test
configMap:
name: kiada-config
在这种情况下不需要 subPath 字段。如果您想将 my-app.conf
重新映射到不同的名称,either 将很有用..
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/my-app-new-name.conf
name: my-test
subPath: my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
..或,如果您的 ConfigMap 中有多个配置文件并且只想将其中一个映射到您的 Pod 中:
apiVersion: v1
kind: ConfigMap
metadata:
name: kiada-config
data:
my-app.conf: |
key: value
status-message: This status message is set in the kiada-config config map2
my-second-app.conf: |
error: not in use
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/my-app.conf
name: my-test
subPath: my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
pod yaml
containers:
- name: kiada
image: :kiada-0.1
volumeMounts:
- name: my-test
subPath: my-app.conf
mountPath: /html/my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
配置图
➜ v5-kubernetes git:(master) ✗ k get cm kiada-config -oyaml
apiVersion: v1
data:
key: value\n
status-message: This status message is set in the kiada-config config map2\n
kind: ConfigMap
metadata:
creationTimestamp: "2022-05-18T03:01:15Z"
name: kiada-config
namespace: default
resourceVersion: "135185128"
uid: 8c8875ce-47f5-49d4-8bc7-d8dbc2d7f7ba
pod 有我的-app.conf
root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/
total 12
drwxrwxrwx 3 root root 4096 May 21 02:29 .
drwxr-xr-x 1 root root 4096 May 21 02:29 ..
drwxr-xr-x 2 root root 4096 May 21 02:29 ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root 31 May 21 02:29 ..data -> ..2022_05_21_02_29_41.554311630
lrwxrwxrwx 1 root root 10 May 21 02:29 key -> ..data/key
lrwxrwxrwx 1 root root 21 May 21 02:29 status-message -> ..data/status-message
root@kiada2-7cc7bf55d8-m97tt:/# ls -al /html/my-app.conf/
如果我在 pod yaml 中添加子路径
spec:
containers:
- name: kiada
image: kiada-0.1
volumeMounts:
- name: my-test
subPath: my-app.conf
mountPath: /html/my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
结果
root@kiada2-c89749c8-x9qwq:/# ls -al html/my-app.conf/
total 8
drwxrwxrwx 2 root root 4096 May 21 02:36 .
drwxr-xr-x 1 root root 4096 May 21 02:36 ..
为什么我使用子路径,配置映射键不存在,怎么了?
你的配置映射中没有文件我建议检查一下:https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#add-configmap-data-to-a-volume
配置映射:
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
SPECIAL_LEVEL: very
SPECIAL_TYPE: charm
POD 部署
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
当 pod 运行时,命令 ls /etc/config/
产生以下输出:
SPECIAL_LEVEL
SPECIAL_TYPE
如果你想用不同的文件名注入 configmap 你可以使用 items
items:
- key: SPECIAL_LEVEL
path: keys
为了在 Pod 的文件系统中生成一个名为 my-app.config
的文件,其中包含您的应用程序配置,必须确保此文件 存在于您的配置映射 中:
apiVersion: v1
kind: ConfigMap
metadata:
name: kiada-config
data:
my-app.conf: |
key: value
status-message: This status message is set in the kiada-config config map2
然后,您可以像这样将它安装到您的 Pod 中:
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/
name: my-test
volumes:
- name: my-test
configMap:
name: kiada-config
在这种情况下不需要 subPath 字段。如果您想将 my-app.conf
重新映射到不同的名称,either 将很有用..
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/my-app-new-name.conf
name: my-test
subPath: my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config
..或,如果您的 ConfigMap 中有多个配置文件并且只想将其中一个映射到您的 Pod 中:
apiVersion: v1
kind: ConfigMap
metadata:
name: kiada-config
data:
my-app.conf: |
key: value
status-message: This status message is set in the kiada-config config map2
my-second-app.conf: |
error: not in use
apiVersion: v1
kind: Pod
metadata:
labels:
run: kiada
name: kiada
spec:
containers:
- name: kiada
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
volumeMounts:
- mountPath: /html/my-app.conf
name: my-test
subPath: my-app.conf
volumes:
- name: my-test
configMap:
name: kiada-config