在K8S中,有兩種方式管理資源的配置,分別是configmap和secret,他們的最大區別是:spring
單配置更新法:api
多配置更換法:bash
使用configmap前,須要先作以下準備工做app
先將前面部署的3個dubbo服務的POD個數所有調整(scale)爲0個,避免在應用configmap過程當中可能的報錯,也爲了節省資源
直接在dashboard上操做便可,運維
將3個zk組成的集羣,拆分紅獨立的zk單機服務,分別表示測試環境和開發環境(節約資源)工具
IP地址 | ZK地址 | 角色 |
---|---|---|
10.4.7.11 | zk1.zq.com | test測試環境 |
10.4.7.12 | zk2.zq.com | pro生產環境 |
中止3個zk服務測試
sh /opt/zookeeper/bin/zkServer.sh stop rm -rf /data/zookeeper/data/* rm -rf /data/zookeeper/logs/*
註釋掉集羣配置spa
sed -i 's@^server@#server@g' /opt/zookeeper/conf/zoo.cfg
啓動zk單機命令行
sh /opt/zookeeper/bin/zkServer.sh start sh /opt/zookeeper/bin/zkServer.sh status
老規矩,資源清單在7.200
運維機上統一操做rest
cd /data/k8s-yaml/dubbo-monitor
cat >cm.yaml <<'EOF' apiVersion: v1 kind: ConfigMap metadata: name: dubbo-monitor-cm namespace: infra data: dubbo.properties: | dubbo.container=log4j,spring,registry,jetty dubbo.application.name=simple-monitor dubbo.application.owner=zqkj dubbo.registry.address=zookeeper://zk1.zq.com:2181 dubbo.protocol.port=20880 dubbo.jetty.port=8080 dubbo.jetty.directory=/dubbo-monitor-simple/monitor dubbo.charts.directory=/dubbo-monitor-simple/charts dubbo.statistics.directory=/dubbo-monitor-simple/statistics dubbo.log4j.file=/dubbo-monitor-simple/logs/dubbo-monitor.log dubbo.log4j.level=WARN EOF
其實就是把dubbo-monitor配置文件中的內容用configmap語法展現出來了
固然最前面加上了相應的元數據信息
若是轉換不來格式,也可使用命令行工具直接將配置文件轉換爲configmap
kubectl create configmap <map-name> <data-source> # <map-name> 是但願建立的ConfigMap的名稱,<data-source>是一個目錄、文件和具體值。
案例以下:
# 1.經過單個文件建立ConfigMap kubectl create configmap game-config-1 --from-file=/xxx/xxx.properties # 2.經過多個文件建立ConfigMap kubectl create configmap game-config-2 \ --from-file=/xxx/xxx.properties \ --from-file=/xxx/www.properties # 3.經過在一個目錄下的多個文件建立ConfigMap kubectl create configmap game-config-3 --from-file=/xxx/www/
爲了和原來的dp.yaml
對比,咱們新建一個dp-cm.yaml
cat >dp-cm.yaml <<'EOF' kind: Deployment apiVersion: extensions/v1beta1 metadata: name: dubbo-monitor namespace: infra labels: name: dubbo-monitor spec: replicas: 1 selector: matchLabels: name: dubbo-monitor template: metadata: labels: app: dubbo-monitor name: dubbo-monitor spec: containers: - name: dubbo-monitor image: harbor.zq.com/infra/dubbo-monitor:latest ports: - containerPort: 8080 protocol: TCP - containerPort: 20880 protocol: TCP imagePullPolicy: IfNotPresent #----------------start--------------------------- volumeMounts: - name: configmap-volume mountPath: /dubbo-monitor-simple/conf volumes: - name: configmap-volume configMap: name: dubbo-monitor-cm #----------------end----------------------------- imagePullSecrets: - name: harbor restartPolicy: Always terminationGracePeriodSeconds: 30 securityContext: runAsUser: 0 schedulerName: default-scheduler strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1 revisionHistoryLimit: 7 progressDeadlineSeconds: 600 EOF
註釋中的內容就是新增在原
dp.yaml
中增長的內容,解釋以下:
- 申明一個卷,卷名爲
configmap-volume
- 指定這個卷使用名爲
dubbo-monitor-cm
的configMap- 在
containers
中掛載卷,卷名與申明的卷相同- 用
mountPath
的方式掛載到指定目錄
kubectl apply -f http://k8s-yaml.zq.com/dubbo-monitor/cm.yaml kubectl apply -f http://k8s-yaml.zq.com/dubbo-monitor/dp-cm.yaml
在dashboard中查看infra
名稱空間中的configmap
資源
而後檢查容器中的配置
kubectl -n infra exec -it dubbo-monitor-5b7cdddbc5-xpft6 bash # 容器內 bash-4.3# cat /dubbo-monitor-simple/conf/dubbo.properties dubbo.container=log4j,spring,registry,jetty dubbo.application.name=simple-monitor dubbo.application.owner=zqkj dubbo.registry.address=zookeeper://zk1.zq.com:2181 ....
再準備一個configmap叫cm-pro.yaml
cp cm.yaml cm-pro.yaml # 把資源名字改爲dubbo-monitor-cm-pro sed -i 's#dubbo-monitor-cm#dubbo-monitor-cm-pro#g' cm-pro.yaml # 把服務註冊到zk2.zq.com上 sed -i 's#zk1#zk2#g' cm-pro.yaml
sed -i 's#dubbo-monitor-cm#dubbo-monitor-cm-pro#g' dp-cm.yaml
# 應用新configmap kubectl apply -f http://k8s-yaml.zq.com/dubbo-monitor/cm-pro.yaml # 更新deploy kubectl apply -f http://k8s-yaml.zq.com/dubbo-monitor/dp-cm.yaml
新的pod已經起來了
~]# kubectl -n infra get pod NAME READY STATUS RESTARTS AGE dubbo-monitor-c7fbf68b9-7nffj 1/1 Running 0 52s
進去看看是否是應用的新的configmap配置:
kubectl -n infra exec -it dubbo-monitor-5cb756cc6c-xtnrt bash # 容器內 bash-4.3# cat /dubbo-monitor-simple/conf/dubbo.properties |grep zook dubbo.registry.address=zookeeper://zk2.zq.com:2181
看下dubbo-monitor的頁面:已是zk2了。
咱們使用的是mountPath,這個是掛載整個目錄,會使容器內的被掛載目錄中原有的文件不可見,能夠看見咱們。
查看咱們pod容器啓動的命令能夠看見原來腳本中的命令已經沒法對掛載的目錄操做了
如何單獨掛載一個配置文件:
只掛載單獨一個文件而不是整個目錄,須要添加subPath
方法
在dp-cm.yaml
的配置中,將原來的volume配置作一下更改
#----------------start--------------------------- volumeMounts: - name: configmap-volume mountPath: /dubbo-monitor-simple/conf volumes: - name: configmap-volume configMap: name: dubbo-monitor-cm #----------------end----------------------------- # 調整爲 #----------------start--------------------------- volumeMounts: - name: configmap-volume mountPath: /dubbo-monitor-simple/conf - name: configmap-volume mountPath: /var/dubbo.properties subPath: dubbo.properties volumes: - name: configmap-volume configMap: name: dubbo-monitor-cm #----------------end-----------------------------
kubectl apply -f http://k8s-yaml.zq.com/dubbo-monitor/dp-cm.yaml kubectl -n infra exec -it dubbo-monitor-5cb756cc6c-xtnrt bash # 容器內操做 bash-4.3# ls -l /var/ total 4 drwxr-xr-x 1 root root 29 Apr 13 2016 cache -rw-r--r-- 1 root root 459 May 10 10:02 dubbo.properties drwxr-xr-x 2 root root 6 Apr 1 2016 empty .....