Help Center/ Cloud Container Engine/ Best Practices/ Terraform/ Creating a Node Pool and Adding It to an ECS Group Using Terraform
Updated on 2026-08-20 GMT+08:00

Creating a Node Pool and Adding It to an ECS Group Using Terraform

An ECS group uses an anti-affinity policy to schedule ECSs in the same group to different physical machines. This prevents multiple ECSs from becoming unavailable at the same time due to a single point of failure (SPOF), thereby improving service availability. You can add nodes in the same node pool to the same ECS group for node-level disaster recovery and enhance cluster stability.

This section describes how to use Terraform to create a node pool in a CCE cluster and an ECS group so that nodes in the same node pool are automatically added to the same ECS group, improving the cluster's disaster recovery capability.

Prerequisites

Procedure

Step 1: Create a Project Directory and Initialize the Configuration

  1. Create a node pool management directory (the directory name is user-defined).
    mkdir terraform_cce_nodepool
    cd ./terraform_cce_nodepool
  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 ID.

    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 an ECS group.

    Create the server_group.tf file, create an ECS group, and configure an anti-affinity policy so that ECSs in the group are scheduled to different physical machines.

    variable "ecs_group_name" {
      description = "ECS group name"
      type        = string
    }
    
    variable "ecs_group_policies" {
      description = "ECS group policy"
      type        = list(string)
      default     = ["anti-affinity"]
    }
    
    resource "huaweicloud_compute_servergroup" "test-sg" {
      name     = var.ecs_group_name
      policies = var.ecs_group_policies
    }

    The involved parameters are described in the table below.

    Parameter

    Description

    name

    ECS group name

    policies

    ECS group policy. anti-affinity: schedules ECSs in a group to different physical machines. affinity: schedules ECSs in a group to the same physical machine.

  3. Create a node pool and add it to the ECS group.

    Create the node_pool_with_sg.tf file, create a CCE node pool, associate it with the ECS group created in step 2, and enable auto scaling.

    variable "node_pool_name" {
      description = "Node pool name"
      type        = string
    }
    
    variable "node_pool_os_type" {
      description = "Node pool OS"
      type        = string
      default     = "Huawei Cloud EulerOS 2.0"
    }
    
    variable "node_pool_password" {
      description = "Password for logging in to nodes in the node pool"
      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 "root_volume_type" {
      description = "Root volume type"
      type        = string
      default     = "SSD"
    }
    
    variable "root_volume_size" {
      description = "Root volume size"
      type        = number
      default     = 50
    }
    
    variable "data_volume_type" {
      description = "Data disk type"
      type        = string
      default     = "SSD"
    }
    
    variable "data_volume_size" {
      description = "Data disk size"
      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
      ecs_group_id             = huaweicloud_compute_servergroup.test-sg.id
      scall_enable             = true
      scale_down_cooldown_time = 100
      priority                 = 1
      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
      }
    }

    The involved parameters are described in the table below. Replace the values based on site and service requirements.

    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.

    ecs_group_id

    ECS group ID, which is the ID of the group created in step 2.

    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 seconds.

    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.

  4. 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"
    ecs_group_name     = "ecs-group-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.

    ecs_group_name

    Name of the ECS group, on which an anti-affinity policy will be created.

    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.

  5. (Optional) If you need to specify an endpoint, create the endpoints.tf file.
    provider "huaweicloud" {
      insecure  = true  # Skip SSL certificate verification.
      endpoints = {
        ecs = "https://ecs.{region}.myhuaweicloud.com"
        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.
    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, nodes, and ECS group 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