0
点赞
收藏
分享

微信扫一扫

K8S 之 configmap

幺幺零 2022-08-27 阅读 117

1、ConfigMap是一种API对象,用来将非加密数据保存到键值对中。可以用作环境变量、命令行参数或者存储卷中的配置文件。

2、ConfigMap可以将环境变量配置信息和容器镜像解耦,便于应用配置的修改。如果需要存储加密信息时可以使用Secret对象。


通过yaml 文件创建一个configmap

#config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
key1: hello
key2: world

kubectl apply -f config.yaml

               K8S 之 configmap_云计算

创建带configmap 的pod, 将configmap挂载到容器中

# test-pod-projected-configmap-volume.yaml

apiVersion: v1
kind: Pod
metadata:
name: test-pod-projected-configmap-volume
spec:
containers:
- name: test-pod-busybox
image: busybox
imagePullPolicy: IfNotPresent
args:
- sleep
- "86400"
volumeMounts:
- name: config-volume
mountPath: "/projected-volume"
readOnly: true
volumes:
- name: config-volume
projected:
sources:
- configMap:
name: my-config

kubectl apply -f test-pod-projected-configmap-volume.yaml

               K8S 之 configmap_k8s_02

edit configmap

kubectl edit configmap my-config

增加一行 key3: ok

               K8S 之 configmap_云计算_03

重新进容器查看卷情况

kubectl exec -it test-pod-projected-configmap-volume -- /bin/sh

               K8S 之 configmap_云计算_04

发现容器卷中,内容已更新

通过volume挂载和环境变量的区别

通过Volume挂载到容器内部时,当该configmap的值发生变化时,容器内部具备自动更新的能力,但是通过环境变量设置到容器内部该值不具备自动更新的能力。

且看下面例子

[root@k8s-master1 test]# cat test.yaml

apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
containers:
- name: test-pod-busybox
image: busybox
imagePullPolicy: IfNotPresent
args:
- sleep
- "86400"
envFrom:
- configMapRef:
name: my-config

kubectl apply -f test.yaml

查看容器中的环境变量发现key1,key2 已经注入


[root@k8s-master1 test]# kubectl exec test-pod -- env |grep key

               K8S 之 configmap_云计算_05

编辑configmap 增加key3 ,看下容器中环境变量是否发生变化


[root@k8s-master1 test]# kubectl edit configmap my-config

configmap/my-config edited


[root@k8s-master1 test]# kubectl describe configmap my-config

如下所示configmap 已修改

               K8S 之 configmap_云计算_06

重新查看容器中环境变量,发现没有变化


[root@k8s-master1 test]# kubectl exec test-pod -- env |grep key

               K8S 之 configmap_k8s_07


举报

相关推荐

0 条评论