K8S之存储ConfigMap概述与说明,并详解常用ConfigMap示例
主机配置规划
服务器名称(hostname) | 系统版本 | 配置 | 内网IP | 外网IP(模拟) |
---|---|---|---|---|
k8s-master | CentOS7.7 | 2C/4G/20G | 172.16.1.110 | 10.0.0.110 |
k8s-node01 | CentOS7.7 | 2C/4G/20G | 172.16.1.111 | 10.0.0.111 |
k8s-node02 | CentOS7.7 | 2C/4G/20G | 172.16.1.112 | 10.0.0.112 |
ConfigMap概述
ConfigMap 是一种 API 对象,用来将非机密性的数据保存到健值对中。使用时可以用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 将环境配置信息和容器镜像解耦,便于应用配置的修改。当你需要储存机密信息时可以使用 Secret 对象。
备注:ConfigMap 并不提供保密或者加密功能。如果你想存储的数据是机密的,请使用 Secret;或者使用其他第三方工具来保证数据的私密性,而不是用 ConfigMap。
ConfigMap创建方式
通过目录创建
配置文件目录
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# ll /root/k8s_practice/storage/configmap # 配置文件存在哪个目录下4 total 8 5 -rw-r--r-- 1 root root 159 Jun 7 14:52game.properties6 -rw-r--r-- 1 root root 83 Jun 7 14:53ui.properties7 [root@k8s-master storage]#8 [root@k8s-master storage]# cat configmap/game.properties # 涉及文件19 enemies=aliens10 lives=3 11 enemies.cheat=true 12 enemies.cheat.level=noGoodRotten13 secret.code.passphrase=UUDDLRLRBABAs14 secret.code.allowed=true 15 secret.code.lives=30 16 17 [root@k8s-master storage]#18 [root@k8s-master storage]# cat configmap/ui.properties # 涉及文件219 color.good=purple20 color.bad=yellow21 allow.textmode=true 22 how.nice.to.look=fairlyNice
创建ConfigMap并查看状态
1 [root@k8s-master storage]# kubectl create configmap game-config --from-file=/root/k8s_practice/storage/configmap2 configmap/game-config created3 [root@k8s-master storage]#4 [root@k8s-master storage]# kubectl get configmap5 NAME DATA AGE6 game-config 2 14s
查看ConfigMap有哪些数据
1 [root@k8s-master storage]# kubectl get configmap -o yaml ##### 查看方式12 apiVersion: v13 items:4 -apiVersion: v15 data:6 game.properties: |+ ##### 本段最后有一行空格,+表示保留字符串行末尾的换行7 enemies=aliens8 lives=3 9 enemies.cheat=true 10 enemies.cheat.level=noGoodRotten11 secret.code.passphrase=UUDDLRLRBABAs12 secret.code.allowed=true 13 secret.code.lives=30 14 15 ui.properties: | 16 color.good=purple17 color.bad=yellow18 allow.textmode=true 19 how.nice.to.look=fairlyNice20 kind: ConfigMap21 metadata:22 creationTimestamp: "2020-06-07T06:57:28Z" 23 name: game-config24 namespace: default25 resourceVersion: "889177" 26 selfLink: /api/v1/namespaces/default/configmaps/game-config27 uid: 6952ac85-ded0-4c5e-89fd-b0c6f0546ecf28 kind: List29 metadata:30 resourceVersion: "" 31 selfLink: "" 32 [root@k8s-master storage]#33 [root@k8s-master storage]# kubectl describe configmap game-config ##### 查看方式234 Name: game-config35 Namespace: default36 Labels:37 Annotations: 38 39 Data40 ==== 41 game.properties:42 ---- 43 enemies=aliens44 lives=3 45 enemies.cheat=true 46 enemies.cheat.level=noGoodRotten47 secret.code.passphrase=UUDDLRLRBABAs48 secret.code.allowed=true 49 secret.code.lives=30 50 51 52 ui.properties:53 ---- 54 color.good=purple55 color.bad=yellow56 allow.textmode=true 57 how.nice.to.look=fairlyNice58 59 Events:
通过文件创建
配置文件位置
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# cat /root/k8s_practice/storage/configmap/game.properties4 enemies=aliens5 lives=3 6 enemies.cheat=true 7 enemies.cheat.level=noGoodRotten8 secret.code.passphrase=UUDDLRLRBABAs9 secret.code.allowed=true 10 secret.code.lives=30
创建ConfigMap并查看状态
1 [root@k8s-master storage]# kubectl create configmap game-config-2 --from-file=/root/k8s_practice/storage/configmap/game.properties2 configmap/game-config-2created3 [root@k8s-master storage]#4 [root@k8s-master storage]# kubectl get configmap game-config-2 5 NAME DATA AGE6 game-config-2 1 29s
查看ConfigMap有哪些数据
1 [root@k8s-master storage]# kubectl get configmap game-config-2 -o yaml ##### 查看方式12 apiVersion: v13 data:4 game.properties: |+ ##### 本段最后有一行空格,+表示保留字符串行末尾的换行5 enemies=aliens6 lives=3 7 enemies.cheat=true 8 enemies.cheat.level=noGoodRotten9 secret.code.passphrase=UUDDLRLRBABAs10 secret.code.allowed=true 11 secret.code.lives=30 12 13 kind: ConfigMap14 metadata:15 creationTimestamp: "2020-06-07T07:05:47Z" 16 name: game-config-2 17 namespace: default18 resourceVersion: "890437" 19 selfLink: /api/v1/namespaces/default/configmaps/game-config-2 20 uid: 02d99802-c23f-45ad-b4e1-dea9bcb166d821 [root@k8s-master storage]#22 [root@k8s-master storage]# kubectl describe configmap game-config-2##### 查看方式223 Name: game-config-2 24 Namespace: default25 Labels:26 Annotations: 27 28 Data29 ==== 30 game.properties:31 ---- 32 enemies=aliens33 lives=3 34 enemies.cheat=true 35 enemies.cheat.level=noGoodRotten36 secret.code.passphrase=UUDDLRLRBABAs37 secret.code.allowed=true 38 secret.code.lives=30 39 40 41 Events:
通过命令行创建
创建ConfigMap并查看状态
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# kubectl create configmap special-config --from-literal=special.how=very --from-literal="special.type=charm" 4 configmap/special-config created5 [root@k8s-master storage]#6 [root@k8s-master storage]# kubectl get configmap special-config7 NAME DATA AGE8 special-config 2 23s
查看ConfigMap有哪些数据
1 [root@k8s-master storage]# kubectl get configmap special-config -o yaml ##### 查看方式12 apiVersion: v13 data:4 special.how: very5 special.type: charm6 kind: ConfigMap7 metadata:8 creationTimestamp: "2020-06-07T09:32:04Z" 9 name: special-config10 namespace: default11 resourceVersion: "912702" 12 selfLink: /api/v1/namespaces/default/configmaps/special-config13 uid: 76698e78-1380-4826-b5ac-d9c81f746eac14 [root@k8s-master storage]#15 [root@k8s-master storage]# kubectl describe configmap special-config ##### 查看方式216 Name: special-config17 Namespace: default18 Labels:19 Annotations: 20 21 Data22 ==== 23 special.how:24 ---- 25 very26 special.type:27 ---- 28 charm29 Events:
通过yaml文件创建
yaml文件
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# catconfigmap.yaml4 apiVersion: v15 kind: ConfigMap6 metadata:7 name: configmap-demo8 data:9 # 类属性键;每一个键都映射到一个简单的值10 player_initial_lives: "3" 11 ui_properties_file_name: 'user-interface.properties' 12 #13 # 类文件键14 game.properties: | 15 enemy.types=aliens,monsters16 player.maximum-lives=5 17 user-interface.properties: | 18 color.good=purple19 color.bad=yellow20 allow.textmode=true
创建ConfigMap并查看状态
1 [root@k8s-master storage]# kubectl apply -f configmap.yaml2 configmap/configmap-demo created3 [root@k8s-master storage]# kubectl get configmap configmap-demo4 NAME DATA AGE5 configmap-demo 4 2m59s
查看ConfigMap有哪些数据
1 [root@k8s-master storage]# kubectl get configmap configmap-demo -o yaml ##### 查看方式12 apiVersion: v13 data:4 game.properties: | 5 enemy.types=aliens,monsters6 player.maximum-lives=5 7 player_initial_lives: "3" 8 ui_properties_file_name: user-interface.properties9 user-interface.properties: | 10 color.good=purple11 color.bad=yellow12 allow.textmode=true 13 kind: ConfigMap14 metadata:15 annotations:16 kubectl.kubernetes.io/last-applied-configuration: | 17 {"apiVersion":"v1","data":{"game.properties":"enemy.types=aliens,monsters\nplayer.maximum-lives=5\n","player_initial_lives":"3","ui_properties_file_name":"user-interface.properties","user-interface.properties":"color.good=purple\ncolor.bad=yellow\nallow.textmode=true\n"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"configmap-demo","namespace":"default"}}18 creationTimestamp: "2020-06-07T11:36:46Z" 19 name: configmap-demo20 namespace: default21 resourceVersion: "931685" 22 selfLink: /api/v1/namespaces/default/configmaps/configmap-demo23 uid: fdad7000-87bd-4b72-be98-40dd8fe6400a24 [root@k8s-master storage]#25 [root@k8s-master storage]#26 [root@k8s-master storage]# kubectl describe configmap configmap-demo ##### 查看方式227 Name: configmap-demo28 Namespace: default29 Labels:30 Annotations: kubectl.kubernetes.io/last-applied-configuration:31 {"apiVersion":"v1","data":{"game.properties":"enemy.types=aliens,monsters\nplayer.maximum-lives=5\n","player_initial_lives":"3","ui_proper... 32 33 Data34 ==== 35 game.properties:36 ---- 37 enemy.types=aliens,monsters38 player.maximum-lives=5 39 40 player_initial_lives:41 ---- 42 3 43 ui_properties_file_name:44 ---- 45 user-interface.properties46 user-interface.properties:47 ---- 48 color.good=purple49 color.bad=yellow50 allow.textmode=true 51 52 Events:
Pod中使用ConfigMap
如何在Pod中使用上述的ConfigMap信息。
当前存在的ConfigMap
1 [root@k8s-master storage]# kubectl get configmap2 NAME DATA AGE3 configmap-demo 430m4 game-config 25h9m5 game-config-2 15h1m6 special-config 2 5m48s
使用ConfigMap来替代环境变量
yaml文件
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# catpod_configmap_env.yaml4 apiVersion: v15 kind: Pod6 metadata:7 name: pod-configmap-env 8 spec:9 containers:10 -name: myapp11 image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v112 command: ["/bin/sh", "-c", "env"]13 ### 引用方式114 env:15 -name: SPECAIL_HOW_KEY16 valueFrom:17 configMapKeyRef:18 name: special-config ### 这个name的值来自 ConfigMap19 key: special.how ### 这个key的值为需要取值的键20 -name: SPECAIL_TPYE_KEY21 valueFrom:22 configMapKeyRef:23 name: special-config24 key: special.type25 ### 引用方式226 envFrom:27 -configMapRef:28 name: game-config-2### 这个name的值来自 ConfigMap29 restartPolicy: Never
启动pod并查看状态
1 [root@k8s-master storage]# kubectl apply -f pod_configmap_env.yaml2 pod/pod-configmap-envcreated3 [root@k8s-master storage]#4 [root@k8s-master storage]# kubectl get pod -o wide5 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES6 pod-configmap-env 0/1 Completed 0 6s 10.244.2.147 k8s-node02
查看打印日志
1 [root@k8s-master storage]# kubectl logs pod-configmap-env 2 MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156 3 KUBERNETES_SERVICE_PORT=443 4 KUBERNETES_PORT=tcp://10.96.0.1:443 5 MYAPP_SVC_PORT_80_TCP_PORT=80 6 HOSTNAME=pod-configmap-env 7 SHLVL=1 8 MYAPP_SVC_PORT_80_TCP_PROTO=tcp9 HOME=/root10 SPECAIL_HOW_KEY=very ### 来自ConfigMap11 game.properties=enemies=aliens ### 来自ConfigMap12 lives=3### 来自ConfigMap13 enemies.cheat=true### 来自ConfigMap14 enemies.cheat.level=noGoodRotten ### 来自ConfigMap15 secret.code.passphrase=UUDDLRLRBABAs ### 来自ConfigMap16 secret.code.allowed=true### 来自ConfigMap17 secret.code.lives=30### 来自ConfigMap18 19 20 SPECAIL_TPYE_KEY=charm ### 来自ConfigMap21 MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80 22 NGINX_VERSION=1.12.2 23 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 24 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin25 KUBERNETES_PORT_443_TCP_PORT=443 26 KUBERNETES_PORT_443_TCP_PROTO=tcp27 MYAPP_SVC_SERVICE_HOST=10.98.57.156 28 KUBERNETES_SERVICE_PORT_HTTPS=443 29 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 30 PWD=/ 31 KUBERNETES_SERVICE_HOST=10.96.0.1 32 MYAPP_SVC_SERVICE_PORT=80 33 MYAPP_SVC_PORT=tcp://10.98.57.156:80
使用ConfigMap设置命令行参数
yaml文件
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# catpod_configmap_cmd.yaml4 apiVersion: v15 kind: Pod6 metadata:7 name: pod-configmap-cmd8 spec:9 containers:10 -name: myapp11 image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v112 command: ["/bin/sh", "-c", "echo \"===$(SPECAIL_HOW_KEY)===$(SPECAIL_TPYE_KEY)===\""]13 env:14 -name: SPECAIL_HOW_KEY15 valueFrom:16 configMapKeyRef:17 name: special-config18 key: special.how19 -name: SPECAIL_TPYE_KEY20 valueFrom:21 configMapKeyRef:22 name: special-config23 key: special.type24 restartPolicy: Never
启动pod并查看状态
1 [root@k8s-master storage]# kubectl apply -f pod_configmap_cmd.yaml2 pod/pod-configmap-cmd created3 [root@k8s-master storage]#4 [root@k8s-master storage]# kubectl get pod -o wide5 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES6 pod-configmap-cmd 0/1 Completed 0 5s 10.244.4.125 k8s-node01
查看打印日志
1 [root@k8s-master storage]# kubectl logs pod-configmap-cmd2 ===very===charm===
通过数据卷插件使用ConfigMap【推荐】
在数据卷里面使用ConfigMap,最基本的就是将文件填入数据卷,在这个文件中,键就是文件名【第一层级的键】,键值就是文件内容。
yaml文件
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# catpod_configmap_volume.yaml4 apiVersion: v15 kind: Pod6 metadata:7 name: pod-configmap-volume8 spec:9 containers:10 -name: myapp11 image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v112 #command: ["/bin/sh", "-c", "ls -l /etc/config/"]13 command: ["/bin/sh", "-c", "sleep 600"]14 volumeMounts:15 - name: config-volume16 mountPath: /etc/config17 volumes:18 - name: config-volume19 configMap:20 name: configmap-demo21 restartPolicy: Never
启动pod并查看状态
1 [root@k8s-master storage]# kubectl apply -f pod_configmap_volume.yaml2 pod/pod-configmap-volume created3 [root@k8s-master storage]#4 [root@k8s-master storage]# kubectl get pod -o wide5 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES6 pod-configmap-volume 1/1 Running 0 5s 10.244.2.153 k8s-node02
进入pod并查看
1 [root@k8s-master storage]# kubectl exec -it pod-configmap-volume sh 2 / # ls /etc/config3 game.properties player_initial_lives ui_properties_file_name user-interface.properties4 /#5 /#6 /#7 / # cat /etc/config/player_initial_lives8 3/#9 /#10 /#11 / # cat /etc/config/ui_properties_file_name12 user-interface.properties/#13 /#14 /#15 / # cat /etc/config/game.properties16 enemy.types=aliens,monsters17 player.maximum-lives=5 18 /#19 /#20 / # cat /etc/config/user-interface.properties21 color.good=purple22 color.bad=yellow23 allow.textmode=true
ConfigMap热更新
准备工作
yaml文件
1 [root@k8s-master storage]# pwd 2 /root/k8s_practice/storage3 [root@k8s-master storage]# catpod_configmap_hot.yaml4 apiVersion: v15 kind: ConfigMap6 metadata:7 name: log-config8 namespace: default9 data:10 log_level: INFO11 --- 12 apiVersion: apps/v113 kind: Deployment14 metadata:15 name: myapp-deploy16 namespace: default17 spec:18 replicas: 2 19 selector:20 matchLabels:21 app: myapp22 release: v123 template:24 metadata:25 labels:26 app: myapp27 release: v128 env: test29 spec:30 containers:31 -name: myapp32 image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v133 imagePullPolicy: IfNotPresent34 ports:35 - containerPort: 80 36 volumeMounts:37 - name: config-volume38 mountPath: /etc/config39 volumes:40 - name: config-volume41 configMap:42 name: log-config
应用yaml文件并查看状态
1 [root@k8s-master storage]# kubectl apply -f pod_configmap_hot.yaml2 configmap/log-config created3 deployment.apps/myapp-deploy created4 [root@k8s-master storage]#5 [root@k8s-master storage]# kubectl get configmap log-config6 NAME DATA AGE7 log-config 121s8 [root@k8s-master storage]#9 [root@k8s-master storage]# kubectl get pod -o wide10 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES11 myapp-deploy-58ff9c997-drhwk 1/1 Running 0 30s 10.244.2.154 k8s-node0212 myapp-deploy-58ff9c997-n68j2 1/1 Running 0 30s 10.244.4.126 k8s-node01
查看ConfigMap信息
1 [root@k8s-master storage]# kubectl get configmap log-config -o yaml2 apiVersion: v13 data:4 log_level: INFO5 kind: ConfigMap6 metadata:7 annotations:8 kubectl.kubernetes.io/last-applied-configuration: | 9 {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}}10 creationTimestamp: "2020-06-07T16:08:11Z" 11 name: log-config12 namespace: default13 resourceVersion: "971348" 14 selfLink: /api/v1/namespaces/default/configmaps/log-config15 uid: 7e78e1d7-12de-4601-9915-cefbc96ca305
查看pod中的ConfigMap信息
1 [root@k8s-master storage]# kubectl exec -it myapp-deploy-58ff9c997-drhwk -- cat /etc/config/log_level2 INFO
热更新
修改ConfigMap
1 [root@k8s-master storage]# kubectl edit configmap log-config ### 将 INFO 改为了 DEBUG2 # Please edit the object below. Lines beginning with a '#'will be ignored,3 # and an empty file will abort the edit. If an error occurs while saving this filewill be4 # reopened with the relevant failures.5 #6 apiVersion: v17 data:8 log_level: DEBUG9 kind: ConfigMap10 metadata:11 annotations:12 kubectl.kubernetes.io/last-applied-configuration: | 13 {"apiVersion":"v1","data":{"log_level":"DEBUG"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}}14 creationTimestamp: "2020-06-07T16:08:11Z" 15 name: log-config16 namespace: default17 resourceVersion: "971348" 18 selfLink: /api/v1/namespaces/default/configmaps/log-config19 uid: 7e78e1d7-12de-4601-9915-cefbc96ca305
查看ConfigMap信息
1 [root@k8s-master storage]# kubectl get configmap log-config -o yaml2 apiVersion: v13 data:4 log_level: DEBUG5 kind: ConfigMap6 metadata:7 annotations:8 kubectl.kubernetes.io/last-applied-configuration: | 9 {"apiVersion":"v1","data":{"log_level":"DEBUG"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}}10 creationTimestamp: "2020-06-07T16:08:11Z" 11 name: log-config12 namespace: default13 resourceVersion: "972893" 14 selfLink: /api/v1/namespaces/default/configmaps/log-config15 uid: 7e78e1d7-12de-4601-9915-cefbc96ca305
稍后10秒左右,再次查看pod中的ConfigMap信息
1 [root@k8s-master storage]# kubectl exec -it myapp-deploy-58ff9c997-drhwk -- cat /etc/config/log_level2 DEBUG
由此可见,完成了一次热更新
相关阅读
2、Kubernetes K8S之通过yaml创建pod与pod文件常用字段详解
———END———
如果觉得不错就关注下呗 (-^O^-) !
评论 (0)