|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

 

 

posted on   yanqi_vip  阅读(50)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示