By default, nodes in ACK node pools and managed node pools do not support auto scaling. You can use Terraform to create a node pool with auto scaling enabled.
You can run the sample code in this topic with a single click.Run with one click
Prerequisites
-
The auto scaling feature depends on the Alibaba Cloud Auto Scaling service. Before you enable node auto scaling, you must activate Auto Scaling and grant the default role. For more information, see Activate Auto Scaling.
NoteIf you previously used the alicloud_cs_kubernetes_autoscaler add-on, the Auto Scaling service is already activated.
-
Grant permission to CloudOps Orchestration Service (OOS). You can create the AliyunOOSLifecycleHook4CSRole role to grant OOS the required permission.
-
Click AliyunOOSLifecycleHook4CSRole.
Note-
If you are using an Alibaba Cloud account, click AliyunOOSLifecycleHook4CSRole to grant the permission.
-
If you are using a RAM user, ensure that the primary Alibaba Cloud account has the AliyunOOSLifecycleHook4CSRole role and that the AliyunRAMReadOnlyAccess system policy is attached to your RAM user. For more information, see Manage RAM user permissions.
-
-
On the RAM Quick Authorization page of RAM, click Confirm.
-
-
Prepare a Terraform environment. You can use one of the following methods to run Terraform.
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online environment to run Terraform. You do not need to install Terraform. You can log on to use and try Terraform online. This method is suitable for scenarios where you want to try and debug Terraform quickly and conveniently at no cost.
Cloud Shell: Alibaba Cloud Cloud Shell has Terraform components pre-installed and identity credentials configured. You can run Terraform commands directly in Cloud Shell. This method is suitable for scenarios where you want to access and use Terraform quickly and conveniently at a low cost.
Install and configure Terraform on your local machine: This method is suitable for scenarios with poor network connectivity or when you need a custom development environment.
Background information
Terraform is an open-source tool that uses providers to support new infrastructure. You can use Terraform to securely and efficiently preview, provision, and manage cloud infrastructure and resources. For more information, see What is Terraform?.
In earlier versions of the Alibaba Cloud Provider, ACK provided an add-on named alicloud_cs_kubernetes_autoscaler. This add-on supports node scaling, but has the following limitations:
-
The configuration is complex and the operational overhead is high.
-
Scaled nodes are added to the default node pool, and auto-scaled nodes cannot be managed separately.
-
Some configuration parameters cannot be changed.
Starting from version 1.111.0, the Alibaba Cloud Provider lets you create a node pool with auto scaling enabled by using the alicloud_cs_kubernetes_node_pool resource. This method has the following benefits:
-
Simple configuration. You only need to specify the minimum and maximum number of nodes in the scaling group.
-
For optional settings, such as the operating system image, ACK uses safe default values to prevent infrastructure inconsistencies caused by misoperations.
-
You can easily track node changes in the node pool from the ACK console.
Terraform resources
Release the resources when you no longer need them to avoid incurring charges.
-
alicloud_instance_types: queries ECS instance types that meet specified conditions.
-
alicloud_vpc: creates a Virtual Private Cloud (VPC).
-
alicloud_vswitch: creates a vSwitch to define one or more subnets for a VPC.
-
alicloud_cs_managed_kubernetes: creates an ACK managed cluster.
-
alicloud_cs_kubernetes_node_pool: creates a node pool for an ACK managed cluster.
Generate Terraform request parameters in the console
To ensure correct parameter combinations or to create a configuration not covered in the examples, you can generate the Terraform request parameters for creating a node pool in the ACK console.
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
In the Create Node Pool panel, configure the node pool parameters, click Confirm, and then click Console-to-Code in the lower-left corner.
-
In the side panel, click the Terraform tab. The code block on this tab shows the parameter combination required to create the node pool. Click the copy icon
in the upper-right corner of the code block to copy the code.
Create an auto-scaling node pool
With the alicloud_cs_kubernetes_autoscaler add-on
If your cluster previously used the alicloud_cs_kubernetes_autoscaler add-on, follow these steps to smoothly migrate. This process allows you to use the alicloud_cs_kubernetes_node_pool resource to create a node pool with auto scaling enabled.
-
Modify the autoscaler-meta ConfigMap of the cluster.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
In the upper-left corner of the ConfigMaps page, select kube-system from the Namespace drop-down list. Then, find the autoscaler-meta ConfigMap and click Edit in the Actions column.
-
In the Edit panel, modify the value of the autoscaler-meta ConfigMap.
Change the type of the
taintsvalue from a string to an array. In the Value text box, change"taints":""to"taints":[]. -
Click OK.
-
Synchronize the node pool.
In the left navigation pane of the cluster management page, choose .
-
In the upper-right corner of the Node Pools page, click Sync Node Pool.
Without the alicloud_cs_kubernetes_autoscaler add-on
-
Create a configuration file for the node pool.
Existing cluster
The following example shows how to create a node pool with auto scaling enabled in an existing cluster.
provider "alicloud" { } # Create a node pool with auto scaling enabled for an existing cluster. resource "alicloud_cs_kubernetes_node_pool" "at1" { # The ID of the target cluster. cluster_id = "" name = "np-test" # The vSwitches used by nodes in the node pool. You must specify at least one vSwitch ID. vswitch_ids = ["vsw-bp1mdigyhmilu2h4v****"] instance_types = ["ecs.e3.medium"] password = "Hello1234" scaling_config { # The minimum number of nodes. min_size = 1 # The maximum number of nodes. max_size = 5 } }New cluster
The following example shows how to create a cluster that includes a node pool with auto scaling enabled.
provider "alicloud" { region = var.region_id } variable "region_id" { type = string default = "cn-shenzhen" } variable "cluster_spec" { type = string description = "The specifications of the ACK cluster. Valid values: ack.standard (Standard managed cluster) and ack.pro.small (Professional managed cluster). If you leave this parameter empty, a standard managed cluster is created." default = "ack.pro.small" } # The availability zones of the vSwitches. variable "availability_zone" { description = "The availability zones of vSwitches." default = ["cn-shenzhen-c", "cn-shenzhen-e", "cn-shenzhen-f"] } # The list of CIDR blocks for the new vSwitches. variable "node_vswitch_cidrs" { type = list(string) default = ["172.16.0.0/23", "172.16.2.0/23", "172.16.4.0/23"] } # The CIDR block of the vSwitch used by Terway. variable "terway_vswitch_cidrs" { type = list(string) default = ["172.16.208.0/20", "172.16.224.0/20", "172.16.240.0/20"] } # The ECS instance types used to launch worker nodes. variable "worker_instance_types" { description = "The ECS instance types used to launch worker nodes." default = ["ecs.g6.2xlarge", "ecs.g6.xlarge"] } # The password of the worker nodes. variable "password" { description = "The password of the ECS instances." default = "Test123456" } # The name prefix of the ACK managed cluster. variable "k8s_name_prefix" { description = "The name prefix used to create the managed Kubernetes cluster." default = "tf-ack-shenzhen" } # The add-ons to install in the ACK cluster. The add-ons include Terway (network), csi-plugin (storage), csi-provisioner (storage), logtail-ds (log), Nginx Ingress Controller, ack-arms-prometheus (monitoring), and ack-node-problem-detector (node diagnostics). variable "cluster_addons" { type = list(object({ name = string config = string })) default = [ { "name" = "terway-eniip", "config" = "", }, { "name" = "logtail-ds", "config" = "{\"IngressDashboardEnabled\":\"true\"}", }, { "name" = "nginx-ingress-controller", "config" = "{\"IngressSlbNetworkType\":\"internet\"}", }, { "name" = "arms-prometheus", "config" = "", }, { "name" = "ack-node-problem-detector", "config" = "{\"sls_project_name\":\"\"}", }, { "name" = "csi-plugin", "config" = "", }, { "name" = "csi-provisioner", "config" = "", } ] } # The default resource names. locals { k8s_name_terway = "k8s_name_terway_${random_integer.default.result}" vpc_name = "vpc_name_${random_integer.default.result}" autoscale_nodepool_name = "autoscale-node-pool-${random_integer.default.result}" } # Query for available ECS instance types based on CPU and memory. data "alicloud_instance_types" "default" { cpu_core_count = 8 memory_size = 32 availability_zone = var.availability_zone[0] kubernetes_node_role = "Worker" } resource "random_integer" "default" { min = 10000 max = 99999 } # The VPC. resource "alicloud_vpc" "default" { vpc_name = local.vpc_name cidr_block = "172.16.0.0/12" } # The node vSwitches. resource "alicloud_vswitch" "vswitches" { count = length(var.node_vswitch_cidrs) vpc_id = alicloud_vpc.default.id cidr_block = element(var.node_vswitch_cidrs, count.index) zone_id = element(var.availability_zone, count.index) } # The pod vSwitches. resource "alicloud_vswitch" "terway_vswitches" { count = length(var.terway_vswitch_cidrs) vpc_id = alicloud_vpc.default.id cidr_block = element(var.terway_vswitch_cidrs, count.index) zone_id = element(var.availability_zone, count.index) } # The ACK managed cluster. resource "alicloud_cs_managed_kubernetes" "default" { name = local.k8s_name_terway # The name of the Kubernetes cluster. cluster_spec = var.cluster_spec # Creates an ACK Pro cluster. worker_vswitch_ids = split(",", join(",", alicloud_vswitch.vswitches.*.id)) # The vSwitches of the node pool. Specify the IDs of one or more vSwitches. The vSwitches must be in the availability zones specified by availability_zone. pod_vswitch_ids = split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # The pod vSwitches. new_nat_gateway = true # Specifies whether to create a new NAT Gateway when you create the Kubernetes cluster. Default value: true. service_cidr = "10.11.0.0/16" # The CIDR block of the pod network. This parameter is required if cluster_network_type is set to flannel. The CIDR block cannot be the same as the VPC CIDR block or overlap with the CIDR blocks of other Kubernetes clusters in the VPC. The CIDR block cannot be modified after the cluster is created. The maximum number of nodes in the cluster is 256. slb_internet_enabled = true # Specifies whether to create an Internet-facing load balancer for the API server. Default value: false. enable_rrsa = true control_plane_log_components = ["apiserver", "kcm", "scheduler", "ccm"] # The control plane logs. dynamic "addons" { # The add-on management. for_each = var.cluster_addons content { name = lookup(addons.value, "name", var.cluster_addons) config = lookup(addons.value, "config", var.cluster_addons) } } } # Creates a node pool with auto scaling enabled. The node pool can scale out to a maximum of 10 nodes and a minimum of 1 node. resource "alicloud_cs_kubernetes_node_pool" "autoscale_node_pool" { cluster_id = alicloud_cs_managed_kubernetes.default.id node_pool_name = local.autoscale_nodepool_name vswitch_ids = split(",", join(",", alicloud_vswitch.vswitches.*.id)) scaling_config { min_size = 1 max_size = 10 } instance_types = var.worker_instance_types password = var.password # The password for SSH logon to the cluster nodes. install_cloud_monitor = true # Specifies whether to install CloudMonitor on the Kubernetes nodes. system_disk_category = "cloud_efficiency" system_disk_size = 100 image_type = "AliyunLinux3" data_disks { # The configurations of node data disks. category = "cloud_essd" # The category of the node data disk. size = 120 # The size of the node data disk. } } -
Run the following command to initialize the Terraform runtime environment:
terraform initA successful initialization returns the following output:
Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. -
Run the
terraform applycommand to create the resources. -
Verify the result.
After the node pool is created, you can find it in the node pool list. The Auto Scaling Enabled label is displayed under the node pool name. On the Node Pools page, you can confirm that auto scaling is successfully enabled when you see the Auto Scaling Enabled label under a node pool name (for example, np-test).
Clean up resources
When you no longer need the resources that were created or managed by using Terraform, run the terraform destroy command to release the resources. For more information about terraform destroy, see Common Commands.
terraform destroy
Complete example
You can run the sample code in this topic with a single click.Run with one click
Sample code
To try more complete examples, see the product-specific folders in the More examples on GitHub repository.