当前位置:网站首页>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》
边栏推荐
- 初识OpenGL (3)片段着色器(Fragment Shader)
- 我的sql没问题为什么还是这么慢|MySQL加锁规则
- Implementation of SAP ABAP daemon
- ZABBIX 6.2.0 deployment
- 2022年最新西藏建筑施工架子工(建筑特种作业)模拟考试试题及答案
- Summary of key knowledge of C language
- 【物理模拟】超简单的shape matching模拟刚体运动
- Jointly discuss the opening of public data, and the "digital document scheme" appeared at the digital China Construction Summit
- PAT甲级 1046 Shortest Distance
- Pat grade a 1050 string subtraction
猜你喜欢

Re8:读论文 Hier-SPCNet: A Legal Statute Hierarchy-based Heterogeneous Network for Computing Legal Case

综合设计一个OPPE主页--明星机型的设计

Implementation of SAP ABAP daemon

German EMG e-anji thruster ed301/6 HS

PAT甲级 1049 Counting Ones

Technology vane | interpretation of cloud native technology architecture maturity model

Google Earth Engine——MERRA-2 M2T1NXAER:1980-2022年气溶胶逐日数据集

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

互联网协议

Implementation of personalized healthy diet recommendation system based on SSM
随机推荐
JVM 的类初始化机制
初识OpenGL (3)片段着色器(Fragment Shader)
初识OpenGL (4)链接着色器
2022年最新北京建筑安全员模拟题库及答案
剑指offer专项突击版第11天
初识OpenGL (2)编译着色器
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
请问一下各位大佬,mysql-cdc建表如何指定表的字符集呢,在官网没找到相应的连接器参数。我读一个
How to use job plug-in type to call a kettle job through ETL scheduling tool taskctl?
Pat class a 1047 student list for course
Google Earth engine - merra-2 m2t1nxlv: 1980 present global pressure, temperature, wind and other data sets
Clojure Web Development -- ring user guide
Internet Protocol
TDengine 落地协鑫能科,数百亿数据压缩至 600GB
朋友圈如何测试(思维导图)
Interface test for quick start of JMeter
First knowledge of OpenGL (3) fragment shader
【物理模拟】最简单的shape matching的原理与实践
Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
Simulation of three-phase voltage source inverter based on SISOTOOL pole assignment PI parameters and pless