All Documents
Current Document

Content is empty

If you don't find the content you expect, please try another search term

Documentation

Horizontal pod autoscaling

Last updated:2021-05-11 10:41:43

A Horizontal Pod Autoscaler (HPA) automatically adjusts the number of pods in a Kubernetes cluster based on custom metrics such as CPU utilization.

HPA algorithm

An HPA obtains the specified pod monitoring metric from the metrics server every 30s. If the target utilization is specified, the HPA must calculate the percentage of the monitoring metric to the resource request of containers in each pod. If a target metric value is specified, the HPA directly uses the value. The HPA calculates the expected number of workload replicas based on the current number of workload replicas and the target metric value by using the following formula:

Expected number of replicas = Collected utilization/User-defined utilization × Current number of pods

You can configure multiple policies for the HPA. The HPA calculates the expected number of replicas based on each target metric value and uses the largest one as the final expected number of replicas.

Operations in the KCE console

Create an HPA

You can create an HPA by using the following three methods.

Method 1: Create an HPA on the HPA list page
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to create an HPA. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Click Create. On the Create HPA page, complete the following configurations:
    • Name: the name of the HPA to be created.
    • Namespace: the namespace of the cluster to which the HPA belongs.
    • Connectdeployment: the Deployment associated with the HPA.
    • Trigger Policy: the metrics on which the HPA depends.
    • Pod Range: the pod range of the HPA. The number of pods is adjusted within the pod range.

Note: The following metrics can be specified in trigger policies:

  • CPU Utilization: the percentage of the CPU usage to the CPU request of containers.
  • CPU Usage: the vCPUs used by containers.
  • Memory Utilization: the percentage of the memory usage to the memory request of containers.
  • Memory Usage: the memory space used by containers, in MiB.
  1. Click Create.
Method 2: Configure an HPA when you create a Deployment
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to create an HPA. The cluster details page appears.
  4. In the left navigation pane, choose Workload > Deployment. The Deployment list page appears.
  5. Click Create. The Create Deployment page appears. Set basic information and proceed to Deployment Configuration. Select Automatic Adjustment for Pod Count and complete the following configurations:
    • Trigger Policy: the metrics on which the HPA depends.
    • Pod Range: the pod range of the HPA. The number of pods is adjusted within the pod range.

image.png

Method 3: Use a YAML file
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to create an HPA. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Click Create Resources by YAML in the upper-right corner. On the Create Resources by YAML page, modify the configurations as required and click Create.

Modify HPA configurations

You can modify HPA configurations by using the following three methods.

Method 1: Modify HPA configurations on the HPA list page
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to modify HPA configurations. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Find the HPA that you want to modify and click Change Configuration in the Operation column. In the Modify HPA Configuration dialog box, modify the configurations as required and click OK.
Method 2: Edit the YAML file
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to modify HPA configurations. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Find the HPA that you want to modify and click Edit YAML in the Operation column. On the UpdateHPA page, modify the configurations as required and click Update.
Method 3: Modify HPA configurations when you adjust the pod count
  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to modify HPA configurations. The cluster details page appears.
  4. In the left navigation pane, choose Workload > Deployment. The Deployment list page appears.
  5. Find the Deployment for which you want to adjust the pod count and click Adjust Pod Count in the Operation column. In the Adjust Pod Count dialog box, select Automatic Adjustment for Pod Count, modify the configurations as required, and then click OK.

Check HPA configurations

  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to check HPA configurations. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Click the name of the HPA that you want to view.
  6. Check the configurations on the Details, Event, and YAML tabs.

Delete an HPA

  1. Log in to the KCE console.
  2. In the left navigation pane, click Cluster.
  3. Click the ID of the cluster in which you want to delete an HPA. The cluster details page appears.
  4. In the left navigation pane, choose HPA > HPA. The HPA list page appears.
  5. Find the HPA that you want to delete and click Delete in the Operation column. In the message that appears, click OK.

Operations by using kubectl

You can create and edit an HPA by using a YAML file.

Sample hpa-example.yaml file:

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-example
  namespace: default
spec:
  minReplicas: 1                    # The minimum number of replicas.
  maxReplicas: 3                    # The maximum number of replicas.
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 50       # Indicates that HPA is triggered when CPU utilization is 50%, and the pod range is 1 to 3.
  scaleTargetRef:
    apiVersion: apps/v1beta2
    kind: Deployment
    name: nginx

Apply the hpa-example.yaml file.

# kubectl apply -f hpa-example.yaml

Notes

  • When you set Trigger Policy to CPU Utilization or Memory Utilization, you must also specify the container request.
  • An HPA allows a fluctuation factor of 10% when it calculates the expected number of replicas. If the number of replicas is within the fluctuation range, the HPA does not adjust it.
  • After a decision is made, an HPA stops making scaling decisions within a period of time. This period of time is 3 minutes for a scale-out and 5 minutes for a scale-in.
  • Load balancing is required for user requests.
On this page
Pure ModeNormal Mode

Pure Mode

Click to preview the document content in full screen
Feedback