当前位置:   article > 正文

k8s中Pod控制器简介,三种处理无状态pod应用ReplicaSet、Deployment、HPA控制器、处理有状态pod应用StatefulSet控制器介绍_k8s 无状态pod

k8s 无状态pod

目录

一.Pod控制器简介

二.ReplicaSet(简写rs)

1.简介

(1)主要功能

(2)rs较完整参数解释

2.创建和删除

(1)创建

(2)删除

3.扩容和缩容

(1)scale方式

(2)edit方式

4.镜像版本更改

(1)set image方式

(2)edit方式

(3)尤其注意

三.Deployment(简写deploy)

1.简介

(1)主要功能

(2)deploy较完整参数解释

2.创建和删除

(1)创建

(2)删除

3.扩容和缩容

4.镜像版本更改

(1)Recreate重建更新

(2)RollingUpdate滚动更新

5.镜像版本回滚(类似于历史记录)

(1)原理由来

(2)kubectl rollout参数

(3)案例演示

6.Deployment的金丝雀发布(为稳定运行)

四.Horizontal Pod Autoscaler(简写hpa)

1.主要功能

2.下载安装metrics-server

3.创建/删除HPA

4.使用ab对该hpa进行负载测试

(1)下载httpd-tools

(2)对刚才的service暴露出来的IP进行压测,总访问量为200000,每次访问1000次

(3)接下来减少访问量看看他们的变化如何,期望和运行的pod自动减少

五.StatefulSet

1.简介

2.案例演示

3.StatefulSet的金丝雀发布

(1)滚动更新,同deployment

(2)删除后更新OnDelete


一.Pod控制器简介

(1)之前我们提到kubernetes的最小管理单元室pod,也讲到过有自主式pod和控制器管理pod(推荐)两种。pod控制器结余用户管理和pod中间,利用pod控制器来告知pod自己的期望需求,同时还包含有编排pod的多种策略。这里我们主要介绍ReplicaSet(无状态)、Deployment(无状态)、Horizontal Pod Autoscaler(通常无状态)、DaemonSet(守护型)、Job(批处理)、Cronjob(批处理)、StatefulSet(有状态)七个控制器。

(2)有/无状态

a.无状态Pod应用

是一种简单的 Pod,它不会存储任何持久化数据。当 Pod 被删除或重新启动时,容器内的数据也会被删除。

没有顺序要求且不用考虑在哪个node上运行,甚至可以自由扩/缩容。

通常用于部署无需持久化存储数据的应用程序,例如 Web 服务器、负载均衡器等。

b.有状态Pod应用

是一种可以存储持久化数据的 Pod。它们可以使用网络存储卷来存储数据,并且每个 Pod 都有一个唯一的标识符。这使得有状态 Pod 可以保持其身份和状态,并且在重新启动后可以保留数据

几乎和无状态呈相反特点,他需要考虑运行在哪个node,需要按照顺序来进行扩/缩容,且有状态应用pod相互独立(etcd,zooleeper元数据不同),具有唯一性和启动顺序性。

通常用于需要存储状态信息的应用程序,例如数据库、缓存服务器等。

(3)守护型

守护型 Pod 会自动在每个节点上创建和维护副本,以确保在整个集群中的每个节点上都能运行该 Pod,可以通过适当的配置进行扩展、升级和维护,适用于收集日志、执行后台任务,运行网络服务。

(4)批处理

批处理型 Pod 旨在执行一次性任务或定期任务,而不是持续运行的服务,适用于数据处理、定时任务和一次性理性任务执行。

二.ReplicaSet(简写rs)

1.简介

(1)主要功能

保障pod的副本维持在期望数量,持续监听pod运行状态(故障后进行重启或重创键),进行pod扩/缩容,进行镜像的升级,主要是处理无状态pod应用。

(2)rs较完整参数解释

截取edit rs的spec部分

  1. spec:
  2. replicas: 3
  3. selector:
  4.   matchLabels:
  5.     app: my-nginx
  6. template:
  7.   metadata:
  8.     creationTimestamp: null
  9.     labels:
  10.       app: my-nginx
  11.   spec:
  12.     containers:
  13.     - image: nginx:1.17.1
  14.       imagePullPolicy: IfNotPresent
  15.       name: nginx-container
  16.       ports:
  17.       - containerPort: 80
  18.         protocol: TCP
  19.       resources: {}
  20.       terminationMessagePath: /dev/termination-log
  21.       #指定容器终止时日志消息的路径。在容器终止时,Kubernetes 将将容器的终止消息写入指定路径的文件中
  22.       terminationMessagePolicy: File
  23.       #定义容器终止消息的策略。File 表示将终止消息写入到文件中,其他选项还包括 FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
  24.     dnsPolicy: ClusterFirst
  25.     #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
  26.     restartPolicy: Always
  27.     schedulerName: default-scheduler
  28.     #指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器
  29.     securityContext: {}
  30.     terminationGracePeriodSeconds: 30
  31.     #指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止

2.创建和删除

(1)创建

  1. [root@k8s-master pod]# cat myrs.yaml
  2. apiVersion: apps/v1
  3. kind: ReplicaSet
  4. metadata:
  5. name: my-rs
  6. namespace: myns
  7. spec:
  8. replicas: 3
  9. selector:   #选择器部分,使用matchLabels机制来定义label,定义选择器可以表明这个控制器能够管理哪些pod
  10.   matchLabels:
  11.     app: my-nginx
  12. template:   #副本数量不足时,根据模版创建pod副本
  13.   metadata:
  14.     labels:
  15.       app: my-nginx
  16.   spec:
  17.     containers:
  18.     - name: nginx-container
  19.       image: nginx
  20.       ports:
  21.       - containerPort: 80
  22.        
  23. [root@k8s-master pod]# kubectl get rs -n myns
  24. NAME   DESIRED   CURRENT   READY   AGE
  25. my-rs   3         3         3       8s
  26. #DESIRED指期望副本数,CURRENT指当前副本数
  27. [root@k8s-master pod]# kubectl get pods -n myns
  28. NAME         READY   STATUS   RESTARTS   AGE
  29. my-rs-jb2cd   1/1     Running   0         12s
  30. my-rs-jsr84   1/1     Running   0         12s
  31. my-rs-sdk9l   1/1     Running   0         12s

