All Products
Search
Document Center

:HPA

Last Updated:Dec 06, 2023

Horizontal Pod Autoscaling (HPA) can dynamically change the number of replicated pods to meet business requirements when traffic fluctuations occur. You can use HPA to scale workloads that support the scale operation, such as Deployments and StatefulSets. This topic describes how to use HPA to control the number of replicated pods in associated clusters when you distribute applications in Distributed Cloud Container Platform for Kubernetes (ACK One).

Prerequisites

  • The Fleet management feature is enabled. For more information, see Enable Fleet management.

  • Multiple clusters are associated with the Fleet instance. For more information, see Associate clusters with a Fleet instance.

  • The kubeconfig file of the Fleet instance is obtained in the ACK One console and a kubectl client is connected to the Fleet instance.

  • The AMC command-line tool is installed. For more information, see Use AMC.

Background information

After the Fleet instance distributes an application to the associated clusters, the Fleet instance checks the status of the associated clusters every 5 minutes and ensures the consistency of application resources between the associated clusters and the Fleet instance. If you use HPA to directly change the number of replicated pods after the application is distributed to the associated clusters, an inconsistency error occurs in the Fleet instance. Therefore, you must configure HPA to control the number of replicated pods in the associated clusters when you distribute the application.

Procedure

  1. Run the following command to create a namespace.

    Replace demo with the name of the namespace that you want to create.

    kubectl create namespace demo
  2. Run the following command to query the associated clusters:

    kubectl amc get managedcluster

    Expected output:

    Name             Alias           HubAccepted
    c4a416e****      cluster1        true
  3. Create a Deployment and an HPA resource on the Fleet instance.

    1. Create a file named app-meta.yaml and add the following content to the file. The file defines a Deployment and an HPA resource.

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx
        namespace: demo
        labels:
          app: nginx
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: nginx
        template:
          metadata:
            labels:
              app: nginx
          spec:
            containers:
            - name: nginx
              image: nginx:1.7.9
              ports:
              - containerPort: 80
              resources:
                requests:
                  cpu: 500m
      ---
      apiVersion: autoscaling/v2beta2
      kind: HorizontalPodAutoscaler
      metadata:
        name: nginx-hpa
        namespace: demo
      spec:
        scaleTargetRef:
          apiVersion: apps/v1
          kind: Deployment
          name: nginx
        minReplicas: 1
        maxReplicas: 10
        metrics:
        - type: Resource
          resource:
            name: cpu
            target:
              type: Utilization
              averageUtilization: 50
    2. Run the following command to distribute the resources to the Fleet instance:

      kubectl apply -f app-meta.yaml
  4. Create an application propagation policy named apply-once that allows the Fleet instance to ignore the number of replicated pods created by the Deployment. This way, the Fleet instance does not ensure the consistency of the number of replicated pods between the Fleet instance and the associated clusters.

    1. Create a file named app.yaml and add the following content to the file. The file defines the application propagation policy.

      apiVersion: core.oam.dev/v1beta1
      kind: Application
      metadata:
        name: nginx
        namespace: demo
        annotations:
          app.oam.dev/publishVersion: version1
      spec:
        components:
          - name: nginx
            type: ref-objects
            properties:
              objects:
                - apiVersion: apps/v1
                  kind: Deployment
                  name: nginx
                - apiVersion: autoscaling/v1
                  kind: HorizontalPodAutoscaler
                  name: nginx-hpa
        policies:
          - name: apply-once
            type: apply-once      #The apply-once policy defines which resource changes and field changes are ignored by the Fleet instance. 
            properties:
              enable: true        # Enable the apply-once feature. 
              rules:
                - selector:
                    componentNames: [ "nginx" ]         #Specify a component to enable the apply-once feature. In this example, the Deployment named nginx is specified. 
                    resourceTypes: [ "Deployment" ]     # Select a specific resource to enable the apply-once feature by setting this parameter. 
                  strategy:
                    path: [ "spec.replicas" ]           # Select a specific field of the resource to enable the apply-once feature. 
          - type: topology
            name: beijing-clusters
            properties:
              clusters: ["c4a416e****"]
    2. Run the following command to apply the application propagation policy to the Fleet instance. Then, the Fleet instance distributes the Deployment and HPA resource to the associated clusters based on the application propagation policy.

      kubectl apply -f app.yaml
  5. Run the following command to query the deployment progress of the application:

    Replace demo with the name of the namespace that you created.

    kubectl get app nginx -n demo

    Expected output:

    NAME    COMPONENT   TYPE          PHASE     HEALTHY   STATUS      AGE
    nginx   nginx       ref-objects   running   true      Ready:2/2   51s
  6. Run the following command to query the status of the associated clusters:

    kubectl amc get deployment,hpa -n demo -m c4a416e****

    Expected output:

    Run on ManagedCluster c4a416exxx (cluster1)
    NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
    deployment.apps/nginx   2/2     2            2           63s
    
    NAME                                            REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    horizontalpodautoscaler.autoscaling/nginx-hpa   Deployment/nginx   0%/50%    1         10        2          63s
  7. Run the following command to view the number of replicated pods after the HPA feature takes effect:

    kubectl amc get pod -n demo -m c4a416e****

    Expected output:

    Run on ManagedCluster c4a416e**** (cluster1)
    NAME                     READY   STATUS    RESTARTS   AGE
    nginx-697b955cdc-b94nl   1/1     Running   0          6m

    The output shows that the number of replicated pods is reduced to 1 as expected when no traffic is distributed to the Service.