Kubernetes 如何使用configmap

Pod 中使用 ConfigMap

 

创建测试的两个ConfigMap


[root@k8s-master configmap]# cat special.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

  
[root@k8s-master configmap]# kubectl apply -f special.yml 
configmap/special-config created
[root@k8s-master configmap]# kubectl describe cm special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  
Data
====
special.how:
----
very
special.type:
----
charm
Events:  <none>
[root@k8s-master configmap]# cat env.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
[root@k8s-master configmap]# kubectl apply -f env.yml 
configmap/env-config created

  
[root@k8s-master configmap]# kubectl describe cm env-config
Name:         env-config
Namespace:    default
Labels:       <none>
Annotations:  
Data
====
log_level:
----
INFO
Events:  <none>

(1)使用 ConfigMap 来替代环境变量 


[root@k8s-master configmap]# cat pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:  
  containers:   
  - name: test-container 
    image: busybox 
    command: ["/bin/sh","-c","sleep 3600s"]
    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
[root@k8s-master configmap]# kubectl apply -f pod.yml 
pod/dapi-test-pod created


#这里会导入三个键值 
键 log_level 值INFO
键SPECIAL_LEVEL_KEY对应的值是special.how对应的值
键SPECIAL_TYPE_KEY对应的值是 special.type对应的值charm 

[root@k8s-master configmap]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
dapi-test-pod            1/1     Running   0          43s

[root@k8s-master configmap]# kubectl exec -it dapi-test-pod -- /bin/sh
/ # echo $SPECIAL_LEVEL_KEY  
very
/ # echo $SPECIAL_TYPE_KEY
charm
/ # echo $log_level
INFO

定义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

直接导入 

    envFrom:
      - configMapRef:
          name: env-config

 

使用configmap设置命令行参数 


[root@k8s-master configmap]# vim pod1.yml 
[root@k8s-master configmap]# cat pod1.yml 
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:  
  containers:   
  - name: test-container 
    image: 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
[root@k8s-master configmap]# kubectl apply -f pod1.yml 
pod/test-pod created
[root@k8s-master configmap]# kubectl get pod
NAME                     READY   STATUS             RESTARTS   AGE
dapi-test-pod            1/1     Running            0          12m
test-pod                 0/1     CrashLoopBackOff   1          28s

[root@k8s-master configmap]# kubectl logs test-pod
very charm

 

作为volume的方式挂载到pod内


通过数据卷使用configmap

[root@k8s-master configmap]# cat pod2.yml 
apiVersion: v1
kind: Pod
metadata:
  name: pod
spec:
  volumes:
  - name: config-volume
    configMap: 
      name: special-config  
  containers:   
  - name: test-container 
    image: busybox 
    command: ["/bin/sh","-c","sleep 3600" ]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config

[root@k8s-master configmap]# kubectl exec -it pod -- /bin/sh
/ # cd /etc/config/
/etc/config # ls
special.how   special.type
/etc/config # cat special.how 
very
/etc/config # cat special.type 
charm

 

configmap热更新


[root@k8s-master configmap]# cat nginx.conf 
server {
    listen	8000;
    server_name www.test.com;

    location / {
        root /usr/share/nginx/html;
	index index.html index.htm;
    }
}

[root@k8s-master configmap]# kubectl create configmap nginx-config --from-file=nginx.conf
configmap/nginx-config created

[root@k8s-master configmap]# kubectl describe cm nginx-config
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx.conf:
----
server {
    listen  8000;
    server_name www.test.com;

    location / {
        root /usr/share/nginx/html;
  index index.html index.htm;
    }
}

Events:  <none>


[root@k8s-master configmap]# cat pod.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
          - containerPort: 80
          volumeMounts:
          - name: config-volume
            mountPath: /etc/nginx/conf.d
      volumes:
        - name: config-volume
          configMap:
            name: nginx-config


[root@k8s-master configmap]# kubectl apply -f pod.yml 
deployment.apps/my-nginx created
[root@k8s-master configmap]# kubectl get pod -o wide
NAME                        READY   STATUS             RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
my-nginx-575fd5b9c4-jttrw   1/1     Running            0          87s   10.244.0.19   k8s-master   <none>           <none>

[root@k8s-master configmap]# curl 10.244.0.19:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>



[root@k8s-master configmap]# kubectl exec -it my-nginx-575fd5b9c4-jttrw -- /bin/sh
# cd /etc/nginx/conf.d
# ls
nginx.conf
# cat nginx.conf
server {
    listen	8000;
    server_name www.test.com;

    location / {
        root /usr/share/nginx/html;
	index index.html index.htm;
    }
}

Configmap解决的是配置文件和镜像的解耦,就是把配置文件独立出来 

#这里修改为80
[root@k8s-master configmap]# kubectl edit cm nginx-config
configmap/nginx-config edited

[root@k8s-master configmap]# kubectl describe cm  nginx-config
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx.conf:
----
server {
    listen  80;
    server_name www.test.com;

    location / {
        root /usr/share/nginx/html;
  index index.html index.htm;
    }
}

更新configmap里面的内容并没有触发更新容器内的数据,需要手动删除掉pod后,deployment会继续重新维护拉起一个pod,相当于触发更新。

滚动更新

ConfigMap 更新后滚动更新 Pod,每次通过修改“version/config”来触发Pod滚动更新。

[root@k8s-master configmap]# kubectl patch deployments.apps my-nginx --patch '{"spec":{"template":{"metadata":{"annotations":{"version/config":"20201028"}}}}}'
deployment.apps/my-nginx patched
[root@k8s-master configmap]# kubectl get pod -o wide
NAME                        READY   STATUS              RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
my-nginx-5db58c5f89-tq448   0/1     ContainerCreating   0          3s      <none>        k8s-master   <none>           <none>

[root@k8s-master configmap]# kubectl get pod -o wide
NAME                        READY   STATUS             RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
my-nginx-5db58c5f89-tq448   1/1     Running            0          81s   10.244.0.20   k8s-master   <none>           <none>

这个例子里我们在.spec.template.metadata.annotations中添加version/config,每次通过修改version/config来触发滚动更新(需要手动触发Pod滚动更新, 这样才能再次加载nginx.conf配置文件)

[root@k8s-master configmap]# curl 10.244.0.20:8000
curl: (7) Failed connect to 10.244.0.20:8000; Connection refused
[root@k8s-master configmap]# curl 10.244.0.20:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

总结

!!!更新 ConfigMap 后:

使用该 ConfigMap 挂载的 Env 不会同步更新

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

©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页