(2)删除

a.同以前使用yaml文件创建自主pod一样,同样可以通过yaml文件配合delete进行删除

  1. [root@k8s-master pod]# kubectl get pods -n myns
  2. NAME         READY   STATUS   RESTARTS   AGE
  3. my-rs-2xk2p   1/1     Running   0         10m
  4. my-rs-rl9zw   1/1     Running   0         10m
  5. my-rs-zp6zs   1/1     Running   0         10m
  6. [root@k8s-master pod]# kubectl delete -f myrs.yaml
  7. replicaset.apps "my-rs" deleted
  8. [root@k8s-master pod]# kubectl get pods -n myns
  9. NAME         READY   STATUS       RESTARTS   AGE
  10. my-rs-2xk2p   0/1     Terminating   0         12m
  11. my-rs-rl9zw   0/1     Terminating   0         12m
  12. [root@k8s-master pod]# kubectl get pods -n myns
  13. No resources found in myns namespace.

b.仅删除rs而不删除其下管理的pod

  1. [root@k8s-master pod]# kubectl delete rs my-rs -n myns --cascade=orphan  
  2. #较新的版本用--cascade=orphan来替代--cascade=false,都表示同一个意思
  3. replicaset.apps "my-rs" deleted
  4. [root@k8s-master pod]# kubectl get pods -n myns
  5. NAME         READY   STATUS   RESTARTS   AGE
  6. my-rs-blcgf   1/1     Running   0         107s
  7. my-rs-kkqfp   1/1     Running   0         107s
  8. my-rs-l7tqk   1/1     Running   0         107s
  9. [root@k8s-master pod]# kubectl get rs -n myns
  10. No resources found in myns namespace.

c.rs连同pod一起删除

  1. [root@k8s-master pod]# kubectl get pods -n myns -o wide
  2. NAME         READY   STATUS   RESTARTS   AGE   IP               NODE       NOMINATED NODE   READINESS GATES
  3. my-rs-6lfgd   1/1     Running   0         21s   10.244.169.132   k8s-node2   <none>           <none>
  4. my-rs-cqtfg   1/1     Running   0         21s   10.244.36.70     k8s-node1   <none>           <none>
  5. my-rs-rv6j5   1/1     Running   0         21s   10.244.36.69     k8s-node1   <none>           <none>
  6. [root@k8s-master pod]# kubectl delete rs my-rs -n myns
  7. replicaset.apps "my-rs" deleted
  8. [root@k8s-master pod]# kubectl get pods -n myns
  9. No resources found in myns namespace.
  10. [root@k8s-master pod]# kubectl get rs -n myns
  11. No resources found in myns namespace.

3.扩容和缩容

(1)scale方式

直接通过“replicas=num”即可

  1. [root@k8s-master pod]# kubectl scale rs my-rs --replicas=6 -n myns
  2. replicaset.apps/my-rs scaled
  3. [root@k8s-master pod]# kubectl get rs -n myns
  4. NAME   DESIRED   CURRENT   READY   AGE
  5. my-rs   6         6         6       2m12s
  6. [root@k8s-master pod]# kubectl scale rs my-rs --replicas=2 -n myns
  7. replicaset.apps/my-rs scaled
  8. [root@k8s-master pod]# kubectl get rs -n myns
  9. NAME   DESIRED   CURRENT   READY   AGE
  10. my-rs   2         2         2       2m58s

(2)edit方式

通过edit去编辑RS,更改其中的replicas保存退出后直接查看即可。

ba2630fdd5644455baf483f317e27d29.png

  1. [root@k8s-master pod]# kubectl edit rs my-rs -n myns
  2. replicaset.apps/my-rs edited
  3. [root@k8s-master pod]# kubectl get rs -n myns
  4. NAME   DESIRED   CURRENT   READY   AGE
  5. my-rs   5         5         5       4m

4.镜像版本更改

(1)set image方式

kubectl set image rs rs名称 pod内容器的名称=镜像名:具体版本”

  1. [root@k8s-master pod]# kubectl set image rs my-rs nginx-container=nginx:1.17.1 -n myns
  2. replicaset.apps/my-rs image updated
  3. [root@k8s-master pod]# kubectl get rs -n myns -o wide
  4. NAME   DESIRED   CURRENT   READY   AGE     CONTAINERS       IMAGES         SELECTOR
  5. my-rs   3         3         3       6m35s   nginx-container   nginx:1.17.1   app=my-nginx

(2)edit方式

cf9b2ccebace4582a2529e4061488237.png

  1. [root@k8s-master pod]# kubectl edit rs my-rs -n myns
  2. replicaset.apps/my-rs edited
  3. [root@k8s-master pod]# kubectl get rs -n myns -o wide
  4. NAME   DESIRED   CURRENT   READY   AGE     CONTAINERS       IMAGES         SELECTOR
  5. my-rs   3         3         3       3m48s   nginx-container   nginx:1.17.2   app=my-nginx

(3)尤其注意

