K 8 SにおけるConfigMap

20400 ワード

ConfigMap
ConfigMap機能はKubernetes 1.2リリースでは、多くのアプリケーションがプロファイル、コマンドラインパラメータ、または環境変数から構成情報を読み込みます.コンフィギュレーション情報をコンテナに注入するメカニズムを提供するConfigMap APIは、コンフィギュレーションファイル全体またはJSONバイナリオブジェクト全体を保存するために使用できます.
ConfigMap作成
ConfigMapは、kubectl create configmapを使用して、ファイル、ディレクトリ、またはkey-value文字列の作成などから作成できます.
ディレクトリを使用した作成ファイルを使用した作成フォント値を使用した作成
[root@k8s-master dir]# cat 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-master dir]# cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
[root@k8s-master dir]#
root@k8s-master dir]# kubectl create configmap game-config --from-file=../dir/
configmap/game-config created
[root@k8s-master dir]# kubectl get cm
NAME          DATA   AGE
game-config   2      21s
[root@k8s-master 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: "2019-12-25T13:51:28Z"
  name: game-config
  namespace: default
  resourceVersion: "96998"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: 7fa2195e-08b9-4ab2-927b-21420493e28f
[root@k8s-master dir]# kubectl create configmap game-config2 --from-file=game.properties
configmap/game-config2 created
[root@k8s-master dir]# kubectl get cm
NAME           DATA   AGE
game-config    2      4m11s
game-config2   1      28s
[root@k8s-master dir]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@k8s-master dir]# kubectl get cm
NAME             DATA   AGE
game-config      2      9m29s
game-config2     1      5m46s
special-config   2      3s
[root@k8s-master dir]# kubectl describe cm special-config
Name:         special-config
Namespace:    default
Labels:       >
Annotations:  >

Data
====
special.how:
----
very
special.type:
----
charm
Events:  >
[root@k8s-master dir]# vim env.yaml
[root@k8s-master dir]# kubectl apply -f env.yaml
configmap/env-config created
[root@k8s-master dir]# kubectl get cm
NAME             DATA   AGE
env-config       1      6s
game-config      2      14m
game-config2     1      11m
special-config   2      5m32s

PodでのConfigMapの使用
I、環境変数の代わりにConfigMapを使用する
[root@k8s-master dir]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: wangyanglinux/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-master dir]# ll
    28
-rw-r--r-- 1 root root 376 12  25 22:48 111.yaml
-rw-r--r-- 1 root root 105 12  25 22:06 env.yaml
-rw-r--r-- 1 root root 158 12  25 21:50 game.properties
-rw-r--r-- 1 root root 616 12  25 23:09 log-config.yaml
-rw-r--r-- 1 root root 560 12  25 22:34 pod1.yaml
-rw-r--r-- 1 root root 584 12  25 22:26 pod.yaml
-rw-r--r-- 1 root root  83 12  25 21:50 ui.properties
[root@k8s-master dir]# cat env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
[root@k8s-master dir]#

結果:
[root@k8s-master dir]# vim pod.yaml
[root@k8s-master dir]# kubectl create -f pod.yaml
pod/dapi-test-pod created
[root@k8s-master dir]# kubectl get pod
NAME            READY   STATUS      RESTARTS   AGE
dapi-test-pod   0/1     Completed   0          3s
[root@k8s-master dir]# kubectl logs pod dapi-test-pod
Error from server (NotFound): pods "pod" not found
[root@k8s-master dir]# kubectl logs dapi-test-pod
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_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
MYAPP_SVC_SERVICE_PORT=80
MYAPP_SVC_PORT=tcp://10.98.57.156:80

Ⅱ、ConfigMapでコマンドラインパラメータを設定する
[root@k8s-master dir]# cat pod1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod66
spec:
  containers:
    - name: test-container
      image: wangyanglinux/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-master dir]#

結果:
[root@k8s-master dir]# vim pod1.yaml
[root@k8s-master dir]# kubectl create -f pod1.yaml
pod/dapi-test-pod66 created
[root@k8s-master dir]# kubectl get pod
NAME              READY   STATUS      RESTARTS   AGE
dapi-test-pod     0/1     Completed   0          7m45s
dapi-test-pod66   0/1     Completed   0          5s
[root@k8s-master dir]# kubectl logs dapi-test-pod66
very charm

Ⅲ、データボリュームプラグインによるConfigMapの使用
[root@k8s-master dir]# cat 111.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod11
spec:
  containers:
    - name: test-container
      image: wangyanglinux/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-master dir]#

結果:
[root@k8s-master dir]# vim 111.yaml
[root@k8s-master dir]# kubectl create -f 111.yaml
pod/dapi-test-pod11 created
[root@k8s-master dir]# kubectl get pod
NAME              READY   STATUS      RESTARTS   AGE
dapi-test-pod     0/1     Completed   0          22m
dapi-test-pod11   1/1     Running     0          5s
dapi-test-pod66   0/1     Completed   0          14m
[root@k8s-master dir]# kubectl exec dapi-test-pod11 -it -- /bin/sh
/ # cd /etc/config
/etc/config # ls
special.how   special.type
/etc/config # cat special.how
very/etc/config # cat special.type
charm/etc/config # exit

ConfigMapのホットアップデート
[root@k8s-master dir]# cat log-config.yaml
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
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: wangyanglinux/myapp:v1
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: log-config
[root@k8s-master dir]#

結果:
[root@k8s-master dir]# vim log-config.yaml
[root@k8s-master dir]# kubectl apply -f log-config.yaml
configmap/log-config unchanged
deployment.apps/my-nginx created
[root@k8s-master dir]# kubectl get pod
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-5d57c6897b-fm2ql   1/1     Running   0          8s
[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /tec/config/log_level
cat: can't open '/tec/config/log_level': No such file or directory
command terminated with exit code 1
[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
INFO[root@k8s-master dir]# kubectl edit configmap log-config
configmap/log-config edited
[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
INFO[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
[root@k8s-master dir]# kubectl exec my-nginx-5d57c6897b-fm2ql -it -- cat /etc/config/log_level
DEBUG[root@k8s-master dir]#

ConfigMapの更新後:このConfigMapを使用してマウントされたEnvは、このConfigMapを使用してマウントされたVolumeのデータを同期更新するのに時間(実測では約10秒)がかかる