Terraform是HashiCorp公司提供的一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源,帮助开发者自动化地创建、更新阿里云基础设施资源,并进行版本管理。本文介绍如何使用Terraform创建ACK专有版集群。
前提条件
已安装Terraform。
说明请确认Terraform版本不低于v0.12.28,可通过terraform --version命令查看Terraform版本。
Cloud Shell默认安装配置了Terraform和阿里云账号信息,无需任何额外配置。
如果您不使用Cloud Shell,关于安装Terraform的方式,请参见在本地安装和配置Terraform。
配置阿里云账号信息。
执行如下命令,创建环境变量,用于存放身份认证信息。
Linux 环境
export ALICLOUD_ACCESS_KEY="************" #替换为阿里云账号的AK信息。 export ALICLOUD_SECRET_KEY="************" #替换为阿里云账号的SK信息。 export ALICLOUD_REGION="cn-beijing" #替换为您集群所在的地域。
Windows 环境
set ALICLOUD_ACCESS_KEY="************" #替换为阿里云账号的AK信息。 set ALICLOUD_SECRET_KEY="************" #替换为阿里云账号的SK信息。 set ALICLOUD_REGION="cn-beijing" #替换为您集群所在的地域。
已开通容器服务ACK。
说明若您需要使用Terraform开通,请参考使用Terraform开通容器服务ACK。
创建工作目录,并且在工作目录中创建
variable.tf
配置文件,
alicloud_cs_kubernetes
使用Terraform创建ACK专有版集群(Terway)
在前提条件中创建的工作目录中创建以下名为main.tf的配置文件。
main.tf配置文件描述了以下的Terraform配置:
创建1个新的VPC,并创建3个该VPC下的vSwitch、3个Pod vSwitch。
创建1个专有版ACK专有版集群。
创建1个包含2个节点的节点池。
创建1个自动伸缩节点池。
执行以下命令,初始化Terraform运行环境。
terraform init
返回信息如下,Terraform初始化成功。
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.
执行以下命令,生成资源规划。
terraform plan
返回信息如下,资源规划生成成功。
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. ...
执行以下命令,创建集群。
terraform apply
返回信息如下,输入
yes
,按Enter键,集群创建成功。... 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: 7 added, 0 changed, 1 destroyed.
使用Terraform删除ACK专有版集群
您可以执行以下命令,删除通过Terraform创建的集群。
terraform destroy
返回信息如下,输入yes,按Enter键,集群删除成功。
...
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: 7 destroyed.
- 本页导读