如何在Kubernetes中使用卷挂载合并两个配置映射 [英] How to merge two configmaps using volume mount in kubernetes
问题描述
我有两个不同的配置映射 test-configmap 和 common-config .我试图将它们安装在同一位置,但是一个配置映射覆盖了另一个.然后我读到有关subPath
的信息,但没有用.
I am having two different config maps test-configmap and common-config. I tried to mount them at the same location, but one config map overwrote the other. Then I read about subPath
and did not work.
deploy.yaml
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"
我不确定是否可以合并两个配置映射.如果是的话,那我应该怎么做.
I am not sure if merging two config map achievable of not. And if yes then how should I do it.
推荐答案
您不能将两个ConfigMap安装到同一位置.
You cannot mount two ConfigMaps to the same location.
但是对于每个configmap中的每个项目都提及subPath
和key
,将使您可以从同一位置的两个configmap中获取项目.您必须手动为每个文件编写挂载点:
But mentioning subPath
and key
for every item in each configmaps will let you get items from both configmaps in the same location. You'll have to write mount points for each file manually:
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的密钥将被放入两个不同的目录中:
Another way is to mount them under same directory, but different subPath so that you don't have to specify items by hand. But, here keys from each configmap will be put into two different directories:
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
cm1
和cm2
将是两个目录,分别包含从test-configmap1
和test-configmap2
中的键派生的文件.
cm1
and cm2
will be two directories containing files derived from keys in test-configmap1
and test-configmap2
respectively.
这篇关于如何在Kubernetes中使用卷挂载合并两个配置映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!