k8s基础入门之配置管理ConfigMap

1 configMap概念

主要功能是为了解决应用程序会从配置文件、环境变量中获取配置信息。但是默认情况下配置信息需要与docker images解耦,ConfigMap API为我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制对象 ConfigMap API资源用来保存key-vlaue pair配置数据,这个数据可以在pods里使用,或者被用来为contaroller一样的系统组件存储配置数据。ConfigMap 是为了方便的处理不含铭感信息的字符串,你可以将它理解为Linux系统中的/etc目录,专门用来存储配置文件的目录
注意: ConfigMap不是属性配置文件的代替品,ConfigMap只是作为多个properties文件的引用。ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1 MiB。如果你需要保存超出此尺寸限制的数据,你可能希望考虑挂载存储卷 或者使用独立的数据库或者文件服务。

2 configMap应用场景

生成为容器内的环境变量
设置容器的启动命令的启动参数 (需要设置为环境变量)
以Volume的形式挂载为容器内部的文件或目录

3 创建ConfigMap对象

ConfigMap 资源对象使用key-value形式的键值对来配置数据,这些数据可以在Pod里面使用

3.1 通过 --from-literal

创建 configmap
[root@k8s-master01 ~]# kubectl create configmap testconfig1 --from-literal=db.host=127.0.0.1  --from-literal=db.port=6379 --from-literal=db.password=123456
configmap/testconfig1 created

查看创建的configmap
[root@k8s-master01 ~]# kubectl get cm
NAME          DATA   AGE
testconfig1   3      47s
[root@k8s-master01 ~]# kubectl describe cm testconfig1
Name:         testconfig1
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db.port:
----
6379
db.host:
----
127.0.0.1
db.password:
----
123456
Events:  <none>

[root@k8s-master01 ~]# kubectl get cm testconfig1  -o yaml
apiVersion: v1
data:
  db.host: 127.0.0.1
  db.password: "123456"
  db.port: "6379"
kind: ConfigMap
metadata:
  creationTimestamp: "2021-03-24T08:13:50Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:db.host: {}
        f:db.password: {}
        f:db.port: {}
    manager: kubectl
    operation: Update
    time: "2021-03-24T08:13:50Z"
  name: testconfig1
  namespace: default
  resourceVersion: "22300853"
  selfLink: /api/v1/namespaces/default/configmaps/testconfig1
  uid: 99ba63aa-d2f6-4934-bee0-75cd9064c49e

3.2 通过 --from-file

创建configmap
echo -n 172.18.8.200 > ./db.host
echo -n 3306 > ./db.port

[root@k8s-master01 ~]# kubectl create cm test-config2 --from-file=./db.host --from-file=./db.port
configmap/test-config2 created

查看configmap
[root@k8s-master01 ~]#  kubectl get cm test-config2 -o yaml
apiVersion: v1
data:
  db.host: 172.18.8.200
  db.port: "3306"
kind: ConfigMap
metadata:
  creationTimestamp: "2021-03-24T08:17:04Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:db.host: {}
        f:db.port: {}
    manager: kubectl
    operation: Update
    time: "2021-03-24T08:17:04Z"
  name: test-config2
  namespace: default
  resourceVersion: "22301419"
  selfLink: /api/v1/namespaces/default/configmaps/test-config2
  uid: 91a7fcff-f020-483b-842c-6b2333908173
每个文件对应一个变量内容

3.3 通过--from-env-file

创建configmap
cat << EOF > env.txt
db.host=172.18.8.200
db.port=3306
db.password=123456

EOF
[root@k8s-master01 ~]# kubectl create cm test-config3 --from-env-file=env.txt
configmap/test-config3 created

查看创建的configmap
[root@k8s-master01 ~]# kubectl get cm test-config3 -o yaml
apiVersion: v1
data:
  db.host: 172.18.8.200
  db.password: "123456"
  db.port: "3306"
kind: ConfigMap
metadata:
  creationTimestamp: "2021-03-24T08:19:42Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:db.host: {}
        f:db.password: {}
        f:db.port: {}
    manager: kubectl
    operation: Update
    time: "2021-03-24T08:19:42Z"
  name: test-config3
  namespace: default
  resourceVersion: "22301888"
  selfLink: /api/v1/namespaces/default/configmaps/test-config3
  uid: 405939cf-92a9-4b32-84a5-cc74fc43e367

3.4 YAML 配置文件

创建configmap
[root@k8s-master01 ~]# cat configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config4
data:
  db.host: 172.18.8.200
  db.port: "3306"
  db.password: "123456"
  heeh: heeh

[root@k8s-master01 ~]# kubectl create -f configmap.yaml 
configmap/test-config4 created
查看configmap

[root@k8s-master01 ~]# kubectl get cm test-config4 -o yaml
apiVersion: v1
data:
  db.host: 172.18.8.200
  db.password: "123456"
  db.port: "3306"
  heeh: heeh
