All Products
Search
Document Center

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

Last Updated:Aug 18, 2023

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. For more information, see Install and configure Terraform on your on-premises machine.

  • The auto scaling feature is dependent 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.

  • Your account information is configured.

    You can specify identity information in environment variables.

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

  • Complex configurations and high costs are required.

  • 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, click the name of the cluster that you want to manage and choose Configurations > ConfigMaps in the left-side navigation pane.

    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 of 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           = ""
          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.medium"]
          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 of how to create an ACK managed cluster and a node pool that has auto scaling enabled in the 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               = 2
          memory_size                  = 4
          kubernetes_node_role         = "Worker"
        }
        
        resource "alicloud_vpc" "default" {
          name                         = var.name
          cidr_block                   = "10.1.0.0/21"
        }
        resource "alicloud_vswitch" "default" {
          name                         = var.name
          vpc_id                       = alicloud_vpc.default.id
          cidr_block                   = "10.1.1.0/24"
          availability_zone            = data.alicloud_zones.default.zones.0.id
        }
        resource "alicloud_key_pair" "default" {
          key_name                     = var.name
        }
        
        # Create an ACK managed cluster. 
        resource "alicloud_cs_managed_kubernetes" "default" {
          name                         = var.name
          count                        = 1
          cluster_spec                 = "ack.pro.small"
          is_enterprise_security_group = true
          worker_number                = 2
          password                     = "Hello1234"
          pod_cidr                     = "172.20.0.0/16"
          service_cidr                 = "172.21.0.0/20"
          worker_vswitch_ids           = [alicloud_vswitch.default.id]
          worker_instance_types        = [data.alicloud_instance_types.default.instance_types.0.id]
        }
        
        # Create a node pool that has auto scaling enabled in the cluster. 
        resource "alicloud_cs_kubernetes_node_pool" "at1" {
          cluster_id                   = alicloud_cs_managed_kubernetes.default.0.id
          name                         = var.name
          vswitch_ids                  = [alicloud_vswitch.default.id]
          instance_types               = [data.alicloud_instance_types.default.instance_types.0.id]
          key_name                     = alicloud_key_pair.default.key_name
         
          scaling_config {
            min_size     = 1
            max_size     = 5
          }
        
        }

    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.