赞
踩
ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 将你的环境配置信息和 容器镜像 解耦,便于应用配置的修改。
注意:
ConfigMap 并不提供保密或者加密功能。 如果你想存储的数据是机密的,请使用 Secret, 或者使用其他第三方工具来保证你的数据的私密性,而不是用 ConfigMap。
# 栗一:
echo hello > file1.txt
echo world > file2.txt
# 在one-namespace 命名空间下创建一个configmap
kubectl create configmap my-config --from-file=key1=file1.txt --from-file=key2=file2.txt -n one-namespace
# 查看已经创建好的configmap
kubectl get configmap my-config -n one-namespace -o yaml
"""
apiVersion: v1
data:
key1: |
hello
key2: |
world
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-26T10:07:41Z"
name: my-config
namespace: one-namespace
resourceVersion: "1116742"
uid: d4bb0d53-b118-4609-b2ad-0bf81487c28f
"""
# 栗二:
echo -e "a=1\nb=2\nc=3" | tee file3.txt
# 根据file3.txt 在命名空间one-namespace下创建config3
kubectl create configmap config3 --from-env-file ./file3.txt -n one-namespace
# 查看创建好的configmap
kubectl -n one-namespace get configmap config3 -o yaml
"""
apiVersion: v1
data:
a: "1"
b: "2"
c: "3"
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-26T11:18:57Z"
name: config3
namespace: one-namespace
resourceVersion: "1137450"
uid: 74da9687-39ab-489f-96d9-2742a9d4386c
"""
mkdir app-config
echo "one_value" > ./app-config/file1.txt
echo "two_value" > ./app-config/file2.txt
kubectl -n one-namespace create configmap config4 --from-file=app-config/
kubectl -n one-namespace get configmap config4 -o yaml
"""
apiVersion: v1
data:
file1.txt: |
one_value
file2.txt: |
two_value
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-26T15:36:39Z"
name: config4
namespace: one-namespace
resourceVersion: "1212342"
uid: 6d183514-d0b9-48e9-a809-38d71e54e868
"""
kubectl create configmap config5 --from-literal=k1=v1 --from-literal=k2=v2 -n one-namespace
kubectl -n one-namespace get configmap config5 -o yaml
"""
apiVersion: v1
data:
k1: v1
k2: v2
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-26T15:42:46Z"
name: config5
namespace: one-namespace
resourceVersion: "1214125"
uid: 3453205a-acde-4882-9d2d-19d862f295a5
"""
cat config6.yaml
"""
apiVersion: v1
kind: ConfigMap
metadata:
name: config6
namespace: one-namespace
data:
animal.type: dog
animal.name: "旺财"
"""
kubectl create -f config6.yaml
kubectl -n one-namespace get configmap config6 -o yaml
"""
apiVersion: v1
data:
animal.name: 旺财
animal.type: dog
kind: ConfigMap
metadata:
creationTimestamp: "2022-06-26T15:49:27Z"
name: config6
namespace: one-namespace
resourceVersion: "1216067"
uid: 39cacb04-5e7e-41fb-83ed-c18d044b247f
"""
# 创建需要用到的configmap
kubectl -n one-namespace create configmap config1 --from-literal=key1=value1 --from-literal=key2=value2
# 编辑pod1.yaml中的内容
vim pod1.yaml
"""# pod1.yaml内容如下
apiVersion: v1
kind: Pod
metadata:
name: pod1
namespace: one-namespace
spec:
containers:
- name: nginx1
image: nginx:latest
imagePullPolicy: IfNotPresent
env:
- name: e1
valueFrom:
configMapKeyRef:
name: config1
key: key1
- name: e2
valueFrom:
configMapKeyRef:
name: config1
key: key2
"""
# 创建pod1
kubectl create -f pod1.yaml
# 查看pod是否启动成功
kubectl get po -n one-namespace
# 进入pod内部查看环境变量
kubectl -n one-namespace exec -it pod1 -- bash
# 在pod内部输出e1变量的值。值是value1
echo $e1
# 创建需要用到的configmap
kubectl -n one-namespace create configmap config1 --from-literal=key1=value1 --from-literal=key2=value2
vim pod2.yaml
"""# pod2.yaml内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod2
namespace: one-namespace
spec:
containers:
- name: nginx1
image: nginx:latest
imagePullPolicy: IfNotPresent
command: [ "bash","-c","echo ${e1} ${e2}"]
env:
- name: e1
valueFrom:
configMapKeyRef:
name: config1
key: key1
- name: e2
valueFrom:
configMapKeyRef:
name: config1
key: key2
restartPolicy: Never
"""
# 创建pod2
kubectl create -f pod2.yaml
# 查看是否成功
kubectl -n one-namespace logs pod2
# 正常会输出内容:”value1 value2“
# 创建需要用到的configmap
kubectl -n one-namespace create configmap config1 --from-literal=key1=value1 --from-literal=key2=value2
vim pod3.yaml
""" pod3.yaml内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod3
namespace: one-namespace
spec:
containers:
- name: nginx1
image: nginx:latest
imagePullPolicy: IfNotPresent
volumeMounts:
- name: volume1
mountPath: "/vv1"
readOnly: true
volumes:
- name: volume1
projected:
sources:
- configMap:
name: config1
"""
kubectl create -f pod3.yaml
# 进入pod3中查看是否挂载成功
kubectl -n one-namespace exec -it pod3 -- bash
cat /vv1/key1
# 正常会输出value1
# 创建需要用到的configmap
kubectl -n one-namespace create configmap config1 --from-literal=key1=value1 --from-literal=key2=value2
vim pod4.yaml
""" pod4.yaml内容如下:
apiVersion: v1
kind: Pod
metadata:
name: pod4
namespace: one-namespace
spec:
containers:
- name: nginx
image: nginx:latest
command: [ "bash","-c","cat /etc/config1/keys/one" ]
volumeMounts:
- name: volume1
mountPath: /etc/config1
volumes:
- name: volume1
configMap:
name: config1
items:
- key: key1
path: keys/one
restartPolicy: Never
"""
kubectl create -f pod4.yaml
# 查看日志,因为pod4.yaml中配置了command,所以正常会输出config1中key1的值,即:会输出value1
kubectl -n one-namespace logs pod4
会覆盖/etc/nginx目录下文件的写法:
# 挂载出来后是/etc/nginx/key_file 文件,文件中的内容是value1
# 但是/etc/nginx 目录下原本的其他文件就不在了
apiVersion: v1
kind: Pod
metadata:
name: pod5
namespace: one-namespace
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
- name: volume1
mountPath: /etc/nginx
volumes:
- name: volume1
configMap:
name: config1
items:
- key: key1
path: key_file
restartPolicy: Never
通过subPath 来避免覆盖/etc/nginx目录下文件的写法
# value1 值仍然存在于/etc/nginx/key_file文件中
apiVersion: v1
kind: Pod
metadata:
name: pod5
namespace: one-namespace
spec:
containers:
- name: nginx
image: nginx:latest
command: ["bash", "-c", "ls /etc/nginx && cat /etc/nginx/key_file"]
volumeMounts:
- name: volume1
mountPath: /etc/nginx/key_file
subPath: key_file
volumes:
- name: volume1
configMap:
name: config1
items:
- key: key1
path: key_file
restartPolicy: Never
# 可以通过命令:kubectl -n one-namespace logs pod5 来查看command的执行结果来确定是否成功
在如下yaml文件中,也不会覆盖掉/etc/nginx目录下的文件,但因为是以/etc/nginx/key_file作为根目录,所以目录结构实际上为/etc/nginx/key_file/key_file。value1值存放在最后一层的key_file文件中
apiVersion: v1
kind: Pod
metadata:
name: pod5
namespace: one-namespace
spec:
containers:
- name: nginx
image: nginx:latest
volumeMounts:
- name: volume1
mountPath: /etc/nginx/key_file
volumes:
- name: volume1
configMap:
name: config1
items:
- key: key1
path: key_file
restartPolicy: Never
特性状态: Kubernetes v1.21 [stable]
可以通过将 immutable 字段设置为 true 创建不可变更的 ConfigMap。
apiVersion: v1
kind: ConfigMap
metadata:
...
data:
...
immutable: true
一旦某 ConfigMap 被标记为不可变更,则无法逆转这一变化,也无法更改 data 或 binaryData 字段的内容。你只能删除并重建 ConfigMap。 因为现有的 Pod 会维护一个已被删除的 ConfigMap 的挂载点,建议重新创建这些 Pods。
kubernetes从1.6版本开始引入了一个新的字段envFrom,实现了在pod环境中将ConfigMap(也可用于Secret资源对象)中所有定义的key=value自动生成为环境变量:
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cm-test
image: busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: cm-appvars
restartPolicy: Never
◼ 通过Volume挂载到容器内部时,当该configmap的值发生变化时,容器内部具备自动更新的能力,但是通过环境变量设置到容器内部该值不具备自动更新的能力。
◼ ConfigMap必须在Pod使用它之前创建
◼ 使用envFrom时,将会自动忽略无效的键(就是说,如果包含非法字符,则系统将跳过该条环境变量的创建,并记录一个Event来提示环境变量无法生成,但并不阻止pod的启动)。
◼ Pod只能使用同一个命名空间的ConfigMap
◼ 使用 ConfigMap 作为 subPath 卷挂载的容器将不会收到 ConfigMap 的更新。
◼ ConfigMap无法用于静态Pod。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。