Skip to content

Commit 6fae1c3

Browse files
Atmosphere10shanye997
authored andcommitted
feat: 解决方案elastic-application-with-ess tf文件完成
1 parent 60278b3 commit 6fae1c3

File tree

5 files changed

+334
-0
lines changed

5 files changed

+334
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[自动弹性,稳定交付](https://www.aliyun.com/solution/tech-solution/improve-app-availability), 涉及到专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、应用型负载均衡器(ALB) 、弹性伸缩(ESS)等资源的部署。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example is used to implement solution [Auto Scaling and Stable Delivery](https://www.aliyun.com/solution/tech-solution/improve-app-availability), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), VSwitch, Elastic Compute Service (ECS), Application Load Balancer (ALB), and Elastic Scaling Service (ESS).
8+
<!-- DOCS_DESCRIPTION_EN -->
9+
10+
<!-- BEGIN_TF_DOCS -->
11+
## Providers
12+
13+
| Name | Version |
14+
|------|---------|
15+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
16+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
17+
18+
## Modules
19+
20+
No modules.
21+
22+
## Resources
23+
24+
| Name | Type |
25+
|------|------|
26+
| [alicloud_alb_listener.alb_listener](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/alb_listener) | resource |
27+
| [alicloud_alb_load_balancer.alb](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/alb_load_balancer) | resource |
28+
| [alicloud_alb_server_group.alb_server_group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/alb_server_group) | resource |
29+
| [alicloud_ess_scaling_configuration.ess_scaling_configuration](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scaling_configuration) | resource |
30+
| [alicloud_ess_scaling_group.ess_scaling_group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scaling_group) | resource |
31+
| [alicloud_ess_scaling_rule.ess_scale_down_rule](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scaling_rule) | resource |
32+
| [alicloud_ess_scaling_rule.ess_scale_up_rule](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scaling_rule) | resource |
33+
| [alicloud_ess_scheduled_task.scheduled_scale_down_task](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scheduled_task) | resource |
34+
| [alicloud_ess_scheduled_task.scheduled_scale_up_task](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_scheduled_task) | resource |
35+
| [alicloud_ess_server_group_attachment.ess_server_group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ess_server_group_attachment) | resource |
36+
| [alicloud_security_group.security_group](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
37+
| [alicloud_security_group_rule.security_group_http](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
38+
| [alicloud_security_group_rule.security_group_https](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
39+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
40+
| [alicloud_vswitch.alb_vswitch_3](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
41+
| [alicloud_vswitch.alb_vswitch_4](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
42+
| [alicloud_vswitch.ecs_vswitch_1](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
43+
| [alicloud_vswitch.ecs_vswitch_2](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
44+
| [random_integer.default](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer) | resource |
45+
| [alicloud_alb_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/alb_zones) | data source |
46+
| [alicloud_images.instance_image](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/images) | data source |
47+
| [alicloud_instance_types.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/instance_types) | data source |
48+
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |
49+
50+
## Inputs
51+
52+
| Name | Description | Type | Default | Required |
53+
|------|-------------|------|---------|:--------:|
54+
| <a name="input_common_name"></a> [common\_name](#input\_common\_name) | 弹性应用名称 | `string` | `"elastic-app"` | no |
55+
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/中的特殊符号)` | `string` | n/a | yes |
56+
| <a name="input_region"></a> [region](#input\_region) | 地域 | `string` | `"cn-hangzhou"` | no |
57+
| <a name="input_scale_down_time"></a> [scale\_down\_time](#input\_scale\_down\_time) | 自动缩容执行时间 | `string` | `null` | no |
58+
| <a name="input_scale_up_time"></a> [scale\_up\_time](#input\_scale\_up\_time) | 自动扩容执行时间 | `string` | `null` | no |
59+
<!-- END_TF_DOCS -->
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
data "alicloud_instance_types" "default" {
2+
instance_type_family = "ecs.g7"
3+
}
4+
5+
data "alicloud_zones" "default" {
6+
available_instance_type = data.alicloud_instance_types.default.ids.0
7+
}
8+
9+
data "alicloud_alb_zones" "default" {}
10+
11+
data "alicloud_images" "instance_image" {
12+
name_regex = "^aliyun_3_9_x64_20G_*"
13+
most_recent = true
14+
owners = "system"
15+
}
16+
17+
# 创建VPC
18+
resource "alicloud_vpc" "vpc" {
19+
vpc_name = var.common_name
20+
cidr_block = "192.168.0.0/16"
21+
}
22+
23+
# 创建VSwitch1
24+
resource "alicloud_vswitch" "ecs_vswitch_1" {
25+
vpc_id = alicloud_vpc.vpc.id
26+
cidr_block = "192.168.1.0/24"
27+
zone_id = data.alicloud_zones.default.ids.0
28+
vswitch_name = "${var.common_name}-vsw"
29+
}
30+
31+
# 创建VSwitch2
32+
resource "alicloud_vswitch" "ecs_vswitch_2" {
33+
vpc_id = alicloud_vpc.vpc.id
34+
cidr_block = "192.168.2.0/24"
35+
zone_id = data.alicloud_zones.default.ids.1
36+
vswitch_name = "${var.common_name}-vsw"
37+
}
38+
39+
# 创建VSwitch3
40+
resource "alicloud_vswitch" "alb_vswitch_3" {
41+
vpc_id = alicloud_vpc.vpc.id
42+
cidr_block = "192.168.3.0/24"
43+
zone_id = data.alicloud_alb_zones.default.ids.0
44+
vswitch_name = "${var.common_name}-vsw"
45+
}
46+
47+
# 创建VSwitch4
48+
resource "alicloud_vswitch" "alb_vswitch_4" {
49+
vpc_id = alicloud_vpc.vpc.id
50+
cidr_block = "192.168.4.0/24"
51+
zone_id = data.alicloud_alb_zones.default.ids.1
52+
vswitch_name = "${var.common_name}-vsw"
53+
}
54+
55+
# 创建安全组
56+
resource "alicloud_security_group" "security_group" {
57+
security_group_name = "${var.common_name}-sg"
58+
vpc_id = alicloud_vpc.vpc.id
59+
}
60+
61+
# 添加安全组规则 - HTTP
62+
resource "alicloud_security_group_rule" "security_group_http" {
63+
security_group_id = alicloud_security_group.security_group.id
64+
type = "ingress"
65+
ip_protocol = "tcp"
66+
port_range = "80/80"
67+
cidr_ip = "0.0.0.0/0"
68+
}
69+
70+
# 添加安全组规则 - HTTPS
71+
resource "alicloud_security_group_rule" "security_group_https" {
72+
security_group_id = alicloud_security_group.security_group.id
73+
type = "ingress"
74+
ip_protocol = "tcp"
75+
port_range = "443/443"
76+
cidr_ip = "0.0.0.0/0"
77+
}
78+
79+
# 创建ALB负载均衡器
80+
resource "alicloud_alb_load_balancer" "alb" {
81+
load_balancer_name = "${var.common_name}-alb"
82+
load_balancer_edition = "Basic"
83+
address_allocated_mode = "Fixed"
84+
vpc_id = alicloud_vpc.vpc.id
85+
address_type = "Internet"
86+
87+
load_balancer_billing_config {
88+
pay_type = "PayAsYouGo"
89+
}
90+
91+
zone_mappings {
92+
zone_id = data.alicloud_alb_zones.default.ids.0
93+
vswitch_id = alicloud_vswitch.alb_vswitch_3.id
94+
}
95+
96+
zone_mappings {
97+
zone_id = data.alicloud_alb_zones.default.ids.1
98+
vswitch_id = alicloud_vswitch.alb_vswitch_4.id
99+
}
100+
}
101+
102+
# 创建ALB服务器组
103+
resource "alicloud_alb_server_group" "alb_server_group" {
104+
server_group_name = "${var.common_name}-server-group"
105+
vpc_id = alicloud_vpc.vpc.id
106+
protocol = "HTTP"
107+
108+
health_check_config {
109+
health_check_enabled = true
110+
health_check_protocol = "HTTP"
111+
health_check_path = "/"
112+
health_check_codes = ["http_2xx", "http_3xx"]
113+
health_check_connect_port = 80
114+
}
115+
116+
sticky_session_config {
117+
sticky_session_enabled = false
118+
}
119+
120+
lifecycle {
121+
ignore_changes = [servers]
122+
}
123+
}
124+
125+
# 创建ALB监听器
126+
resource "alicloud_alb_listener" "alb_listener" {
127+
listener_protocol = "HTTP"
128+
listener_port = 80
129+
load_balancer_id = alicloud_alb_load_balancer.alb.id
130+
131+
default_actions {
132+
type = "ForwardGroup"
133+
forward_group_config {
134+
server_group_tuples {
135+
server_group_id = alicloud_alb_server_group.alb_server_group.id
136+
}
137+
}
138+
}
139+
}
140+
141+
resource "random_integer" "default" {
142+
min = 10000
143+
max = 99999
144+
}
145+
146+
# 创建ESS伸缩组,min_size设置为2,在伸缩组启用后,会自动创建两台ECS实例
147+
resource "alicloud_ess_scaling_group" "ess_scaling_group" {
148+
scaling_group_name = "${var.common_name}-scaling-group-${random_integer.default.result}"
149+
min_size = 2
150+
max_size = 5
151+
vswitch_ids = [alicloud_vswitch.ecs_vswitch_1.id, alicloud_vswitch.ecs_vswitch_2.id]
152+
removal_policies = ["NewestInstance"]
153+
default_cooldown = 300
154+
multi_az_policy = "COMPOSABLE"
155+
az_balance = true
156+
depends_on = [alicloud_security_group.security_group]
157+
}
158+
159+
# 配置ESS服务器组关联
160+
resource "alicloud_ess_server_group_attachment" "ess_server_group" {
161+
scaling_group_id = alicloud_ess_scaling_group.ess_scaling_group.id
162+
server_group_id = alicloud_alb_server_group.alb_server_group.id
163+
port = 80
164+
type = "ALB"
165+
weight = 100
166+
force_attach = true
167+
}
168+
169+
locals {
170+
instance_user_data = <<-SHELL
171+
#!/bin/bash
172+
yum -y install nginx-1.20.1
173+
instanceId=`curl http://100.100.100.200/latest/meta-data/instance-id`
174+
echo "This instance from ess, the instance id is $instanceId" > /usr/share/nginx/html/index.html
175+
systemctl start nginx
176+
systemctl enable nginx
177+
SHELL
178+
}
179+
180+
# 创建ESS伸缩配置
181+
resource "alicloud_ess_scaling_configuration" "ess_scaling_configuration" {
182+
scaling_group_id = alicloud_ess_scaling_group.ess_scaling_group.id
183+
enable = true
184+
active = true
185+
force_delete = true
186+
image_id = data.alicloud_images.instance_image.images[0].id
187+
instance_types = [data.alicloud_instance_types.default.ids.0]
188+
security_group_id = alicloud_security_group.security_group.id
189+
system_disk_category = "cloud_essd"
190+
system_disk_size = 40
191+
password = var.ecs_instance_password
192+
instance_name = "${var.common_name}-ess"
193+
user_data = local.instance_user_data
194+
}
195+
196+
# 创建ESS伸缩规则(扩容)
197+
resource "alicloud_ess_scaling_rule" "ess_scale_up_rule" {
198+
scaling_group_id = alicloud_ess_scaling_group.ess_scaling_group.id
199+
scaling_rule_name = "${var.common_name}-asr-scale_up_rule"
200+
scaling_rule_type = "SimpleScalingRule"
201+
adjustment_type = "QuantityChangeInCapacity"
202+
adjustment_value = 1
203+
cooldown = 60
204+
}
205+
206+
# 创建ESS伸缩规则(缩容)
207+
resource "alicloud_ess_scaling_rule" "ess_scale_down_rule" {
208+
scaling_group_id = alicloud_ess_scaling_group.ess_scaling_group.id
209+
scaling_rule_name = "${var.common_name}-asr-scale_down_rule"
210+
scaling_rule_type = "SimpleScalingRule"
211+
adjustment_type = "QuantityChangeInCapacity"
212+
adjustment_value = -1
213+
cooldown = 60
214+
}
215+
216+
resource "time_static" "example" {}
217+
218+
# 创建定时任务(自动扩容)
219+
resource "alicloud_ess_scheduled_task" "scheduled_scale_up_task" {
220+
scheduled_task_name = "${var.common_name}-scale_up_task-${random_integer.default.result}"
221+
launch_time = var.scale_up_time != null && var.scale_up_time != "" ? var.scale_up_time : formatdate("YYYY-MM-DD'T'HH:mm'Z'", timeadd(time_static.example.rfc3339, "1h"))
222+
scheduled_action = alicloud_ess_scaling_rule.ess_scale_up_rule.ari
223+
launch_expiration_time = 10
224+
}
225+
226+
# 创建定时任务(自动缩容)
227+
resource "alicloud_ess_scheduled_task" "scheduled_scale_down_task" {
228+
scheduled_task_name = "${var.common_name}-scale_down_task-${random_integer.default.result}"
229+
launch_time = var.scale_down_time != null && var.scale_down_time != "" ? var.scale_down_time : formatdate("YYYY-MM-DD'T'HH:mm'Z'", timeadd(time_static.example.rfc3339, "2h"))
230+
scheduled_action = alicloud_ess_scaling_rule.ess_scale_down_rule.ari
231+
launch_expiration_time = 10
232+
}
233+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 输出负载均衡地址
2+
output "WebUrl" {
3+
description = <<EOT
4+
{
5+
"zh-cn": "Web 访问地址。",
6+
"en": "The Addresses of Web."
7+
}
8+
EOT
9+
value = format("http://%s", alicloud_alb_load_balancer.alb.dns_name)
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "region" {
2+
description = "地域"
3+
type = string
4+
default = "cn-hangzhou"
5+
}
6+
7+
variable "common_name" {
8+
description = "弹性应用名称"
9+
type = string
10+
default = "elastic-app"
11+
}
12+
13+
variable "ecs_instance_password" {
14+
description = "服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/中的特殊符号)"
15+
type = string
16+
sensitive = true
17+
}
18+
19+
variable "scale_up_time" {
20+
description = "自动扩容执行时间"
21+
type = string
22+
default = null
23+
}
24+
25+
variable "scale_down_time" {
26+
description = "自动缩容执行时间"
27+
type = string
28+
default = null
29+
}

0 commit comments

Comments
 (0)