configmap:缩写 cm
configmap介绍
Kubernetes 允许将配置选项分离到单独的资源对象 ConfigMap 中,本质上就是一个键值对映射,值可以是短字面量,也可以是完整的配置文件。
应用无须直接读取 ConfigMap,甚至根本不需要知道其是否存在。映射的内容通过环境变量或者卷文件的形式传递给容器,而并非直接传递给容器。命令行参数的定义中可以通过$(ENV_VAR)
语法引用环境变量,因而可以达到将 ConfigMap 的条目当作命令行参数传递给进程的效果。
创建configmap
使用指令 kubectl 创建 configmap
以创建test-config
为例:
kubectl create configmap test-config --from-literal=age=25
信息释义:
test-config:configmap 中的键名,仅包含数字字母、-
、_
以及.
。
–from-literal:创建一个键值对的项,如–from-literal=age=25,若是多个项增加多个 --from-literal=
即可。
编写YAML创建 configmap
创建test-config.yaml文件:
apiVersion: v1
kind: ConfigMap
metadata:
# 映射的名称(通过这个名称引用configmap)
name: test-config
data:
# 数据需要用引号标记
username: admin
age: "25"
type: "1"
执行命令:kubectl create -f .\test-config.yaml
从文件内容创建 configmap 条目
kubectl create configmap
命令支持从磁盘上读取文件,并将文件内容单独存储为 configmap 中的条目:
还是上面的那个文件,然后执行命令:
kubectl create configmap test-config --from-file=test-config.yaml
命令释义:运行上面命令时,kubectl 会在当前目录下查找test-config.yaml文件,并将文件存储在 configmap 中以 test-config.yaml 为键名的条目下。
当然你如果想手动指定键名,也可以:
kubectl create configmap test-config --from-file=test-config=test-config.yaml
同样,多次使用 --form-file 参数可以增加多个文件条目。
从文件夹创建 configmap
可以引入某个文件夹的所有文件,这种情况下,kubectl 会为文件夹中的每个文件单独创建条目,仅限于那些文件名可作为合法的 configmap 键名的文件(有一个文件不合法,不会创建成功):
kubectl create configmap test-config --from-file=./config
configmap 的使用方式
通过环境变量的方式传递给pod
以上面的test-config.yaml文件为基础创建pod,新建pod-cm.yaml文件:
apiVersion: v1
kind: Pod
metadata:
name: pod-cm
spec:
containers:
- name: node-test
image: keyson/kubia
ports:
- containerPort: 8080
protocol: TCP
envFrom:
- configMapRef:
# configmap 的名称
name: test-config
执行命令:kubectl create -f pod-cm.yaml
当pod/pod-cm created 创建成功后,执行命令:kubectl exec pod-cm -- env
可看到结果,已经将配置信息传递进去了:
HOSTNAME=pod-cm
age=25
type=1
username=admin
KUBIA_HTTP_SERVICE_PORT=8080
...
NODE_VERSION=16.15.0
YARN_VERSION=1.22.18
HOME=/root
是的,上面是将test-config.yaml中所有内容作为环境变量,有没有说部分引入呢,比如中我只想要某个配置?答案是有的,接下创建新文件pod-cm-part.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod-cm
spec:
containers:
- name: node-test
image: keyson/kubia
ports:
- containerPort: 8080
protocol: TCP
# 这里使用env,不再是envFrom
env:
# 设置环境变量名
- name: TEST-CONFIG-USERNAME
valueFrom:
# 选择configmap中的键
configMapKeyRef:
# 指定configmap名称
name: test-config
# 需要引用的key
key: username
- name: TEST-CONFIG-AGE
valueFrom:
configMapKeyRef:
name: test-config
key: age
跟之前操作一样执行创建,然后查看结果:
PS C:\Users> kubectl exec pod-cm -- env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=pod-cm
TEST-CONFIG-USERNAME=admin
TEST-CONFIG-AGE=25
KUBERNETES_PORT_443_TCP_PORT=443
...
NODE_VERSION=16.15.0
YARN_VERSION=1.22.18
HOME=/root
通过volume的方式挂载到pod内
新建pod-cm2.yaml:
apiVersion: v1
kind: Pod
metadata:
name: pod-cm2
spec:
containers:
- name: node-test
image: keyson/kubia
ports:
- containerPort: 8080
protocol: TCP
# 用volume挂载方式
volumeMounts:
# 对应下面的volume名
- name: volume-cm
# 挂载到容器内部的路径
mountPath: "/data/config"
# 只读
readOnly: true
volumes:
# 卷的名称
- name: volume-cm
configMap:
# configMap的名称
name: test-config
执行命令:kubectl create -f pod-cm2.yaml
当pod/pod-cm2 created 创建成功后,执行命令:kubectl exec pod-cm2 -- ls /data/config
,查看结果:
PS C:\Users> kubectl exec pod-cm2 -- ls /data/config
age
type
username
执行命令:kubectl exec pod-cm2 -- cat /data/config/username
,查看结果:
PS C:\Users> kubectl exec pod-cm2 -- cat /data/config/username
admin
通过卷的 items 属性可以实现指定 KEY 挂载到容器或为配置文件设置别名:
apiVersion: v1
kind: Pod
metadata:
name: pod-cm2
spec:
containers:
- name: node-test
image: keyson/kubia
ports:
- containerPort: 8080
protocol: TCP
# 用volume挂载方式
volumeMounts:
# 对应下面的volume名
- name: volume-cm
# 挂载到容器内部的路径
mountPath: "/data/config"
# 只读
readOnly: true
volumes:
# 卷的名称
- name: volume-cm
configMap:
# configMap的名称
name: test-config
# 部分key挂载到容器
items:
- key: username
# 设置别名
path: username-name
- key: age
path: age
注意
volumeMount 额外的 subPath 字段可以被用作挂载卷中的某个独立文件或是文件夹,无须挂载完整卷。
configmap 常用命令总结
# 创建configmap
kubectl create configmap test-config --from-literal=sleep-interval=25
# 删除configmap
kubectl delete configmap test-config
# 查看已存在的configmap
kubectl get cm
# 编辑configmap
kubectl edit cm test-config
# 以yaml的方式查看某个configmap内容等
kubectl get cm test-config -o yaml
参考书籍:《Kubernetes in action》