关于configmap要引出,stateFulSet
1 |
stateFulSet:是用于支持有状态服务的资源。有状态服务需要一定的存储能力 |
k8s中的存储机制:
- 1.configMap (存储配置文件)本文只讨论configMap
- 2.secret (加密方案) 请看这里 传送门
- 3.volume (为pod提供共享存储卷的能力,比如本地磁盘共享,nfs共享)
- 4.pv(persistent Volume) (持久卷)
一.configMap(配置文件注册中心)
看图

ConfigMap 的创建
1.使用目录创建
1 2 |
#创建目录(这个随意) mkdir -p /root/k8s/configmap/dir && cd /root/k8s/configmap/dir |
创建配置文件
game.properties
1 2 3 4 5 6 7 |
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
1 2 3 4 |
color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice |
创建configmap
1 |
kubectl create configmap game-config --from-file=/root/k8s/configmap/dir |

1 |
—from-file 指定目录下的所有文件都会被用在 ConfifigMap 里面创建一个键值对,键的名字就是文件名,值就 是文件的内容 |
查看详细
1 |
kubectl describe configmap game-config |

查看配置文件
1 |
kubectl get cm game-config -o yaml |

2.使用文件创建(与目录区别是这个指定的是文件本身)
1 2 3 4 |
kubectl create configmap game-config-2 --from-file=/root/k8s/configmap/dir/game.properties 查看详细 kubectl get configmap game-config-2 -o yaml |

3. 用字面值创建
使用文字值创建,利用 —from-literal 参数传递配置信息,该参数可以使用多次,格式如下
1 2 3 4 |
kubectl create configmap special-config --from-literal=special.how=very --fromliteral=special.type=charm #查看 kubectl get configmaps special-config -o yaml |
Pod 中使用 ConfigMap
1.使用 ConfifigMap 来替代环境变量
yaml方式创建configmap
1 2 3 4 5 6 7 8 |
apiVersion: v1 kind: ConfigMap metadata: name: special-config namespace: default data: special.how: very special.type: charm |
1 2 3 4 5 6 7 |
apiVersion: v1 kind: ConfigMap metadata: name: env-config namespace: default data: log_level: INFO |

创建pod (env 命令打印系统环境变量)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: hub.atshooter.com/k8s/nginx:v1.0 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: #env(环境变量)从哪里来?是从configMap ,哪个configmap ? env-config - configMapRef: name: env-config restartPolicy: Never |

2.用 ConfigMap 设置命令行参数
还是 调用special-config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: hub.atguigu.com/library/myapp:v1 command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ] env: - name: SPECIAL_LEVEL_KEY #获取special-config 配置文件里的 special.how 的值 赋值给 SPECIAL_LEVEL_KEY valueFrom: configMapKeyRef: name: special-config key: special.how - name: SPECIAL_TYPE_KEY #获取special-config 配置文件里的 special.type 的值 赋值给 SPECIAL_TYPE_KEY valueFrom: configMapKeyRef: name: special-config key: special.type restartPolicy: Never |

3. 通过数据卷插件使用ConfifigMap环境变量
在数据卷里面使用这个 ConfigMap,有不同的选项。最基本的就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容
还是调用special-config
创建pod调用configmap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
apiVersion: v1 kind: Pod metadata: name: dapi-test-pod spec: containers: - name: test-container image: hub.atshooter.com/k8s/nginx:v1.0 command: [ "/bin/sh", "-c", "cat /etc/config/special.how" ] volumeMounts: #挂在volume - name: config-volume #挂载的是哪个volume 挂在的是config-volume (与下面的volumes名称遥相呼应) mountPath: /etc/config #挂载到哪里 volumes: - name: config-volume #名称 configMap: #volume从哪里来 是从configMap挂在来的 name: special-config #挂载的哪个configMap special-config restartPolicy: Never |
pod启动后查看日志
1 |
kubectl logs dapi-test-pod |

configmap热更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
apiVersion: v1 kind: ConfigMap metadata: name: log-config namespace: default data: log_level: INFO --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 1 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: hub.atguigu.com/library/myapp:v1 ports: - containerPort: 80 volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: log-config |
修改前查看

修改配置
1 |
kubectl edit configmap log-config |

修改后查看(大约等待1分钟,看个人配置)

configMap更新后 并不会重启nginx, 更新 ConfifigMap 目前并不会触发相关 Pod 的滚动更新,可以通过修改 pod annotations 的方式强制触发滚动更新。
1 |
kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations":{"version/config": "20190411" }}}}}' |
其实就是修改了这里

1 |
这个例子里我们在 .spec.template.metadata.annotations 中添加 version/config ,每次通过修改 version/config 来触发滚动更新 |
但是虽然配置文件修改了,容器中也更新了,但是nginx没有重启,所以项目中配置并没有生效。(这时要用到k8s钩子操作)
1 2 3 |
就是我们需要在应用容器启动后执行一些初始化操作,比如设置容器的dns参数等,说到这里就不得不多提一句,k8s到目前为止尚不支持通过为kubelet添加参数的方式为应用容器设置dns的options。事实上我们在生产中之所以使用到本篇文档所说的这种钩子,就是为了在应用容器启动后为其设置一个dns的options。 除了为容器添加启动后的钩子之外,还可以为容器添加销毁之前的钩子。 |
定义启动后和销毁前钩子示例:
k8s钩子操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
apiVersion: v1 kind: Pod metadata: name: lifecycle-demo spec: containers: - name: lifecycle-demo-container image: nginx lifecycle: postStart: exec: command: ["/usr/sbin/nginx", "-s", "reload"] preStop: exec: command: ["/usr/sbin/nginx","-s","quit"] |
下来我修改下我自身的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
apiVersion: apps/v1beta2 kind: Deployment metadata: name: blog-nginx labels: app: wordpress spec: replicas: 3 selector: matchLabels: app: blog-nginx tier: frontend strategy: type: Recreate template: metadata: labels: app: blog-nginx tier: frontend spec: containers: - name: nginx image: nginx:1.16.1 lifecycle: postStart: exec: command: ["/usr/sbin/nginx", "-s", "reload"] imagePullPolicy: IfNotPresent ports: - containerPort: 80 name: wordpress volumeMounts: - name: wordpress-persistent-storage mountPath: /usr/local/nginx/html - name: config mountPath: /usr/local/nginx/conf/vhost/site.conf subPath: site.conf volumes: - name: wordpress-persistent-storage persistentVolumeClaim: claimName: nginx-pv-claim - name: config configMap: name: nginx-wp-config |
- 本文固定链接: https://www.yoyoask.com/?p=2249
- 转载请注明: shooter 于 SHOOTER 发表