Terraform is an open source tool provided by HashiCorp. Terraform allows you to securely and efficiently preview, configure, and manage cloud infrastructure and resources. You can use Terraform to automatically create and update infrastructure and resources on Alibaba Cloud, and manage versions based on your requirements. This topic describes how to use Terraform to create a Container Service for Kubernetes (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.
By default, Cloud Shell has preinstalled Terraform and configured your account information. 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.
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.
NoteTo 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.
ACK is activated.
NoteFor more information about how to use Terraform to activate ACK, see Use Terraform to assign default roles to ACK when you use ACK for the first time.
Create a working directory and a file named
variable.tf
in the directory.
Use Terraform to create an ACK managed cluster when Flannel is selected
Create a file named main.tf in the working directory that you created in the Prerequisites section.
The main.tf file is used to configure the following settings for Terraform:
Create a virtual private cloud (VPC) and create a vSwitch in the VPC.
Create an ACK managed cluster.
Create a node pool that contains two nodes.
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: 7 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.flannel: Creation complete after 8m26s [id=************] Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Use Terraform to create an ACK managed cluster when Terway is selected
Create a file named main.tf in the working directory that you created in the Prerequisites section.
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 node pool that contains two nodes.
Create a node pool that has auto scaling enabled.
Create a managed node pool.
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.
Use Terraform to delete an ACK managed cluster
You can run the following command to delete an ACK managed cluster that is created by using Terraform:
terraform destroy
When the following information is returned, input yes
and press Enter. Then, the cluster is deleted.
...
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
...
Destroy complete! Resources: 5 destroyed.