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创建方式
通过目录创建
配置文件目录
[root@k8s-master storage]# pwd
/root/k8s_practice/storage
[root@k8s-master storage]# ll /root/k8s_practice/storage/configmap # 配置文件存在哪个目录下
total 8
-rw-r--r-- 1 root root 159 Jun 7 14:52 game.properties
-rw-r--r-- 1 root root 83 Jun 7 14:53 ui.properties
[root@k8s-master storage]#
[root@k8s-master storage]# cat configmap/game.properties # 涉及文件1
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAs
secret.code.allowed=true
secret.code.lives=30
[root@k8s-master storage]#
[root@k8s-master storage]# cat configmap/ui.properties # 涉及文件2
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
创建ConfigMap并查看状态
[root@k8s-master storage]# kubectl create configmap game-config --from-file=/root/k8s_practice/storage/configmap
configmap/game-config created
[root@k8s-master storage]#
[root@k8s-master storage]# kubectl get configmap
NAME DATA AGE
game-config 2 14s
查看ConfigMap有哪些数据
[root@k8s-master storage]# kubectl get configmap -o yaml ##### 查看方式1
apiVersion: v1
items:
- apiVersion: v1
data:
game.properties: |+ ##### 本段最后有一行空格,+ 表示保留字符串行末尾的换行
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAs
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: "2020-06-07T06:57:28Z"
name: game-config
namespace: default
resourceVersion: "889177"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: 6952ac85-ded0-4c5e-89fd-b0c6f0546ecf
kind: List
metadata:
resourceVersion: ""
selfLink: ""
[root@k8s-master storage]#
[root@k8s-master storage]# kubectl describe configmap game-config ##### 查看方式2
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAs
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events: <none>
通过文件创建
配置文件位置
[root@k8s-master storage]# pwd
/root/k8s_practice/storage
[root@k8s-master storage]# cat /root/k8s_practice/storage/configmap/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAs
secret.code.allowed=true
secret.code.lives=30
创建ConfigMap并查看状态
[root@k8s-master storage]# kubectl create configmap game-config-2 --from-file=/root/k8s_practice