当前位置:网站首页>kubernetes之ConfigMap
kubernetes之ConfigMap
2022-07-26 16:07:00 【keyson R】
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》
边栏推荐
- Analyzing method and proc in Ruby
- Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
- RE9: read the paper deal inductive link prediction for nodes having only attribute information
- There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
- Bugku login1
- zabbix 6.2.0部署
- C语言重点知识总结
- DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
- I would like to ask you guys, how to specify the character set of MySQL CDC tables? I can't find the corresponding connector parameters on the official website. I read one
- Interface test for quick start of JMeter
猜你喜欢

The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact

我的sql没问题为什么还是这么慢|MySQL加锁规则

【ARM学习(9) arm 编译器了解学习(armcc/armclang)】

Operating system migration practice: deploying MySQL database on openeuler

Google Earth engine - merra-2 m2t1nxlv: 1980 present global pressure, temperature, wind and other data sets

PAT甲级 1046 Shortest Distance

2022牛客暑期多校训练营1(ACDGIJ)

Paper: all models are wrong, but many are useful: all models are wrong, but many are useful: understand the importance of variables by studying a whole class of prediction models at the same time

2022 latest Tibet Construction scaffolder (construction special operation) simulation exam questions and answers

Pat grade a 1045 favorite color stripe
随机推荐
Pat grade a 1044 shopping in Mars
Bucher gear pump qx81-400r301
操作系统迁移实战之在openEuler上部署MySQL数据库
2022 latest Beijing Construction Safety Officer simulation question bank and answers
Jointly discuss the opening of public data, and the "digital document scheme" appeared at the digital China Construction Summit
初识OpenGL (4)链接着色器
Bugku login1
Analyzing method and proc in Ruby
FTP protocol
量化交易之数字货币篇 - 通过时间戳与方向来合并逐笔成交数据(大单合并)
DTS is equipped with a new self-developed kernel, which breaks through the key technology of the three center architecture of the two places Tencent cloud database
Comprehensively design an oppe homepage -- Design of star models
终于有人把红蓝对抗讲明白了
Implementation of SAP ABAP daemon
[BJDCTF2020]Easy MD5
The solution to the display disorder of several events files in the tensorboard
Re8:读论文 Hier-SPCNet: A Legal Statute Hierarchy-based Heterogeneous Network for Computing Legal Case
Alibaba cloud DMS MySQL cloud database report error, solve!!
2022 test questions and answers for the latest national fire facility operator (senior fire facility operator)
《From SICP to Lisp》视频回播