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

news/2024/4/17 16:10:20

目录

一.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次

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

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


一.Pod控制器简介

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

(2)有/无状态

无状态 Pod 是一种简单的 Pod,它不会存储任何持久化数据。当 Pod 被删除或重新启动时,容器内的数据也会被删除。无状态 Pod 通常用于部署无需持久化存储数据的应用程序,例如 Web 服务器、负载均衡器等。

有状态 Pod 是一种可以存储持久化数据的 Pod。它们可以使用网络存储卷来存储数据,并且每个 Pod 都有一个唯一的标识符。这使得有状态 Pod 可以保持其身份和状态,并且在重新启动后可以保留数据。有状态 Pod 通常用于需要存储状态信息的应用程序,例如数据库、缓存服务器等。

(3)守护型

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

(4)批处理

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

二.ReplicaSet(简写rs)

1.简介

(1)主要功能

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

(2)rs较完整参数解释

截取edit rs的spec部分

spec:replicas: 3selector:matchLabels:app: my-nginxtemplate:metadata:creationTimestamp: nulllabels:app: my-nginxspec:containers:- image: nginx:1.17.1imagePullPolicy: IfNotPresentname: nginx-containerports:- containerPort: 80protocol: TCPresources: {}terminationMessagePath: /dev/termination-log#指定容器终止时日志消息的路径。在容器终止时,Kubernetes 将将容器的终止消息写入指定路径的文件中terminationMessagePolicy: File#定义容器终止消息的策略。File 表示将终止消息写入到文件中,其他选项还包括 FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)dnsPolicy: ClusterFirst#定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名restartPolicy: AlwaysschedulerName: default-scheduler#指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器securityContext: {}terminationGracePeriodSeconds: 30#指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止

2.创建和删除

(1)创建

[root@k8s-master pod]# cat myrs.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:name: my-rsnamespace: myns
spec:replicas: 3selector:   #选择器部分,使用matchLabels机制来定义label,定义选择器可以表明这个控制器能够管理哪些podmatchLabels:app: my-nginxtemplate:   #副本数量不足时,根据模版创建pod副本metadata:labels:app: my-nginxspec:containers:- name: nginx-containerimage: nginxports:- containerPort: 80[root@k8s-master pod]# kubectl get rs -n myns
NAME    DESIRED   CURRENT   READY   AGE
my-rs   3         3         3       8s
#DESIRED指期望副本数,CURRENT指当前副本数
[root@k8s-master pod]# kubectl get pods -n myns
NAME          READY   STATUS    RESTARTS   AGE
my-rs-jb2cd   1/1     Running   0          12s
my-rs-jsr84   1/1     Running   0          12s
my-rs-sdk9l   1/1     Running   0          12s

(2)删除

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

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

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

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

c.rs连同pod一起删除

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

3.扩容和缩容

(1)scale方式

直接通过“replicas=num”即可

[root@k8s-master pod]# kubectl scale rs my-rs --replicas=6 -n myns
replicaset.apps/my-rs scaled
[root@k8s-master pod]# kubectl get rs -n myns
NAME    DESIRED   CURRENT   READY   AGE
my-rs   6         6         6       2m12s
​
[root@k8s-master pod]# kubectl scale rs my-rs --replicas=2 -n myns
replicaset.apps/my-rs scaled
[root@k8s-master pod]# kubectl get rs -n myns
NAME    DESIRED   CURRENT   READY   AGE
my-rs   2         2         2       2m58s

(2)edit方式

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

ba2630fdd5644455baf483f317e27d29.png

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

4.镜像版本更改

(1)set image方式

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

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

(2)edit方式

cf9b2ccebace4582a2529e4061488237.png

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

(3)尤其注意

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

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

三.Deployment(简写deploy)

1.简介

(1)主要功能

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

(2)deploy较完整参数解释

截取edit deploy中的spec部分

