Help Center/ Cloud Container Engine/ Best Practices/ Terraform/ Enabling Auto Scaling for a Node Pool Using Terraform
Updated on 2026-08-20 GMT+08:00

Enabling Auto Scaling for a Node Pool Using Terraform

During service running, increasing traffic may cause insufficient compute resources in the cluster. Manual node scale-out is slow and may be incomplete. CCE supports automatic cluster node scaling. With node pools, CCE can automatically scale nodes. When resources are insufficient, CCE automatically adds nodes. When resources are idle, CCE automatically reduces nodes. This improves resource utilization and reduces O&M costs.

This section describes how to use Terraform to create a node pool in a CCE cluster and enable auto scaling for the node pool. In addition, you can configure an extended scaling group for auto scaling of nodes with multiple flavors, improving the scale-out success rate.

Prerequisites

Procedure

Step 1: Create a Project Directory and Initialize the Configuration

  1. Create an auto scaling management directory (the directory name is user-defined).
    mkdir terraform_cce_autoscale
    cd ./terraform_cce_autoscale
  2. Copy the prepared provider configuration file to the current directory. The init.tf file has been created in the terraform_hw_demo directory in section "Installing Terraform and Configuring the Huawei Cloud Provider".
    cp ../terraform_hw_demo/init.tf .

Step 2: Create Configuration Files

