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

目录

一.Pod控制器简介

二.ReplicaSet(简写rs)

1.简介

(1)主要功能

(2)rs较完整参数解释

2.创建和删除

(1)创建

(2)删除

3.扩容和缩容

(1)scale方式

(2)edit方式

4.镜像版本更改

[(1)set image方式](#(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参数](#(2)kubectl rollout参数)

(3)案例演示

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

[四.Horizontal Pod Autoscaler(简写hpa)](#四.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: 3
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: my-nginx
    spec:
      containers:
      - image: nginx:1.17.1
        imagePullPolicy: IfNotPresent
        name: nginx-container
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        #指定容器终止时日志消息的路径。在容器终止时,Kubernetes 将将容器的终止消息写入指定路径的文件中
        terminationMessagePolicy: File
        #定义容器终止消息的策略。File 表示将终止消息写入到文件中,其他选项还包括 FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
      dnsPolicy: ClusterFirst
      #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
      restartPolicy: Always
      schedulerName: 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-rs
  namespace: myns
spec:
  replicas: 3
  selector:   #选择器部分,使用matchLabels机制来定义label,定义选择器可以表明这个控制器能够管理哪些pod
    matchLabels:
      app: my-nginx
  template:   #副本数量不足时,根据模版创建pod副本
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: nginx-container
        image: nginx
        ports:
        - 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保存退出后直接查看即可。

[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方式

[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.1
    Image 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.2
    Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
[root@k8s-master pod]# kubectl describe rs my-rs -n myns | grep Image
    Image:        nginx:1.17.2
​

三.Deployment(简写deploy)

1.简介

(1)主要功能

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

(2)deploy较完整参数解释

截取edit deploy中的spec部分

spec:
  progressDeadlineSeconds: 600  #部署超时时间,默认600s
  replicas: 3
  revisionHistoryLimit: 10   #保留历史修订版本数
  selector:
    matchLabels:
      app: my-nginx
  strategy:   #策略
    rollingUpdate:     #滚动更新
      maxSurge: 25%   #最大额外可拥有的pod副本数量,整数/百分比
      maxUnavailable: 25%   #最大不可用pod的最大值,整数/百分比
    type: RollingUpdate   #镜像更新方式,支持RollingUpdate和Recreate,后面会详细介绍
  template:    #模版
    metadata:
      creationTimestamp: null
      labels:
        app: my-nginx
    spec:
      containers:
      - image: nginx
        imagePullPolicy: Always
        name: nginx-pod
        resources: {}
        terminationMessagePath: /dev/termination-log    #容器终止时日志消息路径位置
        terminationMessagePolicy: File   
        #容器终止消息策略,File表示将终止消息写入到文件中,其他选项还包括FallbackToLogsOnError(在无法写入文件时回退到容器日志)和 FallbackToLogsOnFailure(在终止消息为空时回退到容器日志)
      dnsPolicy: ClusterFirst
      #定义 Pod 中容器的 DNS 解析策略。ClusterFirst 表示将使用集群内部的 DNS 解析服务来解析主机名
      restartPolicy: Always
      schedulerName: 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-nginx
  namespace: myns
  labels:
    name: my-de
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels: 
        app: my-nginx
    spec:
      containers:
      - name: nginx-pod
        image: 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-nginx
  namespace: myns
  labels:
    name: my-de
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx
  strategy:
    type: Recreate   #指定为Recreate方式
  template:
    metadata:
      labels: 
        app: my-nginx
    spec:
      containers:
      - name: nginx-pod
        image: 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 Image
    Image:          nginx:1.17.1
    Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Image:          nginx:1.17.1
    Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Image:          nginx:1.17.1
    Image 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-nginx
  namespace: myns
  labels:
    name: my-de
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-nginx
  strategy:
    type: RollingUpdate   #指定滚动更新策略
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  template:
    metadata:
      labels: 
        app: my-nginx
    spec:
      containers:
      - name: nginx-pod
        image: 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:          nginx
    Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
    Image:          nginx
    Image 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 Image
    Image:          nginx:1.17.1
    Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Image:          nginx:1.17.1
    Image ID:       docker.io/library/nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
    Image:          nginx:1.17.1
    Image 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 Image
    Image:        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.2
    Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:10d1f5b58f74683ad34eb29287e07dab1e90f10af243f151bb50aa5dbb4d62ee
    Image:          nginx
    Image 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.2
    Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
    Image:          nginx:1.17.2
    Image ID:       docker.io/library/nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
    Image:          nginx:1.17.2
    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"

(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: null
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: myns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-deploy
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-deploy
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
        - containerPort: 80
        resources:
          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: null
  name: nginx-deploy
  namespace: myns
spec:
  maxReplicas: 20
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deploy
  targetCPUUtilizationPercentage: 15   #这里指定的是期望的cpu使用率,超过这个值就会自动扩容
status:
  currentReplicas: 0
  desiredReplicas: 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 requests


Server Software:        nginx/1.25.3
Server Hostname:        10.108.107.216
Server Port:            80

Document Path:          /
Document Length:        615 bytes

Concurrency 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] received

Connection 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   15096

Percentage of the requests served within a certain time (ms)
  50%     34
  66%     42
  75%     48
  80%     52
  90%     75
  95%    273
  98%   1042
  99%   3027
 100%  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
相关推荐
魏 无羡6 小时前
linux CentOS系统上卸载docker
linux·kubernetes·centos
Karoku0666 小时前
【k8s集群应用】kubeadm1.20高可用部署(3master)
运维·docker·云原生·容器·kubernetes
凌虚8 小时前
Kubernetes APF(API 优先级和公平调度)简介
后端·程序员·kubernetes
探索云原生11 小时前
在 K8S 中创建 Pod 是如何使用到 GPU 的: nvidia device plugin 源码分析
ai·云原生·kubernetes·go·gpu
启明真纳11 小时前
elasticache备份
运维·elasticsearch·云原生·kubernetes
jwolf213 小时前
基于K8S的微服务:一、服务发现,负载均衡测试(附calico网络问题解决)
微服务·kubernetes·服务发现
nangonghen14 小时前
在华为云通过operator部署Doris v2.1集群
kubernetes·华为云·doris·operator
会飞的土拨鼠呀15 小时前
chart文件结构
运维·云原生·kubernetes
自在的LEE18 小时前
当 Go 遇上 Windows:15.625ms 的时间更新困局
后端·kubernetes·go
云川之下1 天前
【k8s】访问etcd
kubernetes·etcd