spec:progressDeadlineSeconds: 600  #部署超时时间,默认600sreplicas: 3revisionHistoryLimit: 10   #保留历史修订版本数selector:matchLabels:app: my-nginxstrategy:   #策略rollingUpdate:     #滚动更新maxSurge: 25%   #最大额外可拥有的pod副本数量,整数/百分比maxUnavailable: 25%   #最大不可用pod的最大值,整数/百分比type: RollingUpdate   #镜像更新方式,支持RollingUpdate和Recreate,后面会详细介绍template:    #模版metadata:creationTimestamp: nulllabels:app: my-nginxspec:containers:- image: nginximagePullPolicy: Alwaysname: nginx-podresources: {}terminationMessagePath: /dev/termination-log    #容器终止时日志消息路径位置terminationMessagePolicy: File   #容器终止消息策略,File表示将终止消息写入到文件中,其他选项还包括FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)dnsPolicy: ClusterFirst#定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名restartPolicy: AlwaysschedulerName: default-scheduler #指定用于调度 Pod 的调度器名称。default-scheduler 是默认的调度器securityContext: {}terminationGracePeriodSeconds: 30#指定容器终止的“和平”期限。在收到终止信号后,容器有 30 秒的时间来完成清理和终止操作。超过该时间后,容器将被强制终止

2.创建和删除

(1)创建

[root@k8s-master pod]# cat mydeploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginxnamespace: mynslabels:name: my-de
spec:replicas: 3selector:matchLabels:app: my-nginxtemplate:metadata:labels: app: my-nginxspec:containers:- name: nginx-podimage: nginx
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7b78cb5c86-b2tdh   1/1     Running   0          4m5s
my-nginx-7b78cb5c86-qtcsr   1/1     Running   0          4m5s
my-nginx-7b78cb5c86-v7xr9   1/1     Running   0          4m5s
[root@k8s-master pod]# kubectl get deploy -n myns
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
my-nginx   3/3     3            3           4m10s

(2)删除

方式同ReplicaSet的删除

#只删deploy不删pod
[root@k8s-master pod]# kubectl delete deployment my-nginx -n myns --cascade=orphan
deployment.apps "my-nginx" deleted
[root@k8s-master pod]# kubectl get deploy -n myns
No resources found in myns namespace.
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7b78cb5c86-fqvcm   1/1     Running   0          2m9s
my-nginx-7b78cb5c86-nf5gb   1/1     Running   0          2m9s
my-nginx-7b78cb5c86-wpsbd   1/1     Running   0          2m9s
#deploy和pod一起删除
[root@k8s-master pod]# kubectl apply -f mydeploy.yaml 
deployment.apps/my-nginx created
[root@k8s-master pod]# kubectl delete -f mydeploy.yaml 
deployment.apps "my-nginx" deleted
​
[root@k8s-master pod]# kubectl apply -f mydeploy.yaml 
deployment.apps/my-nginx created
[root@k8s-master pod]# kubectl delete deploy my-nginx -n myns
deployment.apps "my-nginx" deleted

3.扩容和缩容

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

4.镜像版本更改

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

(1)Recreate重建更新

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

