k8s-configmap 在pod中使用

使用包含: 环境变量、文件和目录。

  1. 命令行字面量创建

    kubectl create configmap test-conf003 --from-literal=aa=1 --from-literal=bb=2
  2. 查看创建的configmap,yaml文件展示

    [root@controller01 confmap]# kubectl get configmap test-conf003 -o yaml
    apiVersion: v1
    data:
    aa: "1"
    bb: "2"
    kind: ConfigMap
    metadata:
    creationTimestamp: 2017-06-21T09:36:26Z
    name: test-conf003
    namespace: default
    resourceVersion: "2465633"
    selfLink: /api/v1/namespaces/default/configmaps/test-conf003
    uid: 1900baf0-5665-11e7-901f-6c92bf2e6e88
  3. 通过yaml文件创建pod,yaml文件

    apiVersion: v1
    kind: Pod
    metadata:
    name: testpod001
    spec:
    containers:
       - name: testcontainer
         image: 10.21.1.151/quicksilver/deploy_openresty:1.2
         volumeMounts:
           - name: testvolume
             mountPath: /tmp/config
           - name: nginx-config-volume
             mountPath: /tmp/
         command: ["/bin/sh", "-c", "/usr/local/openresty/nginx/sbin/nginx -c /tmp/nginx.conf"]
         env:
            - name: SPECIAL_KEY1
              valueFrom:
                  configMapKeyRef:
                      name: test-conf003 
                      key:  aa
            - name: SPECIAL_KEY2
              valueFrom:
                  configMapKeyRef:
                      name: test-conf003 
                      key:  bb
    volumes:
       - name: testvolume
         configMap:
            name: testconf004
       - name: nginx-config-volume
         configMap:
            name: nginx-conf
            items:
                - key: nginx.conf
                  path: nginx.conf
  4. exec进入pod

    [root@controller01 confmap]# kubectl exec -it testpod001  /bin/bash
  5. 查看pod从configmap中导入的环境变量

    [root@testpod001 /]# env | grep SPEC*
    SPECIAL_KEY2=2
    SPECIAL_KEY1=1

    这时我们看到configmap中的data数据已经导入到pod中的环境变量了。

  6. 查看文件的挂载

    [root@testpod001 /]# ll /tmp/
    total 0
    drwxrwxrwx 3 root root 143 Jun 22 02:14 config
    lrwxrwxrwx 1 root root  17 Jun 22 02:14 nginx.conf -> ..data/nginx.conf

    我们可以看到该文件夹下面总的文件数为0,config和nginx.conf都是挂载进来的,文件都是软连接

  7. 查看挂载目录中的文件

    [root@testpod001 config]# ll
    total 0
    lrwxrwxrwx 1 root root 14 Jun 22 02:14 test.sh -> ..data/test.sh
    lrwxrwxrwx 1 root root 12 Jun 22 02:14 test1 -> ..data/test1
    lrwxrwxrwx 1 root root 12 Jun 22 02:14 test2 -> ..data/test2
    lrwxrwxrwx 1 root root 15 Jun 22 02:14 test2.sh -> ..data/test2.sh
    lrwxrwxrwx 1 root root 26 Jun 22 02:14 test_configmap.yaml -> ..data/test_configmap.yaml
  8. 验证同挂载目录中的文件相互调用关系正确

    [root@testpod001 config]# cat test.sh
    #!/bin/bash
    cat ./test1
    [root@testpod001 config]# cat test1
    key=123
    [root@testpod001 config]# /bin/bash test.sh
    key=123

    这个例子的使用就是nginx,nginx.conf中需要调用当前目录的mine.type文件。

你可能感兴趣的:(k8s)