Horizontal Pod Autoscaler (HPA) automatically adjusts the number of pod replicas based on resource utilization. In Container Service for Kubernetes (ACK), you can enable HPA for applications by using the console or kubectl.
Prerequisites
-
You have created an ACS cluster.
Create an HPA-enabled application
Use the console
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.
-
On the Deployments page, click Create with Image.
-
On the Basic Information page, set the application parameters and click Next.
Parameter
Description
Namespace
The namespace for the application. Defaults to
default.Application Name
The name of the application.
Replicas
The number of pods for the application. Default: 2.
Workload
The type of workload. Select Deployments, StatefulSets, Jobs, or CronJobs.
Labels
A label to identify the application.
Annotations
An annotation for the application.
Instance Type
The instance type of the pod. Select General-purpose, BestEffort, or Performance-enhanced.
-
On the Container page, configure the container, select an image, and specify resource requests. Then, click Next. For more information, see Container configuration.
NoteHPA requires resource requests to be specified for the Deployment.
-
On the Advanced page, in the Access Control section, click Create next to Service, and configure the Service parameters. For more information, see Advanced configurations.
-
On the Advanced page, select Enable for HPA and configure the scaling parameters.
-
Metric: The supported metrics are CPU and memory. Must match the configured required resource type.
-
Condition:: The resource utilization percentage. The container scales out when utilization exceeds this value. For more information about the Horizontal Pod Autoscaling algorithm, see Algorithm details.
-
Max. Replicas: The maximum number of containers the Deployment can run.
-
Min. Replicas: The lower limit on the number of replicas.
-
-
Click Create to create the HPA-enabled Deployment.
Use kubectl
You can also use an orchestration template and kubectl to create an HPA object and bind it to a Deployment.
The following example uses an NGINX application.
-
Create a file named nginx.yaml and copy the following content into it.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/nginx:1.7.9 # Replace with your image name and tag ports: - containerPort: 80 resources: requests: ## Required for HPA to function. cpu: 500m -
Run the following command to deploy the NGINX application.
kubectl apply -f nginx.yaml -
Create an
hpa.yamlfile with the following content to define the HPA.The scaleTargetRef field specifies the resource that HPA scales. In this example, it targets the nginx Deployment.
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: nginx-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 # The minimum replica count. Must be >= 1. maxReplicas: 10 # The maximum replica count. Must be > minReplicas. metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50 # The target average utilization across all pods, calculated as (average usage / requested amount). -
Run the following command to create the HPA.
kubectl apply -f hpa.yaml -
After the HPA is created, run
kubectl describe hpa <HPA_NAME>.The following output indicates that HPA is working correctly.
Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulRescale 4m53s horizontal-pod-autoscaler New size: 1; reason: All metrics below target
Related information
For scheduled autoscaling, see Cron Horizontal Pod Autoscaler (CronHPA).