ConfigMap

 

configMap 描述信息

ConfigMap是一种API对象,用来将非加密数据保存到键值对中。可以用作环境变量、命令行参数或者存储卷中的配置文件。

ConfigMap可以将环境变量配置信息和容器镜像解耦,便于应用配置的修改。如果需要存储加密信息时可以使用Secret对象。

ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配 置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也 可以用来保存整个配置文件或者 JSON 二进制大对象

 作用就是存储配置信息!

ConfigMap 的创建

 

使用目录创建

 ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties

$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

 

—from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容

 

[root@k8s-master01 ~]# pwd
/root
[root@k8s-master01 ~]# mkdir configmap
[root@k8s-master01 ~]# cd configmap/
[root@k8s-master01 configmap]# mkdir dir
[root@k8s-master01 configmap]# cd dir/


[root@k8s-master01 dir]# vim game.properties
[root@k8s-master01 dir]# vim ui.properties


[root@k8s-master01 dir]# kubectl create configmap game-config --from-file=../dir
configmap/game-config created


[root@k8s-master01 dir]# kubectl get cm
NAME               DATA   AGE
game-config        2      11s
kube-root-ca.crt   1      10d


[root@k8s-master01 dir]# kubectl get cm game-config -o yaml
apiVersion: v1
data:
  game.properties: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: "2022-07-13T02:22:15Z"
  name: game-config
  namespace: default
  resourceVersion: "57642"
  uid: 64e4024d-92e8-4f83-acb0-ad3d2716b65f


[root@k8s-master01 dir]# kubectl  describe cm game-config
Name:         game-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice


BinaryData
====

Events:  <none>

 

 

 

使用文件创建

只要指定为一个文件就可以从单个文件中创建 ConfigMap

 


$ kubectl create configmap game-config-2 --from-file=docs/user•guide/configmap/kubectl/game.properties

$ kubectl get configmaps game-config-2 -o yaml

—from-file 这个参数可以使用多次,你可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的

 

 

 [root@k8s-master01 dir]# kubectl create configmap game-config-2 --from-file=game.properties
configmap/game-config-2 created


[root@k8s-master01 dir]# kubectl get cm
NAME               DATA   AGE
game-config        2      7m56s
game-config-2      1      7s
kube-root-ca.crt   1      10d


[root@k8s-master01 dir]# kubectl describe cm game-config-2
Name:         game-config-2
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
game.properties:(键名)
----
enemies=aliens(键值)
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30


BinaryData
====

Events:  <none>
[root@k8s-master01 dir]#

 

使用字面值创建

使用文字值创建,利用 —from-literal 参数传递配置信息,该参数可以使用多次,格式如下


$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

$ kubectl get configmaps special-config -o yaml

[root@k8s-master01 dir]#  kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created

 [root@k8s-master01 dir]# kubectl describe cm special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:(键名)
----
very               (键值)
special.type:(键名)
----
charm            (键值)

BinaryData
====

Events:  <none>
[root@k8s-master01 dir]#


 

 

Pod 中使用 ConfigMap

 

使用 ConfigMap 来替代环境变量

 1、


apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm

 


[root@k8s-master01 env]# kubectl get cm
NAME               DATA   AGE
game-config        2      17m
game-config-2      1      10m
kube-root-ca.crt   1      10d
special-config     2      5m51s

 

2、


apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO

 

root@k8s-master01 configmap]# pwd
/root/configmap
[root@k8s-master01 configmap]# ls
dir
[root@k8s-master01 configmap]# mkdir env
[root@k8s-master01 configmap]# cd env/


[root@k8s-master01 env]# vim env.yaml


[root@k8s-master01 env]# kubectl apply -f env.yaml
configmap/env-config created


[root@k8s-master01 env]# kubectl get cm
NAME               DATA   AGE
env-config         1      9s
game-config        2      17m
game-config-2      1      10m
kube-root-ca.crt   1      10d
special-config     2      5m51s
[root@k8s-master01 env]#

3、

apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
  - name: test-container
    image: hub.atguigu.com/library/myapp:v1
    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@k8s-master01 env]# vim pod.yaml

 [root@k8s-master01 env]# kubectl create -f pod.yaml
pod/dapi-test-pod created


[root@k8s-master01 env]# kubectl get pod
NAME            READY   STATUS      RESTARTS   AGE
dapi-test-pod   0/1     Completed   0          3s
[root@k8s-master01 env]#