kind: ConfigMap
metadata:
  creationTimestamp: "2021-03-24T08:22:08Z"
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:data:
        .: {}
        f:db.host: {}
        f:db.password: {}
        f:db.port: {}
        f:heeh: {}
    manager: kubectl
    operation: Update
    time: "2021-03-24T08:22:08Z"
  name: test-config4
  namespace: default
  resourceVersion: "22302311"
  selfLink: /api/v1/namespaces/default/configmaps/test-config4
  uid: 8835b6b9-b7fd-4507-8031-8d8d61649202

4 引用ConfigMap的二种方式

  • 第一种是通过环境变量的方式,直接传递给pod;
  • 第二种是作为volume的方式挂载到pod内。

4.1 通过环境变量使用

[root@k8s-master01 ~]# cat configmap_user1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: configmapuser11
spec:
  containers:
  - name: configmapuser11
    image: busybox
    command: [ "/bin/sh","-c","echo {DB_HOST}&& echo{DB_PORT}" ]
    env:
    - name: DB_HOST
      valueFrom:
        configMapKeyRef:
          name: test-config4
          key: db.host
    - name: DB_PORT
      valueFrom:
        configMapKeyRef:
          name: test-config4
          key: db.port

创建引用configmap的pod
[root@k8s-master01 ~]# kubectl apply -f configmap_user1.yaml 
pod/configmapuser11 created

[root@k8s-master01 ~]# kubectl get pods
NAME                     READY   STATUS        RESTARTS   AGE
configmapuser11          0/1     Completed     0          11s

验证
[root@k8s-master01 ~]# kubectl logs configmapuser11
172.18.8.200
3306

4.2 引用文件,不定义key

还可以通过envFrom、configMapRef、name使得configmap中的所有key/value对都自动变成环境变量。

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    command: [ "/bin/sh","-c","echo {DB_HOST}&& echo{DB_PORT}" ]
    envFrom:
    - configMapRef:
        name: test-config3

4.3 作为volume挂载使用

[root@k8s-master01 ~]# kubectl apply -f configmap_nginx.yaml 
configmap/configmap-demo-volume created
pod/test-demo-configmap-vloume created
[root@k8s-master01 ~]# cat configmap_nginx.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo-volume
  namespace: default
data:
  nginx.conf: |
    nginx.host: localhost
    nginx.port: "8080"

---

apiVersion: v1
kind: Pod
metadata:
  name: test-demo-configmap-vloume
spec:
  containers:
    - name: test-demo-cm
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/nginx.conf" ]
      volumeMounts:         #挂载声明
      - name: config-volume       #volume名称
        mountPath: /etc/config    #挂载点
  volumes:        
  - name: config-volume  #和volumeMounts的名称要一致
    configMap:
      name: configmap-demo-volume   #configmap名称

验证:
[root@k8s-master01 ~]# kubectl logs test-demo-configmap-vloume
nginx.host: localhost
nginx.port: "8080"

5 configMap变更生效方式

ConfigMap是名称空间级的资源,因此,引用它的Pod必须处于同一名称空间中。
ConfigMap 更新后滚动更新 Pod更新 ConfigMap 目前并不会触发相关 Pod的滚动更新,可以通过修改 pod annotations 的方式强制触发滚动更新
在.spec.template.metadata.annotations中添加version/config,每次通过修改version/config来触发滚动更新
更新 ConfigMap 后:使用该 ConfigMap 挂载的 Env 不会同步更新使用该 ConfigMap
挂载的 Volume 中的数据需要一段时间(实测大概10秒)才能同步更新

configmap变更触发应用更新
应用程序设置监听配置文件变更重新加载配置文件
sidecar容器监听配置文件是否更新,如果变化触发socket,http通知应用热更新
采用配置中心,如nacos,apollo

[root@k8s-master01 ~]# cat configmap_nginx.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo-volume
  namespace: default
data:
  nginx.conf: |
    nginx.host: localhost
    nginx.port: "8081"

---

apiVersion: v1
kind: Pod
metadata:
  name: test-demo-configmap-vloume
spec:
  containers:
    - name: test-demo-cm
      image: busybox
      args: ["/bin/sh", "-c", "sleep 30000"]
      volumeMounts:         #挂载声明
      - name: config-volume       #volume名称
        mountPath: /etc/config    #挂载点
  volumes:        
  - name: config-volume  #和volumeMounts的名称要一致
    configMap:
      name: configmap-demo-volume   #configmap名称

[root@k8s-master01 ~]# kubectl apply -f configmap_nginx.yaml
configmap/configmap-demo-volume created
pod/test-demo-configmap-vloume created
[root@k8s-master01 ~]# 
[root@k8s-master01 ~]# 
[root@k8s-master01 ~]# 
[root@k8s-master01 ~]# kubectl exec -it  test-demo-configmap-vloume sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
/ # cat /etc/config/nginx.conf 
nginx.host: localhost
nginx.port: "8081"
/ # 

修改configmap
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo-volume
  namespace: default
data:
  nginx.conf: |
    nginx.host: localhost
    nginx.port: "8082"

验证是否生效
/ # cat /etc/config/nginx.conf 
nginx.host: localhost
nginx.port: "8082"

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: