Terraform是HashiCorp公司提供的一种开源工具,用于安全高效地预览,配置和管理云基础架构和资源,帮助开发者自动化地创建、更新阿里云基础设施资源,并进行版本管理。本文介绍如何使用Terraform创建ACK托管版集群。
前提条件
- 安装Terraform。
说明 请确认Terraform版本不低于v0.12.28,可通过terraform --version命令查看Terraform版本。
- Cloud Shell默认安装配置了Terraform和阿里云账号信息,无需任何额外配置。
- 如果您不使用Cloud Shell,关于安装Terraform的方式,请参见在本地安装和配置Terraform。
- 配置阿里云账号信息。
- 创建环境变量,用于存放身份认证信息。
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"
- 通过在配置文件的
provider
代码块中指定身份认证信息。provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }
- 创建环境变量,用于存放身份认证信息。
- 展开查看本文用到的variable.tf文件
variable "availability_zone" { description = "The availability zones of vswitches." default = ["cn-shenzhen-d", "cn-shenzhen-e", "cn-shenzhen-f"] } variable "node_vswitch_ids" { description = "List of existing node vswitch ids for terway." type = list(string) default = [] } variable "node_vswitch_cirds" { description = "List of cidr blocks used to create several new vswitches when 'node_vswitch_ids' is not specified." type = list(string) default = ["172.16.0.0/23", "172.16.2.0/23", "172.16.4.0/23"] } variable "terway_vswitch_ids" { description = "List of existing pod vswitch ids for terway." type = list(string) default = [] } variable "terway_vswitch_cirds" { description = "List of cidr blocks used to create several new vswitches when 'terway_vswitch_ids' is not specified." type = list(string) default = ["172.16.208.0/20", "172.16.224.0/20", "172.16.240.0/20"] } # Node Pool worker_instance_types variable "worker_instance_types" { description = "The ecs instance types used to launch worker nodes." default = ["ecs.g6.2xlarge", "ecs.g6.xlarge"] } # Password for Worker nodes variable "password" { description = "The password of ECS instance." default = "yjh@ACK123" } # Cluster Addons 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" = "", "disabled": false, }, { "name" = "ack-node-problem-detector", "config" = "{\"sls_project_name\":\"\"}", "disabled": false, }, { "name" = "csi-plugin", "config" = "", }, { "name" = "csi-provisioner", "config" = "", } ] } # Cluster Addons for Flannel variable "cluster_addons_flannel" { type = list(object({ name = string config = string })) default = [ { "name" = "flannel", "config" = "", }, { "name" = "logtail-ds", "config" = "{\"IngressDashboardEnabled\":\"true\"}", }, { "name" = "nginx-ingress-controller", "config" = "{\"IngressSlbNetworkType\":\"internet\"}", }, { "name" = "arms-prometheus", "config" = "", "disabled": false, }, { "name" = "ack-node-problem-detector", "config" = "{\"sls_project_name\":\"\"}", "disabled": false, }, { "name" = "csi-plugin", "config" = "", }, { "name" = "csi-provisioner", "config" = "", } ] }
使用Terraform创建ACK托管版集群(Flannel)
使用Terraform创建ACK托管版集群(Terway)
使用Terraform删除ACK托管版集群
您可以执行以下命令删除通过Terraform创建的集群。
terraform destroy
预期输出:...
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.