Kubernetes-11:ConfigMap介紹及演示
阿新 • • 發佈:2020-07-09
ConfigMap存在的意義
ConfigMap 功能在 Kubernetes1.2版本引入,許多應用程式會從配置檔案、命令列引數或環境變數中讀取配置資訊,ConfigMap API 給我們提供了向容器中注入配置資訊的機制,ConfigMap 可以用來儲存單個屬性,也可以用來儲存整個配置檔案或者JSON二進位制大物件
ConfigMap的建立
I、使用目錄建立
## 建立目錄 [root@Centos8 k8sYaml]# mkdir dir [root@Centos8 k8sYaml]# cd dir/ ## 建立2個檔案,檔案記憶體放值 [root@Centos8 dir]# cat<< EOF > 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 > EOF [root@Centos8 dir]# cat << EOF > ui.properties > color.good=purple> color.bad=yellow > allow.textmode=true > how.nice.to.look=fairlyNice > EOF [root@Centos8 dir]# ls game.properties ui.properties ## 開始建立ConfigMap [root@Centos8 dir]# kubectl create configmap game-config --from-file=/root/k8sYaml/dir configmap/game-config created ### 檢視ConfigMap [root@Centos8dir]# kubectl get cm NAME DATA AGE game-config 2 2m19s [root@Centos8 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>
--from-file= 表示指定該目錄下所有的檔案都會被用在ConfigMap裡建立一個鍵值對,鍵的名字就是檔名,值就是檔案內容
II、使用檔名建立
## 建立檔案 [root@Centos8 dir]# cat wuzi.examplate NAME=wuzi URL=www.wuzi.com ## 建立ConfigMap [root@Centos8 dir]# kubectl create cm wuzi.com --from-file=/root/k8sYaml/dir/wuzi.examplate configmap/wuzi.com created ## 檢視cm [root@Centos8 dir]# kubectl get cm NAME DATA AGE game-config 2 7m37s wuzi.com 1 41s [root@Centos8 dir]# kubectl describe cm wuzi.com Name: wuzi.com Namespace: default Labels: <none> Annotations: <none> Data ==== wuzi.examplate: ---- NAME=wuzi URL=www.wuzi.com Events: <none>
--from-file 可以重複使用,當重複指定多個檔案時,效果與直接指定目錄相同
III、使用字面值建立(直接建立)
使用字面值建立使用 --from-literal 引數傳遞配置資訊,該引數可以使用多次,格式如下:
## 建立cm [root@Centos8 dir]# kubectl create cm spec.examplate --from-literal=spec.how=very --from-literal=spec.type=charm configmap/spec.examplate created ## 檢視cm [root@Centos8 dir]# kubectl describe cm spec.examplate Name: spec.examplate Namespace: default Labels: <none> Annotations: <none> Data ==== spec.how: ---- very spec.type: ---- charm Events: <none> ## 也可以通過資源清單的方式來建立ConfigMap vim map.yaml ... apiVersion: v1 kind: ConfigMap metadata: name: spec-config namespace: default data: special.now: 6\.4 special.time: 12\:00 ... ## 檢視 [root@Centos8 dir]# kubectl create -f map.yaml configmap/spec-config created [root@Centos8 dir]# kubectl describe cm spec-config Name: spec-config Namespace: default Labels: <none> Annotations: <none> Data ==== special.now: ---- 6\.4 special.time: ---- 12\:00 Events: <none>
建立Pod 測試ConfigMap
I、使用ConfigMap設定環境變數
vim cm-test.yaml ... apiVersion: v1 kind: Pod metadata: name: cm-pod spec: containers: - name: cm-container image: hub.vfancloud.com/test/myapp:v1 command: [ "/bin/sh","-c","env" ] env: #將名為spec.examplate的ConfigMap中名為spec.how的key的value賦值給環境變數SPEC_LEVEL_KEY - name: SPEC_LEVEL_KEY valueFrom: configMapKeyRef: name: spec.examplate key: spec.how - name: SPEC_TYPE_KEY valueFrom: configMapKeyRef: name: spec.examplate key: spec.type envFrom: #將名為wizi.com的ConfigMap中定義的所有key:value都匯入環境變數 - configMapRef: name: wuzi.com restartPolicy: Never ... ## 檢視環境變數 [root@Centos8 dir]# kubectl log cm-pod | grep -E "SPEC_LEVEL_KEY|SPEC_TYPE_KEY|NAME|URL" HOSTNAME=cm-pod SPEC_LEVEL_KEY=very wuzi.examplate=NAME=wuzi URL=www.wuzi.com SPEC_TYPE_KEY=charm
II、通過資料卷外掛使用configMap
vim volum.yaml ... apiVersion: v1 kind: Pod metadata: name: volum-cm spec: containers: - name: volum-ct image: hub.vfancloud.com/test/myapp:v1 volumeMounts: # 掛載共享資料卷 - name: config-volume # 要掛載共享資料卷的名字 mountPath: /etc/config # 掛載到指定的目錄下 volumes: # 定義共享資料卷 - name: config-volume # 共享資料卷名稱 configMap: name: game-config # ConfigMap的名稱 # name: wuzi.com # name: spec-config restartPolicy: Never ... [root@Centos8 dir]# kubectl create -f volum.yaml ### 分別以文章開頭的第一步建立的ConfigMap型別(目錄、檔案、鍵值)三種形式進行試驗 ## ConfigMap型別為目錄的,直接將此目錄下的所有檔案共享到了container指定目錄中 /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 ## ConfigMap型別為檔案的,只將此檔案共享到了container目錄下 /etc/config # ls wuzi.examplate /etc/config # cat wuzi.examplate NAME=wuzi URL=www.wuzi.com ## ConfigMap型別為鍵值對的,將key儲存成了檔名,value儲存為檔案內容 /etc/config # ls special.now special.time /etc/config # cat special.time 12\:00/etc/config # /etc/config # cat special.now 6\.4/etc/config #
滾動更新
## 先建立一個索引檔案 [root@Centos8 dir]# cat index1.html Hello World ## 使用此檔案建立ConfigMap [root@Centos8 dir]# kubectl create cm nginx-cm --from-file=./index1.html configmap/nginx-cm created ## 建立Deployment [root@Centos8 dir]# vim nginx-cm.yaml ... apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-cm namespace: default spec: replicas: 3 template: metadata: labels: run: nginx spec: containers: - name: nginx-cm image: hub.vfancloud.com/test/myapp:v1 ports: - containerPort: 80 volumeMounts: - name: config-nginx mountPath: /usr/share/nginx/html/config volumes: - name: config-nginx configMap: name: nginx-cm ... kubectl apply -f nginx-cm.yaml ## 測試訪問 [root@Centos8 dir]# curl http://10.244.3.170/config/index1.html Hello World ## 現在建立一個新檔案index2.html [root@Centos8 dir]# cat index2.html It is a wonderful world ## 刪除ConfigMap重建替換index1.html為index2.html [root@Centos8 dir]# kubectl delete cm nginx-cm configmap "nginx-cm" deleted [root@Centos8 dir]# kubectl get cm No resources found. [root@Centos8 dir]# kubectl create cm nginx-cm --from-file=./index2.html configmap/nginx-cm created ## 再次測試,滾動更新完成 [root@Centos8 dir]# curl http://10.244.3.170/config/index2.html It is a wonderful world
需要等一會才能同步更新,可能10-30s