首页 > 其他分享 >Kubernetes configmap 笔记

Kubernetes configmap 笔记

时间:2022-12-06 23:11:20浏览次数:73  
标签:configmap Kubernetes ConfigMap root master01 笔记 config special

ConigMap

什么是ConfigMap

ConfigMap 采用 key-value 格式进行保存数据,一般用来保存非敏感数据,Pods可以将configmap作为环境变量、命令行参数或卷中的配置文件使用。ConfigMap 将特定环境的配置从容器中解耦。

创建ConfigMap

官方文档

  1. 从目录创建

  2. 从文件创建

  3. 从envfile创建

  4. 从 literal values 创建

  5. ...

使用ConfigMap

  1. 以key-value为例

    创建 ConfigMap

kubectl create configmap special-config --from-literal=special.how=very
[root@master01 ~]# kubectl create configmap special-config --from-literal=special.how=very
configmap/special-config created
[root@master01 ~]# kubectl get configmap
NAME               DATA   AGE
kube-root-ca.crt   1      42d
special-config     1      9s
[root@master01 ~]# kubectl describe configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very


Events:  <none>

创建Pod

[root@master01 configmap]# cat configmap.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信息

[root@master01 configmap]# kubectl describe po dapi-test-pod
Name:         dapi-test-pod
Namespace:    default
Priority:     0
Node:         node01/192.168.44.13
Start Time:   Tue, 06 Dec 2022 22:06:41 +0800
Labels:       <none>
Annotations:  <none>
Status:       Succeeded
IP:           172.29.55.34
IPs:
  IP:  172.29.55.34
Containers:
  test-container:
    Container ID:  docker://341fdf9b58e1254265de902d6fd5e23be205fb66353e400174b7abd869afc2e7
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:59f225fdf34f28a07d22343ee415ee417f6b8365cf4a0d3a2933cbd8fd7cf8c1
    Port:          <none>
    Host Port:     <none>
    Command:
      /bin/sh
      -c
      env
    State:          Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Tue, 06 Dec 2022 22:07:01 +0800
      Finished:     Tue, 06 Dec 2022 22:07:01 +0800
    Ready:          False
    Restart Count:  0
    Environment:
      SPECIAL_LEVEL_KEY:  <set to the key 'special.how' of config map 'special-config'>  Optional: false
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-c7jnm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-c7jnm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  6m32s  default-scheduler  Successfully assigned default/dapi-test-pod to node01
  Normal  Pulling    6m31s  kubelet            Pulling image "busybox"
  Normal  Pulled     6m12s  kubelet            Successfully pulled image "busybox" in 18.272935062s
  Normal  Created    6m12s  kubelet            Created container test-container
  Normal  Started    6m12s  kubelet            Started container test-container
  1. 使用 yaml 创建 configmap
[root@master01 configmap]# cat config-mutikeys.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
[root@master01 configmap]# kubectl create -f config-mutikeys.yaml 
[root@master01 configmap]# kubectl get configmap
NAME               DATA   AGE
kube-root-ca.crt   1      42d
special-config     3      19m
[root@master01 configmap]# kubectl describe configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
SPECIAL_LEVEL:
----
very
SPECIAL_TYPE:
----
charm
Events:  <none>

创建Pod

[root@master01 configmap]# cat muti-keys-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: config-map-demo 
spec:
  containers:
    - name: test-config-map-1
      image: busybox
      command:
       - sleep
       - "3600"
      envFrom:
      - configMapRef:
          name: 
[root@master01 configmap]# cat muti-keys-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: config-map-demo 
spec:
  containers:
    - name: test-config-map-1
      image: busybox
      command:
       - sleep
       - "3600"
      envFrom:
      - configMapRef:
          name: special-config

