|NO.Z.00176|——————————|CloudNative|——|KuberNetes&配置管理.V07|——|configmap.v07|configmap挂载到容器中充当配置文件|
一、把configmap挂载到容器中充当配置文件:形式一:修改configmap配置参数
### --- 删除之前的容器
[root@k8s-master01 configmap]# kubectl delete -f pod-single-configmap-env-variable.yaml
pod "dapi-test-pod" deleted
### --- 修改配置参数,挂载到容器中
[root@k8s-master01 ~]# kubectl describe cm special-config
Name: special-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
special.type: // 这个configmap是以kv的形式存在的,可能会把这个special.type:当做文件名挂载进去,下面可能会是内容
----
charm
special.how:
----
very // 形式一:它直接把key:special.how当做文件名,vlelel当做文件的内容,very
Events: <none>
二、形式二:修改配置configmap配置参数
### --- 修改配置参数,把配置挂载到容器中
[root@k8s-master01 ~]# kubectl describe cm game-ui-cm-custom-name
Name: game-ui-cm-custom-name
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
ui-pro: // 这个可能是文件名,下面的是它的内容,
----
color.good=purple // 形式二:把configmap创建的文件名当做文件名,把velelel当做里面的内容挂载到容器里面,当做配置文件。
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events: <none>
三、修改容器配置参数pod-single-configmap-env-variable.yaml
### --- 实验把两种形式全部挂载进去,修改配置yaml文件
[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", "sleep 3600" ]
volumeMounts:
- name: config-volume
mountPath: /mnt/1
- name: contain-filename
mountPath: /mnt/2
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
value: test-value
- name: musqlHostAddress
value: 10.10.10.10
- name: mysqlPort
value: "3306" #only string
restartPolicy: Never
volumes:
- name: config-volume
configMap:
name: special-config
- name: contain-filename
configMap:
name: game-ui-cm-custom-name
四、创建容器
### --- 创建容器
[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 1/1 Running 0 15s 172.25.92.79 k8s-master02 <none> <none>
### --- 进入容器查看容器配置参数
[root@k8s-master01 configmap]# kubectl exec -ti dapi-test-pod sh
/ # cd /mnt/
/mnt # cat 1/special.how // key名
very // 值
~~~ 形式一:它直接把key:special.how当做文件名,vlelel当做文件的内容
/mnt # cat 2/ui-pro
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
~~~ 形式二:把configmap创建的文件名当做文件名,把velelel当做里面的内容
### --- 挂载时会覆盖目录的,它会把整个目录挂载使用,
~~~ 在生产环境中,使用最普遍的是直接挂载一个配置文件,比如nginx的配置文件,
~~~ 或者zookeeper的配置文件。
~~~ 也可以直接指定key
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:你的「微服务管家」又秀新绝活了