CodeSample小助手 2021-12-01
HashiCorp Terraform是一个IT基础架构自动化编排工具,可以用代码来管理维护IT资源。Terraform的命令行接口提供了一种简单机制,用于将配置文件部署到阿里云或其他任意支持的云上,并对其进行版本控制。它编写了描述云资源拓扑的配置文件中的基础结构,例如虚拟机、存储账户和网络接口。
Terraform适用于多云方案,将相类似的基础结构部署到阿里云、其他云提供商或者本地数据中心。开发人员能够使用相同的工具和相似的配置文件同时管理不同云提供商的资源。
Terraform能够创建配置文件的模板,以可重复、可预测的方式定义、预配和配置ECS资源,减少因人为因素导致的部署和管理错误。能够多次部署同一模板,创建相同的开发、测试和生产环境。
可以用代码来管理维护资源。允许保存基础设施状态,从而使您能够跟踪对系统(基础设施即代码)中不同组件所做的更改,并与其他人共享这些配置。
您通过按需创建开发和部署环境来降低成本。并且,您可以在系统更改之前进行评估。
通过Terraform自动搭建经典负载均衡架构的示意图如下:
通过以下云资源的配置信息搭建经典架构:
资源名称 | 说明 |
---|---|
alicloud_instance | 创建ECS实例。 |
alicloud_vpc | 新建专有网络。 |
alicloud_vswitch | 新建交换机。 |
alicloud_slb | 创建LoadBalancer。 |
alicloud_slb_listener | 创建负载均衡监听。 |
alicloud_slb_attachment | 挂载ECS实例。 |
alicloud_security_group | 创建安全组。 |
alicloud_security_group_rule | 创建安全组规则。 |
alicloud_db_instance | 创建数据库实例。 |
alicloud_db_database | 创建数据库。 |
alicloud_db_account | 创建数据库账号。 |
alicloud_oss_bucket | 创建OSS bucket。 |
以下示例定义了一个用于一键部署经典负载均衡架构的Terraform模板,Terraform将基于该模板首先创建出资源所需要的网络环境VPC、vSwitch、SLB以及安全组,然后在该网络环境中创建ECS,RDS等计算资源,最后在SLB中创建监听并将ECS实例挂载到SLB上,成为其后端服务器。
// Images data source to get image_id
data "alicloud_images" "default" {
most_recent = true
owners = "system"
name_regex = "${var.image_name_regex}"
}
// Instance_types data source to get instance_type
data "alicloud_instance_types" "default" {
cpu_core_count = "${var.cpu_core_count}"
memory_size = "${var.memory_size}"
}
// Zones data source to get availability_zone
data "alicloud_zones" "default" {
available_instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
available_resource_creation = "Rds"
}
// If you do not specify vpc_id, the module will create a new VPC
resource "alicloud_vpc" "vpc" {
count = "${var.vpc_id == "" ? 1 : 0}"
cidr_block = "${var.vpc_cidr}"
name = "${var.vpc_name == "" ? var.resource_group_name : var.vpc_name}"
}
// Create vSwitches
resource "alicloud_vswitch" "vswitches" {
count = "${length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)}"
vpc_id = "${var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id}"
cidr_block = "${element(var.vswitch_cidrs, count.index)}"
availability_zone = "${lookup(data.alicloud_zones.default.zones[count.index], "id")}"
name = "${var.vswitch_name_prefix == "" ? format("%s-%s", var.resource_group_name, format(var.number_format, count.index+1)) : format("%s-%s", var.vswitch_name_prefix, format(var.number_format, count.index+1))}"
}
// Security Group Resource for Module
resource "alicloud_security_group" "default" {
vpc_id = "${var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id}"
name = "${var.group_name == "" ? var.resource_group_name : var.group_name}"
}
// ECS Instance Resource for Web Tier
resource "alicloud_instance" "web" {
count = "${var.number_of_web_instances}"
image_id = "${var.image_id == "" ? data.alicloud_images.default.images.0.id : var.image_id }"
instance_type = "${var.instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.instance_type}"
security_groups = ["${ alicloud_security_group.default.id }"]
instance_name = "${var.number_of_web_instances < 2 ? var.web_instance_name : format("%s-%s", var.web_instance_name, format(var.number_format, count.index+1))}"
host_name = "${var.number_of_web_instances < 2 ? var.web_host_name : format("%s-%s", var.web_host_name, format(var.number_format, count.index+1))}"
internet_charge_type = "${var.internet_charge_type}"
internet_max_bandwidth_out = "${var.internet_max_bandwidth_out}"
instance_charge_type = "${var.instance_charge_type}"
system_disk_category = "${var.system_category}"
system_disk_size = "${var.system_size}"
password = "${var.ecs_password}"
vswitch_id = "${length(var.vswitch_ids) > 0 ? element(split(",", join(",", var.vswitch_ids)), count.index%length(split(",", join(",", var.vswitch_ids)))) : length(var.vswitch_cidrs) < 1 ? "" : element(split(",", join(",", alicloud_vswitch.vswitches.*.id)), count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id))))}"
period = "${var.period}"
period_unit = "${var.period_unit}"
}
// ECS Instance Resource for app Tier
resource "alicloud_instance" "app" {
count = "${var.number_of_app_instances}"
image_id = "${var.image_id == "" ? data.alicloud_images.default.images.0.id : var.image_id }"
instance_type = "${var.instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.instance_type}"
security_groups = ["${alicloud_security_group.default.id}"]
instance_name = "${var.number_of_app_instances < 2 ? var.app_instance_name : format("%s-%s", var.app_instance_name, format(var.number_format, count.index+1))}"
host_name = "${var.number_of_app_instances < 2 ? var.app_host_name : format("%s-%s", var.app_host_name, format(var.number_format, count.index+1))}"
internet_charge_type = "${var.internet_charge_type}"
internet_max_bandwidth_out = "${var.internet_max_bandwidth_out}"
instance_charge_type = "${var.instance_charge_type}"
system_disk_category = "${var.system_category}"
system_disk_size = "${var.system_size}"
password = "${var.ecs_password}"
vswitch_id = "${length(var.vswitch_ids) > 0 ? element(split(",", join(",", var.vswitch_ids)), count.index%length(split(",", join(",", var.vswitch_ids)))) : length(var.vswitch_cidrs) < 1 ? "" : element(split(",", join(",", alicloud_vswitch.vswitches.*.id)), count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id))))}"
period = "${var.period}"
period_unit = "${var.period_unit}"
}
// SLB Instance Resource for intranet
resource "alicloud_slb" "intranet" {
internet = false
name = "${var.slb_intranet_name == "" ? var.resource_group_name : var.slb_intranet_name}"
}
resource "alicloud_slb_attachment" "intranet" {
load_balancer_id = "${alicloud_slb.intranet.id}"
instance_ids = ["${alicloud_instance.web.*.id}", "${alicloud_instance.app.*.id}"]
}
// SLB Instance Resource for internet
resource "alicloud_slb" "internet" {
internet = true
bandwidth = "${var.slb_max_bandwidth}"
name = "${var.slb_internet_name == "" ? var.resource_group_name : var.slb_internet_name}"
}
resource "alicloud_slb_attachment" "internet" {
load_balancer_id = "${alicloud_slb.internet.id}"
instance_ids = ["${alicloud_instance.web.*.id}"]
}
// RDS Resource
resource "alicloud_db_instance" "default" {
count = "${var.number_of_rds_instances}"
instance_name = "${var.number_of_rds_instances < 2 ? var.rds_name_prefix : format("%s-%s", var.rds_name_prefix, format(var.number_format, count.index+1))}"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_type = "${var.db_instance_type}"
instance_storage = "${var.storage}"
vswitch_id = "${length(var.vswitch_ids) > 0 ? element(split(",", join(",", var.vswitch_ids)), count.index%length(split(",", join(",", var.vswitch_ids)))) : length(var.vswitch_cidrs) < 1 ? "" : element(split(",", join(",", alicloud_vswitch.vswitches.*.id)), count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id))))}"
security_ips = ["${alicloud_instance.app.*.private_ip}"]
}
resource "alicloud_db_account" "default" {
count = "${var.number_of_rds_instances}"
instance_id = "${element(alicloud_db_instance.default.*.id, count.index)}"
name = "${var.rds_account_name_prefix}${count.index}"
password = "${var.rds_account_password}"
}
resource "alicloud_db_database" "default" {
count = "${var.number_of_rds_instances}"
instance_id = "${element(alicloud_db_instance.default.*.id, count.index)}"
name = "${var.rds_database_name_prefix}_${count.index}"
}
// OSS Resource
resource "alicloud_oss_bucket" "default" {
bucket = "${var.bucket_name == "" ? var.resource_group_name : var.bucket_name}"
acl = "${var.bucket_acl}"
}
参考以下步骤,运行该模板:
您可以登录访问控制管理控制台创建并查看AccessKey,或者联系系统管理员获取AccessKey。
$ export ALICLOUD_ACCESS_KEY=XXXXX
$ export ALICLOUD_SECRET_KEY=XXXXX
$ export ALICLOUD_REGION=regionID
$ git clone https://github.com/terraform-alicloud-modules/terraform-alicloud-classic-load-balance.git
$ cd terraform-alicloud-classic-load-balance
$ terraform plan
$ terraform apply
$ terraform destroy