[root@master01 configmap]# kubectl create -f muti-keys-demo.yaml 
pod/config-map-demo created
[root@master01 configmap]# kubectl get po 
NAME                                READY   STATUS        RESTARTS   AGE
busybox                             1/1     Terminating   8          33d
config-map-demo                     1/1     Running       0          4s
nginx-deployment-5787596d54-42qfx   1/1     Running       0          50m
nginx-deployment-5787596d54-6ffh4   1/1     Terminating   3          28d
nginx-deployment-5787596d54-7m47n   1/1     Running       4          28d
nginx-deployment-5787596d54-cnjb8   1/1     Terminating   3          28d
nginx-deployment-5787596d54-d4lkw   1/1     Running       0          50m
[root@master01 configmap]# kubectl describe po config-map-demo
Name:         config-map-demo
Namespace:    default
Priority:     0
Node:         node01/192.168.44.13
Start Time:   Tue, 06 Dec 2022 22:38:37 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           172.29.55.39
IPs:
  IP:  172.29.55.39
Containers:
  test-config-map-1:
    Container ID:  docker://d6c068ee4c3d771c0ce73f3be41fcb8abffe17f56b968974ed579af5b007edfc
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:59f225fdf34f28a07d22343ee415ee417f6b8365cf4a0d3a2933cbd8fd7cf8c1
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      3600
    State:          Running
      Started:      Tue, 06 Dec 2022 22:38:40 +0800
    Ready:          True
    Restart Count:  0
    Environment Variables from:
      special-config  ConfigMap  Optional: false
    Environment:      <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-c7jnm (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-c7jnm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-c7jnm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  16s   default-scheduler  Successfully assigned default/config-map-demo to node01
  Normal  Pulling    15s   kubelet            Pulling image "busybox"
  Normal  Pulled     13s   kubelet            Successfully pulled image "busybox" in 1.951543384s
  Normal  Created    13s   kubelet            Created container test-config-map-1
  Normal  Started    13s   kubelet            Started container test-config-map-1
[root@master01 configmap]# kubectl exec -ti  config-map-demo  -- sh 
/ # echo $SPECIAL_LEVEL
very
/ # echo $very

/ # echo $SPECIAL_TYPE
charm

用存储在ConfigMap中的数据填充卷

[root@master01 configmap]# cat configmap-volume.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-container-pod
spec:
  containers:
    - name: test-container-1
      image: busybox
      command:
       - sleep
       - "3600"
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config #挂载到 /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
[root@master01 configmap]# kubectl create -f configmap-volume.yaml 
pod/test-container-pod created
[root@master01 configmap]# kubectl get po 
NAME                                READY   STATUS        RESTARTS   AGE
busybox                             1/1     Terminating   8          33d
nginx-deployment-5787596d54-42qfx   1/1     Running       0          63m
nginx-deployment-5787596d54-6ffh4   1/1     Terminating   3          28d
nginx-deployment-5787596d54-7m47n   1/1     Running       4          28d
nginx-deployment-5787596d54-cnjb8   1/1     Terminating   3          28d
nginx-deployment-5787596d54-d4lkw   1/1     Running       0          63m
test-container-pod                  1/1     Running       0          4s
[root@master01 configmap]# kubectl exec -ti test-container-pod -- sh 
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # ls /etc/
config/      group        hostname     hosts        localtime    mtab         network/     passwd       resolv.conf  shadow
/ # ls /etc/config/
SPECIAL_LEVEL  SPECIAL_TYPE   special.how
/ # ll /etc/config/
sh: ll: not found
/ # ls -al  /etc/config/
total 0
drwxrwxrwx    3 root     root           119 Dec  6 14:51 .
drwxr-xr-x    1 root     root            20 Dec  6 14:51 ..
drwxr-xr-x    2 root     root            66 Dec  6 14:51 ..2022_12_06_14_51_09.874549422
lrwxrwxrwx    1 root     root            31 Dec  6 14:51 ..data -> ..2022_12_06_14_51_09.874549422
lrwxrwxrwx    1 root     root            20 Dec  6 14:51 SPECIAL_LEVEL -> ..data/SPECIAL_LEVEL
lrwxrwxrwx    1 root     root            19 Dec  6 14:51 SPECIAL_TYPE -> ..data/SPECIAL_TYPE
lrwxrwxrwx    1 root     root            18 Dec  6 14:51 special.how -> ..data/special.how

If there are some files in the /etc/config/ directory, they will be deleted.

注意事项

  1. 在 Pod 规约中引用某个 ConfigMap 之前,必须先创建这个对象, 或者在 Pod 规约中将 ConfigMap 标记为 optional如果所引用的 ConfigMap 不存在,并且没有将应用标记为 optional 则 Pod 将无法启动。

  2. 如果你使用 envFrom 来基于 ConfigMap 定义环境变量,那么无效的键将被忽略。 Pod 可以被启动,但无效名称将被记录在事件日志中(InvalidVariableNames

    kubectl get events
    
  3. 在 Pod 规约中将对 ConfigMap 的引用标记为 可选(optional)。 如果 ConfigMap 不存在,那么它在 Pod 中为其提供数据的配置(例如环境变量、挂载的卷)将为空。 如果 ConfigMap 存在,但引用的键不存在,那么数据也是空的

  4. 当某个已被挂载的 ConfigMap 被更新,所对应得内容跟最终也会被更新。但是使用 ConfigMap 作为 subPath 的数据卷不会更新

标签:configmap,Kubernetes,ConfigMap,root,master01,笔记,config,special
From: https://www.cnblogs.com/arvinhuang/p/16961728.html

相关文章

  • 系统分析师学习笔记(10)-结构化布线系统
    结构化布线系统包括:工作区子系统:接口到终端设备的连接水平子系统:信息插孔到楼层管理间的子系统垂直子系统:不同楼层之间的管理;管理子系统:将水平工作区子系统插孔连接过来的线......
  • 【《硬件架构的艺术》读书笔记】03 处理多个时钟(3)
    3.8异步FIFO(双时钟FIFO)  如上图,X通过xclk将数据写入FIFO,Y通过yclk将数据读出。注意这里写满标志信号在写时钟域,空信号在读时钟域。对比握手信号,异步FIFO用于对性能......
  • 笔记-Markdown语法
    1、图片缩放代码:<imgsrc="url"width="500"/>修改:制定比例:<imgsrc=""style="zoom:50%"/>......
  • 安卓APP源码和设计报告——麻雀笔记
    目录一安卓应用程序开发背景31.1开发背景31.2开发环境4二安卓应用程序开发理论与方法4三记事本应用程序的设计与实现53.1拟解决的问题及目标53.2总体设计63.3详细设计......
  • 详解支持向量机-SVC真实数据案例:预测明天是否会下雨-建模与模型评估以及不同方向的调
    视频作者:菜菜TsaiTsai链接:【技术干货】菜菜的机器学习sklearn【全85集】Python进阶_哔哩哔哩_bilibilifromsklearn.svmimportSVCfromsklearn.model_selectionimpo......
  • ES6笔记 - 原型对象扩展
    原型对象扩展目录原型对象扩展1.__proto__属性2.Object.setPrototypeOf()3.Object.getPrototypeOf()1.__proto__属性__proto__属性用来读取或设置当前对象的protot......
  • ConfigMap
    12.ConfigMap可以通过卷的方式挂载传递配置文信息或者可以通过环境变量来传递信息概述一般用ConfigMap去管理一些配置文件、或者一些大量的环境变量信息。ConfigM......
  • Kubernetes静态Pod
    一、什么是StaticPod静态Pod在指定的节点上由kubelet守护进程直接管理,不需要API服务器监管。与由控制面管理的Pod(例如,Deployment、RC、DaemonSet)不同;kubelet......
  • ES6笔记 - 扩展运算符
    扩展运算符目录扩展运算符1.数组中的扩展运算符1.1应用场景2.对象中的扩展运算符2.1应用场景3.解构赋值中【不一样的】扩展运算符4.浅拷贝问题1.数组中的扩展运算......
  • JS个人笔记
    1、引入javascript的几种方式<scripttype="text/javascript">window.alert("hellojs");//alert函数会阻塞整个页面加载的作用,当我们把script放到最前面......