Terraform is an open source tool provided by HashiCorp. Terraform allows you to securely and efficiently preview, configure, and manage cloud infrastructures and resources. You can use Terraform to automatically create and update Alibaba Cloud infrastructures and resources, 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 is preinstalled with Terraform and configured with your account information. You do not need to modify the configurations.
If you do not use Cloud Shell, you can directly install Terraform. For more information, see Install and configure Terraform in the local PC.
Your account information is configured. You can specify identity information in environment variables.
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"
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 the RAM user.
Cskpro container service is activated.
Use Terraform to create an ACK managed cluster when Flannel is selected
Create a working directory and a file named main.tf in the directory.
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
Expected output:
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
Expected output:
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: 5 to add, 0 to change, 0 to destroy. ...
Run the following command to create the cluster:
terraform apply
Expected output:
... 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: 5 added, 0 changed, 0 destroyed.
Use Terraform to create an ACK managed cluster when Terway is selected
Create a working directory and a file named main.tf in the directory.
The main.tf file is used to configure the following settings for Terraform:
Create a VPC and create two vSwitches in the VPC.
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
Expected output:
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
Expected output:
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: 8 to add, 0 to change, 0 to destroy. ...
Run the following command to create the resources:
terraform apply
Expected output:
... 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: 8 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
Expected output:
...
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.