系统整理K8S的配置管理实战-建议收藏系列( 二 )

apiVersion: v1kind: ConfigMapmetadata:name: game-demodata:# 类属性键;每一个键都映射到一个简单的值player_initial_lives: "3"ui_properties_file_name: "user-interface.properties"# 类文件键game.properties: |enemy.types=aliens,monstersplayer.maximum-lives=5user-interface.properties: |color.good=purplecolor.bad=yellowallow.textmode=true1.2、Pod使用ConfigMap1.2.1、valueFrom推荐将下面这种kv格式的configMap注入为容器的配置文件
[root@master01 yamls]# kubectl get cm cm-env-file -oyamlapiVersion: v1data:app.name: nginxlocation.prefix: /appkind: ConfigMapmetadata:creationTimestamp: "2022-03-31T01:04:12Z"name: cm-env-filenamespace: defaultresourceVersion: "761496"uid: 8e885d31-0a83-423c-a59f-6ea8b0473103创建dp , 并将cm挂在进去
[root@master01 yamls]#kubectl create deployment dp-test-cm --image=nginx --dry-run=client -oyaml > dp-test-cm.yaml修改
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: dp-test-cmname: dp-test-cmspec:replicas: 1selector:matchLabels:app: dp-test-cmtemplate:metadata:labels:app: dp-test-cmspec:containers:- image: nginxname: nginxenv:# 直接定义ENV的kv- name: Levelvalue: Pro# 将cm中的指定key的值作为AppName这个环境变量的值- name: AppNamevalueFrom:configMapKeyRef:name: cm-env-filekey: app.name验证:
[root@master01 yamls]# kubectl get poNAMEREADYSTATUSRESTARTSAGEdp-test-cm-7c5666c5c4-wnhtk1/1Running0111s[root@master01 yamls]# kubectl exec -ti dp-test-cm-7c5666c5c4-wnhtk -- sh# echo $AppNamenginx# echo $LevelPro1.2.2、envFrom一次性配置多个环境变量
注意点:如果key不存在 , 不会影响pod的运用 , 但是也不会创建这个环境变量
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: dp-test-cmname: dp-test-cmspec:replicas: 1selector:matchLabels:app: dp-test-cmtemplate:metadata:labels:app: dp-test-cmspec:containers:- image: nginxname: nginx# 和env同级 , 将redis-cm中的所有kv均作为环境变量注入到容器中envFrom:- configMapRef:name: redis-cmenv:- name: Levelvalue: Pro- name: AppNamevalueFrom:configMapKeyRef:name: cm-env-filekey: app.name验证:
[root@master01 yamls]# kubectl exec -ti dp-test-cm-6b7d8dc675-wf2vm -- sh# echo $ip10.10.10.1011.2.3、volumeMounts将基于文件创建的configMap以文件的形式挂在Pod的指定目录中
关键字:volume、volumeMounts
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: dp-test-cmname: dp-test-cmspec:replicas: 1selector:matchLabels:app: dp-test-cmtemplate:metadata:labels:app: dp-test-cmspec:volumes:- name: brm-configconfigMap:name: cm-from-filecontainers:- image: nginxname: nginxvolumeMounts:- name: brm-configmountPath: "/etc/foo"验证:可以看到它会将ConfigMap.data中的每一项 , 都作为一个单独的文件挂载进Pod中 。
[root@master01 yamls]# kubectl get cm cm-from-file -oyamlapiVersion: v1data:nginx.conf: |app.name=nginxlocation.prefix=/appredis.conf: |ip=10.10.10.101port=2379kind: ConfigMapmetadata:creationTimestamp: "2022-03-31T00:52:21Z"name: cm-from-filenamespace: defaultresourceVersion: "759788"uid: 5a41bdbe-66d0-45fa-8a5e-b6f6b005d671[root@master01 yamls]# kubectl exec -ti dp-test-cm-68d77c6c6-4jtqr -- sh# cd /etc/foo# lsnginx.confredis.conf# cat nginx.confapp.name=nginxlocation.prefix=/app1.2.4、自定义文件名称自定义挂载进Pod容器的文件名关键字:spec.spec.volumes.configMap.items
apiVersion: apps/v1kind: Deploymentmetadata:labels:app: dp-test-cmname: dp-test-cmspec:replicas: 1selector:matchLabels:app: dp-test-cmtemplate:metadata:labels:app: dp-test-cmspec:volumes:- name: brm-configconfigMap:name: cm-from-fileitems:- key: redis.confpath: redis.conf.cuscontainers:- image: nginxname: nginxvolumeMounts:- name: brm-configmountPath: "/etc/foo"1.2.5、控制文件权限关键字:

经验总结扩展阅读