Container Horizontal Scaling
CCSE supports quickly creating applications that support HPA on the console interface, enabling autoscaling of container resources. You can also configure it by defining the YAML file of HPA (Horizontal Pod Autoscaling).
Background
Starting with v1.18, the K8s v2beta2 API allows the scaling behavior to be configured through the "behavior" field of HPA. You can specify the scaleUp and scaleDown fields in the behavior parameter to specify scale-out and scale-in settings. If you want to use HPA for Pod scaling that only scales either up or down, you can achieve this by enabling metric scaling and clicking on Disable Scale-Down or Disable Scale-Up.
Default: Neither is disabled.
Disable Scale-Up: Selecting the value "Disabled" for selectPolicy will deactivate scaling up in the given direction. Therefore, using the following policy will prevent scaling up.
behavior: scaleUp: selectPolicy: Disabled |
Disable Scale-Down: Selecting the value "Disabled" for selectPolicy will deactivate scaling down in the given direction. Therefore, using the following policy will prevent scaling down.
behavior: scaleDown: selectPolicy: Disabled |
Creating an HPA Application through the Container Service Console
The eSurfing Cloud CCSE has integrated HPA, allowing you to easily create it through the container service console. You can create HPA when creating an application or enable HPA based on existing applications.
Method 1: Enable HPA during the application creation process.
l Log in to the CCSE console.
l In the left navigation pane of the console, click on Clusters.
l In the cluster list page, click on the target cluster name.
l In the left navigation pane on the Cluster Management page, select Workload > Stateless.
l Click on the Add button in the upper left corner.
l Fill in the basic information, select Autoscaling in the instance quantity, and set the scaling conditions and configuration.
n Resource rules support CPU and memory, allowing both percentage and average calculation methods.
n Pod rules are compatible with Pod metric objects, supporting average calculation methods and specific metrics.
n Object rules are compatible with Service metric objects, supporting the threshold calculation method and specified metrics.
l Click Submit in the lower left corner to create a Deployment that supports HPA.
Result Verification
Click Workload > Stateless and click on the application name to view the autoscaling group information in the deployment details.
In the actual usage environment, applications scale according to CPU load. You can also verify autoscaling in the test environment. By giving the Pod a CPU stress test, you can see that the Pod can complete horizontal scaling.
Method 2: Enable HPA for existing applications.
l Log in to the CCSE console.
l In the left navigation pane of the console, click on Clusters.
l In the cluster list page, click on the target cluster name.
l In the left navigation pane on the Cluster Management page, select Workload > Stateless.
l On the application list page, select the Target Application and click the More button.
l Set the scaling conditions and configurations.
n Resource rules support CPU and memory, allowing both percentage and average calculation methods.
n Pod rules are compatible with Pod metric objects, supporting average calculation methods and specific metrics.
n Object rules are compatible with Service metric objects, supporting the threshold calculation method and specified metrics.
l Click Confirm to complete the HPA configuration for the Deployment.
Creating an HPA Application Through kubectl Command
You can also manually create HPA through YAML, bind it to the Deployment object to be scaled, and configure container autoscaling through the kubectl command.
Here is an example with a Nginx application.
l Create and copy the following content to nginx.yml.
The orchestration template for the Deployment is as follows.
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: nginx:1.7.9 # replace it with your exactly <image_name:tags> ports: - containerPort: 80 resources: requests: ## This setting is required for HPA to function properly. cpu: 500m |
l Execute the following command to create an Nginx application.
kubectl create -f nginx.yml |
l Create HPA.
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: nginx-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx minReplicas: 1 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50 |
l Execute the following command to create a Nginx HPA.
kubectl create -f nginx-pha.yml |
l Once the HPA is created, run the command kubectl describe hpa <HPA name> again.
If you see the following information, it indicates that the HPA is running normally.
Normal SuccessfulRescale 30s horizontal-pod-autoscaler New size: 1; reason: All metrics below target |
In this case, when the utilization of the Nginx Pod exceeds the set rate of 50%, it triggers a horizontal scale-up; when it falls below 50%, it triggers a scale-down.