k8s教程06(kubernetes-存储configmap)

configMap描述信息

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

ConfigMap的创建

1、使用目录创建

$ 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 ~]# mkdir configmap
[root@k8s-master01 ~]# cd configmap/
[root@k8s-master01 configmap]# mkdir dir
[root@k8s-master01 configmap]# cd dir
[root@k8s-master01 dir]# vi 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
[root@k8s-master01 dir]# vi ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
[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      30s
[root@k8s-master01 dir]# kubectl get cm game-config
NAME          DATA   AGE
game-config   2      52s
[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-06-02T09:50:58Z"
  name: game-config
  namespace: default
  resourceVersion: "354938"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: e009339a-1f8a-4ae2-9bca-762d4c3b3e1c
[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

Events:  <none>


2、使用文件创建

只要指定为一个文件就可以从单个文件中创建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 get cm
NAME            DATA   AGE
game-config     2      5m56s
game-config-2   1      82s
[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

Events:  <none>

3、使用字面值创建

使用文字值创建,利用 --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 configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.type:
----
charm
special.how:
----
very
Events:  <none>

Pod中使用ConfigMap

1、使用ConfigMap来替代环境变量

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

查看已经有了special-config 就不用创建上面的了

[root@k8s-master01 ~]# kubectl get cm
NAME             DATA   AGE
game-config      2      3d20h
game-config-2    1      3d20h
special-config   2      3d20h
[root@k8s-master01 configmap]# mkdir env
[root@k8s-master01 configmap]# cd env/
[root@k8s-master01 env]# vi env.yaml

vi env.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
[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      74s
game-config      2      3d20h
game-config-2    1      3d20h
special-config   2      3d20h
[root@k8s-master01 env]# kubectl describe cm env-config
Name:         env-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"env-config","namespace":"default"}}

Data
====
log_level:
----
INFO
Events:  <none>
[root@k8s-master01 env]# kubectl get cm special-config
NAME             DATA   AGE
special-config   2      3d20h
[root@k8s-master01 env]# kubectl describe cm special-config   
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very
special.type:
----
charm
Events:  <none>

[root@k8s-master01 env]# kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
deployment1-7d967c7ff5-7sfn2   1/1     Running   1          3d22h
deployment1-7d967c7ff5-87t6s   1/1     Running   1          3d22h
deployment2-659cdb8b84-64lxg   1/1     Running   1          3d22h
deployment2-659cdb8b84-pfvbk   1/1     Running   1          3d22h
deployment3-66cd495565-n6q4h   1/1     Running   1          3d22h
deployment3-66cd495565-x8h7m   1/1     Running   1          3d22h
[root@k8s-master01 env]# kubectl delete deployment --all
deployment.extensions "deployment1" deleted
deployment.extensions "deployment2" deleted
deployment.extensions "deployment3" deleted
[root@k8s-master01 env]# kubectl delete ingress --all
ingress.extensions "https" deleted
ingress.extensions "ingress-with-auth" deleted
ingress.extensions "ingress1" deleted
ingress.extensions "ingress2" deleted
ingress.extensions "nginx-test" deleted
[root@k8s-master01 env]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP   3d22h
svc-1        ClusterIP   10.106.164.106   <none>        80/TCP    3d22h
svc-2        ClusterIP   10.101.111.202   <none>        80/TCP    3d22h
svc-3        ClusterIP   10.105.96.244    <none>        80/TCP    3d22h
[root@k8s-master01 env]# kubectl delete svc svc-1 svc-2 svc-3 
service "svc-1" deleted
service "svc-2" deleted
service "svc-3" deleted

vi pod.yaml

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]# 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          9m15s
[root@k8s-master01 env]# kubectl log dapi-test-pod 
log is DEPRECATED and will be removed in a future version. Use logs instead.
MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
MYAPP_SVC_PORT_80_TCP_PORT=80
HOSTNAME=dapi-test-pod
SHLVL=1
MYAPP_SVC_PORT_80_TCP_PROTO=tcp
HOME=/root
SPECIAL_TYPE_KEY=charm
MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80
NGINX_VERSION=1.12.2
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYAPP_SVC_SERVICE_HOST=10.98.57.156
SPECIAL_LEVEL_KEY=very
log_level=INFO
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
MYAPP_SVC_SERVICE_PORT=80
MYAPP_SVC_PORT=tcp://10.98.57.156:80

2、用ConfigMap设置命令行参数

special-config已经存在,就不用创建了

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

vi pod1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod66
spec:
  containers:
    - name: test-container
      image: hub.atguigu.com/library/myapp:v1
      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]# 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          23m
dapi-test-pod66   0/1     Completed   0          38s
[root@k8s-master01 env]# kubectl log dapi-test-pod66
log is DEPRECATED and will be removed in a future version. Use logs instead.
very charm

3、通过数据卷插件使用ConfigMap

special-config已经存在,就不用创建了

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

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

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod77
spec:
  containers:
    - name: test-container
      image: hub.atguigu.com/library/myapp:v1
      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]# kubectl delete pod --all
