Skip to content

Commit c922eb3

Browse files
committed
feat: 解决方案distributed-task-scheduling tf文件完成
1 parent 7233118 commit c922eb3

File tree

5 files changed

+278
-0
lines changed

5 files changed

+278
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[快速集成分布式任务调度](https://www.aliyun.com/solution/tech-solution/mse-schedulerx), 涉及专有网络(VPC)、交换机(VSwitch)、RDS数据库(RDS)、云服务器(ECS)。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example is used to implement solution [Quick Integration Of Distributed Task Scheduling](https://www.aliyun.com/solution/tech-solution/mse-schedulerx), which involves the creation and deployment of resources such as Virtual Private Cloud (Vpc), Virtual Switch (VSwitch), RDS Database (Rds), Elastic Compute Service (Ecs).
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+
17+
## Modules
18+
19+
No modules.
20+
21+
## Resources
22+
23+
| Name | Type |
24+
|------|------|
25+
| [alicloud_db_instance.rds](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/db_instance) | resource |
26+
| [alicloud_ecs_command.deploy](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_command) | resource |
27+
| [alicloud_ecs_invocation.deploy_invocation](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_invocation) | resource |
28+
| [alicloud_instance.ecs](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance) | resource |
29+
| [alicloud_rds_account.create_db_user](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/rds_account) | resource |
30+
| [alicloud_security_group.sg](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
31+
| [alicloud_security_group_rule.http](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group_rule) | resource |
32+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
33+
| [alicloud_vswitch.vsw](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
34+
| [alicloud_db_instance_classes.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/db_instance_classes) | data source |
35+
| [alicloud_images.instance_image](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/images) | data source |
36+
| [alicloud_instance_types.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/instance_types) | data source |
37+
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |
38+
39+
## Inputs
40+
41+
| Name | Description | Type | Default | Required |
42+
|------|-------------|------|---------|:--------:|
43+
| <a name="input_common_name"></a> [common\_name](#input\_common\_name) | 应用名称 | `string` | `"scheduler-demo"` | no |
44+
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | RDS数据库密码,必须包含三种及以上类型:大写字母、小写字母、数字、特殊符号。长度为8~32位。特殊字符包括!@#$%^&*()\_+-= | `string` | n/a | yes |
45+
| <a name="input_db_user_name"></a> [db\_user\_name](#input\_db\_user\_name) | RDS数据库账号,由2到32个小写字母组成,支持小写字母、数字和下划线,以小写字母开头 | `string` | `"user_test"` | no |
46+
| <a name="input_demo_user_name"></a> [demo\_user\_name](#input\_demo\_user\_name) | 在浏览器中登录示例应用程序时的用户名。 | `string` | `"demo-user-example"` | no |
47+
| <a name="input_demo_user_password"></a> [demo\_user\_password](#input\_demo\_user\_password) | 登录示例应用程序时的登录密码,必须包含三种及以上类型:大写字母、小写字母、数字、特殊符号。长度为8~32位。特殊字符包括!@#$%^&*()\_+-= | `string` | `"Demo123.."` | no |
48+
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号) | `string` | n/a | yes |
49+
| <a name="input_region"></a> [region](#input\_region) | 地域 | `string` | `"cn-hangzhou"` | no |
50+
| <a name="input_scheduler_x_app_key"></a> [scheduler\_x\_app\_key](#input\_scheduler\_x\_app\_key) | SchedulerX应用密钥,请输入在SchedulerX控制台的接入配置中获取的应用密钥 | `string` | n/a | yes |
51+
| <a name="input_scheduler_x_endpoint"></a> [scheduler\_x\_endpoint](#input\_scheduler\_x\_endpoint) | SchedulerX接入地址,请输入在SchedulerX控制台的接入配置中获取的接入地址 | `string` | `"addr-hz-internal.edas.aliyun.com"` | no |
52+
| <a name="input_scheduler_x_group_id"></a> [scheduler\_x\_group\_id](#input\_scheduler\_x\_group\_id) | SchedulerX应用ID,请输入在SchedulerX控制台的接入配置中获取的应用ID | `string` | `"test"` | no |
53+
| <a name="input_scheduler_x_namespace"></a> [scheduler\_x\_namespace](#input\_scheduler\_x\_namespace) | SchedulerX命名空间,请输入在SchedulerX控制台的接入配置中获取的命名空间 | `string` | `"00000000-00000000-00000000-00000000"` | no |
54+
<!-- END_TF_DOCS -->
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# 查询实例实例规格
2+
data "alicloud_instance_types" "default" {
3+
instance_type_family = "ecs.g7"
4+
sorted_by = "CPU"
5+
}
6+
7+
data "alicloud_zones" "default" {
8+
available_instance_type = data.alicloud_instance_types.default.ids.0
9+
available_resource_creation = "Rds"
10+
}
11+
12+
locals {
13+
zone_id = data.alicloud_zones.default.ids[length(data.alicloud_zones.default.ids) - 1]
14+
}
15+
16+
# 查询实例规格
17+
data "alicloud_db_instance_classes" "default" {
18+
instance_charge_type = "PostPaid"
19+
engine = "MySQL"
20+
engine_version = "8.0"
21+
db_instance_storage_type = "cloud_essd"
22+
category = "Basic"
23+
zone_id = local.zone_id
24+
}
25+
26+
resource "alicloud_vpc" "vpc" {
27+
vpc_name = "${var.common_name}-vpc"
28+
cidr_block = "192.168.0.0/16"
29+
}
30+
31+
resource "alicloud_vswitch" "vsw" {
32+
vpc_id = alicloud_vpc.vpc.id
33+
cidr_block = "192.168.0.0/24"
34+
zone_id = local.zone_id
35+
vswitch_name = "${var.common_name}-vsw"
36+
}
37+
38+
resource "alicloud_security_group" "sg" {
39+
security_group_name = "${var.common_name}-sg"
40+
vpc_id = alicloud_vpc.vpc.id
41+
}
42+
43+
resource "alicloud_security_group_rule" "http" {
44+
type = "ingress"
45+
ip_protocol = "tcp"
46+
port_range = "80/80"
47+
cidr_ip = "0.0.0.0/0"
48+
security_group_id = alicloud_security_group.sg.id
49+
}
50+
51+
data "alicloud_images" "instance_image" {
52+
name_regex = "^aliyun_3_9_x64_20G_*"
53+
most_recent = true
54+
owners = "system"
55+
instance_type = data.alicloud_instance_types.default.ids.0
56+
}
57+
58+
resource "alicloud_instance" "ecs" {
59+
instance_name = "${var.common_name}-ecs"
60+
instance_type = data.alicloud_instance_types.default.ids.0
61+
vswitch_id = alicloud_vswitch.vsw.id
62+
security_groups = [alicloud_security_group.sg.id]
63+
image_id = data.alicloud_images.instance_image.images.0.id
64+
system_disk_category = "cloud_essd"
65+
internet_max_bandwidth_out = 5
66+
password = var.ecs_instance_password
67+
count = 2
68+
}
69+
70+
resource "alicloud_db_instance" "rds" {
71+
engine = "MySQL"
72+
engine_version = "8.0"
73+
instance_type = data.alicloud_db_instance_classes.default.ids.0
74+
instance_storage = 40
75+
instance_charge_type = "Postpaid"
76+
vswitch_id = alicloud_vswitch.vsw.id
77+
security_ips = ["192.168.0.0/24"]
78+
}
79+
80+
resource "alicloud_rds_account" "create_db_user" {
81+
db_instance_id = alicloud_db_instance.rds.id
82+
account_name = var.db_user_name
83+
account_password = var.db_password
84+
account_type = "Super"
85+
}
86+
87+
resource "alicloud_ecs_command" "deploy" {
88+
name = "${var.common_name}-deploy"
89+
type = "RunShellScript"
90+
command_content = base64encode(local.deploy_application_script)
91+
working_dir = "/root"
92+
timeout = 300
93+
}
94+
95+
resource "alicloud_ecs_invocation" "deploy_invocation" {
96+
instance_id = alicloud_instance.ecs.*.id
97+
command_id = alicloud_ecs_command.deploy.id
98+
timeouts {
99+
create = "5m"
100+
}
101+
depends_on = [alicloud_rds_account.create_db_user]
102+
}
103+
104+
locals {
105+
deploy_application_script = <<-SHELL
106+
#!/bin/bash
107+
108+
function log_info() {
109+
printf "%s [INFO] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
110+
}
111+
112+
function log_error() {
113+
printf "%s [ERROR] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
114+
}
115+
116+
function debug_exec(){
117+
local cmd="$@"
118+
log_info "$cmd"
119+
eval "$cmd"
120+
ret=$?
121+
echo ""
122+
log_info "$cmd, exit code: $ret"
123+
return $ret
124+
}
125+
126+
cat << EOF >> ~/.bash_profile
127+
export DEMO_SCHEDULERX_ENDPOINT="addr-hz-internal.edas.aliyun.com"
128+
export DEMO_SCHEDULERX_NAMESPACE="${var.scheduler_x_namespace}"
129+
export DEMO_SCHEDULERX_GROUPID="${var.scheduler_x_group_id}"
130+
export DEMO_SCHEDULERX_APPKEY="${var.scheduler_x_app_key}"
131+
132+
export DEMO_MYSQL_URL="${alicloud_db_instance.rds.connection_string}:3306"
133+
export DEMO_MYSQL_USERNAME="${var.db_user_name}"
134+
export DEMO_MYSQL_PASSWORD="${var.db_password}"
135+
136+
export DEMO_USERNAME="${var.demo_user_name}"
137+
export DEMO_PASSWORD="${var.demo_user_password}"
138+
139+
export ROS_DEPLOY=true
140+
EOF
141+
source ~/.bash_profile
142+
143+
curl -fsSL https://help-static-aliyun-doc.aliyuncs.com/install-script/mse-schedulerx/install.sh | bash
144+
SHELL
145+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "DemoUserName" {
2+
description = "Login Username."
3+
value = var.demo_user_name
4+
}
5+
6+
output "DemoUrl" {
7+
description = "Demo URL."
8+
value = "http://${alicloud_instance.ecs[0].public_ip}/schedulerx-demo"
9+
}
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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
variable "region" {
2+
description = "地域"
3+
type = string
4+
default = "cn-hangzhou"
5+
}
6+
7+
variable "demo_user_name" {
8+
type = string
9+
description = "在浏览器中登录示例应用程序时的用户名。"
10+
default = "demo-user-example"
11+
}
12+
13+
variable "demo_user_password" {
14+
type = string
15+
description = "登录示例应用程序时的登录密码,必须包含三种及以上类型:大写字母、小写字母、数字、特殊符号。长度为8~32位。特殊字符包括!@#$%^&*()_+-="
16+
sensitive = true
17+
default = "Demo123.."
18+
}
19+
20+
variable "ecs_instance_password" {
21+
type = string
22+
description = "服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、特殊符号)"
23+
sensitive = true
24+
}
25+
26+
variable "db_user_name" {
27+
type = string
28+
description = "RDS数据库账号,由2到32个小写字母组成,支持小写字母、数字和下划线,以小写字母开头"
29+
default = "user_test"
30+
}
31+
32+
variable "db_password" {
33+
type = string
34+
description = "RDS数据库密码,必须包含三种及以上类型:大写字母、小写字母、数字、特殊符号。长度为8~32位。特殊字符包括!@#$%^&*()_+-="
35+
sensitive = true
36+
}
37+
38+
variable "scheduler_x_endpoint" {
39+
type = string
40+
description = "SchedulerX接入地址,请输入在SchedulerX控制台的接入配置中获取的接入地址"
41+
default = "addr-hz-internal.edas.aliyun.com"
42+
}
43+
44+
variable "scheduler_x_namespace" {
45+
type = string
46+
description = "SchedulerX命名空间,请输入在SchedulerX控制台的接入配置中获取的命名空间"
47+
default = "00000000-00000000-00000000-00000000"
48+
}
49+
50+
variable "scheduler_x_group_id" {
51+
type = string
52+
description = "SchedulerX应用ID,请输入在SchedulerX控制台的接入配置中获取的应用ID"
53+
default = "test"
54+
}
55+
56+
variable "scheduler_x_app_key" {
57+
type = string
58+
description = "SchedulerX应用密钥,请输入在SchedulerX控制台的接入配置中获取的应用密钥"
59+
sensitive = true
60+
default = "SzcxxxxxxxxxxPw"
61+
}
62+
63+
variable "common_name" {
64+
type = string
65+
description = "应用名称"
66+
default = "scheduler-demo"
67+
}

0 commit comments

Comments
 (0)