All Products
Search
Document Center

Container Service for Kubernetes:Use Terraform to create a node pool that has auto scaling enabled

Last Updated:Sep 06, 2024

By default, nodes in node pools and managed node pools of Container Service for Kubernetes (ACK) cannot automatically scale in or out. You can use Terraform to create a node pool that has auto scaling enabled. This topic describes how to use Terraform to create a node pool that has auto scaling enabled.

Prerequisites

  • Terraform is installed.

    Note

    You must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.

    • By default, Cloud Shell is preinstalled with Terraform and your account information is also specified. You do not need to modify the configurations.

    • For more information about how to install Terraform by using a method other than Cloud Shell, see Install and configure Terraform in the local PC.

  • The auto scaling feature is reliant on the Alibaba Cloud service Auto Scaling. Therefore, you must activate Auto Scaling and assign the default role for Auto Scaling to your account before you enable auto scaling for nodes. For more information, see Activate Auto Scaling.

    Note

    If you have used the alicloud_cs_kubernetes_autoscaler component, Auto Scaling is activated.

  • Auto Scaling-related policies are attached to your account in the Resource Access Management (RAM) console. This grants your cluster the required permissions to access Auto Scaling. For more information about how to attach RAM policies, see Authorization.

  • Permissions are granted to access Operation Orchestration Service (OOS). You can perform the following steps to create the AliyunOOSLifecycleHook4CSRole role that provides OOS access permissions.

    1. Click AliyunOOSLifecycleHook4CSRole.

      Note
      • If the current account is an Alibaba Cloud account, click AliyunOOSLifecycleHook4CSRole.

      • If the current account is a RAM user, make sure that your Alibaba Cloud account is assigned the AliyunOOSLifecycleHook4CSRole role. Then, attach the AliyunRAMReadOnlyAccess policy to the RAM user. For more information, see Grant permissions to RAM users.

    2. On the Cloud Resource Access Authorization page, click Agree to Authorization.

  • Your account information is configured.

    Run the following commands to create environment variables to store identity authentication information.

    • Linux environment

      export ALICLOUD_ACCESS_KEY="************"   # Replace the value with the AccessKey ID of your Alibaba Cloud account. 
      export ALICLOUD_SECRET_KEY="************"   # Replace the value with the AccessKey secret of your Alibaba Cloud account. 
      export ALICLOUD_REGION="cn-beijing"         # Replace the value with the ID of the region in which your cluster resides.
    • Windows environment

      set ALICLOUD_ACCESS_KEY="************"   # Replace the value with the AccessKey ID of your Alibaba Cloud account. 
      set ALICLOUD_SECRET_KEY="************"   # Replace the value with the AccessKey secret of your Alibaba Cloud account. 
      set ALICLOUD_REGION="cn-beijing"         # Replace the value with the ID of the region in which your cluster resides.
    Note

    To improve the flexibility and security of permission management, we recommend that you create a Resource Access Management (RAM) user named Terraform. Then, create an AccessKey pair for the RAM user and grant permissions to the RAM user. For more information, see Create a RAM user and Grant permissions to RAM users.

Background information

Terraform is an open source tool that supports new infrastructures through Terraform providers. You can use Terraform to preview, configure, and manage cloud infrastructures and resources. For more information, see What is Terraform?.

In earlier versions of Alibaba Cloud Provider, ACK provides a component named alicloud_cs_kubernetes_autoscaler. The alicloud_cs_kubernetes_autoscaler component can be used to enable auto scaling for nodes. However, the following limits apply:

  • The configuration is complex and the cost is high.

  • Each node to be scaled is added to the default node pool and cannot be separately maintained.

  • Some parameters cannot be modified.

Alibaba Terraform Provider 1.111.0 and later allow you to create node pools that have auto scaling enabled by using the alicloud_cs_kubernetes_node_pool component. This component has the following benefits:

  • Provides simple scaling configurations. You only need to set the lower and upper limits of the node quantity in the scaling group.

  • Uses default settings for optional parameters to prevent inconsistent environments among nodes. This prevents user errors. For example, you may configure different operating system (OS) images for different nodes.

  • Allows you to explicitly view the changes of nodes in a node pool in the ACK console.

Use Terraform to create a node pool that has auto scaling enabled

