Kubernetes Pod Horizontal Autoscaling (HPA)

Pod auto-scaling

I mentioned before that the expansion and contraction can be achieved by manually executing kubectl scalecommands and Dashboardoperating on the Internet, but after all, it needs to be manually operated once each time, and it is uncertain when the business request volume will be large, so if it cannot be automated PodIt is also a very troublesome thing to expand and shrink. It would be great if Kubernetesthe system could Podautomatically scale up and down according to the current load changes, because this process is not fixed and happens frequently, so the manual method is not very realistic.

Fortunately, Kuberneteswe have provided us with such a resource object: Horizontal Pod Autoscaling(Pod horizontal auto-scaling), abbreviation HPA. It is the most basic principle to determine whether the number of replicas needs to be adjusted HAPby monitoring, analyzing RCor Deploymentcontrolling all Podload changes .PodHPA
insert image description here

HPAkubernetesIt is designed as one in the cluster , controllerwe can simply kubectl autoscalecreate a HPAresource object through the command, HPA Controllerthe default polling time is 30s (can be set by kube-controller-managerthe flag --horizontal-pod-autoscaler-sync-period), query the resource usage in the specified resource (RC or Deployment) Pod, and Compared with the value and index set at the time of creation, the function of automatic scaling can be realized.

After you create it HPA, you will get the average value of each utilization rate or original value HPAfrom Heapsterthe user-defined terminal, and then compare it with the indicators defined in it, and calculate the specific value that needs to be scaled and perform corresponding operations. Currently, data can be obtained from two places:RESTClientPodHPAHPA

  • Heapster: only supports CPUusage
  • Custom monitoring: We will explain how to use this part in the following monitoring article

Now let’s introduce Heapsterthe method of automatic expansion and contraction from the acquisition of monitoring data, so first we have to install it Heapster. In the previous kubeadmarticle on building a cluster, we have actually Heapsterpulled the relevant images to the nodes by default, so Next, we only need to deploy. We are using Heapsterversion 1.4.2 here. Go Heapsterto githubthe page:

https://github.com/kubernetes/heapster

We yamlsave the files under this directory to our cluster, and then use kubectlthe command line tool to create them. In addition, after the creation is completed, if we need to Dashboardsee the monitoring chart in it, we also need to Dashboardconfigure our in it heapster-host.

Similarly, let's create a Deploymentmanaged NginxPod and use it HPAfor automatic scaling. DeploymentThe defined YAMLfile is as follows: (hap-deploy-demo.yaml)

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hpa-nginx-deploy
  labels:
    app: nginx-demo
spec:
  revisionHistoryLimit: 15
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Then create Deployment:

$ kubectl create -f hpa-deploy-demo.yaml

Now let's create one HPA, which can be kubectl autoscalecreated with the command:

$ kubectl autoscale deployment hpa-nginx-deploy --cpu-percent=10 --min=1 --max=10
deployment "hpa-nginx-deploy" autoscaled
···
$ kubectl get hpa                                                         
NAME        REFERENCE              TARGET    CURRENT   MINPODS   MAXPODS   AGE
hpa-nginx-deploy   Deployment/hpa-nginx-deploy   10%       0%        1         10        13s

This command creates an associated resource hpa-nginx-deploy HPA, the minimum number of pod replicas is 1, and the maximum is 10. HPAThe number of pods will be dynamically increased or decreased according to the set cpu usage (10%).

Of course, in addition to using kubectl autoscalecommands to create, we can still YAMLcreate HPAresource objects in the form of creating files. If we don't know how to write it, we can check the file created by the above command HPAline YAML:

$ kubectl get hpa hpa-nginx-deploy -o yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: 2017-06-29T08:04:08Z
  name: nginxtest
  namespace: default
  resourceVersion: "951016361"
  selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/nginxtest
  uid: 86febb63-5ca1-11e7-aaef-5254004e79a3
spec:
  maxReplicas: 5 //资源最大副本数
  minReplicas: 1 //资源最小副本数
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment //需要伸缩的资源类型
    name: nginxtest  //需要伸缩的资源名称
  targetCPUUtilizationPercentage: 50 //触发伸缩的cpu使用率
status:
  currentCPUUtilizationPercentage: 48 //当前资源下pod的cpu使用率
  currentReplicas: 1 //当前的副本数
  desiredReplicas: 2 //期望的副本数
  lastScaleTime: 2017-07-03T06:32:19Z

YAMLOk, now we can create a description file based YAMLon the above file HPA.

Now let's increase the load to test, let's create one busybox, and iterate through the services created above.

$ kubectl run -i --tty load-generator --image=busybox /bin/sh
If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://172.16.255.60:4000; done

As you can see in the figure below, HPA has started to work.

$ kubectl get hpa
NAME        REFERENCE              TARGET    CURRENT   MINPODS   MAXPODS   AGE
hpa-nginx-deploy   Deployment/hpa-nginx-deploy   10%       29%        1         10        27m

At the same time, we check the number of copies of the related resource hpa-nginx-deploy, and the number of copies has changed from 1 to 3.

$ kubectl get deployment hpa-nginx-deploy
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hpa-nginx-deploy   3         3         3            3           4d

At the same time, check again HPA, due to the increase in the number of copies, the utilization rate has also remained at about 10%.

$ kubectl get hpa
NAME        REFERENCE              TARGET    CURRENT   MINPODS   MAXPODS   AGE
hpa-nginx-deploy   Deployment/hpa-nginx-deploy   10%       9%        1         10        35m

At the same time, let's turn it off busyboxto reduce the load, and then wait for a while to observe HPAand Deploymentobject

$ kubectl get hpa     
NAME        REFERENCE              TARGET    CURRENT   MINPODS   MAXPODS   AGE
hpa-nginx-deploy   Deployment/hpa-nginx-deploy   10%       0%        1         10        48m
$ kubectl get deployment hpa-nginx-deploy
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hpa-nginx-deploy   1         1         1            1           4d

You can see that the number of copies has changed from 3 to 1.

However, the current indicator HPAis only the utilization rate, which is not very flexible. In the following articles, we will automatically expand and shrink the capacity CPUaccording to our custom monitoring .Pod


おすすめ

転載: blog.csdn.net/u010674953/article/details/129355707