• K8S中ConfigMap


    ConfigMap

    ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配
    置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也
    可以用来保存整个配置文件或者 JSON 二进制大对象

    ConfigMap创建

    可以使用kubectl create configmap从文件、目录或者key-value字符串创建等创建ConfigMap。

    使用目录创建 使用文件创建 使用字面值创建

    [root@k8s-master dir]# cat game.properties
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
    [root@k8s-master dir]# cat ui.properties
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
    [root@k8s-master dir]#
    root@k8s-master dir]# kubectl create configmap game-config --from-file=../dir/
    configmap/game-config created
    [root@k8s-master dir]# kubectl get cm
    NAME          DATA   AGE
    game-config   2      21s
    [root@k8s-master dir]# kubectl get cm game-config -o yaml
    apiVersion: v1
    data:
      game.properties: |
        enemies=aliens
        lives=3
        enemies.cheat=true
        enemies.cheat.level=noGoodRotten
        secret.code.passphrase=UUDDLRLRBABAS
        secret.code.allowed=true
        secret.code.lives=30
      ui.properties: |
        color.good=purple
        color.bad=yellow
        allow.textmode=true
        how.nice.to.look=fairlyNice
    kind: ConfigMap
    metadata:
      creationTimestamp: "2019-12-25T13:51:28Z"
      name: game-config
      namespace: default
      resourceVersion: "96998"
      selfLink: /api/v1/namespaces/default/configmaps/game-config
      uid: 7fa2195e-08b9-4ab2-927b-21420493e28f
    [root@k8s-master dir]# kubectl create configmap game-config2 --from-file=game.properties
    configmap/game-config2 created
    [root@k8s-master dir]# kubectl get cm
    NAME           DATA   AGE
    game-config    2      4m11s
    game-config2   1      28s
    [root@k8s-master dir]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
    configmap/special-config created
    [root@k8s-master dir]# kubectl get cm
    NAME             DATA   AGE
    game-config      2      9m29s
    game-config2     1      5m46s
    special-config   2      3s
    [root@k8s-master dir]# kubectl describe cm special-config
    Name:         special-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    special.how:
    ----
    very
    special.type:
    ----
    charm
    Events:  <none>
    [root@k8s-master dir]# vim env.yaml
    [root@k8s-master dir]# kubectl apply -f env.yaml
    configmap/env-config created
    [root@k8s-master dir]# kubectl get cm
    NAME             DATA   AGE
    env-config       1      6s
    game-config      2      14m
    game-config2     1      11m
    special-config   2      5m32s

    Pod 中使用 ConfigMap

    Ⅰ、使用 ConfigMap 来替代环境变量

    [root@k8s-master dir]# cat pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: wangyanglinux/myapp:v1
          command: [ "/bin/sh", "-c", "env" ]
          env:
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: special.how
            - name: SPECIAL_TYPE_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: special.type
          envFrom:
            - configMapRef:
                name: env-config
      restartPolicy: Never
    [root@k8s-master dir]# ll
    总用量 28
    -rw-r--r-- 1 root root 376 12月 25 22:48 111.yaml
    -rw-r--r-- 1 root root 105 12月 25 22:06 env.yaml
    -rw-r--r-- 1 root root 158 12月 25 21:50 game.properties
    -rw-r--r-- 1 root root 616 12月 25 23:09 log-config.yaml
    -rw-r--r-- 1 root root 560 12月 25 22:34 pod1.yaml
    -rw-r--r-- 1 root root 584 12月 25 22:26 pod.yaml
    -rw-r--r-- 1 root root  83 12月 25 21:50 ui.properties
    [root@k8s-master dir]# cat env.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: env-config
      namespace: default
    data:
      log_level: INFO
    [root@k8s-master dir]#

    结果:

    [root@k8s-master dir]# vim pod.yaml
    [root@k8s-master dir]# kubectl create -f pod.yaml
    pod/dapi-test-pod created
    [root@k8s-master dir]# kubectl get pod
    NAME            READY   STATUS      RESTARTS   AGE
    dapi-test-pod   0/1     Completed   0          3s
    [root@k8s-master dir]# kubectl logs pod dapi-test-pod
    Error from server (NotFound): pods "pod" not found
    [root@k8s-master dir]# kubectl logs dapi-test-pod
    MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156
    KUBERNETES_PORT=tcp://10.96.0.1:443
    KUBERNETES_SERVICE_PORT=443
    MYAPP_SVC_PORT_80_TCP_PORT=80
    HOSTNAME=dapi-test-pod
    SHLVL=1
    MYAPP_SVC_PORT_80_TCP_PROTO=tcp
    HOME=/root
    SPECIAL_TYPE_KEY=charm
    MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80
    NGINX_VERSION=1.12.2
    KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    KUBERNETES_PORT_443_TCP_PORT=443
    KUBERNETES_PORT_443_TCP_PROTO=tcp
    MYAPP_SVC_SERVICE_HOST=10.98.57.156
    SPECIAL_LEVEL_KEY=very
    log_level=INFO
    KUBERNETES_SERVICE_PORT_HTTPS=443
    KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
    PWD=/
    KUBERNETES_SERVICE_HOST=10.96.0.1
    MYAPP_SVC_SERVICE_PORT=80
    MYAPP_SVC_PORT=tcp://10.98.57.156:80

    Ⅱ、用 ConfigMap 设置命令行参数

    [root@k8s-master dir]# cat pod1.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod66
    spec:
      containers:
        - name: test-container
          image: wangyanglinux/myapp:v1
          command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
          env:
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: special.how
            - name: SPECIAL_TYPE_KEY
              valueFrom:
                configMapKeyRef:
                  name: special-config
                  key: special.type
      restartPolicy: Never
    [root@k8s-master dir]#

    结果:

    [root@k8s-master dir]# vim pod1.yaml
    [root@k8s-master dir]# kubectl create -f pod1.yaml
    pod/dapi-test-pod66 created
    [root@k8s-master dir]# kubectl get pod
    NAME              READY   STATUS      RESTARTS   AGE
    dapi-test-pod     0/1     Completed   0          7m45s
    dapi-test-pod66   0/1     Completed   0          5s
    [root@k8s-master dir]# kubectl logs dapi-test-pod66
    very charm

    Ⅲ、通过数据卷插件使用ConfigMap

    [root@k8s-master dir]# cat 111.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod11
    spec:
      containers:
        - name: test-container
          image: wangyanglinux/myapp:v1
          command: [ "/bin/sh", "-c", "sleep 600s" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: special-config
      restartPolicy: Never
    [root@k8s-master dir]#

    结果:

    [root@k8s-master dir]# vim 111.yaml
    [root@k8s-master dir]# kubectl create -f 111.yaml
    pod/dapi-test-pod11 created
    [root@k8s-master dir]# kubectl get pod
    NAME              READY   STATUS      RESTARTS   AGE
    dapi-test-pod     0/1     Completed   0          22m
    dapi-test-pod11   1/1     Running     0          5s
    dapi-test-pod66   0/1     Completed   0          14m
    [root@k8s-master dir]# kubectl exec dapi-test-pod11 -it -- /bin/sh
    / # cd /etc/config
    /etc/config # ls
    special.how   special.type
    /etc/config # cat special.how
    very/etc/config # cat special.type
    charm/etc/config # exit

    ConfigMap 的热更新

    [root@k8s-master dir]# cat log-config.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: log-config
      namespace: default
    data:
      log_level: INFO
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-nginx
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: my-nginx
      template:
        metadata:
          labels:
            app: my-nginx
        spec:
          containers:
          - name: my-nginx
            image: wangyanglinux/myapp:v1
            ports:
            - containerPort: 80
            volumeMounts:
            - name: config-volume
              mountPath: /etc/config
          volumes:
            - name: config-volume
              configMap:
                name: log-config
    [root@k8s-master dir]#

    结果:

    [root@k8s-master dir]# vim log-config.yaml
    [root@k8s-master dir]# kubectl apply -f log-config.yaml
    configmap/log-config unchanged
    deployment.apps/my-nginx created
    [root@k8s-master dir]# kubectl get pod
    NAME                        READY   STATUS    RESTARTS   AGE
    my-nginx-5d57c6897b-fm2ql   1/1     Running   0          8s
    [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /tec/config/log_level
    cat: can't open '/tec/config/log_level': No such file or directory
    command terminated with exit code 1
    [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
    INFO[root@k8s-master dir]# kubectl edit configmap log-config
    configmap/log-config edited
    [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
    INFO[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
    [root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
    DEBUG[root@k8s-master dir]#

    更新 ConfigMap 后:
    使用该 ConfigMap 挂载的 Env 不会同步更新
    使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新

  • 相关阅读:
    Round robin
    Linux命令之nslookup
    VLAN
    基础网络概念
    python开发_filecmp
    python开发_stat
    python开发_fileinput
    python开发_os.path
    python开发_bisect
    python开发_copy(浅拷贝|深拷贝)_博主推荐
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12099708.html
Copyright © 2020-2023  润新知