在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 .....