Auto Scaling
In Pod Orchestration and Scheduling, we introduce controllers such as Deployment to control the number of pod replicas. You can adjust the number of replicas to manually scale your applications. However, manual scaling is sometimes complex and fails to cope with unexpected traffic spikes.
Kubernetes supports auto scaling of pods and cluster nodes. You can set rules to trigger auto scaling when certain metrics (such as CPU usage) reach the configured threshold.
Prometheus and Metrics Server
A prerequisite for auto scaling is that your container running data can be collected, such as number of cluster nodes/pods, and CPU and memory usage of containers. Kubernetes does not provide such monitoring capabilities itself. You can use extensions to monitor and collect your data.
- Prometheus is an open-source monitoring and alarming framework that can collect multiple types of metrics. Prometheus has been a standard monitoring solution of Kubernetes.
- Metrics Server is a cluster-wide aggregator of resource utilization data. Metrics Server collects metrics from the Summary API exposed by kubelet. These metrics are set for core Kubernetes resources, such as pods, nodes, containers, and Services. Metrics Server provides a set of standard APIs for external systems to collect these metrics.
Horizontal Pod Autoscaler (HPA) can work with Metrics Server to implement auto scaling based on the CPU and memory usage. It can also work with Prometheus to implement auto scaling based on custom monitoring metrics.
How HPA Works
HPA is a controller that controls horizontal pod scaling. HPA periodically checks the pod metrics, calculates the number of replicas required to meet the target values configured for HPA resources, and then adjusts the value of the replicas field in the target resource object (such as a Deployment).
You can configure one or more metrics for the HPA. When configuring a single metric, you only need to sum up the current pod metrics, divide the sum by the expected target value, and then round up the result to obtain the expected number of replicas. For example, if a Deployment controls three pods, the CPU usage of each pod is 70%, 50%, and 90%, and the expected CPU usage configured in the HPA is 50%, the expected number of replicas is calculated as follows: (70 + 50 + 90)/50 = 4.2. The result is rounded up to 5. That is, the expected number of replicas is 5.
If multiple metrics are configured, the expected number of replicas of each metric is calculated and the maximum value will be used.
Using the HPA
The following example demonstrates how to use the HPA. First, use the Nginx image to create a Deployment with four replicas.
$ kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 4/4 4 4 77s $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-7cc6fd654c-5xzlt 1/1 Running 0 82s nginx-deployment-7cc6fd654c-cwjzg 1/1 Running 0 82s nginx-deployment-7cc6fd654c-dffkp 1/1 Running 0 82s nginx-deployment-7cc6fd654c-j7mp8 1/1 Running 0 82s
Create an HPA. The expected CPU usage is 70% and the number of replicas ranges from 1 to 10.
apiVersion: autoscaling/v2beta1 kind: HorizontalPodAutoscaler metadata: name: scale namespace: default spec: maxReplicas: 10 # Maximum number of replicas of the target resource minReplicas: 1 # Minimum number of replicas of the target resource metrics: # Metric. The expected CPU usage is 70%. - resource: name: cpu targetAverageUtilization: 70 type: Resource scaleTargetRef: # Target resource apiVersion: apps/v1 kind: Deployment name: nginx-deployment
Query the created HPA.
$ kubectl create -f hpa.yaml horizontalpodautoscaler.autoscaling/celue created $ kubectl get hpa NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE scale Deployment/nginx-deployment 0%/70% 1 10 4 18s
In the command output, the expected value of TARGETS is 70%, but the actual value is 0%. This means that the HPA will perform scale-in. The expected number of replicas can be calculated as follows: (0 + 0 + 0 + 0)/70 = 0. However, the minimum number of replicas has been set to 1. Therefore, the number of pods is changed to 1. After a while, the number of pods changes to 1.
$ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-7cc6fd654c-5xzlt 1/1 Running 0 7m41s
Query the HPA again and a record similar to the following is displayed under Events. In this example, the record indicates that the HPA successfully performed a scale-in 21 seconds ago and the number of pods is changed to 1, and the scale-in is triggered because the values of all metrics are lower than the target values.
$ kubectl describe hpa scale ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulRescale 21s horizontal-pod-autoscaler New size: 1; reason: All metrics below target
If you want to query the Deployment details, you can check the records similar to the following under Events. In this example, the second record indicates that the number of replicas of the Deployment is set to 1, which is the same as that in the HPA.
$ kubectl describe deploy nginx-deployment ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 7m deployment-controller Scaled up replica set nginx-deployment-7cc6fd654c to 4 Normal ScalingReplicaSet 1m deployment-controller Scaled down replica set nginx-deployment-7cc6fd654c to 1
Cluster AutoScaler
The HPA is designed for pods. However, if the cluster resources are insufficient, you can only add nodes. Scaling of cluster nodes could be laborious. Now with clouds, you can add or delete nodes by simply calling APIs.
Cluster Autoscaler is a component provided by Kubernetes for auto scaling of cluster nodes based on the pod scheduling status and resource usage. You can refer to the API documentation of your cloud service provider to implement auto scaling.
For details about the implementation on CCE, see Creating a Node Scaling Policy.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot