Kubernetes外掛配置管理—ConfigMap介紹

目錄貼:Kubernetes學習系列

  其餘容器編排調度工具會大談特談「輕應用」、「十二要素應用」,這樣就勢必會對企業級複雜應用作很大的改動。Kubernetes是爲了解決「如何合理使用容器支撐企業級複雜應用」這個問題而誕生的,因此它的設計理念是要支持絕大多數應用的原生形態。例如,不少應用程序的配置須要經過配置文件,命令行參數和環境變量的組合配置來完成(「十二要素應用」等均要求去配置)。這些配置應該從image內容中解耦,以此來保持容器化應用程序的可移植性。ConfigMap API資源提供了將配置數據注入容器的方式,同時保證該機制對容器來講是透明的。ConfigMap能夠被用來保存單個屬性,也能夠用來保存整個配置文件或者JSON二進制大對象。html

  ConfigMap API資源存儲鍵/值對配置數據,這些數據能夠在pods裏使用。ConfigMap跟Secrets相似,可是ConfigMap能夠更方便的處理不包含敏感信息的字符串。示例以下:api

kind: ConfigMap
apiVersion: v1
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  example.property.1: hello
  example.property.2: world
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3

  經過示例代碼能夠看到:ConfigMap能夠包含細粒度的配置項,如:example.property.1;也能夠包含粗粒度的配置文件,如:example.property.file。工具

一、建立ConfigMap

1.1 從文件夾建立

[root@k8s-master propertirs]# cat /home/yaml/propertirs/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 propertirs]# cat /home/yaml/propertirs/ui.properties 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
[root@k8s-master propertirs]# kubectl create configmap game-config --from-file=/home/yaml/propertirs/
configmap "game-config" created
[root@k8s-master propertirs]# kubectl describe configmaps game-config  
#該方法只能獲得ConfigMap的Key和size
Name:        game-config
Namespace:    default
Labels:        <none>
Annotations:    <none>

Data
====
ui.properties:        83 bytes
game.properties:    158 bytes
#若想獲得詳細信息,可經過如下命令:
[root@k8s-master propertirs]# kubectl get configmaps 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: 2017-03-21T03:22:34Z
  name: game-config
  namespace: default
  resourceVersion: "3002770"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: a04f90f0-0de5-11e7-b3d5-fa163ebba51b

1.2 從文件建立

[root@k8s-master propertirs]# kubectl create configmap game-config-2 --from-file=/home/yaml/propertirs/game.properties --from-file=/home/yaml/propertirs/ui.properties
configmap "game-config-2" created
[root@k8s-master propertirs]# kubectl get configmaps game-config-2 -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: 2017-03-21T03:30:15Z
  name: game-config-2
  namespace: default
  resourceVersion: "3003415"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-2
  uid: b2e4dfab-0de6-11e7-b3d5-fa163ebba51b

1.3  指定data中的key

[root@k8s-master propertirs]# kubectl create configmap game-config-3 --from-file=game-special-key=/home/yaml/propertirs/game.properties
configmap "game-config-3" created
[root@k8s-master propertirs]# kubectl get configmaps game-config-3 -o yaml
apiVersion: v1
data:
  game-special-key: |
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T03:33:23Z
  name: game-config-3
  namespace: default
  resourceVersion: "3003678"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-3
  uid: 2345dad3-0de7-11e7-b3d5-fa163ebba51b

1.4  指定具體的值

[root@k8s-master propertirs]# kubectl create configmap game-config-4 --from-literal=special.user=zhenyu --from-literal=special.passwd=yaodidiao
configmap "game-config-4" created
[root@k8s-master propertirs]# kubectl get configmaps game-config-4 -o yaml
apiVersion: v1
data:
  special.passwd: yaodidiao
  special.user: zhenyu
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T03:36:12Z
  name: game-config-4
  namespace: default
  resourceVersion: "3003915"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b

二、使用ConfigMap

2.1 環境變量或參數

  建立一個Pod,並將一個已經建立好的ConfigMap做爲環境變量,注入到Pod中。post

[root@k8s-master propertirs]# kubectl get configmaps game-config-4 -o yaml
apiVersion: v1
data:
  special.passwd: yaodidiao
  special.user: zhenyu
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T03:36:12Z
  name: game-config-4
  namespace: default
  resourceVersion: "3003915"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: 8802f6d2-0de7-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testEnv.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: testenv
    role: master
  name: testenv
spec:
  containers:
    - name: testenv
      image: busybox
      imagePullPolicy: IfNotPresent
      env:
        - name: SPECIAL_USER
          valueFrom:
            configMapKeyRef:
              name: game-config-4
              key: special.user
      command:
      - sleep
      - "360000"
[root@k8s-master propertirs]# kubectl create -f testEnv.yaml 
pod "testenv" created
[root@k8s-master propertirs]# kubectl exec -ti testenv sh
/ # echo $SPECIAL_USER
zhenyu
/ #

2.2 掛載文件數據卷

[root@k8s-master propertirs]# kubectl get configmaps 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: 2017-03-21T03:22:34Z
  name: game-config
  namespace: default
  resourceVersion: "3002770"
  selfLink: /api/v1/namespaces/default/configmaps/game-config
  uid: a04f90f0-0de5-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# cat testVolume.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: testvolume
    role: master
  name: testvolume
spec:
  containers:
    - name: testvolume
      image: busybox
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
      command:
      - sleep
      - "360000"
  volumes:
    - name: config-volume
      configMap:
        name: game-config
[root@k8s-master propertirs]# kubectl create -f testVolume.yaml 
pod "testvolume" created
[root@k8s-master propertirs]# kubectl exec -ti testvolume sh
/ # cd /etc/config/
/etc/config # ls
game.properties  ui.properties
/etc/config # 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
/etc/config # cat ui.properties 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #

2.3 掛載信息數據卷

[root@k8s-master propertirs]# cat testVolume.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: testvolume
    role: master
  name: testvolume
spec:
  containers:
    - name: testvolume
      image: busybox
      imagePullPolicy: IfNotPresent
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
      command:
      - sleep
      - "360000"
  volumes:
    - name: config-volume
      configMap:
        name: game-config-4
[root@k8s-master propertirs]# kubectl get  configmaps game-config-4 -o yaml
apiVersion: v1
data:
  special.passwd: yaodidiao
  special.user: zhenyuyaodidiao
kind: ConfigMap
metadata:
  creationTimestamp: 2017-03-21T06:29:29Z
  name: game-config-4
  namespace: default
  resourceVersion: "3018779"
  selfLink: /api/v1/namespaces/default/configmaps/game-config-4
  uid: bd086dca-0dff-11e7-b3d5-fa163ebba51b
[root@k8s-master propertirs]# kubectl create -f testVolume.yaml 
pod "testvolume" created
[root@k8s-master propertirs]# kubectl exec -ti testvolume sh
/ # cd /etc/config/
/etc/config # ls
special.passwd  special.user
/etc/config # cat special.user 
zhenyuyaodidiao/etc/config # 
/etc/config # exit

2.4 熱更新

  當ConfigMap以數據卷的形式掛載進Pod的時,這時更新ConfigMap(或刪掉重建ConfigMap),Pod內掛載的配置信息會熱更新。這時能夠增長一些監測配置文件變動的腳本,而後reload對應服務。學習

相關文章
相關標籤/搜索