Skip to content

Commit de91155

Browse files
author
shaobolin
committed
feat: 解决方案serverless-high-availability-architecture tf文件完成
1 parent 5ab7b50 commit de91155

File tree

5 files changed

+477
-0
lines changed

5 files changed

+477
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[极简运维,Serverless 高可用架构](https://www.aliyun.com/solution/tech-solution/serverless-ha), 涉及到专有网络(VPC)、交换机(VSwitch)、云原生数据库PolarDB MySQL版、应用型负载均衡(ALB)等资源的部署。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example is used to implement solution [Minimal Operations, Serverless High Availability Architecture](https://www.aliyun.com/solution/tech-solution/serverless-ha), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), VSwitch, PolarDB for MySQL, Application Load Balancer (ALB).
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+
| <a name="provider_time"></a> [time](#provider\_time) | n/a |
18+
19+
## Modules
20+
21+
No modules.
22+
23+
## Resources
24+
25+
| Name | Type |
26+
|------|------|
27+
| [alicloud_alb_load_balancer.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/alb_load_balancer) | resource |
28+
| [alicloud_polardb_account.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_account) | resource |
29+
| [alicloud_polardb_account_privilege.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_account_privilege) | resource |
30+
| [alicloud_polardb_cluster.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_cluster) | resource |
31+
| [alicloud_polardb_database.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/polardb_database) | resource |
32+
| [alicloud_sae_application.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/sae_application) | resource |
33+
| [alicloud_sae_ingress.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/sae_ingress) | resource |
34+
| [alicloud_sae_namespace.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/sae_namespace) | resource |
35+
| [alicloud_security_group.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
36+
| [alicloud_security_group_rule.allow_http](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
37+
| [alicloud_security_group_rule.allow_https](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
38+
| [alicloud_security_group_rule.allow_mysql](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
39+
| [alicloud_vpc.main](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
40+
| [alicloud_vswitch.db_01](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
41+
| [alicloud_vswitch.pub_01](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
42+
| [alicloud_vswitch.pub_02](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
43+
| [alicloud_vswitch.web_01](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
44+
| [alicloud_vswitch.web_02](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
45+
| [random_string.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
46+
| [time_sleep.wait_app](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
47+
48+
## Inputs
49+
50+
| Name | Description | Type | Default | Required |
51+
|------|-------------|------|---------|:--------:|
52+
| <a name="input_common_name"></a> [common\_name](#input\_common\_name) | 通用名称前缀 | `string` | `"serverless"` | no |
53+
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | MySQL数据库密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号) | `string` | n/a | yes |
54+
| <a name="input_db_username"></a> [db\_username](#input\_db\_username) | MySQL数据库账号 | `string` | `"applets"` | no |
55+
| <a name="input_region"></a> [region](#input\_region) | 阿里云地域 | `string` | `"cn-hangzhou"` | no |
56+
| <a name="input_zone_id_1"></a> [zone\_id\_1](#input\_zone\_id\_1) | 可用区1 | `string` | n/a | yes |
57+
| <a name="input_zone_id_2"></a> [zone\_id\_2](#input\_zone\_id\_2) | 可用区2 | `string` | n/a | yes |
58+
<!-- END_TF_DOCS -->
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
2+
locals {
3+
zone_id_1 = data.alicloud_polardb_node_classes.default.classes[0].zone_id
4+
zone_id_2 = data.alicloud_zones.default.zones.0.id
5+
}
6+
# 生成随机后缀用于资源命名
7+
resource "random_string" "suffix" {
8+
length = 5
9+
special = false
10+
upper = false
11+
}
12+
13+
data "alicloud_regions" "default" {
14+
current = true
15+
}
16+
data "alicloud_polardb_node_classes" "default" {
17+
db_type = "MySQL"
18+
db_version = "8.0"
19+
category = "Normal"
20+
pay_type = "PostPaid"
21+
db_node_class = "polar.mysql.sl.small"
22+
}
23+
24+
data "alicloud_zones" "default" {
25+
available_resource_creation = "VSwitch"
26+
}
27+
28+
# 创建VPC
29+
resource "alicloud_vpc" "main" {
30+
vpc_name = "${var.common_name}-vpc"
31+
cidr_block = "192.168.0.0/16"
32+
33+
tags = {
34+
Name = "${var.common_name}-vpc"
35+
}
36+
}
37+
38+
# 创建交换机1 (Web层 - 可用区1)
39+
resource "alicloud_vswitch" "web_01" {
40+
vpc_id = alicloud_vpc.main.id
41+
cidr_block = "192.168.1.0/24"
42+
zone_id = local.zone_id_1
43+
vswitch_name = "${var.common_name}-web-01"
44+
45+
tags = {
46+
Name = "${var.common_name}-web-01"
47+
}
48+
}
49+
50+
# 创建交换机2 (Web层 - 可用区2)
51+
resource "alicloud_vswitch" "web_02" {
52+
vpc_id = alicloud_vpc.main.id
53+
cidr_block = "192.168.2.0/24"
54+
zone_id = local.zone_id_2
55+
vswitch_name = "${var.common_name}-web-02"
56+
57+
tags = {
58+
Name = "${var.common_name}-web-02"
59+
}
60+
}
61+
62+
# 创建交换机3 (数据库层 - 可用区1)
63+
resource "alicloud_vswitch" "db_01" {
64+
vpc_id = alicloud_vpc.main.id
65+
cidr_block = "192.168.3.0/24"
66+
zone_id = local.zone_id_1
67+
vswitch_name = "${var.common_name}-db-01"
68+
69+
tags = {
70+
Name = "${var.common_name}-db-01"
71+
}
72+
}
73+
74+
# 创建交换机4 (公网层 - 可用区1)
75+
resource "alicloud_vswitch" "pub_01" {
76+
vpc_id = alicloud_vpc.main.id
77+
cidr_block = "192.168.4.0/24"
78+
zone_id = local.zone_id_1
79+
vswitch_name = "${var.common_name}-pub-01"
80+
81+
tags = {
82+
Name = "${var.common_name}-pub-01"
83+
}
84+
}
85+
86+
# 创建交换机5 (公网层 - 可用区2)
87+
resource "alicloud_vswitch" "pub_02" {
88+
vpc_id = alicloud_vpc.main.id
89+
cidr_block = "192.168.5.0/24"
90+
zone_id = local.zone_id_2
91+
vswitch_name = "${var.common_name}-pub-02"
92+
93+
tags = {
94+
Name = "${var.common_name}-pub-02"
95+
}
96+
}
97+
98+
# 创建安全组
99+
resource "alicloud_security_group" "main" {
100+
security_group_name = "${var.common_name}-sg"
101+
vpc_id = alicloud_vpc.main.id
102+
description = "Serverless高可用架构安全组"
103+
104+
tags = {
105+
Name = "${var.common_name}-sg"
106+
}
107+
}
108+
109+
# 安全组规则 - 允许HTTPS访问
110+
resource "alicloud_security_group_rule" "allow_https" {
111+
type = "ingress"
112+
ip_protocol = "tcp"
113+
port_range = "443/443"
114+
security_group_id = alicloud_security_group.main.id
115+
cidr_ip = "0.0.0.0/0"
116+
}
117+
118+
# 安全组规则 - 允许HTTP访问
119+
resource "alicloud_security_group_rule" "allow_http" {
120+
type = "ingress"
121+
ip_protocol = "tcp"
122+
port_range = "80/80"
123+
security_group_id = alicloud_security_group.main.id
124+
cidr_ip = "0.0.0.0/0"
125+
}
126+
127+
# 安全组规则 - 允许MySQL访问
128+
resource "alicloud_security_group_rule" "allow_mysql" {
129+
type = "ingress"
130+
ip_protocol = "tcp"
131+
port_range = "3306/3306"
132+
security_group_id = alicloud_security_group.main.id
133+
cidr_ip = "0.0.0.0/0"
134+
}
135+
136+
# 创建PolarDB集群
137+
resource "alicloud_polardb_cluster" "main" {
138+
db_type = "MySQL"
139+
db_version = "8.0"
140+
db_node_class = data.alicloud_polardb_node_classes.default.classes.0.supported_engines.0.available_resources.0.db_node_class
141+
pay_type = "PostPaid"
142+
vswitch_id = alicloud_vswitch.db_01.id
143+
zone_id = local.zone_id_1
144+
security_group_ids = [alicloud_security_group.main.id]
145+
146+
# Serverless配置
147+
serverless_type = "AgileServerless"
148+
scale_min = 1
149+
scale_max = 16
150+
scale_ro_num_min = 1
151+
scale_ro_num_max = 4
152+
153+
description = "Serverless高可用架构PolarDB集群"
154+
155+
tags = {
156+
Name = "${var.common_name}-polardb"
157+
}
158+
}
159+
160+
# 创建数据库
161+
resource "alicloud_polardb_database" "main" {
162+
db_cluster_id = alicloud_polardb_cluster.main.id
163+
db_name = "applets"
164+
character_set_name = "utf8mb4"
165+
db_description = "serverless demo"
166+
}
167+
168+
# 创建数据库账号
169+
resource "alicloud_polardb_account" "main" {
170+
db_cluster_id = alicloud_polardb_cluster.main.id
171+
account_name = var.db_user_name
172+
account_password = var.db_password
173+
account_type = "Normal"
174+
}
175+
176+
# 为账号授权数据库
177+
resource "alicloud_polardb_account_privilege" "main" {
178+
db_cluster_id = alicloud_polardb_cluster.main.id
179+
account_name = alicloud_polardb_account.main.account_name
180+
db_names = [alicloud_polardb_database.main.db_name]
181+
account_privilege = "ReadWrite"
182+
}
183+
184+
# 创建应用负载均衡器(ALB)
185+
resource "alicloud_alb_load_balancer" "main" {
186+
load_balancer_name = "${var.common_name}-alb"
187+
load_balancer_edition = "Basic"
188+
vpc_id = alicloud_vpc.main.id
189+
address_type = "Internet"
190+
address_allocated_mode = "Fixed"
191+
192+
load_balancer_billing_config {
193+
pay_type = "PayAsYouGo"
194+
}
195+
196+
zone_mappings {
197+
zone_id = local.zone_id_1
198+
vswitch_id = alicloud_vswitch.web_01.id
199+
}
200+
201+
zone_mappings {
202+
zone_id = local.zone_id_2
203+
vswitch_id = alicloud_vswitch.web_02.id
204+
}
205+
206+
tags = {
207+
Name = "${var.common_name}-alb"
208+
}
209+
}
210+
211+
# 创建SAE命名空间
212+
resource "alicloud_sae_namespace" "main" {
213+
namespace_name = "serverless-demo"
214+
namespace_id = "${data.alicloud_regions.default.regions.0.id}:serverless${random_string.suffix.result}"
215+
}
216+
217+
# 创建SAE应用
218+
resource "alicloud_sae_application" "main" {
219+
app_name = "serverless-demo-${random_string.suffix.result}"
220+
app_description = "serverless-demo"
221+
namespace_id = alicloud_sae_namespace.main.id
222+
223+
package_type = "FatJar"
224+
package_version = "1718956564756"
225+
package_url = "https://help-static-aliyun-doc.aliyuncs.com/tech-solution/sae-demo-0.0.3.jar"
226+
227+
vpc_id = alicloud_vpc.main.id
228+
security_group_id = alicloud_security_group.main.id
229+
vswitch_id = "${alicloud_vswitch.pub_01.id},${alicloud_vswitch.pub_02.id}"
230+
231+
cpu = 2000
232+
memory = 4096
233+
replicas = 2
234+
235+
jdk = "Open JDK 8"
236+
timezone = "Asia/Shanghai"
237+
238+
jar_start_args = "$JAVA_HOME/bin/java $Options -jar $CATALINA_OPTS \"$package_path\" $args"
239+
jar_start_options = "-XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/admin/nas/gc-$${POD_IP}-$(date '+%s').log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/admin/nas/dump-$${POD_IP}-$(date '+%s').hprof"
240+
241+
envs = jsonencode([
242+
{
243+
name = "APPLETS_MYSQL_ENDPOINT"
244+
value = alicloud_polardb_cluster.main.connection_string
245+
},
246+
{
247+
name = "APPLETS_MYSQL_USER"
248+
value = var.db_user_name
249+
},
250+
{
251+
name = "APPLETS_MYSQL_PASSWORD"
252+
value = var.db_password
253+
},
254+
{
255+
name = "APPLETS_MYSQL_DB_NAME"
256+
value = "applets"
257+
},
258+
{
259+
name = "APP_MANUAL_DEPLOY"
260+
value = "false"
261+
}
262+
])
263+
264+
readiness_v2 {
265+
exec {
266+
command = ["sleep", "6s"]
267+
}
268+
initial_delay_seconds = 15
269+
timeout_seconds = 12
270+
}
271+
272+
liveness_v2 {
273+
http_get {
274+
path = "/"
275+
port = 80
276+
scheme = "HTTP"
277+
}
278+
initial_delay_seconds = 10
279+
timeout_seconds = 10
280+
period_seconds = 10
281+
}
282+
283+
tags = {
284+
Name = "serverless-demo-${random_string.suffix.result}"
285+
}
286+
}
287+
288+
# 等待应用部署完成
289+
resource "time_sleep" "wait_app" {
290+
depends_on = [alicloud_sae_application.main]
291+
create_duration = "180s"
292+
}
293+
294+
# 创建SAE Ingress规则
295+
resource "alicloud_sae_ingress" "main" {
296+
depends_on = [time_sleep.wait_app]
297+
namespace_id = alicloud_sae_namespace.main.id
298+
slb_id = alicloud_alb_load_balancer.main.id
299+
description = "serverless-demo-router"
300+
301+
load_balance_type = "alb"
302+
listener_protocol = "HTTP"
303+
listener_port = 80
304+
305+
rules {
306+
app_name = alicloud_sae_application.main.app_name
307+
app_id = alicloud_sae_application.main.id
308+
container_port = 80
309+
domain = "example.com"
310+
path = "/"
311+
}
312+
313+
default_rule {
314+
app_name = alicloud_sae_application.main.app_name
315+
app_id = alicloud_sae_application.main.id
316+
container_port = 80
317+
}
318+
}

0 commit comments

Comments
 (0)