Pod auto-scaling
I mentioned before that the expansion and contraction can be achieved by manually executing kubectl scale
commands and Dashboard
operating 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 Pod
It is also a very troublesome thing to expand and shrink. It would be great if Kubernetes
the system could Pod
automatically 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, Kubernetes
we 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 HAP
by monitoring, analyzing RC
or Deployment
controlling all Pod
load changes .Pod
HPA
HPA
kubernetes
It is designed as one in the cluster , controller
we can simply kubectl autoscale
create a HPA
resource object through the command, HPA Controller
the default polling time is 30s (can be set by kube-controller-manager
the 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 HPA
from Heapster
the 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:RESTClient
Pod
HPA
HPA
- Heapster: only supports
CPU
usage - Custom monitoring: We will explain how to use this part in the following monitoring article
Now let’s introduce Heapster
the method of automatic expansion and contraction from the acquisition of monitoring data, so first we have to install it Heapster
. In the previous kubeadm
article on building a cluster, we have actually Heapster
pulled the relevant images to the nodes by default, so Next, we only need to deploy. We are using Heapster
version 1.4.2 here. Go Heapster
to github
the page:
https://github.com/kubernetes/heapster
We yaml
save the files under this directory to our cluster, and then use kubectl
the command line tool to create them. In addition, after the creation is completed, if we need to Dashboard
see the monitoring chart in it, we also need to Dashboard
configure our in it heapster-host
.
Similarly, let's create a Deployment
managed Nginx
Pod and use it HPA
for automatic scaling. Deployment
The defined YAML
file 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 autoscale
created 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. HPA
The number of pods will be dynamically increased or decreased according to the set cpu usage (10%).
Of course, in addition to using kubectl autoscale
commands to create, we can still YAML
create HPA
resource 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 HPA
line 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
YAML
Ok, now we can create a description file based YAML
on 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 busybox
to reduce the load, and then wait for a while to observe HPA
and Deployment
object
$ 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 HPA
is only the utilization rate, which is not very flexible. In the following articles, we will automatically expand and shrink the capacity CPU
according to our custom monitoring .Pod