ReplicaSet修改镜像版本,对rs进行更改表面上rs的标识的版本号已经改变,但是pod的镜像版本仍然未变,修改过后需要自己手动删除原有pod(不删除rs),删除过后rs会自动重新创建pod

  1. [root@k8s-master pod]# kubectl set image rs my-rs nginx-container=nginx:1.17.2 -n myns
  2. replicaset.apps/my-rs image updated
  3. [root@k8s-master pod]# kubectl describe rs my-rs -n myns | grep Image   #rs版本变化
  4.   Image:       nginx:1.17.2
  5. [root@k8s-master pod]# kubectl describe pod my-rs-mshs6 -n myns | grep Image #pod版本未变
  6.   Image:         nginx:1.17.1
  7.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  8. [root@k8s-master pod]# kubectl delete pod my-rs-6wl6h my-rs-d5rd5 my-rs-mshs6 -n myns #手动删除pod副本
  9. pod "my-rs-6wl6h" deleted
  10. pod "my-rs-d5rd5" deleted
  11. pod "my-rs-mshs6" deleted
  12. [root@k8s-master pod]# kubectl get pods -n myns   #自动重新创建三个副本
  13. NAME         READY   STATUS   RESTARTS   AGE
  14. my-rs-bg6fc   1/1     Running   0         119s
  15. my-rs-czgdc   1/1     Running   0         119s
  16. my-rs-dwdbd   1/1     Running   0         119s
  17. [root@k8s-master pod]# kubectl describe pod my-rs-bg6fc -n myns | grep Image   #再查看就成功更改了
  18.   Image:         nginx:1.17.2
  19.   Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
  20. [root@k8s-master pod]# kubectl describe rs my-rs -n myns | grep Image
  21.   Image:       nginx:1.17.2

三.Deployment(简写deploy)

1.简介

(1)主要功能

在ReplicaSet的基础上进一步对pod进行编排服务,除了支持比ReplicaSet的所有功能(监控、扩/缩容、镜像版本管理)还可以进行发布管理、滚动升级和回滚版本。主要处理无状态应用。

(2)deploy较完整参数解释

截取edit deploy中的spec部分

  1. spec:
  2. progressDeadlineSeconds: 600 #部署超时时间,默认600s
  3. replicas: 3
  4. revisionHistoryLimit: 10   #保留历史修订版本数
  5. selector:
  6.   matchLabels:
  7.     app: my-nginx
  8. strategy:   #策略
  9.   rollingUpdate:     #滚动更新
  10.     maxSurge: 25%   #最大额外可拥有的pod副本数量,整数/百分比
  11.     maxUnavailable: 25%   #最大不可用pod的最大值,整数/百分比
  12.   type: RollingUpdate   #镜像更新方式,支持RollingUpdate和Recreate,后面会详细介绍
  13. template:   #模版
  14.   metadata:
  15.     creationTimestamp: null
  16.     labels:
  17.       app: my-nginx
  18.   spec:
  19.     containers:
  20.     - image: nginx
  21.       imagePullPolicy: Always
  22.       name: nginx-pod
  23.       resources: {}
  24.       terminationMessagePath: /dev/termination-log   #容器终止时日志消息路径位置
  25.       terminationMessagePolicy: File  
  26.       #容器终止消息策略,File表示将终止消息写入到文件中,其他选项还包括FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
  27.     dnsPolicy: ClusterFirst
  28.     #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
  29.     restartPolicy: Always
  30.     schedulerName: default-scheduler #指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器
  31.     securityContext: {}
  32.     terminationGracePeriodSeconds: 30
  33.     #指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止

2.创建和删除

(1)创建

  1. [root@k8s-master pod]# cat mydeploy.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-nginx
  6. namespace: myns
  7. labels:
  8.   name: my-de
  9. spec:
  10. replicas: 3
  11. selector:
  12.   matchLabels:
  13.     app: my-nginx
  14. template:
  15.   metadata:
  16.     labels:
  17.       app: my-nginx
  18.   spec:
  19.     containers:
  20.     - name: nginx-pod
  21.       image: nginx
  22. [root@k8s-master pod]# kubectl get pods -n myns
  23. NAME                       READY   STATUS   RESTARTS   AGE
  24. my-nginx-7b78cb5c86-b2tdh   1/1     Running   0         4m5s
  25. my-nginx-7b78cb5c86-qtcsr   1/1     Running   0         4m5s
  26. my-nginx-7b78cb5c86-v7xr9   1/1     Running   0         4m5s
  27. [root@k8s-master pod]# kubectl get deploy -n myns
  28. NAME       READY   UP-TO-DATE   AVAILABLE   AGE
  29. my-nginx   3/3     3           3           4m10s

(2)删除

