当前位置: 首页 > 工具软件 > MapD Core > 使用案例 >

云原生|kubernetes|关于configMap的一些学习

徐皓君
2023-12-01

前言:

configMap顾名思义--配置文件集合。主要作用是:

configmap是k8s中的应用配置管理方案,在configmap中,各个配置项都是以key-value的方式存在的,value的数据可以是一个配置文件的内容,这些配置项被保存在k8s使用的持久化存储etcd中。

这样就形成了一个k8s中的配置中心,可以独立的对configmap中的数据进行修改,然后将configmap挂载到pod中进行使用,可以以env的方式,也可以以配置文件的方式在pod中进行引用。这样配置和pod就实现了解耦,都是k8s中独立的资源对象了。




configMap的引用形式

configMap没有什么特别的类型就一种,主要作用是:

  • 将ConfigMap中的数据设置为环境变量
  • 将ConfigMap中的数据设置为命令行参数
  • 使用Volume将ConfigMap作为文件或目录挂载

(1)

将configMap中的数据设置为环境变量

命令行生成configMap文件cm-test1.yaml,其中定义了变量env1(字符串形式),它的值是CSDN。

k create cm cm-test1  --from-literal=env1=CSDN -n dev --dry-run=client -o yaml >cm-test1.yaml
k apply -f cm-test1.yaml

 将此configMap定义的变量挂载到pod内,这个pod是tomcat的:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-pod
  template:
    metadata:
      labels:
        app: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5-jre10-slim
        env:
          - name: FUCK  #在容器内的变量名称
            valueFrom:
              configMapKeyRef:
                name: cm-test1  #configMap的名称,上面的cm文件定义的名称
                key: env1   #要使用的key的值,上面只定义了这一个key

简单的验证:

部署这个tomcat的pod,等待pod启动正常如下:


[root@k8s-master ~]# k get po -A -owide
NAMESPACE     NAME                                                    READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
default       mysql-5.7.23-5f9bd6468c-h4gqn                           1/1     Running   4          3d6h    10.244.169.130   k8s-node2    <none>           <none>
dev           nginx-deployment-b785b4498-s26js                        1/1     Running   0          5h2m    10.244.36.73     k8s-node1    <none>           <none>
dev           tomcat-deployment-789b44ffcd-z7bfx                      1/1     Running   0          15m     10.244.169.142   k8s-node2    <none>           <none>

 进入pod,打印变量值,验证无误。

[root@k8s-master ~]# k exec -it tomcat-deployment-789b44ffcd-z7bfx -n dev -- /bin/bash
root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# env |grep CSDN
FUCK=CSDN
root@tomcat-deployment-789b44ffcd-z7bfx:/usr/local/tomcat# echo $FUCK
CSDN

(2)

命令行方式通过文件生成configMap,使用Volume将ConfigMap作为文件或目录挂载:

例如,现有一个tomcat,将首页index.jsp 更改后挂载到pod内,实现首页的变更

首页文件模板:

[root@k8s-master ~]# cat index.jsp 
this is a test page!!!!!
this is a test page!!!!!
this is a test page!!!!!
this is a test page!!!!!

生成configMap,这里叫cm-test ,namespace指定的是dev,最后执行此文件。

k create configmap cm-test --from-file=index.jsp -n dev -oyaml  --dry-run=client

[root@k8s-master ~]# cat cm-test.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-test
  namespace: dev
data:
  index.jsp: |
    this is a test page!!!!!
    this is a test page!!!!!
    this is a test page!!!!!
    this is a test page!!!!!
    this is a test page!!!!!
    sfsds this is a test page!!!!!
k apply -f cm-test.yaml

 

pod引用此configMap:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  namespace: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat-pod
  template:
    metadata:
      labels:
        app: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5-jre10-slim
        volumeMounts:
        - name: conf
          mountPath: /usr/local/tomcat/webapps/ROOT/index.jsp#由于这个版本的tomcat此目录下有其它文件,为防止被覆盖,因此设置subPath
          subPath: index.jsp
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp   #key不能写错,cm文件里定义的就是这个
            path: index.jsp  #挂载在容器后叫什么文件名
      nodeName: k8s-node2

 (3)

命令行方式,通过文件夹生成configMap清单文件(test文件夹内有三个文件):


echo hello > test/hello.txt
echo world > test/world.txt
cat test/index.jsp 
this is a test page!!!!!
this is a test page!!!!!
this is a test page!!!!!
this is a test page!!!!!

k create cm cm-test3 --from-file=test/  --dry-run=client -o yaml > cm-test3.yaml

 查看命令生成的文件的内容:

