本教程介绍如何通过Terraform创建一个基于Master/Slave主从模式的分布式集群,并向集群中部署更新文件。
前提条件
在开始之前,请您确保完成以下操作:- 使用Terraform,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
- 已经安装并配置了Terraform,具体操作请参见在本地安装和配置Terraform和在Cloud Shell中使用Terraform。
操作步骤
- 编写Terraform脚本代码。本教程以Cloud Shell中运行terraform为例。
- 运行terraform init初始化。
terraform init
命令输出结果类似如下:
Initializing modules... Downloading Starnop/app-deployer/alicloud 0.0.1 for app-deployer... - app-deployer in .terraform/modules/app-deployer/Starnop-terraform-alicloud-app-deployer-8434639 Downloading alibaba/vpc/alicloud 1.4.2 for vpc... - vpc in .terraform/modules/vpc/terraform-alicloud-modules-terraform-alicloud-vpc-7e25cee Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "null" (hashicorp/null) 2.1.2... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.68.0... - Downloading plugin for provider "local" (hashicorp/local) 1.4.0... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.local: version = "~> 1.4" * provider.null: version = "~> 2.1" Terraform has been successfully initialized! 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 apply开始创建。
terraform apply
命令输出结果类似如下:
module.app-deployer.data.alicloud_zones.zones_ds: Refreshing state... module.vpc.data.alicloud_zones.default: Refreshing state... module.vpc.data.alicloud_vpcs.this: Refreshing state... module.vpc.data.alicloud_route_tables.this: Refreshing state... module.app-deployer.data.alicloud_images.images["worker"]: Refreshing state... module.app-deployer.data.alicloud_instance_types.ecs_type["master"]: Refreshing state... module.app-deployer.data.alicloud_instance_types.ecs_type["worker"]: Refreshing state... module.app-deployer.data.alicloud_images.images["master"]: Refreshing state... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: Terraform will perform the following actions: ... Plan: 23 to add, 0 to change, 0 to destroy. 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 module.vpc.alicloud_vpc.vpc[0]: Creating... ... Apply complete! Resources: 23 added, 0 changed, 0 destroyed. Outputs: instances = { "master_0" = { "availability_zone" = "cn-chengdu-a" "credit_specification" = "" "data_disks" = [] "deletion_protection" = false "description" = "OWNER: default\nmaster node" "dry_run" = false "host_name" = "master0" ... } "master_1" = { "availability_zone" = "cn-chengdu-a" "credit_specification" = "" "data_disks" = [] "deletion_protection" = false "description" = "OWNER: default\nmaster node" "dry_run" = false "host_name" = "master1" ... } "worker_0" = { "availability_zone" = "cn-chengdu-a" "credit_specification" = "" "data_disks" = [] "deletion_protection" = false "description" = "OWNER: default\nworker node" "dry_run" = false "host_name" = "worker0" ... } "worker_1" = { "availability_zone" = "cn-chengdu-a" "credit_specification" = "" "data_disks" = [] "deletion_protection" = false "description" = "OWNER: default\nworker node" "dry_run" = false "host_name" = "worker1" ... } "worker_2" = { "availability_zone" = "cn-chengdu-a" "credit_specification" = "" "data_disks" = [] "deletion_protection" = false "description" = "OWNER: default\nworker node" "dry_run" = false "host_name" = "worker2" ... } }
脚本执行完后,可登录服务器查看,部署的文件在服务器/tmp/example路径下。