All the following configuration files are created in the project directory.

  1. Query the CCE cluster information.

    Create the data_cce_cluster.tf file to query the cluster ID based on the cluster name. The cluster ID will be used to specify the cluster that the node pool belongs to during node pool creation.

    variable "cluster_name" {
      description = "CCE cluster name"
      type        = string
    }
    
    data "huaweicloud_cce_cluster" "cluster" {
      name   = var.cluster_name
      status = "Available"
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    name

    CCE cluster name, which must be the same as the name of the created cluster.

    status

    Cluster status. Available indicates that only available clusters are queried.

  2. Create a node pool and enable auto scaling.

    Create the node_pool_with_scale.tf file to create a CCE node pool, enable auto scaling, and configure an extended scaling group to support auto scaling of nodes with multiple flavors.

    variable "node_pool_name" {
      description = "Node pool name"
      type        = string
    }
    
    variable "node_pool_os_type" {
      description = "Node OS"
      type        = string
      default     = "Huawei Cloud EulerOS 2.0"
    }
    
    variable "node_pool_password" {
      description = "Node login password"
      type        = string
    }
    
    variable "node_pool_initial_node_count" {
      description = "Initial number of nodes in the node pool"
      type        = number
      default     = 2
    }
    
    variable "availability_zone" {
      description = "AZ where the nodes are located"
      type        = string
      default     = "cn-north-4a"
    }
    
    variable "scale_down_cooldown_time" {
      description = "Scale-in cooldown period, in minutes"
      type        = number
      default     = 10
    }
    
    variable "node_pool_priority" {
      description = "Node pool priority"
      type        = number
      default     = 1
    }
    
    variable "root_volume_type" {
      description = "System disk type"
      type        = string
      default     = "SSD"
    }
    
    variable "root_volume_size" {
      description = "System disk size, in GB"
      type        = number
      default     = 50
    }
    
    variable "data_volume_type" {
      description = "Data disk type"
      type        = string
      default     = "SSD"
    }
    
    variable "data_volume_size" {
      description = "Data disk size, in GB"
      type        = number
      default     = 100
    }
    
    resource "huaweicloud_cce_node_pool" "node_pool" {
      cluster_id               = data.huaweicloud_cce_cluster.cluster.id
      name                     = var.node_pool_name
      os                       = var.node_pool_os_type
      initial_node_count       = var.node_pool_initial_node_count
      flavor_id                = "c9.large.2"
      availability_zone        = var.availability_zone
      password                 = var.node_pool_password
      scale_enable             = true
      scale_down_cooldown_time = var.scale_down_cooldown_time
      priority                 = var.node_pool_priority
      type                     = "vm"
    
      root_volume {
        size       = var.root_volume_size
        volumetype = var.root_volume_type
      }
      data_volumes {
        size       = var.data_volume_size
        volumetype = var.data_volume_type
      }
    
      extension_scale_groups {
        metadata {
          name = "group1"
        }
        spec {
          flavor = "c9.xlarge.2"
          az     = var.availability_zone
    
          autoscaling {
            extension_priority = 1
            enable             = true
          }
        }
      }
    
      extension_scale_groups {
        metadata {
          name = "group2"
        }
        spec {
          flavor = "c9.2xlarge.2"
          az     = var.availability_zone
    
          autoscaling {
            extension_priority = 1
            enable             = true
          }
        }
      }
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    cluster_id

    CCE cluster ID, which can be obtained from the query result in step 1.

    name

    Node pool name.

    os

    Node OS. The default OS is Huawei Cloud EulerOS 2.0.

    initial_node_count

    Initial number of nodes in the node pool. The default value is 2.

    flavor_id

    Node flavor, for example, c9.large.2

    availability_zone

    AZ where the nodes are located. Replace it with the actual AZ of the site.

    password

    Node login password

    scale_enable

    Whether to enable auto scaling. The value true indicates that auto scaling is enabled.

    scale_down_cooldown_time

    Scale-in cooldown period, in minutes. The default value is 10.

    priority

    Node pool priority. A smaller value indicates a higher priority.

    type

    Node type. The value vm indicates a VM.

    root_volume.size

    System disk size, in GB.

    root_volume.volumetype

    System disk type. The value can be SSD, GPSSD, or ESSD.

    data_volumes.size

    Data disk size, in GB.

    data_volumes.volumetype

    Data disk type.

    extension_scale_groups

    Extended scaling group, which scales nodes of multiple flavors.

    • The values of flavor_id and availability_zone are for reference only. Replace them based on site and service requirements. You will be billed for ECSs when you create nodes. Select proper node flavors based on service requirements.
    • extension_scale_groups: Nodes of multiple flavors can be configured in the same node pool. When resources of a flavor are insufficient, nodes of another flavor are automatically used, improving the scale-out success rate. In this example, three flavors are configured: default flavor c9.large.2 and extended flavors c9.xlarge.2 and c9.2xlarge.2.
  3. Configure resource parameters.

    Create the terraform.tfvars file and preset the input parameters required by the resources. The following is an example configuration. Modify the parameters based on service requirements.

    cluster_name       = "cluster-xxx"
    node_pool_name     = "tf-node-pool"
    node_pool_password = "*******"
    availability_zone   = "cn-north-7c"           # Replace it with the actual AZ.

    The involved parameters are described in the table below.

    Parameter

    Description

    cluster_name

    Name of the created cluster.

    node_pool_name

    Node pool name.

    node_pool_password

    Node login password. Set this parameter to a password that meets the complexity requirements.

    availability_zone

    AZ where the nodes are located. Replace it with the actual AZ of the site.

  4. (Optional) If you need to specify an endpoint, create the endpoints.tf file.
    provider "huaweicloud" {
      insecure  = true  # Skip SSL certificate verification.
      endpoints = {
        cce = "https://cce.{region}.myhuaweicloud.com"
      }
    }

    Replace {region} with the actual region (for example, cn-north-7c).

Step 3: Initialize and Apply the Terraform Configuration

  1. Initialize the environment and download the required provider plugin.
    terraform init
  2. View the execution plan and check whether the resource changes meet your expectation.
    terraform plan
  3. After confirming the plan, create resources. Terraform will ask for your confirmation. Enter yes to confirm the execution.
    terraform apply
  4. View the created resources.
    terraform show

Step 4: Clear Resources

This operation will permanently delete the node pool and nodes created by Terraform. Ensure that no important service data exists in the resources before performing this operation. Before clearing a node pool, you need to release the nodes in the node pool.

If you no longer need the resources created by Terraform, run the following command to release them:

terraform destroy

Terraform will ask for your confirmation. Enter yes to confirm the execution.

Helpful Links