目录
1. 环境的清理
2. Configmap配置管理
2.1 使用字面值创建
2.2 使用文件创建
2.3 使用目录创建
2.4.编写configmap的yaml文件
3. 如何使用configmap
3.1 使用configmap设置环境变量
3.2 使用conigmap设置命令行参数
3.3 通过数据卷使用configmap
3.4 configmap热更新(使用数据卷)
3.5 pod滚动更新
3.5.1 pod滚动更新的两种方式
##删除namespace [root@server2 ingress]# kubectl get ns ##查看所有namespace,并删除 [root@server2 ingress]# kubectl delete pod --all -n demo --force ##先删除里面pod在删除ns会快一点 [root@server2 ingress]# kubectl delete ns demo ##删除pod [root@server2 ingress]# kubectl delete pod --all -n test --force [root@server2 ingress]# kubectl delete ns test [root@server2 ingress]# kubectl delete pod nginx --force [root@server2 ingress]# kubectl delete deployments.apps deployment [root@server2 ingress]# kubectl delete pod demo --force ##删除服务 [root@server2 ingress]# kubectl get svc [root@server2 ingress]# kubectl delete svc nginx-svc ##删除ingress服务 [root@server2 ingress]# kubectl delete ingress ingress-demo ## 删除网络策略 [root@server2 ingress]# kubectl delete networkpolicies. --all
- Configmap用于保存配置数据,以键值对形式存储。 configMap 资源提供了向 Pod 注入配置数据的方法。 旨在让镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。 典型的使用场景: 1.填充环境变量的值 2.设置容器内的命令行参数 3.填充卷的配置文件 ##使用较多 - 创建ConfigMap的方式有4种: 1.使用字面值创建 2.使用文件创建 3.使用目录创建 4.编写configmap的yaml文件创建 - 1.使用字面值创建 $ kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2 - 2.使用文件创建 $ kubectl create configmap my-config-2 --from-file=/etc/resolv.conf key的名称是文件名称,value的值是这个文件的内容 - 3.使用目录创建 $ kubectl create configmap my-config-3 --from-file=test 目录中的文件名为key,文件内容是value - 4.编写configmap的yaml文件 $ vim cm1.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm1-config data: db_host: "172.25.0.250" db_port: "3306" $ kubectl create -f cm1.yaml
2.1 使用字面值创建
[root@server2 ingress]# kubectl get cm [root@server2 ~]# kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2 [root@server2 ~]# kubectl describe cm my-config
2.2 使用文件创建
[root@server2 ~]# kubectl create configmap my-config-2 --from-file=/etc/resolv.conf [root@server2 ~]# kubectl describe cm my-config-2
2.3 使用目录创建
[root@server2 ~]# mkdir congfigmap [root@server2 ~]# cd congfigmap/ [root@server2 congfigmap]# mkdir test [root@server2 congfigmap]# cp /etc/resolv.conf test/ [root@server2 congfigmap]# ll test/ total 4 -rw-r--r-- 1 root root 28 Feb 24 16:16 resolv.conf [root@server2 congfigmap]# kubectl create configmap my-config-3 --from-file=test [root@server2 congfigmap]# kubectl describe cm my-config-3
2.4.编写configmap的yaml文件
[root@server2 congfigmap]# vim cm1.yaml [root@server2 congfigmap]# cat cm1.yaml apiVersion: v1 kind: ConfigMap metadata: name: cm1-config data: db_host: "172.25.13.250" db_port: "3306" [root@server2 congfigmap]# kubectl apply -f cm1.yaml ##应用 [root@server2 congfigmap]# kubectl describe cm cm1-config ##描述详细信息
- 如何使用configmap: 1.通过环境变量的方式直接传递给pod 2.通过在pod的命令行下运行的方式 3.作为volume的方式挂载到pod内 ##此方式最常用
3.1 使用configmap设置环境变量
[root@server2 congfigmap]# vim pod1.yaml [root@server2 congfigmap]# cat pod1.yaml apiVersion: v1 kind: Pod metadata: name: pod1 spec: containers: - name: pod1 image: busyboxplus command: ["/bin/sh", "-c", "env"] env: - name: key1 valueFrom: configMapKeyRef: name: cm1-config key: db_host - name: key2 valueFrom: configMapKeyRef: name: cm1-config key: db_port restartPolicy: Never [root@server2 congfigmap]# kubectl apply -f pod1.yaml [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE pod1 0/1 Completed 0 8s [root@server2 congfigmap]# kubectl logs pod1 ##查看日志是否有cm1-config信息 key1=172.25.13.250 key2=3306
3.2 使用conigmap设置命令行参数
[root@server2 congfigmap]# [root@server2 congfigmap]# vim pod2.yaml [root@server2 congfigmap]# cat pod2.yaml apiVersion: v1 kind: Pod metadata: name: pod2 spec: containers: - name: pod2 image: busyboxplus command: ["/bin/sh", "-c", "echo $(db_host) $(db_port)"] envFrom: - configMapRef: name: cm1-config restartPolicy: Never [root@server2 congfigmap]# kubectl apply -f pod2.yaml pod/pod2 created [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE pod1 0/1 Completed 0 4m9s pod2 0/1 Completed 0 9s [root@server2 congfigmap]# kubectl logs pod2 172.25.13.250 3306
3.3 通过数据卷使用configmap
[root@server2 congfigmap]# kubectl apply -f pod2.yaml pod/pod2 created [root@server2 congfigmap]# cat pod2.yaml apiVersion: v1 kind: Pod metadata: name: pod2 spec: containers: - name: pod2 image: busyboxplus command: ["/bin/sh", "-c", "cat /config/db_host"] volumeMounts: - name: config-volume mountPath: /config volumes: - name: config-volume configMap: name: cm1-config restartPolicy: Never [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE pod1 0/1 Completed 0 15m pod2 0/1 Completed 0 36s pod3 1/1 Running 0 8m2s [root@server2 congfigmap]# kubectl logs pod2 172.25.13.250[root@server2 congfigmap]#
3.4 configmap热更新(使用数据卷)
## 1. 配置并查看数据卷内容 [root@server2 congfigmap]# vim pod3.yaml [root@server2 congfigmap]# cat pod3.yaml apiVersion: v1 kind: Pod metadata: name: pod3 spec: containers: - name: pod3 image: busyboxplus stdin: true tty: true volumeMounts: - name: config-volume mountPath: /config volumes: - name: config-volume configMap: name: cm1-config [root@server2 congfigmap]# kubectl apply -f pod3.yaml [root@server2 congfigmap]# kubectl get pod [root@server2 congfigmap]# kubectl attach pod3 -it ##进入pod3并查看数据卷内容 / # cd /config/ /config # cat * 172.25.13.2503306/config # ## 2. 准备热更新 [root@server2 congfigmap]# kubectl edit cm cm1-config ##编辑文件内容 [root@server2 congfigmap]# kubectl get pod #查看pod3是否运行 [root@server2 congfigmap]# kubectl attach pod3 -it / # cd /config/ /config # cat * 172.25.13.1008080/config # ##内容更新成功
3.5 pod滚动更新
- configmap热更新后,并不会触发相关Pod的滚动更新,需要手动触发
[root@server2 congfigmap]# vim demo.yaml [root@server2 congfigmap]# cat demo.yaml apiVersion: apps/v1 kind: Deployment metadata: name: demo spec: replicas: 1 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:v1 volumeMounts: - name: config-volume mountPath: /etc/nginx/conf.d volumes: - name: config-volume configMap: name: nginx-config [root@server2 congfigmap]# vim www.conf [root@server2 congfigmap]# cat www.conf server { listen 8080; ##此处自己书写的测试文件端口是8080 server_name _; location / { root /usr/share/nginx/html; index index.html index.htm; } } [root@server2 congfigmap]# mv www.conf default.conf [root@server2 congfigmap]# kubectl create configmap nginx-config --from-file=default.conf ##创建cm [root@server2 congfigmap]# kubectl get cm [root@server2 congfigmap]# kubectl describe cm nginx-config [root@server2 congfigmap]# kubectl apply -f demo.yaml ##创建pod [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE demo-75679c99b4-cq52d 1/1 Running 0 9s [root@server2 congfigmap]# kubectl describe pod demo-75679c99b4-cq52d ##查看pod的详细信息 [root@server2 congfigmap]# kubectl get pod -o wide ##查看pod详细信息 NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES demo-75679c99b4-cq52d 1/1 Running 0 3m52s 10.244.141.198 server3
[root@server2 congfigmap]# curl 10.244.141.198 ##默认80端口是访问不到的 curl: (7) Failed connect to 10.244.141.198:80; Connection refused [root@server2 congfigmap]# curl 10.244.141.198:8080 ##使用8080端口访问成功 Hello MyApp | Version: v1 | Pod Name [root@server2 congfigmap]# kubectl exec -it demo-75679c99b4-cq52d -- sh ##进入终端查看nginx的默认conf文件 3.5.1 pod滚动更新的两种方式
3.5.1.1 使用命令更新(打补丁)
[root@server2 congfigmap]# kubectl edit cm nginx-config ##编辑文件,修改端口号 [root@server2 congfigmap]# kubectl describe cm nginx-config ##查看热更新是否成功 [root@server2 congfigmap]# curl 10.244.141.198:8080 ##发现更新成功后还是只能使用8080进行访问,这是因为pod没有更新 Hello MyApp | Version: v1 | Pod Name [root@server2 congfigmap]# kubectl exec -it demo-75679c99b4-cq52d -- sh / # cat /etc/nginx/conf.d/default.conf ##查看配置文件是否是热更新 server { listen 80; server_name _; location / { root /usr/share/nginx/html; index index.html index.htm; } } / # netstat -antlp ##查看端口发现还是8080 ##实现pod滚动更新 [root@server2 congfigmap]# kubectl patch deployments.apps demo --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "2021022401"}}}}}' ##打补丁 [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE demo-7f476857fb-8xsmz 1/1 Running 0 8s [root@server2 congfigmap]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES demo-7f476857fb-8xsmz 1/1 Running 0 55s 10.244.22.5 server4
[root@server2 congfigmap]# curl 10.244.22.5 Hello MyApp | Version: v1 | Pod Name 3.5.1.2 直接删除pod更新
- 适用于有控制器的pod
[root@server2 congfigmap]# kubectl edit cm nginx-config ##把端口在改成8080 [root@server2 congfigmap]# kubectl edit cm nginx-config [root@server2 congfigmap]# kubectl get pod NAME READY STATUS RESTARTS AGE demo-7f476857fb-8xsmz 1/1 Running 0 6m44s [root@server2 congfigmap]# kubectl delete pod demo-7f476857fb-8xsmz [root@server2 congfigmap]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES demo-7f476857fb-w7wbs 1/1 Running 0 77s 10.244.141.199 server3
[root@server2 congfigmap]# curl 10.244.141.199:8080 Hello MyApp | Version: v1 | Pod Name