pod "dapi-test-pod" deleted
pod "dapi-test-pod66" deleted
pod "dapi-test-pod77" deleted
[root@k8s-master01 env]# kubectl apply -f pod2.yaml 
pod/dapi-test-pod77 created
[root@k8s-master01 env]# kubectl get pod
NAME              READY   STATUS    RESTARTS   AGE
dapi-test-pod77   1/1     Running   0          9s
[root@k8s-master01 env]# kubectl exec dapi-test-pod77 -it -- /bin/sh
/ # cd /etc/config/
/etc/config # ls
special.how   special.type
/etc/config # cat special.type 
charm
/etc/config # cat special.how 
very
/etc/config # 

ConfigMap的热更新

vi abc.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: log-config
  namespace: default
data:
  log_level: INFO
---
apiversion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: hub.atguigu.com/library/myapp:v1
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: log-config

[root@k8s-master01 env]# kubectl delete pod --all
pod "dapi-test-pod77" deleted
[root@k8s-master01 env]# kubectl get cm
NAME             DATA   AGE
env-config       1      83m
game-config      2      3d22h
game-config-2    1      3d21h
special-config   2      3d21h
[root@k8s-master01 env]# kubectl delete cm --all
configmap "env-config" deleted
configmap "game-config" deleted
configmap "game-config-2" deleted
configmap "special-config" deleted
[root@k8s-master01 env]# kubectl get cm
No resources found.
[root@k8s-master01 env]# cd ..
[root@k8s-master01 configmap]# mkdir config
[root@k8s-master01 configmap]# cd config/
[root@k8s-master01 config]# kubectl apply -f abc.yaml 
configmap/log-config unchanged
deployment.extensions/my-nginx created
[root@k8s-master01 config]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7b55868ff4-nnczm   1/1     Running   0          11s
[root@k8s-master01 config]# kubectl exec my-nginx-7b55868ff4-nnczm -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_level
INFO

修改ConfigMap

$ kubectl edit configmap log-config
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  log_level: DEBUG     # 将INFO 改成DEBUG
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"log_level":"DEBUG"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}}
  creationTimestamp: "2022-06-06T07:57:58Z"
  name: log-config
  namespace: default
  resourceVersion: "369625"
  selfLink: /api/v1/namespaces/default/configmaps/log-config
  uid: 72a7a6b0-6a65-4579-b181-a29af085be3f

修改log_level的值为DEBUG 等待大概10秒钟时间,再次查看环境变量的值

$ kubectl exec `kubectl get pods -l run=my-nginx -o=name|cut -d "/" -f2` cat/tmp/log_level
DEBUG

[root@k8s-master01 config]# kubectl exec my-nginx-7b55868ff4-nnczm -it -- cat /etc/config/log_level
DEBUG[root@k8s-master01 config]#

特别注意configMap如果以ENV的方式挂载至容器,修改 configmap 并不会现热更新

ConfigMap更新后滚动更新Pod
更新ConfigMap目前并不会触发相关Pod的滚动更新,可以通过修改pod annotations的方式强制触发滚动更新

$ kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata":{"annotations": {"version/config": "20200411"}}}}}'

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值