[root@k8s-master01 env]# kubectl logs dapi-test-pod
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=dapi-test-pod
HOME=/root
PKG_RELEASE=1~bullseye
SPECIAL_TYPE_KEY=charm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NGINX_VERSION=1.21.5
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
NJS_VERSION=0.7.1
KUBERNETES_PORT_443_TCP_PROTO=tcp
SPECIAL_LEVEL_KEY=very
log_level=INFO
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
[root@k8s-master01 env]#

 

这个就是通过configmap把环境变量注入到pod内部

 

用 ConfigMap 设置命令行参数

 


apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm

apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
  - name: test-container
    image: nginx
    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@k8s-master01 env]# vim pod1.yaml
[root@k8s-master01 env]# kubectl create -f pod1.yaml
pod/dapi-test-pod66 created


[root@k8s-master01 env]# kubectl get pod
NAME              READY   STATUS      RESTARTS   AGE
dapi-test-pod     0/1     Completed   0          8m37s
dapi-test-pod66   0/1     Completed   0          10s


[root@k8s-master01 env]# kubectl logs dapi-test-pod66
very charm
[root@k8s-master01 env]#

 

通过数据卷插件使用ConfigMap

 


apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm

在数据卷里面使用这个 ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容

 


apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
  - name: test-container
    image: nginx
    command: [ "/bin/sh", "-c", "sleep 600s" ]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
volumes:
  - name: config-volume
    configMap:
      name: special-config
restartPolicy: Never

 

 

[root@k8s-master01 env]# vim 111.yaml


[root@k8s-master01 env]# kubectl apply -f 111.yaml
pod/dapi-test-pod11 created

[root@k8s-master01 env]# kubectl get pod
NAME              READY   STATUS      RESTARTS   AGE
dapi-test-pod     0/1     Completed   0          16m
dapi-test-pod11   1/1     Running   0          26s
dapi-test-pod66   0/1     Completed   0          8m28s
[root@k8s-master01 env]#

[root@k8s-master01 env]# kubectl exec dapi-test-pod11 -it -- /bin/sh
# cd /etc/config
# ls
special.how  special.type
# cat special.how
very# cat special.type
charm#

 

 

ConfigMap 的热更新


apiVersion: v1
kind: ConfigMap
metadata:
name: log-config
namespace: default
data:
log_level: INFO
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
template:
  metadata:
    labels:
      run: my-nginx
  spec:
    containers:
    - name: my-nginx
      image: nginx
      ports:
      - containerPort: 80
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
    volumes:
      - name: config-volume
        configMap:
          name: log-config

 

 

[root@k8s-master01 config]# kubectl apply -f 111.yaml
configmap/log-config created
The Deployment "my-nginx" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"run":"my-nginx"}: `selector` does not match template `labels`
(这里会报错!)

 

 

 

 

[root@k8s-master01 config]# kubectl apply -f 111.yaml
configmap/log-config unchanged
deployment.apps/my-nginx created
[root@k8s-master01 config]#

[root@k8s-master01 config]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-6cc567cbd6-fg4t8   1/1     Running   0          2m10s


[root@k8s-master01 config]# kubectl exec my-nginx-6cc567cbd6-fg4t8 -it -- cat /etc/config/log_level
INFO[root@k8s-master01 config]#

 

(kubectl exec kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2 cat/etc/config/log_levelINFO)

 

修改 ConfigMap

 

 [root@k8s-master01 config]# kubectl edit  configmap log-config
configmap/log-config edited

 

 

修改 log_level 的值为 DEBUG

 

 

 

 

[root@k8s-master01 config]# kubectl exec my-nginx-6cc567cbd6-fg4t8 -it -- cat /etc/config/log_level
DEBUG

[root@k8s-master01 config]#

 

 

理解:

我们刚刚进入修改confimap,pod的内容已经发生了变化。如果这个是nginx的配置文件,这样的话说明已经达到了热更新的目的了!

 

 

(kubectl exec kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2 cat /tmp/log_levelDEBUG)

 

ConfigMap 更新后滚动更新 Pod

 

kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations":{"version/config": "20190411" }}}}}'(修改时间)

这个例子里我们在 .spec.template.metadata.annotations 中添加 version/config ,每次通过修改version/config 来触发滚动更新

 

!!! 更新 ConfigMap 后:

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

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

 

 

 

 

 



posted @ 2022-07-13 11:51  屯子里唯一的架构师  阅读(84)  评论(0编辑  收藏  举报