|NO.Z.00175|——————————|CloudNative|——|KuberNetes&配置管理.V06|——|configmap.v06|使用configmap定义容器|
一、使用configmap数据定义容器
### --- 使用congfigmap数据定义容器环境变量
~~~ 使用来自单个configmap的数据定义容器环境变量
[root@k8s-master01 configmap]# vim pod-single-configmap-env-variable.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
restartPolicy: Never
二、查看变量执行结果
### --- 创建pod
~~~ 现在,Pod的输出包括环境变量SPECIAL_LEVEL_KEY=very
[root@k8s-master01 configmap]# kubectl create -f pod-single-configmap-env-variable.yaml
pod/dapi-test-pod created
### --- 在创建pod的时候若是镜像拉取失败,可以更改使用本地镜像,不选择下载所有
[root@k8s-master01 configmap]# kubectl describe po busybox | grep Image
Image: busybox:1.28
Image ID: docker-pullable://busybox@sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
### --- 查看创建的configmap.yaml配置文件
[root@k8s-master01 configmap]# vim pod-single-configmap-env-variable.yaml
spec:
nodeName: k8s-master02
containers:
- name: test-container
image: busybox:1.28
imagePullPolicy: IfNotPresent
### --- 查看创建的容器
~~~ Completed;执行这个变量后就退出了,所以显示正常状态。
[root@k8s-master01 configmap]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dapi-test-pod 0/1 Completed 0 11m 172.27.14.215 k8s-node02 <none> <none>
三、查看变量执行结果
### --- 查看这个环境变量
~~~ 这种方式在生产环境下使用的不是很多,
[root@k8s-master01 configmap]# kubectl logs -f dapi-test-pod
SPECIAL_LEVEL_KEY=very
[root@k8s-master01 configmap]# kubectl get cm
NAME DATA AGE
special-config 2 38m
[root@k8s-master01 configmap]# kubectl get cm special-config -oyaml
special.how: very // special.how的值是very,它被用于这个下面这个pod
[root@k8s-master01 configmap]# cat pod-single-configmap-env-variable.yaml
valueFrom:
configMapKeyRef: // 这个pod把自己的值挂载到容器里面
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how // 读取的key值为special.how;把它的值赋给了变量,叫做special_level_key
四、可以直接在配置文件中赋值定义变量
### --- 删除之前的pod
[root@k8s-master01 configmap]# kubectl delete -f pod-single-configmap-env-variable.yaml
pod "dapi-test-pod" deleted
### --- 修改configmap.yaml配置参数
~~~ 使用单个key的,肯定没有这种写法方便,使用单个key定义环境变量是很不理想的状态。
[root@k8s-master01 configmap]# vim pod-single-configmap-env-variable.yaml
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
nodeName: k8s-master02
containers:
- name: test-container
image: busybox:1.28
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
env:
# Define the environment variable
#- name: SPECIAL_LEVEL_KEY
# valueFrom:
# configMapKeyRef:
# # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
# name: special-config
# # Specify the key associated with the value
# key: special.how
- name: test # 直接写一个key-velel的形式
value: test-value
- name: musqlHostAddress
value: 10.10.10.10
- name: mysqlPort
value: "3306" #only string
restartPolicy: Never
五、replace容器
### --- replace容器
[root@k8s-master01 configmap]# kubectl create -f pod-single-configmap-env-variable.yaml
pod/dapi-test-pod created
[root@k8s-master01 configmap]# kubectl get po -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dapi-test-pod 0/1 Completed 0 18s 172.25.92.78 k8s-master02 <none> <none>
### --- 查看打印的环境变量
~~~ # 形式一:读取configmap充当环境变量,
~~~ # 形式二:读取configmap定义配置文件。
[root@k8s-master01 configmap]# kubectl logs -f dapi-test-pod
mysqlPort=3306 // 自动手动添加的
special.type=charm // 从配置文件生成的;这个变量时从special-configmap中自动生成的。
special.how=very // 从配置文件生成的
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了