[root@k8s-master pod]# cat mydeploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginxnamespace: mynslabels:name: my-de
spec:replicas: 3selector:matchLabels:app: my-nginxstrategy:type: Recreate   #指定为Recreate方式template:metadata:labels: app: my-nginxspec:containers:- name: nginx-podimage: nginx[root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns
deployment.apps/my-nginx image updated
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-79df9bbf76-586sz   1/1     Running   0          14s
my-nginx-79df9bbf76-9jq2n   1/1     Running   0          14s
my-nginx-79df9bbf76-lw8f2   1/1     Running   0          14s
[root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep ImageImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb[root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #再次更改后查看,原pod已无法找到,新建立了pod
deployment.apps/my-nginx image updated
[root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-586sz my-nginx-79df9bbf76-9jq2n my-nginx-79df9bbf76-lw8f2 -n myns | grep Image
Error from server (NotFound): pods "my-nginx-79df9bbf76-586sz" not found
Error from server (NotFound): pods "my-nginx-79df9bbf76-9jq2n" not found
Error from server (NotFound): pods "my-nginx-79df9bbf76-lw8f2" not found
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7978cf754c-2xs4q   1/1     Running   0          11s
my-nginx-7978cf754c-rs2vk   1/1     Running   0          11s
my-nginx-7978cf754c-zlp4k   1/1     Running   0          11s

(2)RollingUpdate滚动更新

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

[root@k8s-master pod]# cat mydeploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginxnamespace: mynslabels:name: my-de
spec:replicas: 3selector:matchLabels:app: my-nginxstrategy:type: RollingUpdate   #指定滚动更新策略rollingUpdate:maxSurge: 25%maxUnavailable: 25%template:metadata:labels: app: my-nginxspec:containers:- name: nginx-podimage: nginx
​
[root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.1 -n myns  #更改版本
deployment.apps/my-nginx image updated
​
[root@k8s-master pod]# kubectl describe pod my-nginx-7b78cb5c86-5f56f my-nginx-7b78cb5c86-m27zq my-nginx-7b78cb5c86-vfrfx -n myns | grep Image
#仔细观察,此处已经开始部分pod的更新了,新pod正在创建中Image:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62eeImage:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62eeImage:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS              RESTARTS   AGE
my-nginx-79df9bbf76-8szdt   0/1     ContainerCreating   0          2s
my-nginx-79df9bbf76-9gxtz   1/1     Running             0          6s
my-nginx-7b78cb5c86-5f56f   1/1     Running             0          94s
my-nginx-7b78cb5c86-vfrfx   1/1     Running             0          94s
​
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS              RESTARTS   AGE
my-nginx-79df9bbf76-8szdt   1/1     Running             0          6s
my-nginx-79df9bbf76-9d9jm   0/1     ContainerCreating   0          2s
my-nginx-79df9bbf76-9gxtz   1/1     Running             0          10s
my-nginx-7b78cb5c86-5f56f   1/1     Running             0          98s
​
[root@k8s-master pod]# kubectl get pods -n myns   #最后所有旧pod都被替换为新版本了
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-79df9bbf76-8szdt   1/1     Running   0          10s
my-nginx-79df9bbf76-9d9jm   1/1     Running   0          6s
my-nginx-79df9bbf76-9gxtz   1/1     Running   0          14s
​
[root@k8s-master pod]# kubectl describe pod my-nginx-79df9bbf76-8szdt my-nginx-79df9bbf76-9d9jm my-nginx-79df9bbf76-9gxtz -n myns | grep ImageImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbImage:          nginx:1.17.1Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb

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

(1)原理由来

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

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

(2)kubectl rollout参数

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

(3)案例演示

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

[root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns  #查看版本历史
deployment.apps/my-nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
​
[root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:1.17.2 -n myns #进行两次版本升级
deployment.apps/my-nginx image updated
[root@k8s-master pod]# kubectl set image deploy my-nginx nginx-pod=nginx:latest -n myns
deployment.apps/my-nginx image updated
​
[root@k8s-master pod]# kubectl rollout history deploy my-nginx -n myns  #升级记录已多出两次
deployment.apps/my-nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>
4         <none>
​
​
[root@k8s-master pod]# kubectl rollout undo deploy my-nginx --to-revision=2 -n myns  #指定回到历史记录中的2,即nginx:1.17.1
deployment.apps/my-nginx rolled back
​
[root@k8s-master pod]# kubectl describe deploy my-nginx -n myns | grep ImageImage:        nginx:1.17.1

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

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

下面有一个案例演示

[root@k8s-master pod]# kubectl apply -f mydeploy.yaml 
deployment.apps/my-nginx created
​
[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
#更新操作成功开始后立即停止更新
deployment.apps/my-nginx image updated
deployment.apps/my-nginx paused
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7978cf754c-hjg2b   1/1     Running   0          10s
my-nginx-7b78cb5c86-kzbw5   1/1     Running   0          103s
my-nginx-7b78cb5c86-lwjxf   1/1     Running   0          103s
my-nginx-7b78cb5c86-mbhr5   1/1     Running   0          103s
​
[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
#可以看到目前新建了一个新版本的pod,但是还没有删除原有旧版本pod,说明已经停止更新,如果需要对pod进行相关的测试就可以现在做Image:          nginx:1.17.2Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41Image:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62eeImage:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62eeImage:          nginxImage ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
​
[root@k8s-master pod]# kubectl rollout resume deploy my-nginx -n myns
#测试结果满意之后就继续执行更新操作
deployment.apps/my-nginx resumed
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS              RESTARTS   AGE
my-nginx-7978cf754c-cwtkw   1/1     Running             0          7s
my-nginx-7978cf754c-hjg2b   1/1     Running             0          2m16s
my-nginx-7978cf754c-ktm5f   0/1     ContainerCreating   0          3s
my-nginx-7b78cb5c86-lwjxf   1/1     Running             0          3m49s
[root@k8s-master pod]# kubectl get pods -n myns
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7978cf754c-cwtkw   1/1     Running   0          10s
my-nginx-7978cf754c-hjg2b   1/1     Running   0          2m19s
my-nginx-7978cf754c-ktm5f   1/1     Running   0          6s
[root@k8s-master pod]# kubectl describe pod my-nginx-7978cf754c-cwtkw my-nginx-7978cf754c-hjg2b my-nginx-7978cf754c-ktm5f -n myns | grep Image
#更新完成Image:          nginx:1.17.2Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41Image:          nginx:1.17.2Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41Image:          nginx:1.17.2Image 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

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

3.创建/删除HPA

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

[root@k8s-master pod]# cat nginx-deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: nginx-deployname: nginx-deploynamespace: myns
spec:replicas: 1selector:matchLabels:app: nginx-deploystrategy: {}template:metadata:labels:app: nginx-deployspec:containers:- image: nginxname: nginxports:- containerPort: 80resources:requests:memory: "60M"cpu: "1"limits:memory: "61M"cpu: "1"

(2)暴露80端口服务

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

(3)创建hpa

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

[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
​
[root@k8s-master pod]# cat myhpa.yaml 
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:creationTimestamp: nullname: nginx-deploynamespace: myns
spec:maxReplicas: 20minReplicas: 1scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginx-deploytargetCPUUtilizationPercentage: 15   #这里指定的是期望的cpu使用率,超过这个值就会自动扩容
status:currentReplicas: 0desiredReplicas: 0
​
[root@k8s-master pod]# kubectl apply -f myhpa.yaml
​
[root@k8s-master pod]# kubectl get hpa -n myns
NAME           REFERENCE                 TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
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次

[root@k8s-master pod]# ab -c 1000 -n 250000 http://10.108.107.216/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 10.108.107.216 (be patient)
Completed 25000 requests
Completed 50000 requests
Completed 75000 requests
Completed 100000 requests
Completed 125000 requests
Completed 150000 requests
Completed 175000 requests
Completed 200000 requests
Completed 225000 requests
Completed 250000 requests
Finished 250000 requestsServer Software:        nginx/1.25.3
Server Hostname:        10.108.107.216
Server Port:            80Document Path:          /
Document Length:        615 bytesConcurrency Level:      1000
Time taken for tests:   30.574 seconds
Complete requests:      250000
Failed requests:        0
Write errors:           0
Total transferred:      212000000 bytes
HTML transferred:       153750000 bytes
Requests per second:    8176.76 [#/sec] (mean)
Time per request:       122.298 [ms] (mean)
Time per request:       0.122 [ms] (mean, across all concurrent requests)
Transfer rate:          6771.38 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0   83 543.2     13   15048
Processing:     0   36  86.0     18    5601
Waiting:        0   33  84.8     15    5600
Total:          1  119 551.6     34   15096Percentage of the requests served within a certain time (ms)50%     3466%     4275%     4880%     5290%     7595%    27398%   104299%   3027100%  15096 (longest request)
[root@k8s-master pod]# kubectl get hpa -n myns -w   #随着cpu使用率降低,pod副本期望也回归初始值
NAME           REFERENCE                 TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx-deploy   Deployment/nginx-deploy   99%/15%   1         20        2          68s
nginx-deploy   Deployment/nginx-deploy   68%/15%   1         20        4          75s
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        7          90s
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        7          105s
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        7          2m
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        7          2m15s
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        7          7m
nginx-deploy   Deployment/nginx-deploy   0%/15%    1         20        1          7m15s

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

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

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

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


https://www.xjx100.cn/news/3118784.html

相关文章

ubuntu下训练自己的yolov5数据集

参考文档 yolov5-github yolov5-github-训练文档 csdn训练博客 一、配置环境 1.1 安装依赖包 前往清华源官方地址 选择适合自己的版本替换自己的源 # 备份源文件 sudo cp /etc/apt/sources.list /etc/apt/sources.list_bak # 修改源文件 # 更新 sudo apt update &&a…

企业微信应用文本消息

应用支持推送文本、图片、视频、文件、图文等类型&#xff0c;本篇主要实现发送应用文本消息。 获取企业凭证 发送应用消息首先需要获取调用凭证access_token&#xff0c;此处的凭证为企业凭证&#xff0c;可通过企业授权安装时返回的授权信息中获取access_token&#xff1b;之…

PlantUML语法(全)及使用教程-用例图

目录 1. 用例图1.1、什么是用例图1.2、用例图的构成1.3、参与者1.4、用例1.4.1、用例基本概念1.4.2、用例的识别1.4.3、用例的要点1.4.3、用例的命名1.4.4、用例的粒度 1.5、应用示例1.5.1、用例1.5.2、角色1.5.3、改变角色的样式1.5.4、用例描述1.5.5、改变箭头方向1.5.6、使用…

OpenCV4.x图像处理实例-常见图像滤镜特效实现

常见20多种图像滤镜特效实现 文章目录 常见20多种图像滤镜特效实现0、准备1、亮度调节2、细节强化3、底片效果4、卡通效果5、浮雕效果6、铅笔素描效果7、夏季或温色滤镜8、冬季或冷色滤波9、Splash滤镜10、双色调(Duo-Tone)滤镜11、日光(Daylight)滤镜12、60sTVs效果13、高…

记录一次现网问题排查(分享查域名是否封禁小程序)

背景&#xff1a; 收到工单反馈说现网业务一个功能有异常&#xff0c;具体现象是tc.hb.cn域名无法访问&#xff0c;客户地区是河南省&#xff0c;这里记录下排查过程和思路。 首先梳理链路 客户端域名 tc.hb.cn cname—> domainparking-dnspod.cn(新加坡clb)—> snat—&…

【知识】稀疏矩阵是否比密集矩阵更高效?

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhang.cn] 问题提出 有些地方说&#xff0c;稀疏图比密集图的计算效率更高&#xff0c;真的吗&#xff1f; 原因猜想 这里的效率高&#xff0c;应该是有前提的&#xff1a;当使用稀疏矩阵的存储格式(如CSR)时&#xff0c;计…

国内首个农业开源鸿蒙操作系统联合华为正式发布

2023年11月29日&#xff0c;在中国国际供应链促进博览会上&#xff0c;中信农业科技股份有限公司&#xff08;简称“中信农业”&#xff09;与深圳开鸿数字产业发展有限公司&#xff08;简称“深开鸿”&#xff09;以及华为技术有限公司&#xff08;简称“华为”&#xff09;联…

DeepStream系列之rtmpsink功能,rtsp转rtmp,opencv读取rtsp图像处理后推流rtmp

了解到一个更好的流媒体开源项目&#xff0c;是中国人写的&#xff0c;项目地址 https://github.com/ossrs/srs&#xff0c;有兴趣的可以尝试下&#xff0c;实时性更快 DeepStream系列之rtmpsink功能 实时性没要求&#xff0c;可以用下面的opencv opencv读取rtsp图像处理后推流…