• Kubernetes 第十二章 ConfigMap


    ConfigMap

    ConfigMap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。ConfigMap跟secret很类似,但它可以更方便地处理不包含敏感信息的字符串。

    ConfigMap创建

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

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

    # 从key-value字符串创建ConfigMap
    $ kubectl create configmap special-config --from-literal=special.how=very
    configmap "special-config" created
    $ kubectl get configmap special-config -o go-template='{{.data}}'
    map[special.how:very]
    
    # 从env文件创建
    $ echo -e "a=b
    c=d" | tee config.env
    a=b
    c=d
    $ kubectl create configmap special-config --from-env-file=config.env
    configmap "special-config" created
    $ kubectl get configmap special-config -o go-template='{{.data}}'
    map[a:b c:d]
    
    # 从目录创建
    $ mkdir config
    $ echo a>config/a
    $ echo b>config/b
    $ kubectl create configmap special-config --from-file=config/
    configmap "special-config" created
    $ kubectl get configmap special-config -o go-template='{{.data}}'
    map[a:a
     b:b
    ]

    ConfigMap使用

    ConfigMap可以通过多种方式在Pod中使用,比如设置环境变量、设置容器命令行参数、在Volume中创建配置文件等。

    注意
    
    ConfigMap必须在Pod引用它之前创建
    使用envFrom时,将会自动忽略无效的键
    Pod只能使用同一个命名空间内的ConfigMap
    apiVersion: v1
    kind: Pod
    metadata:
      name: test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          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@kube configMap]# kubectl log test-pod
    log is DEPRECATED and will be removed in a future version. Use logs instead.
    KUBERNETES_PORT=tcp://10.96.0.1:443
    KUBERNETES_SERVICE_PORT=443
    HOSTNAME=test-pod
    MY_SVC_SERVICE_PORT=80
    MY_SVC_PORT=tcp://10.96.31.10:80
    SHLVL=1
    HOME=/root
    MY_SVC_PORT_80_TCP_ADDR=10.96.31.10
    SPECIAL_TYPE_KEY=charm
    MY_SVC_PORT_80_TCP_PORT=80
    MY_SVC_PORT_80_TCP_PROTO=tcp
    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
    MY_SVC_PORT_80_TCP=tcp://10.96.31.10:80
    KUBERNETES_PORT_443_TCP_PROTO=tcp
    SPECIAL_LEVEL_KEY=very
    log_level=INFO
    MY_SVC_SERVICE_PORT_HTTP=80
    KUBERNETES_SERVICE_PORT_HTTPS=443
    KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
    KUBERNETES_SERVICE_HOST=10.96.0.1
    PWD=/
    MY_SVC_SERVICE_HOST=10.96.31.10
    [root@kube configMap]# 

    用作命令行参数

    将ConfigMap用作命令行参数时,需要先把ConfigMap的数据保存在环境变量中,然后通过$(VAR_NAME)的方式引用环境变量.

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          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@kube configMap]# kubectl logs  dapi-test-pod
    very charm
    [root@kube configMap]# 

    使用volume将ConfigMap作为文件或目录直接挂载

    将创建的ConfigMap直接挂载至Pod的/etc/config目录下,其中每一个key-value键值对都会生成一个文件,key为文件名,value为内容

    apiVersion: v1
    kind: Pod
    metadata:
      name: vol-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: special-config
      restartPolicy: Never
    [root@kube configMap]# kubectl logs vol-test-pod
    very
    [root@kube configMap]# 
    [root@kube configMap]# 

     将创建的ConfigMap中special.how这个key挂载到/etc/config目录下的一个相对路径/keys/special.level。如果存在同名文件,直接覆盖。其他的key不挂载

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh","-c","cat /etc/config/keys/special.level" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: special-config
            items:
            - key: special.how
              path: keys/special.level
      restartPolicy: Never
  • 相关阅读:
    mysql,windows自动备份设置
    彻底搞清楚javascript中的require、import和export
    Spring Boot 打包报错Failed to execute goal org.apache.maven.plugins:mavenresourcesplugin:3.2.0
    Spring AOP 切点切面
    12.5M 30M 90M DEM免费下载!【转】
    JS 中的数组遍历方式效率比较[转]
    cesium加载CAD模型(.dwg)
    Cesium发布下一代3D Tiles规范预览
    cesium点击面高亮事件[转]
    MySQL 5.7及8.0版本数据库的root密码遗忘的解决办法
  • 原文地址:https://www.cnblogs.com/zy09/p/11436229.html
Copyright © 2020-2023  润新知