標籤(空格分隔): kubernetes系列linux
- 一: kubernetes的存儲ConfigMap
ConfigMap 功能在 Kubernetes1.2 版本中引入,許多應用程序會從配置文件、命令行參數或環境變量中讀取配 置信息。ConfigMap API 給咱們提供了向容器中注入配置信息的機制,ConfigMap 能夠被用來保存單個屬性,也 能夠用來保存整個配置文件或者 JSON 二進制大對象
配置中心:
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
mkdir configmap/dir -p cd configmap/dir/ vim 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 --- vim ui.properties --- color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice --- —from-file 指定在目錄下的全部文件都會被用在 ConfigMap 裏面建立一個鍵值對,鍵的名字就是文件名,值就 是文件的內容 kubectl create configmap game-config --from-file=../dir/ kubectl get cm game-config -o yaml kubectl describe cm game-config
只要指定爲一個文件就能夠從單個文件中建立 ConfigMap kubectl create configmap game-config-2 --from-file=game.properties kubectl describe cm game-config-2 kubectl get configmaps game-config-2 -o yaml —from-file 這個參數能夠使用屢次,你能夠使用兩次分別指定上個實例中的那兩個配置文件,效果就跟指定整個 目錄是同樣的
使用文字值建立,利用 —from-literal 參數傳遞配置信息,該參數能夠使用屢次,格式以下 kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm kubectl get cm kubectl get cm special-config -o yaml
使用 ConfigMap 來替代環境變量 apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: special.how: very special.type: charm ---- ---- vim env.yaml apiVersion: v1 kind: ConfigMap metadata: name: env-config namespace: default data: log_level: INFO ----------- kubectl apply -f env.yaml
vim evnpod.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 --- kubectl apply -f evnpod.yaml
vim pod1.yaml ---- apiVersion: v1 kind: ConfigMap metadata: name: special-config-1 namespace: default data: special.how: very special.type: charm --- kubectl apply -f pod1.yaml
vim pod2.yaml ---- apiVersion: v1 kind: Pod metadata: name: dapi-test-pod1 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-1 key: special.how - name: SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: special-config-1 key: special.type restartPolicy: Never ---- kubectl apply -f pod2.yaml
vim stoage1.yaml --- apiVersion: v1 kind: ConfigMap metadata: name: special-config-2 namespace: default data: special.how: very special.type: charm --- kubectl apply -f stoage1.yaml
在數據卷裏面使用這個 ConfigMap,有不一樣的選項。最基本的就是將文件填入數據卷,在這個文件中,鍵就是文 件名,鍵值就是文件內容 vim pod3.yaml --- apiVersion: v1 kind: Pod metadata: name: dapi-test-pod4 spec: containers: - name: test-container image: wangyanglinux/myapp:v1 command: [ "/bin/sh", "-c", "sleep 600" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config-2 restartPolicy: Never --- kubectl apply -f pod3.yaml
vim log-cm.yaml --- apiVersion: v1 kind: ConfigMap metadata: name: log-config namespace: default data: log_level: INFO --- kubectl apply -f log-cm.yaml
vim pod4.yaml --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 1 template: metadata: labels: run: 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 --- kubectl apply -f pod4.yaml
修改 log_level 的值爲 DEBUG 等待大概 10 秒鐘時間,再次查看環境變量的值 將 log—level 改成DEBUG kubectl edit cm log_config --- log_level : DEBUG --- kubectl exec my-nginx-c484b98b4-tgblg -ti -- cat /etc/config/log_level
ConfigMap 更新後滾動更新 Pod 更新 ConfigMap 目前並不會觸發相關 Pod 的滾動更新,能夠經過修改 pod annotations 的方式強制觸發滾動更新 這個例子裏咱們在 .spec.template.metadata.annotations 中添加 version/config ,每次經過修改 version/config 來觸發滾動更新 !!! 更新 ConfigMap 後: 使用該 ConfigMap 掛載的 Env 不會同步更新 使用該 ConfigMap 掛載的 Volume 中的數據須要一段時間(實測大概10秒)才能同步更新 kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20190411" }}}}}