If you have used the alicloud_cs_kubernetes_autoscaler component, authorize your cluster to access Auto Scaling and perform the following steps to switch to the alicloud_cs_kubernetes_node_pool component. Then, you can create node pools that have auto scaling enabled in your cluster.

Note

If you have not used the alicloud_cs_kubernetes_autoscaler component, skip Step 1 and Step 2. Go to Step 3.

  1. Modify the autoscaler-meta ConfigMap.

    1. Log on to the ACK console. In the left-side navigation pane, click Clusters.

    2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side navigation pane, choose Configurations > ConfigMaps.

    3. In the upper-left corner of the ConfigMap page, select kube-system from the Namespace drop-down list. Find the autoscaler-meta ConfigMap and click Edit in the Actions column.

    4. In the Edit panel, modify the value of the autoscaler-meta ConfigMap.

      You need to change the value of taints from the string type to the array type. In this case, change "taints":"" to "taints":[] in the Value text box.

    5. Click OK.

  2. Synchronize the node pool.

    1. In the left-side navigation pane of the details page, choose Nodes > Node Pools.

    2. In the upper-right corner of the Node Pools page, click Sync Node Pool.

If you have not used the alicloud_cs_kubernetes_autoscaler component, perform the following steps.

  1. Use Terraform to create a node pool that has auto scaling enabled

    1. Create a node pool configuration file.

      • The following code provides an example on how to create a node pool that has auto scaling enabled in an existing ACK cluster:

        Show sample code

        provider "alicloud" {
        }
        # Create a node pool that has auto scaling enabled in an existing ACK cluster. 
        resource "alicloud_cs_kubernetes_node_pool" "at1" {
          # The ID of the ACK cluster where you want to create the node pool. 
          cluster_id           = "cf8cbb7e461a7456bafd5841f3a8****"
          name                 = "np-test"
          # The vSwitches that are used by nodes in the node pool. You must specify at least one vSwitch. 
          vswitch_ids          = ["vsw-bp1mdigyhmilu2h4v****"]
          instance_types       = ["ecs.e3.m****"]
          password             = "Hello1234"
         
          scaling_config {
            # The minimum number of nodes in the node pool. 
            min_size     = 1
            # The maximum number of nodes in the node pool. 
            max_size     = 5
          }
        
        }
      • The following code provides an example on how to create an ACK managed cluster and a node pool that has auto scaling enabled in the cluster: For more information about how to use Terraform to create a cluster, see Create an ACK managed cluster.

        Show sample code

        provider "alicloud" {
        }
        
        variable "name" {
          default    = "tf-test"
        }
        
        data "alicloud_zones" default {
          available_resource_creation  = "VSwitch"
        }
        data "alicloud_instance_types" "default" {
          availability_zone            = data.alicloud_zones.default.zones.0.id
          cpu_core_count               = 4
          memory_size                  = 8
          kubernetes_node_role         = "Worker"
        }
        
        resource "alicloud_vpc" "default" {
          cidr_block                   = "10.1.0.0/21"
        }
        resource "alicloud_vswitch" "default" {
          vpc_id                       = alicloud_vpc.default.id
          cidr_block                   = "10.1.1.0/24"
          zone_id            					 = data.alicloud_zones.default.zones.0.id
        }
        
        
        # Create an ACK managed cluster. 
        
        # Create a node pool that has auto scaling enabled in the cluster. 
        resource "alicloud_cs_kubernetes_node_pool" "at1" {
          cluster_id                   = "test12334"
          name                         = var.name
          vswitch_ids                  = [alicloud_vswitch.default.id]
          instance_types               = [data.alicloud_instance_types.default.instance_types.0.id]
        
        
          scaling_config {
            min_size     = 1
            max_size     = 5
          }
        
        }

        The following output indicates that the node pool is created.

        alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
        alicloud_cs_managed_kubernetes.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12]
        
        Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
          ~ update in-place
        
        Terraform will perform the following actions:
        
          ............
        
        
                # (2 unchanged blocks hidden)
            }
        Plan: 5 to add, 0 to change, 0 to destroy.
    2. Run the terraform apply command to create the node pool.

    After the node pool is created, you can find the node pool on the Node Pools page. Auto Scaling Enabled appears below the name of the node pool.