[root@k8s-master ~]# cat cm-test3.yaml 
apiVersion: v1
data:
  hello.txt: |
    hello
  index.jsp: |
    this is a test page!!!!!
    this is a test page!!!!!
    this is a test page!!!!!
    this is a test page!!!!!
  world.txt: |
    world
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: cm-test3

configMap的调用还是一样的,要么挂载为文件,要么作为环境变量,和它是由文件还是文件夹还是字符串生成没有什么特别的联系,该怎么调用就怎么调用。 

 

例如,集群的coredns使用的configMap:

[root@k8s-master ~]# k get cm -A
NAMESPACE     NAME                                 DATA   AGE
kube-system   calico-config                        4      2d13h
kube-system   coredns                              1      2d21h
kube-system   extension-apiserver-authentication   6      3d2h

coredns的configMap的详细内容: 

[root@k8s-master ~]# cat coredns/coredns-cm.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |   #这个就是key值了 Corefile
    .:53 {
        errors
        log
        health
        kubernetes cluster.local 10.254.0.0/18
        forward . /etc/resolv.conf
        cache 30
    }
conredns调用它的configMap:
#无关部分省略了
      containers:
      - name: coredns
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.7.0
        imagePullPolicy: IfNotPresent
        args: [ "-conf", "/etc/coredns/Corefile" ]
        volumeMounts:
        - name: config-volume
          mountPath: /etc/coredns
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
#一些存活探针什么的也省略了
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
            - key: Corefile  #coredns-cm.yaml文件里定义的
              path: Corefile  #挂载的文件名称




总结:

configMap和secret是比较类似的,作用基本相同,都是对于kubernetes集群内的配置文件解耦,调用方法也基本类似,都可以通过volume挂载方式直接挂到pod相关的容器内部,也可以作为系统环境变量注入到pod相关的容器内。都可以被多个pod同时调用,比如,Apod调用了名称为B的configMap的某个变量C,Dpod也可以调用BconfigMap的变量C,Epod当然也可以,以此类推。

只是挂载为文件的时候需要注意一点,如果挂载目标路径有文件,那么,挂载文件的时候将会覆盖,如果不想覆盖,比如,挂载到pod的容器的/etc目录下,这个时候肯定不希望覆盖了,如果覆盖容器可能都启动不了,就这个subPath的情况我专门做一下解释:

文件夹+文件的情形:

此时的容器内将会有 /etc/index.jsp 这个文件夹,此文件夹下有index.html 这个文件,也就是最终容器内有/etc/index.jsp/index.html这个文件。

        volumeMounts:
        - name: conf
          mountPath: /etc/index.jsp
         # subPath: index.html
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp
            path: index.html  #挂载在容器后叫什么文件名

 覆盖的情形:

此时的pod启动不了,启动失败,因为etc目录被覆盖了,/etc/目录下就只有一个index.html 文件了。

        volumeMounts:
        - name: conf
          mountPath: /etc/
         # subPath: index.html
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp
            path: index.html  #挂载在容器后叫什么文件名

正确的subPath挂载情形:

此时是挂载的文件,没有任何目录,文件名称是index.jsp,注意,这里使用了subPath,

 

        volumeMounts:
        - name: conf
          mountPath: /etc/index.jsp
          subPath: index.html
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp
            path: index.html  #挂载在容器后叫什么文件名
root@tomcat-deployment-d74966946-f8kpm:/etc# ls -al index.jsp 
-rw-r--r-- 1 root root 156 Oct 12 12:55 index.jsp

 

不能正确挂载configMap的情形:

subPath和path修改的不一样了,此时没有覆盖,但只有/etc/index.jsp文件夹,cm的内容是完全找不到的。 

        volumeMounts:
        - name: conf
          mountPath: /etc/index.jsp
          subPath: index.html
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp
            path: index.jsp  #挂载在容器后叫什么文件名
root@tomcat-deployment-6dc7fc8cbd-v6wcp:/etc# ls -al index.jsp/
total 0
drwxrwxrwx 2 root root  6 Oct 12 13:00 .
drwxr-xr-x 1 root root 23 Oct 12 13:00 ..

强烈推荐的做法:

是一直使用subPath并且subPath和path保持一致(注意了,注意了,这种subPath是推荐使用的,也应该一直使用的方法哦):

        volumeMounts:
        - name: conf
          mountPath: /etc/index.jsp
          subPath: index.jsp
        ports:
        - containerPort: 8080
      volumes:
      - name: conf
        configMap:
          name: cm-test
          items:
          - key: index.jsp
            path: index.jsp  #挂载在容器后叫什么文件名

 类似资料: