Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

文章目录

1、基本概念与控制器特点


在前面的课程中,我们可以通过手工执行kubectl scale命令实现Pod扩容,但是这个显然不符合Kubernetes的定位目标 - 自动化、智能化;因此,Kubernetes期望可以通过检测Pod的使用情况实现Pod数量的自动调整,于是就产生了HPA这种控制器;
HPA可以获取每个Pod的利用率,然后和HAP中定义的指标进行对比,同时计算出需要伸缩的具体值,最后实现Pod数量的调整,HPA与之前的Deployment相同,都属于Kubernetes的资源对象,它通过追踪分析目标Pod的负载变化情况,来确定是否需要针对性的调整Pod数量;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

2、安装Metric-Server


metric-server可以用来手机集群汇总的资源使用情况;可以通过GitHub下载最新版本的YAML文件,用来安装metrics-server;GitHub获取安装包

#直接在Linux下载metrics-server相关YAML文件
[root@master ~]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

#下载完成后修改此文件如下内容
[root@master ~]# vim components.yaml
~
#切记需要修改的位置是Deployment的template中的spec配置内容(请按照要求修改,否则会产生报错!)
   129	    spec:
   130	      hostNetwork: true
   131	      containers:
   132	      - args:
   133	        - --cert-dir=/tmp
   134	        - --secure-port=4443
   135	        - --kubelet-insecure-tls
   136	        - --kubelet-preferred-address-types=InternalIP,Hostname,InternalDNS,ExternalDNS,ExternalIP
   137	        #- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
   138	        #- --kubelet-use-node-status-port
   139	        #- --metric-resolution=15s
   140	        image: registry.cn-hangzhou.aliyuncs.com/google_containers/metrics-server:v0.5.2

#修改完成后调用YAML文件
[root@master ~]# 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

#查看metrics-server的Pod状态
[root@master ~]# kubectl get pod -n kube-system | grep metrics-server
metrics-server-5dbd4664d9-twm2k           1/1     Running   0               3m41s

#查看Node资源使用情况
[root@master ~]# kubectl top node
NAME         CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
master.k8s   104m         1%     2051Mi          26%       
node1.k8s    35m          0%     1098Mi          14%       
node2.k8s    33m          0%     994Mi           12% 

#查看Pod资源使用情况(需要存在Pod方可查看)
[root@master ~]# kubectl top pod
NAME                     CPU(cores)   MEMORY(bytes)   
nginx-7cbb8cd5d8-j4m8l   0m           1Mi             
# CPU回显参数说明1核为1000m

2、部署Deployment控制器和Service


#创建Deployment控制器YAML文件
[root@master ~]# vim pc-hpa-deploy.yaml 
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: pc-deployment
  namespace: dev
spec:
  replicas: 1                   #副本数量
  selector: 
    matchLabels: 
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        resources:
          requests:
            cpu: "20m"
#调用Deployment控制器YAML文件
[root@master ~]# kubectl apply -f pc-hpa-deploy.yaml 
deployment.apps/pc-deployment created

#创建Service
[root@master ~]# kubectl expose deploy pc-deployment --type=NodePort --port=80 -n dev
service/pc-deployment exposed

#查看Deploy、Pod、Service
[root@master ~]# kubectl get deploy,pod,svc -n dev
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/pc-deployment   1/1     1            1           68s

NAME                                READY   STATUS    RESTARTS   AGE
pod/pc-deployment-7d4dcb85c-fmp7n   1/1     Running   0          68s

NAME                    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/pc-deployment   NodePort   10.108.169.162   <none>        80:30925/TCP   31s

3、部署HPA控制器


#创建HPA控制器YAML文件
[root@master ~]# vim pc-hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: pc-hpa
  namespace: dev
spec:
  minReplicas: 1                  #最小副本数量
  maxReplicas: 10                 #最大副本数量
  targetCPUUtilizationPercentage: 3       #CPU使用率指标
  scaleTargetRef:                 #指定需要控制的Deployment信息
    apiVersion: apps/v1
    kind: Deployment
    name: pc-deployment
#调用HPA控制器YAML文件
[root@master ~]# kubectl apply -f pc-hpa.yaml 
horizontalpodautoscaler.autoscaling/pc-hpa created

#查看HPA控制器
[root@master ~]# kubectl get hpa -n dev
NAME     REFERENCE                  TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
pc-hpa   Deployment/pc-deployment   0%/3%     1         10        1          2m15s

4、测试HPA功能


使用Postman工具检测http服务是否可以正常访问;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

4.1 使用软件下发多个http请求

点击[save],保存请求;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

创建保存文件;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

命名保存文件;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

点击保存;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

查看保存http请求是否正确;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

点击[Runner];
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

选择对应的http请求,并修改请求次数(适量即可,不要把自己系统搞崩了)
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

查看http请求的下发进度;
Docker&Kubernetes ❀ Kubernetes集群Pod控制器 - Horizontal Pod Autoscaler(HPA)

4.2 查看HPA、Deployment、Pod监控过程

#监控HPA控制器过程
[root@master ~]# kubectl get hpa -n dev -w
NAME     REFERENCE                  TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
pc-hpa   Deployment/pc-deployment   0%/3%     1         10        1          35s
pc-hpa   Deployment/pc-deployment   10%/3%    1         10        1          2m1s
pc-hpa   Deployment/pc-deployment   10%/3%    1         10        4          2m17s
pc-hpa   Deployment/pc-deployment   75%/3%    1         10        4          3m4s
pc-hpa   Deployment/pc-deployment   75%/3%    1         10        8          3m19s
pc-hpa   Deployment/pc-deployment   75%/3%    1         10        10         3m34s
pc-hpa   Deployment/pc-deployment   20%/3%    1         10        10         4m5s
pc-hpa   Deployment/pc-deployment   0%/3%     1         10        10         5m5s

#监控Deploy控制器过程
[root@master ~]# kubectl get deploy -n dev -w
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
pc-deployment   1/1     1            1           4m34s
pc-deployment   1/4     1            1           5m31s
pc-deployment   1/4     1            1           5m31s
pc-deployment   1/4     1            1           5m31s
pc-deployment   1/4     4            1           5m31s
pc-deployment   2/4     4            2           5m51s
pc-deployment   3/4     4            3           6m19s
pc-deployment   4/4     4            4           6m19s
pc-deployment   4/8     4            4           6m34s
pc-deployment   4/8     4            4           6m34s
pc-deployment   4/8     4            4           6m34s
pc-deployment   4/8     8            4           6m35s
pc-deployment   5/8     8            5           6m48s
pc-deployment   6/8     8            6           6m49s
pc-deployment   6/10    8            6           6m49s
pc-deployment   6/10    8            6           6m49s
pc-deployment   6/10    8            6           6m49s
pc-deployment   6/10    10           6           6m49s
pc-deployment   7/10    10           7           6m53s
pc-deployment   8/10    10           8           7m15s
pc-deployment   9/10    10           9           7m15s
pc-deployment   10/10   10           10          7m16s
#Deployment控制器最多创建10个Pod

#监控Pod创建过程
[root@master ~]# kubectl get pod -n dev -w
NAME                            READY   STATUS    RESTARTS   AGE
pc-deployment-7d4dcb85c-fmp7n   1/1     Running   0          4m15s
pc-deployment-7d4dcb85c-lqlrc   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-blwt2   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-2tpr8   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-lqlrc   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-blwt2   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-2tpr8   0/1     Pending   0          0s
pc-deployment-7d4dcb85c-lqlrc   0/1     ContainerCreating   0          0s
pc-deployment-7d4dcb85c-blwt2   0/1     ContainerCreating   0          0s
pc-deployment-7d4dcb85c-2tpr8   0/1     ContainerCreating   0          0s
pc-deployment-7d4dcb85c-blwt2   0/1     ContainerCreating   0          19s
pc-deployment-7d4dcb85c-blwt2   1/1     Running             0          20s
pc-deployment-7d4dcb85c-2tpr8   0/1     ContainerCreating   0          48s
pc-deployment-7d4dcb85c-lqlrc   0/1     ContainerCreating   0          48s
pc-deployment-7d4dcb85c-lqlrc   1/1     Running             0          48s
pc-deployment-7d4dcb85c-2tpr8   1/1     Running             0          48s
pc-deployment-7d4dcb85c-r2fpw   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-bj7k4   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-lzbfp   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-svgs5   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-bj7k4   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-svgs5   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-r2fpw   0/1     Pending             0          1s
pc-deployment-7d4dcb85c-lzbfp   0/1     Pending             0          1s
pc-deployment-7d4dcb85c-bj7k4   0/1     ContainerCreating   0          1s
pc-deployment-7d4dcb85c-svgs5   0/1     ContainerCreating   0          1s
pc-deployment-7d4dcb85c-r2fpw   0/1     ContainerCreating   0          1s
pc-deployment-7d4dcb85c-lzbfp   0/1     ContainerCreating   0          1s
pc-deployment-7d4dcb85c-svgs5   0/1     ContainerCreating   0          13s
pc-deployment-7d4dcb85c-bj7k4   0/1     ContainerCreating   0          14s
pc-deployment-7d4dcb85c-svgs5   1/1     Running             0          14s
pc-deployment-7d4dcb85c-bj7k4   1/1     Running             0          15s
pc-deployment-7d4dcb85c-n7b6w   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-lb6xm   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-n7b6w   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-lb6xm   0/1     Pending             0          0s
pc-deployment-7d4dcb85c-lb6xm   0/1     ContainerCreating   0          0s
pc-deployment-7d4dcb85c-n7b6w   0/1     ContainerCreating   0          1s
pc-deployment-7d4dcb85c-lb6xm   0/1     ContainerCreating   0          3s
pc-deployment-7d4dcb85c-lb6xm   1/1     Running             0          4s
pc-deployment-7d4dcb85c-lzbfp   0/1     ContainerCreating   0          40s
pc-deployment-7d4dcb85c-r2fpw   0/1     ContainerCreating   0          40s
pc-deployment-7d4dcb85c-lzbfp   1/1     Running             0          41s
pc-deployment-7d4dcb85c-n7b6w   0/1     ContainerCreating   0          26s
pc-deployment-7d4dcb85c-r2fpw   1/1     Running             0          41s
pc-deployment-7d4dcb85c-n7b6w   1/1     Running             0          27s
~
#由于内容较多,不再展示
上一篇:P2859 [USACO06FEB]Stall Reservations S


下一篇:vue移动端和手机端两端(vue-判断设备是手机端还是pc端)