方式同ReplicaSet的删除

  1. #只删deploy不删pod
  2. [root@k8s-master pod]# kubectl delete deployment my-nginx -n myns --cascade=orphan
  3. deployment.apps "my-nginx" deleted
  4. [root@k8s-master pod]# kubectl get deploy -n myns
  5. No resources found in myns namespace.
  6. [root@k8s-master pod]# kubectl get pods -n myns
  7. NAME                       READY   STATUS   RESTARTS   AGE
  8. my-nginx-7b78cb5c86-fqvcm   1/1     Running   0         2m9s
  9. my-nginx-7b78cb5c86-nf5gb   1/1     Running   0         2m9s
  10. my-nginx-7b78cb5c86-wpsbd   1/1     Running   0         2m9s
  11. #deploy和pod一起删除
  12. [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
  13. deployment.apps/my-nginx created
  14. [root@k8s-master pod]# kubectl delete -f mydeploy.yaml
  15. deployment.apps "my-nginx" deleted
  16. [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
  17. deployment.apps/my-nginx created
  18. [root@k8s-master pod]# kubectl delete deploy my-nginx -n myns
  19. deployment.apps "my-nginx" deleted

3.扩容和缩容

用法和ReplicaSet一致,支持scale和edit

4.镜像版本更改

更改方式和ReplicaSet一致,但这里在其基础上介绍Deployment可配置的两种镜像更新策略

(1)Recreate重建更新

该参数表示察觉到版本更改后会先杀死现在版本的pod再重新建立新版本pod

  1. [root@k8s-master pod]# cat mydeploy.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-nginx
  6. namespace: myns
  7. labels:
  8.   name: my-de
  9. spec:
  10. replicas: 3
  11. selector:
  12.   matchLabels:
  13.     app: my-nginx
  14. strategy:
  15.   type: Recreate   #指定为Recreate方式
  16. template:
  17.   metadata:
  18.     labels:
  19.       app: my-nginx
  20.   spec:
  21.     containers:
  22.     - name: nginx-pod
  23.       image: nginx
  24.        
  25. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns
  26. deployment.apps/my-nginx image updated
  27. [root@k8s-master pod]# kubectl get pods -n myns
  28. NAME                       READY   STATUS   RESTARTS   AGE
  29. my-nginx-79df9bbf76-586sz   1/1     Running   0         14s
  30. my-nginx-79df9bbf76-9jq2n   1/1     Running   0         14s
  31. my-nginx-79df9bbf76-lw8f2   1/1     Running   0         14s
  32. [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep Image
  33.   Image:         nginx:1.17.1
  34.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  35.   Image:         nginx:1.17.1
  36.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  37.   Image:         nginx:1.17.1
  38.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  39.    
  40. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #再次更改后查看,原pod已无法找到,新建立了pod
  41. deployment.apps/my-nginx image updated
  42. [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep Image
  43. Error from server (NotFound): pods "my-nginx-79df9bbf76-586sz" not found
  44. Error from server (NotFound): pods "my-nginx-79df9bbf76-9jq2n" not found
  45. Error from server (NotFound): pods "my-nginx-79df9bbf76-lw8f2" not found
  46. [root@k8s-master pod]# kubectl get pods -n myns
  47. NAME                       READY   STATUS   RESTARTS   AGE
  48. my-nginx-7978cf754c-2xs4q   1/1     Running   0         11s
  49. my-nginx-7978cf754c-rs2vk   1/1     Running   0         11s
  50. my-nginx-7978cf754c-zlp4k   1/1     Running   0         11s

(2)RollingUpdate滚动更新

该参数表示察觉到版本更改后,删除一个旧版本pod再新建一个新版本pod,边删边建

  1. [root@k8s-master pod]# cat mydeploy.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: my-nginx
  6. namespace: myns
  7. labels:
  8.   name: my-de
  9. spec:
  10. replicas: 3
  11. selector:
  12.   matchLabels:
  13.     app: my-nginx
  14. strategy:
  15.   type: RollingUpdate   #指定滚动更新策略
  16.   rollingUpdate:
  17.     maxSurge: 25%
  18.     maxUnavailable: 25%
  19. template:
  20.   metadata:
  21.     labels:
  22.       app: my-nginx
  23.   spec:
  24.     containers:
  25.     - name: nginx-pod
  26.       image: nginx
  27. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns #更改版本
  28. deployment.apps/my-nginx image updated
  29. [root@k8s-master pod]# kubectl describe pod my-nginx-7b78cb5c86-5f56f my-nginx-7b78cb5c86-m27zq my-nginx-7b78cb5c86-vfrfx -n myns | grep Image
  30. #仔细观察,此处已经开始部分pod的更新了,新pod正在创建中
  31.   Image:         nginx
  32.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  33.   Image:         nginx
  34.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  35.   Image:         nginx
  36.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  37. [root@k8s-master pod]# kubectl get pods -n myns
  38. NAME                       READY   STATUS             RESTARTS   AGE
  39. my-nginx-79df9bbf76-8szdt   0/1     ContainerCreating   0         2s
  40. my-nginx-79df9bbf76-9gxtz   1/1     Running             0         6s
  41. my-nginx-7b78cb5c86-5f56f   1/1     Running             0         94s
  42. my-nginx-7b78cb5c86-vfrfx   1/1     Running             0         94s
  43. [root@k8s-master pod]# kubectl get pods -n myns
  44. NAME                       READY   STATUS             RESTARTS   AGE
  45. my-nginx-79df9bbf76-8szdt   1/1     Running             0         6s
  46. my-nginx-79df9bbf76-9d9jm   0/1     ContainerCreating   0         2s
  47. my-nginx-79df9bbf76-9gxtz   1/1     Running             0         10s
  48. my-nginx-7b78cb5c86-5f56f   1/1     Running             0         98s
  49. [root@k8s-master pod]# kubectl get pods -n myns   #最后所有旧pod都被替换为新版本了
  50. NAME                       READY   STATUS   RESTARTS   AGE
  51. my-nginx-79df9bbf76-8szdt   1/1     Running   0         10s
  52. my-nginx-79df9bbf76-9d9jm   1/1     Running   0         6s
  53. my-nginx-79df9bbf76-9gxtz   1/1     Running   0         14s
  54. [root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-8szdt my-nginx-79df9bbf76-9d9jm my-nginx-79df9bbf76-9gxtz -n myns | grep Image
  55.   Image:         nginx:1.17.1
  56.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  57.   Image:         nginx:1.17.1
  58.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  59.   Image:         nginx:1.17.1
  60.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb

5.镜像版本回滚(类似于历史记录

(1)原理由来

上面我们更改deploy中pod镜像版本后,可以看到存在一个pod数量为0的rs,理解为这是为deploy留下的回滚依据,更新完成只需要将不使用的rs的pod数降为0,回滚时又将pod数提升到期望数量即可,同时版本又跟着变化

  1. [root@k8s-master pod]# kubectl get rs -n myns
  2. NAME                 DESIRED   CURRENT   READY   AGE
  3. my-nginx-79df9bbf76   3         3         3       6m35s
  4. my-nginx-7b78cb5c86   0         0         0       8m3s

(2)kubectl rollout参数

  1. [root@k8s-master pod]# kubectl rollout status deploy my-nginx -n myns
  2. #显示当前升级状态
  3. [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns
  4. #显示当前升级历史记录
  5. [root@k8s-master pod]# kubectl rollout pause deploy my-nginx -n myns
  6. #暂停当前升级过程
  7. [root@k8s-master pod]# kubectl rollout resume deploy my-nginx -n myns
  8. #继续执行暂停的升级过程
  9. [root@k8s-master pod]# kubectl rollout restart deploy my-nginx -n myns
  10. #重新启动升级过程
  11. [root@k8s-master pod]# kubectl rollout undo deploy my-nginx --to-revision=history内查看到的号 -n myns
  12. #回退逮哪一级版本

(3)案例演示

这里我们对原来的nginx:1.17.1进行两次版本升级,然后最后回退到nginx:1.17.1

  1. [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns #查看版本历史
  2. deployment.apps/my-nginx
  3. REVISION CHANGE-CAUSE
  4. 1         <none>
  5. 2         <none>
  6. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #进行两次版本升级
  7. deployment.apps/my-nginx image updated
  8. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:latest -n myns
  9. deployment.apps/my-nginx image updated
  10. [root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns #升级记录已多出两次
  11. deployment.apps/my-nginx
  12. REVISION CHANGE-CAUSE
  13. 1         <none>
  14. 2         <none>
  15. 3         <none>
  16. 4         <none>
  17. [root@k8s-master pod]# kubectl rollout undo deploy my-nginx --to-revision=2 -n myns #指定回到历史记录中的2,即nginx:1.17.1
  18. deployment.apps/my-nginx rolled back
  19. [root@k8s-master pod]# kubectl describe deploy my-nginx -n myns | grep Image
  20.   Image:       nginx:1.17.1

6.Deployment的金丝雀发布(为稳定运行)

上面讲到deployment可以对pod的镜像版本进行滚动更新,但是在更新后的版本对于用户或者软件来说可能运行错误或不兼容,那么金丝雀就派上用处了,原理就是在更新镜像但只更新了部分pod的情况下暂停更新,待测试正常后再回复更新,利于运行更稳定。

下面有一个案例演示

  1. [root@k8s-master pod]# kubectl apply -f mydeploy.yaml
  2. deployment.apps/my-nginx created
  3. [root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns && kubectl rollout pause deploy my-nginx -n myns
  4. #更新操作成功开始后立即停止更新
  5. deployment.apps/my-nginx image updated
  6. deployment.apps/my-nginx paused
  7. [root@k8s-master pod]# kubectl get pods -n myns
  8. NAME                       READY   STATUS   RESTARTS   AGE
  9. my-nginx-7978cf754c-hjg2b   1/1     Running   0         10s
  10. my-nginx-7b78cb5c86-kzbw5   1/1     Running   0         103s
  11. my-nginx-7b78cb5c86-lwjxf   1/1     Running   0         103s
  12. my-nginx-7b78cb5c86-mbhr5   1/1     Running   0         103s
  13. [root@k8s-master pod]# kubectl describe pod my-nginx-7978cf754c-hjg2b my-nginx-7b78cb5c86-kzbw5 my-nginx-7b78cb5c86-lwjxf my-nginx-7b78cb5c86-mbhr5 -n myns | grep Image
  14. #可以看到目前新建了一个新版本的pod,但是还没有删除原有旧版本pod,说明已经停止更新,如果需要对pod进行相关的测试就可以现在做
  15.   Image:         nginx:1.17.2
  16.   Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
  17.   Image:         nginx
  18.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  19.   Image:         nginx
  20.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  21.   Image:         nginx
  22.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  23. [root@k8s-master pod]# kubectl rollout resume deploy my-nginx -n myns
  24. #测试结果满意之后就继续执行更新操作
  25. deployment.apps/my-nginx resumed
  26. [root@k8s-master pod]# kubectl get pods -n myns
  27. NAME                       READY   STATUS             RESTARTS   AGE
  28. my-nginx-7978cf754c-cwtkw   1/1     Running             0         7s
  29. my-nginx-7978cf754c-hjg2b   1/1     Running             0         2m16s
  30. my-nginx-7978cf754c-ktm5f   0/1     ContainerCreating   0         3s
  31. my-nginx-7b78cb5c86-lwjxf   1/1     Running             0         3m49s
  32. [root@k8s-master pod]# kubectl get pods -n myns
  33. NAME                       READY   STATUS   RESTARTS   AGE
  34. my-nginx-7978cf754c-cwtkw   1/1     Running   0         10s
  35. my-nginx-7978cf754c-hjg2b   1/1     Running   0         2m19s
  36. my-nginx-7978cf754c-ktm5f   1/1     Running   0         6s
  37. [root@k8s-master pod]# kubectl describe pod my-nginx-7978cf754c-cwtkw my-nginx-7978cf754c-hjg2b my-nginx-7978cf754c-ktm5f -n myns | grep Image
  38. #更新完成
  39.   Image:         nginx:1.17.2
  40.   Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
  41.   Image:         nginx:1.17.2
  42.   Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
  43.   Image:         nginx:1.17.2
  44.   Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41

四.Horizontal Pod Autoscaler(简写hpa)

1.主要功能

HPA通过检测所有pod使用情况(使用效率),与定义的指标进行比对,来对pod的数量自动进行调整,超出cpu阈值就另起pod,低于阈值就缩减pod。

2.下载安装metrics-server

(1)下载

链接:百度网盘 请输入提取码 提取码:3oob

(2)更改其中的镜像地址和新增参数

新增参数“- --kubelet-insecure-tls”

更该镜像为registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.6.2“

 9c7279e2f66449e5ac51e15bbb2be099.png

(3)应用该文件并查看是否成功运行其pod

  1. [root@k8s-master pod]# kubectl apply -f components.yaml
  2. serviceaccount/metrics-server created
  3. clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
  4. clusterrole.rbac.authorization.k8s.io/system:metrics-server created
  5. rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
  6. clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
  7. clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
  8. service/metrics-server created
  9. deployment.apps/metrics-server created
  10. apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
  11. [root@k8s-master pod]# kubectl get pod -n kube-system | grep metrics
  12. metrics-server-f974958b-hlb6p             1/1     Running   0         69s

3.创建/删除HPA

(1)以之前用到的deployment和为其创建的service(暴露端口用于外界测试)为例

  1. [root@k8s-master pod]# cat nginx-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. creationTimestamp: null
  6. labels:
  7.   app: nginx-deploy
  8. name: nginx-deploy
  9. namespace: myns
  10. spec:
  11. replicas: 1
  12. selector:
  13.   matchLabels:
  14.     app: nginx-deploy
  15. strategy: {}
  16. template:
  17.   metadata:
  18.     labels:
  19.       app: nginx-deploy
  20.   spec:
  21.     containers:
  22.     - image: nginx
  23.       name: nginx
  24.       ports:
  25.       - containerPort: 80
  26.       resources:
  27.         requests:
  28.           memory: "60M"
  29.           cpu: "1"
  30.         limits:
  31.           memory: "61M"
  32.           cpu: "1"

(2)暴露80端口服务

  1. [root@k8s-master pod]# kubectl expose deployment nginx-deploy --type=NodePort --port=80 -n myns
  2. [root@k8s-master pod]# kubectl get service -n myns   #后面测试用这个IP进行测试
  3. NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)       AGE
  4. nginx-deploy   NodePort   10.108.107.216 <none>       80:30446/TCP   20m

(3)创建hpa

这里使用的快捷命令将结果写入yaml并应用,快捷命令可指定的参数较少,可以后面根据需求又自行添加

  1. [root@k8s-master pod]# kubectl autoscale deployment nginx-deploy --min=1 --max=20 --cpu-percent=15 --dry-run=client -n myns -o yaml > myhpa.yaml
  2. [root@k8s-master pod]# cat myhpa.yaml
  3. apiVersion: autoscaling/v1
  4. kind: HorizontalPodAutoscaler
  5. metadata:
  6. creationTimestamp: null
  7. name: nginx-deploy
  8. namespace: myns
  9. spec:
  10. maxReplicas: 20
  11. minReplicas: 1
  12. scaleTargetRef:
  13.   apiVersion: apps/v1
  14.   kind: Deployment
  15.   name: nginx-deploy
  16. targetCPUUtilizationPercentage: 15   #这里指定的是期望的cpu使用率,超过这个值就会自动扩容
  17. status:
  18. currentReplicas: 0
  19. desiredReplicas: 0
  20. [root@k8s-master pod]# kubectl apply -f myhpa.yaml
  21. [root@k8s-master pod]# kubectl get hpa -n myns
  22. NAME           REFERENCE                 TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
  23. nginx-deploy   Deployment/nginx-deploy   0%/15%   1         20       1         13m

(4)使用delete -f 文件或delete hpa 名称即可删除

4.使用ab对该hpa进行负载测试

(1)下载httpd-tools

 yum install httpd-tools -y

(2)对刚才的service暴露出来的IP进行压测,总访问量为200000,每次访问1000次

  1. [root@k8s-master pod]# ab -c 1000 -n 250000 http://10.108.107.216/
  2. This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking 10.108.107.216 (be patient)
  6. Completed 25000 requests
  7. Completed 50000 requests
  8. Completed 75000 requests
  9. Completed 100000 requests
  10. Completed 125000 requests
  11. Completed 150000 requests
  12. Completed 175000 requests
  13. Completed 200000 requests
  14. Completed 225000 requests
  15. Completed 250000 requests
  16. Finished 250000 requests
  17. Server Software: nginx/1.25.3
  18. Server Hostname: 10.108.107.216
  19. Server Port: 80
  20. Document Path: /
  21. Document Length: 615 bytes
  22. Concurrency Level: 1000
  23. Time taken for tests: 30.574 seconds
  24. Complete requests: 250000
  25. Failed requests: 0
  26. Write errors: 0
  27. Total transferred: 212000000 bytes
  28. HTML transferred: 153750000 bytes
  29. Requests per second: 8176.76 [#/sec] (mean)
  30. Time per request: 122.298 [ms] (mean)
  31. Time per request: 0.122 [ms] (mean, across all concurrent requests)
  32. Transfer rate: 6771.38 [Kbytes/sec] received
  33. Connection Times (ms)
  34. min mean[+/-sd] median max
  35. Connect: 0 83 543.2 13 15048
  36. Processing: 0 36 86.0 18 5601
  37. Waiting: 0 33 84.8 15 5600
  38. Total: 1 119 551.6 34 15096
  39. Percentage of the requests served within a certain time (ms)
  40. 50% 34
  41. 66% 42
  42. 75% 48
  43. 80% 52
  44. 90% 75
  45. 95% 273
  46. 98% 1042
  47. 99% 3027
  48. 100% 15096 (longest request)
  49. [root@k8s-master pod]# kubectl get hpa -n myns -w #随着cpu使用率降低,pod副本期望也回归初始值
  50. NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
  51. nginx-deploy Deployment/nginx-deploy 99%/15% 1 20 2 68s
  52. nginx-deploy Deployment/nginx-deploy 68%/15% 1 20 4 75s
  53. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 90s
  54. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 105s
  55. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 2m
  56. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 2m15s
  57. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 7 7m
  58. nginx-deploy Deployment/nginx-deploy 0%/15% 1 20 1 7m15s

注意:测试的话,运行一个busybox的pod也可以

  1. [root@k8s-master pod]# kubectl run -it mybusybox --image=busybox -- sh
  2. / # while true; do wget -q -O- http://10.108.107.216;done

(3)接下来减少访问量看看他们的变化如何,期望和运行的pod自动减少

  1. [root@k8s-master pod]# ab -c 1000 -n 30000 http://10.108.107.216/
  2. This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking 10.108.107.216 (be patient)
  6. Completed 3000 requests
  7. Completed 6000 requests
  8. Completed 9000 requests
  9. Completed 12000 requests
  10. Completed 15000 requests
  11. Completed 18000 requests
  12. Completed 21000 requests
  13. Completed 24000 requests
  14. Completed 27000 requests
  15. Completed 30000 requests
  16. [root@k8s-master pod]# kubectl get hpa -n myns
  17. NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
  18. nginx-deploy Deployment/nginx-deploy 4%/15% 1 20 2 7m50s
  19. [root@k8s-master pod]# kubectl get deploy -n myns
  20. NAME READY UP-TO-DATE AVAILABLE AGE
  21. nginx-deploy 2/2 2 2 21m
  22. [root@k8s-master pod]# kubectl get pods -n myns
  23. NAME READY STATUS RESTARTS AGE
  24. nginx-deploy-556cd7b4fd-c6r6p 1/1 Running 0 11m
  25. nginx-deploy-556cd7b4fd-n9bnk 1/1 Running 0 11m

五.StatefulSet

1.简介

(1)StatefulSet一般用来部署Zookeeper集群、MySQL集群等,他需要HeadLinessService,一般称其为无头服务。

(2)对于Deployment来说,每一个Pod名称没有顺序(随机字符串),但pod ip时变化的,对于StatefulSet来说,“主机名.无头service的名称.名称空间.svc.cluster.local”是其的唯一标识(可以简写为"主机名.无头service的名称"),可以将这个唯一标识称为dns记录,无头服务能够让这些名称唯一。

(3)无头服务重要性

无头服务不会分配 Cluster IP,它不提供负载均衡或代理功能。

无头服务创建的同时会为pod记录独有的dns记录,如上所述,这会作为其的唯一标识,而通过这个dns直接访问pod,就不会经过服务的负载均衡,利于指定应用程序与特定pod的通信。

无头服务的域名是pod内解析,而不是node上dns解析的,所以在主机上访问域名是访问不到的。

2.案例演示

这个例子创建了一个pod副本数为3的statefulset,并为其创建了一个无头服务,最后通过所记录的dns去访问目标

  1. [root@k8s-master pod]# cat mystatefulset.yaml
  2. apiVersion: v1
  3. kind: Service   #这里为statefulset创建无头服务
  4. metadata:
  5. name: my-statefulset
  6. namespace: myns
  7. spec:
  8. selector:
  9.   app: mypod-nginx
  10. clusterIP: None   #需要将clusterip设置为none,禁用负载均衡和代理功能
  11. ports:
  12. - name: http
  13.   port: 80
  14.   targetPort: 80
  15.   protocol: TCP
  16. ---
  17. apiVersion: apps/v1
  18. kind: StatefulSet
  19. metadata:
  20. name: my-statefulset
  21. namespace: myns
  22. spec:
  23. replicas: 3
  24. serviceName: my-statefulset   #这里就要指定无头服务的名称,引用无头服务
  25. template:
  26.   metadata:
  27.     labels:
  28.       app: mypod-nginx
  29.   spec:
  30.     containers:
  31.     - name: nginx
  32.       image: nginx
  33.       ports:
  34.       - containerPort: 80
  35. selector:
  36.   matchLabels:
  37.     app: mypod-nginx
  38.      
  39. [root@k8s-master pod]# kubectl get pods,service,statefulsets.apps -n myns
  40. NAME                   READY   STATUS   RESTARTS   AGE
  41. pod/my-statefulset-0   1/1     Running   0         4m25s
  42. pod/my-statefulset-1   1/1     Running   0         4m20s
  43. pod/my-statefulset-2   1/1     Running   0         4m16s
  44. NAME                     TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
  45. service/my-statefulset   ClusterIP   None         <none>       80/TCP   4m25s
  46. NAME                             READY   AGE
  47. statefulset.apps/my-statefulset   3/3     4m25s
  48. [root@k8s-master pod]# kubectl exec -it my-statefulset-0 -n myns -- /bin/bash
  49. root@my-statefulset-0:/# curl my-statefulset-0.my-statefulset   #进入pod后,我们就通过标识规则记录了的dns去访问服务
  50. <!DOCTYPE html>
  51. <html>
  52. <head>
  53. <title>Welcome to nginx!</title>
  54. <style>
  55. html { color-scheme: light dark; }
  56. body { width: 35em; margin: 0 auto;
  57. font-family: Tahoma, Verdana, Arial, sans-serif; }
  58. </style>
  59. </head>
  60. <body>
  61. <h1>Welcome to nginx!</h1>
  62. <p>If you see this page, the nginx web server is successfully installed and
  63. working. Further configuration is required.</p>
  64. <p>For online documentation and support please refer to
  65. <a href="http://nginx.org/">nginx.org</a>.<br/>
  66. Commercial support is available at
  67. <a href="http://nginx.com/">nginx.com</a>.</p>
  68. <p><em>Thank you for using nginx.</em></p>
  69. </body>
  70. </html>
  71. root@my-statefulset-0:/# exit

3.StatefulSet的金丝雀发布

功能和deployment的金丝雀发布相同,不过statefulset的金丝雀发布支持OnDelete和RollingUpdate,下方一一介绍

(1)滚动更新,同deployment

示例演示

a.创建并应用yaml文件

  1. [root@k8s-master pod]# cat mystatefulset.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: my-statefulset
  6. namespace: myns
  7. spec:
  8. selector:
  9.   app: mypod-nginx
  10. clusterIP: None
  11. ports:
  12. - name: http
  13.   port: 80
  14.   targetPort: 80
  15.   protocol: TCP
  16. ---
  17. apiVersion: apps/v1
  18. kind: StatefulSet
  19. metadata:
  20. name: my-statefulset
  21. namespace: myns
  22. spec:
  23. replicas: 3
  24. serviceName: my-statefulset
  25. template:
  26.   metadata:
  27.     labels:
  28.       app: mypod-nginx
  29.   spec:
  30.     containers:
  31.     - name: nginx
  32.       image: nginx
  33.       ports:
  34.       - containerPort: 80
  35. selector:
  36.   matchLabels:
  37.     app: mypod-nginx
  38. updateStrategy:   #这部分为关键代码
  39.   rollingUpdate: #滚动更新参数
  40.     partition: 1   #这个参数表示从哪个分区(可以理解为索引值)开始更新,如此文件我们有三个pod,他们的索引是012,若是指定从1开始更新,那么0索引的pod的镜像不会被更新
  41.   type: RollingUpdate   #更新策略
  42.    
  43. [root@k8s-master pod]# kubectl apply -f mystatefulset.yaml
  44. service/my-statefulset created
  45. statefulset.apps/my-statefulset created

b.更改镜像版本,上面是从索引值为1的pod开始更新,我们目前有三个副本,那么第一个副本就不会被更新

  1. [root@k8s-master pod]# kubectl set image statefulsets my-statefulset nginx=nginx:1.17.1 -n myns
  2. statefulset.apps/my-statefulset image updated
  3. [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
  4.   Image:         nginx
  5.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  6.   Image:         nginx:1.17.1
  7.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  8.   Image:         nginx:1.17.1
  9.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  10. c.将partition更改(edit)为0,那么又会将第一个副本更新
  11. [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
  12.   Image:         nginx:1.17.1
  13.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  14.   Image:         nginx:1.17.1
  15.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  16.   Image:         nginx:1.17.1
  17.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb

(2)删除后更新OnDelete

注意:这个就需要自己进行手动删除了,删除一个更新一个

  1. [root@k8s-master pod]# cat mystatefulset.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: my-statefulset
  6. namespace: myns
  7. spec:
  8. selector:
  9.   app: mypod-nginx
  10. clusterIP: None
  11. ports:
  12. - name: http
  13.   port: 80
  14.   targetPort: 80
  15.   protocol: TCP
  16. ---
  17. apiVersion: apps/v1
  18. kind: StatefulSet
  19. metadata:
  20. name: my-statefulset
  21. namespace: myns
  22. spec:
  23. replicas: 3
  24. serviceName: my-statefulset
  25. template:
  26.   metadata:
  27.     labels:
  28.       app: mypod-nginx
  29.   spec:
  30.     containers:
  31.     - name: nginx
  32.       image: nginx
  33.       ports:
  34.       - containerPort: 80
  35. selector:
  36.   matchLabels:
  37.     app: mypod-nginx
  38. updateStrategy:   #指定为OnDelete策略
  39.   type: OnDelete
  40. [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image #更新前查看
  41.   Image:         nginx
  42.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  43.   Image:         nginx
  44.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  45.   Image:         nginx
  46.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  47. [root@k8s-master pod]# kubectl set image statefulsets my-statefulset nginx=nginx:1.17.1 -n myns
  48. statefulset.apps/my-statefulset image updated
  49. [root@k8s-master pod]# kubectl get pods -n myns
  50. NAME               READY   STATUS   RESTARTS   AGE
  51. my-statefulset-0   1/1     Running   0         2m29s
  52. my-statefulset-1   1/1     Running   0         2m24s
  53. my-statefulset-2   1/1     Running   0         2m18s
  54. [root@k8s-master pod]# kubectl delete pods my-statefulset-0 -n myns
  55. pod "my-statefulset-0" deleted
  56. [root@k8s-master pod]# kubectl get pods -n myns
  57. NAME               READY   STATUS             RESTARTS   AGE
  58. my-statefulset-0   0/1     ContainerCreating   0         3s
  59. my-statefulset-1   1/1     Running             0         2m40s
  60. my-statefulset-2   1/1     Running             0         2m34s
  61. [root@k8s-master pod]# kubectl get pods -n myns
  62. NAME               READY   STATUS   RESTARTS   AGE
  63. my-statefulset-0   1/1     Running   0         6s
  64. my-statefulset-1   1/1     Running   0         2m43s
  65. my-statefulset-2   1/1     Running   0         2m37s
  66. [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
  67.   Image:         nginx:1.17.1
  68.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  69.   Image:         nginx
  70.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  71.   Image:         nginx
  72.   Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
  73.    
  74. [root@k8s-master pod]# kubectl delete pod my-statefulset-1 my-statefulset-2 -n myns
  75. pod "my-statefulset-1" deleted
  76. pod "my-statefulset-2" deleted
  77. [root@k8s-master pod]# kubectl describe pods -n myns -l app=mypod-nginx | grep Image
  78.   Image:         nginx:1.17.1
  79.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  80.   Image:         nginx:1.17.1
  81.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
  82.   Image:         nginx:1.17.1
  83.   Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/162332
推荐阅读
相关标签
  

闽ICP备14008679号