This topic describes how to use Terraform to create an ACK managed cluster.
Prerequisites
Terraform is installed.
NoteYou must install Terraform 0.12.28 or later. You can run the
terraform --version
command to query the Terraform version.When you install Terraform, take note of the following items:
If you want to use Terraform in Cloud Shell, no additional configuration is required because Cloud Shell is preinstalled with Terraform and configured with your account information by default. For more information about how to use Terraform in Cloud Shell, see Use Terraform in Cloud Shell.
If you want to use Terraform on your on-premises machine, refer to Install and configure Terraform on your on-premises machine.
Your account information is configured.
Run the following commands to create environment variables to store identity information.
The identity information of an Alibaba Cloud account is sensitive data because an Alibaba Cloud account has full permissions on the resources that belong to the account. 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 a RAM user.
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.
Container Service for Kubernetes (ACK) is activated. If you want to use Terraform to activate ACK, refer to Activate ACK and assign default roles to ACK.
Create a working directory and create a configuration file named
variable.tf
in the directory.In Terraform, you can define input variables in the variable.tf file and reuse these variables in other configuration files. In subsequent steps, the preceding variables are added to the main.tf file to configure Terraform resources.
Create a virtual private cloud (VPC), three vSwitches in the VPC, and three pod vSwitches.
When you create an ACK managed cluster, the following add-ons are installed in the cluster by default: Terway (network add-on), csi-plugin (volume add-on), csi-provisioner (volume add-on), logtail-ds (logging add-on), Nginx Ingress controller, ack-arms-prometheus (monitoring add-on), and ack-node-problem-detector (node diagnostics add-on).
Use Terraform to create an ACK managed cluster (Terway)
The working directory and the configuration file named main.tf described in Prerequisites are created.
NoteThe main.tf file is used to create and configure ACK cluster resources on Alibaba Cloud. During the resource creation process, the
main.tf
file references variables defined in thevariables.tf
file.The main.tf file is used to configure the following settings for Terraform:
Create a VPC, three vSwitches in the VPC, and three pod vSwitches.
Create an ACK managed cluster.
Create a managed node pool that consists of two nodes.
Create a managed node pool.
For more information about the parameters for creating an ACK managed cluster, see alicloud_cs_managed_kubernetes.
Run the following command to initialize the environment for Terraform:
terraform init
If the following information is returned, Terraform is initialized:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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 following command to create an execution plan:
terraform plan
If the following information is returned, the execution plan is created:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 12 to add, 0 to change, 0 to destroy. ...
Run the following command to create the cluster:
terraform apply
When the following information is returned, input
yes
and press Enter. Then, the cluster is created.... Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes ... alicloud_cs_managed_kubernetes.default: Creation complete after 8m26s [id=************] Apply complete! Resources: 12 added, 0 changed, 0 destroyed.