• Pod控制器详解-Horizontal Pod Autoscaler(HPA)


    Pod控制器介绍

    Pod是kubernetes的最小管理单元,在kubernetes中,按照pod的创建方式可以将其分为两类:

    • 自主式pod:kubernetes直接创建出来的Pod,这种pod删除后就没有了,也不会重建
    • 控制器创建的pod:kubernetes通过控制器创建的pod,这种pod删除了之后还会自动重建

    什么是Pod控制器?
    Pod控制器是管理pod的中间层,使用Pod控制器之后,只需要告诉Pod控制器,想要多少个什么样的Pod就可以了,它会创建出满足条件的Pod并确保每一个Pod资源处于用户期望的目标状态。如果Pod资源在运行中出现故障,它会基于指定策略重新编排Pod。

    在kubernetes中,有很多类型的pod控制器,每种都有自己的适合的场景,常见的有下面这些:

    ReplicationController:比较原始的pod控制器,已经被废弃,由ReplicaSet替代
    ReplicaSet:保证副本数量一直维持在期望值,并支持pod数量扩缩容,镜像版本升级
    Deployment:通过控制ReplicaSet来控制Pod,并支持滚动升级、回退版本
    Horizontal Pod Autoscaler:可以根据集群负载自动水平调整Pod的数量,实现削峰填谷
    DaemonSet:在集群中的指定Node上运行且仅运行一个副本,一般用于守护进程类的任务
    Job:它创建出来的pod只要完成任务就立即退出,不需要重启或重建,用于执行一次性任务
    Cronjob:它创建的Pod负责周期性任务控制,不需要持续后台运行
    StatefulSet:管理有状态应用

    Horizontal Pod Autoscaler(HPA)

    在前面的课程中,我们已经可以实现通过手工执行kubectl scale命令实现Pod扩容或缩容,但是这显然不符合Kubernetes的定位目标–自动化、智能化。 Kubernetes期望可以实现通过监测Pod的使用情况,实现pod数量的自动调整,于是就产生了Horizontal Pod Autoscaler(HPA)这种控制器。

    HPA可以获取每个Pod利用率,然后和HPA中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod的数量的调整。其实HPA与之前的Deployment一样,也属于一种Kubernetes资源对象,它通过追踪分析RC控制的所有目标Pod的负载变化情况,来确定是否需要针对性地调整目标Pod的副本数,这是HPA的实现原理。

    在这里插入图片描述
    接下来,我们来做一个实验

    安装metrics-server

    metrics-server可以用来收集集群中的资源使用情况

    # 安装git
    [root@k8s-master01 ~]# yum install git -y
    # 获取metrics-server, 注意使用的版本
    [root@k8s-master01 ~]# git clone -b v0.3.6 https://github.com/kubernetes-incubator/metrics-server
    # 修改deployment, 注意修改的是镜像和初始化参数
    [root@k8s-master01 ~]# cd /root/metrics-server/deploy/1.8+/
    [root@k8s-master01 1.8+]# vim metrics-server-deployment.yaml
    按图中添加下面选项
    hostNetwork: true
    image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server-amd64:v0.3.6
    args:
    - --kubelet-insecure-tls
    - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    # 安装metrics-server
    [root@k8s-master01 1.8+]# kubectl apply -f ./
    
    # 查看pod运行情况
    [root@k8s-master01 1.8+]# kubectl get pod -n kube-system
    metrics-server-6b976979db-2xwbj   1/1     Running   0          90s
    
    # 使用kubectl top node 查看资源使用情况
    [root@k8s-master01 1.8+]# kubectl top node
    NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
    k8s-master01   289m         14%    1582Mi          54%       
    k8s-node01     81m          4%     1195Mi          40%       
    k8s-node02     72m          3%     1211Mi          41%  
    [root@k8s-master01 1.8+]# kubectl top pod -n kube-system
    NAME                              CPU(cores)   MEMORY(bytes)
    coredns-6955765f44-7ptsb          3m           9Mi
    coredns-6955765f44-vcwr5          3m           8Mi
    etcd-master                       14m          145Mi
    ...
    # 至此,metrics-server安装完成
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    准备deployment和servie

    创建pc-hpa-pod.yaml文件,内容如下:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
      namespace: dev
    spec:
      strategy: # 策略
        type: RollingUpdate # 滚动更新策略
      replicas: 1
      selector:
        matchLabels:
          app: nginx-pod
      template:
        metadata:
          labels:
            app: nginx-pod
        spec:
          containers:
          - name: nginx
            image: nginx:1.17.1
            resources: # 资源配额
              limits:  # 限制资源(上限)
                cpu: "1" # CPU限制,单位是core数
              requests: # 请求资源(下限)
                cpu: "100m"  # CPU限制,单位是core数
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    # 创建deployment
    [root@k8s-master01 1.8+]# kubectl run nginx --image=nginx:1.17.1 --requests=cpu=100m -n dev
    # 创建service
    [root@k8s-master01 1.8+]# kubectl expose deployment nginx --type=NodePort --port=80 -n dev
    
    • 1
    • 2
    • 3
    • 4
    # 查看
    [root@k8s-master01 1.8+]# kubectl get deployment,pod,svc -n dev
    NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/nginx   1/1     1            1           47s
    
    NAME                         READY   STATUS    RESTARTS   AGE
    pod/nginx-7df9756ccc-bh8dr   1/1     Running   0          47s
    
    NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
    service/nginx   NodePort   10.101.18.29   <none>        80:31830/TCP   35s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    部署HPA

    创建pc-hpa.yaml文件,内容如下:

    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: pc-hpa
      namespace: dev
    spec:
      minReplicas: 1  #最小pod数量
      maxReplicas: 10 #最大pod数量
      targetCPUUtilizationPercentage: 3 # CPU使用率指标
      scaleTargetRef:   # 指定要控制的nginx信息
        apiVersion:  /v1
        kind: Deployment
        name: nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    # 创建hpa
    [root@k8s-master01 1.8+]# kubectl create -f pc-hpa.yaml
    horizontalpodautoscaler.autoscaling/pc-hpa created
    
    # 查看hpa
        [root@k8s-master01 1.8+]# kubectl get hpa -n dev
    NAME     REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    pc-hpa   Deployment/nginx   0%/3%     1         10        1          62s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    测试

    使用压测工具对service地址192.168.5.4:31830进行压测,然后通过控制台查看hpa和pod的变化
    hpa变化

    [root@k8s-master01 ~]# kubectl get hpa -n dev -w
    NAME   REFERENCE      TARGETS  MINPODS  MAXPODS  REPLICAS  AGE
    pc-hpa  Deployment/nginx  0%/3%   1     10     1      4m11s
    pc-hpa  Deployment/nginx  0%/3%   1     10     1      5m19s
    pc-hpa  Deployment/nginx  22%/3%   1     10     1      6m50s
    pc-hpa  Deployment/nginx  22%/3%   1     10     4      7m5s
    pc-hpa  Deployment/nginx  22%/3%   1     10     8      7m21s
    pc-hpa  Deployment/nginx  6%/3%   1     10     8      7m51s
    pc-hpa  Deployment/nginx  0%/3%   1     10     8      9m6s
    pc-hpa  Deployment/nginx  0%/3%   1     10     8      13m
    pc-hpa  Deployment/nginx  0%/3%   1     10     1      14m
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    deployment变化

    [root@k8s-master01 ~]# kubectl get deployment -n dev -w
    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    nginx   1/1     1            1           11m
    nginx   1/4     1            1           13m
    nginx   1/4     1            1           13m
    nginx   1/4     1            1           13m
    nginx   1/4     4            1           13m
    nginx   1/8     4            1           14m
    nginx   1/8     4            1           14m
    nginx   1/8     4            1           14m
    nginx   1/8     8            1           14m
    nginx   2/8     8            2           14m
    nginx   3/8     8            3           14m
    nginx   4/8     8            4           14m
    nginx   5/8     8            5           14m
    nginx   6/8     8            6           14m
    nginx   7/8     8            7           14m
    nginx   8/8     8            8           15m
    nginx   8/1     8            8           20m
    nginx   8/1     8            8           20m
    nginx   1/1     1            1           20m
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    pod变化

    [root@k8s-master01 ~]# kubectl get pods -n dev -w
    NAME                     READY   STATUS    RESTARTS   AGE
    nginx-7df9756ccc-bh8dr   1/1     Running   0          11m
    nginx-7df9756ccc-cpgrv   0/1     Pending   0          0s
    nginx-7df9756ccc-8zhwk   0/1     Pending   0          0s
    nginx-7df9756ccc-rr9bn   0/1     Pending   0          0s
    nginx-7df9756ccc-cpgrv   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-8zhwk   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-rr9bn   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-m9gsj   0/1     Pending             0          0s
    nginx-7df9756ccc-g56qb   0/1     Pending             0          0s
    nginx-7df9756ccc-sl9c6   0/1     Pending             0          0s
    nginx-7df9756ccc-fgst7   0/1     Pending             0          0s
    nginx-7df9756ccc-g56qb   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-m9gsj   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-sl9c6   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-fgst7   0/1     ContainerCreating   0          0s
    nginx-7df9756ccc-8zhwk   1/1     Running             0          19s
    nginx-7df9756ccc-rr9bn   1/1     Running             0          30s
    nginx-7df9756ccc-m9gsj   1/1     Running             0          21s
    nginx-7df9756ccc-cpgrv   1/1     Running             0          47s
    nginx-7df9756ccc-sl9c6   1/1     Running             0          33s
    nginx-7df9756ccc-g56qb   1/1     Running             0          48s
    nginx-7df9756ccc-fgst7   1/1     Running             0          66s
    nginx-7df9756ccc-fgst7   1/1     Terminating         0          6m50s
    nginx-7df9756ccc-8zhwk   1/1     Terminating         0          7m5s
    nginx-7df9756ccc-cpgrv   1/1     Terminating         0          7m5s
    nginx-7df9756ccc-g56qb   1/1     Terminating         0          6m50s
    nginx-7df9756ccc-rr9bn   1/1     Terminating         0          7m5s
    nginx-7df9756ccc-m9gsj   1/1     Terminating         0          6m50s
    nginx-7df9756ccc-sl9c6   1/1     Terminating         0          6m50s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    本文摘抄或总结其他笔记,笔记不涉及任何商业用途,如果侵权请及时联系处理

    参考:

    k8s官方文档
    yooome / LearningNotes

  • 相关阅读:
    Linux 内存和SWAP使用
    叶酸&适配体修饰DNA纳米载体|CdS纳米颗粒修饰DNA|科研试剂
    kubernetes集群kubeadm方式安装
    MySQL与postgreSQL数据库的区别
    【东软实训Day04】Java UDP通信
    Wireshark过滤器的使用
    【期末大作业】基于HTML+CSS+JavaScript网上订餐系统(23个页面)
    05 - 雷达的发展与应用
    C++多态案例2----制作饮品
    [正则表达式]php
  • 原文地址:https://blog.csdn.net/qq_43193386/article/details/125605396