Skip to content

Commit d4e58c4

Browse files
CodeArtisanXshanye997
authored andcommitted
add tech-solution real-time-log-analysis-with-selectdb
1 parent aaeee38 commit d4e58c4

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Introduction
2+
<!-- DOCS_DESCRIPTION_CN -->
3+
本示例用于实现解决方案[SelectDB实现日志高效存储与实时分析](https://www.aliyun.com/solution/tech-solution/real-time-log-analysis-with-selectdb),涉及专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、SelectDB数据库(SelectDB)等资源的部署。
4+
<!-- DOCS_DESCRIPTION_CN -->
5+
6+
<!-- DOCS_DESCRIPTION_EN -->
7+
This example is used to implement solution [SelectDB enables efficient log storage and real-time analytics](https://www.aliyun.com/solution/tech-solution/real-time-log-analysis-with-selectdb), which involves the creation and deployment of resources such as Virtual Private Cloud (VPC), Virtual Switch (VSwitch), Elastic Compute Service (ECS), SelectDB Database (SelectDB).
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_ecs_command.run_tpcc_alicloud_ecs_command](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_command) | resource |
27+
| [alicloud_ecs_invocation.run_tpcc_alicloud_ecs_invocation](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/ecs_invocation) | resource |
28+
| [alicloud_instance.ecs_instance](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/instance) | resource |
29+
| [alicloud_security_group.sg_select](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/security_group) | resource |
30+
| [alicloud_selectdb_db_instance.selectdb_instance](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/selectdb_db_instance) | resource |
31+
| [alicloud_vpc.vpc_select](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vpc) | resource |
32+
| [alicloud_vswitch.vsw_select](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/resources/vswitch) | resource |
33+
| [random_id.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |
34+
| [alicloud_images.image_id](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/images) | data source |
35+
| [alicloud_zones.default](https://registry.terraform.io/providers/aliyun/alicloud/latest/docs/data-sources/zones) | data source |
36+
37+
## Inputs
38+
39+
| Name | Description | Type | Default | Required |
40+
|------|-------------|------|---------|:--------:|
41+
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | SelectDB admin账号密码 | `string` | n/a | yes |
42+
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | ECS服务器root账号密码 | `string` | n/a | yes |
43+
| <a name="input_instance_class"></a> [instance\_class](#input\_instance\_class) | SelectDB实例规格 | `string` | `"selectdb.4xlarge"` | no |
44+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | ECS实例类型,建议选择配备 16 vCPU 64 GiB 配置的实例 | `string` | `"ecs.g8i.4xlarge"` | no |
45+
| <a name="input_selectdb_engine_version"></a> [selectdb\_engine\_version](#input\_selectdb\_engine\_version) | SelectDB内核版本 | `string` | `"4.0.4"` | no |
46+
<!-- END_TF_DOCS -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# ------------------------------------------------------------------------------
2+
# 核心资源定义
3+
#
4+
# 本文件包含了模块的核心基础设施资源
5+
# 这里的代码负责根据输入变量来创建和配置所有云资源
6+
# ------------------------------------------------------------------------------
7+
8+
# 配置阿里云提供商
9+
provider "alicloud" {
10+
region = "cn-hangzhou"
11+
}
12+
13+
# 创建随机ID用于资源命名
14+
resource "random_id" "suffix" {
15+
byte_length = 8
16+
}
17+
18+
# 定义本地变量common_name
19+
locals {
20+
common_name = "SelectDB-${random_id.suffix.id}"
21+
ecs_command = <<SHELL
22+
#!/bin/bash
23+
cd /root
24+
export ROS_DEPLOY=true
25+
wget https://help-static-aliyun-doc.aliyuncs.com/install-script/selectdb-observability/yc_log_demo_2.0.1.tar.gz
26+
tar -zxvf yc_log_demo_2.0.1.tar.gz
27+
cd /root/log_demo
28+
bash install.sh
29+
sudo chown -R root:root /root/log_demo
30+
SHELL
31+
}
32+
33+
# 查询可用区
34+
data "alicloud_zones" "default" {
35+
available_disk_category = "cloud_essd"
36+
available_instance_type = var.instance_type
37+
}
38+
39+
# 创建VPC
40+
resource "alicloud_vpc" "vpc_select" {
41+
cidr_block = "192.168.0.0/16"
42+
vpc_name = "${local.common_name}-vpc"
43+
}
44+
45+
# 创建VSwitch
46+
resource "alicloud_vswitch" "vsw_select" {
47+
vpc_id = alicloud_vpc.vpc_select.id
48+
zone_id = data.alicloud_zones.default.ids[0]
49+
cidr_block = "192.168.0.0/24"
50+
vswitch_name = "${local.common_name}-vsw"
51+
}
52+
53+
# 创建安全组
54+
resource "alicloud_security_group" "sg_select" {
55+
security_group_name = "${local.common_name}-sg"
56+
vpc_id = alicloud_vpc.vpc_select.id
57+
}
58+
59+
# 获取阿里云官方镜像
60+
data "alicloud_images" "image_id" {
61+
name_regex = "^aliyun_3_9_x64_20G*"
62+
owners = "system" # 官方镜像
63+
most_recent = true # 获取最新版本
64+
}
65+
66+
# 创建ECS实例
67+
resource "alicloud_instance" "ecs_instance" {
68+
vpc_id = alicloud_vpc.vpc_select.id
69+
vswitch_id = alicloud_vswitch.vsw_select.id
70+
security_groups = [alicloud_security_group.sg_select.id]
71+
image_id = data.alicloud_images.image_id.images[0].id
72+
instance_name = "${local.common_name}-ecs"
73+
instance_type = var.instance_type
74+
system_disk_category = "cloud_essd"
75+
system_disk_size = 100
76+
internet_max_bandwidth_out = 10
77+
password = var.ecs_instance_password
78+
}
79+
80+
# 创建SelectDB实例
81+
resource "alicloud_selectdb_db_instance" "selectdb_instance" {
82+
vpc_id = alicloud_vpc.vpc_select.id
83+
zone_id = data.alicloud_zones.default.ids[0]
84+
vswitch_id = alicloud_vswitch.vsw_select.id
85+
db_instance_description = "${local.common_name}-selectdb"
86+
db_instance_class = var.instance_class
87+
cache_size = 100
88+
payment_type = "PayAsYouGo"
89+
engine_minor_version = var.selectdb_engine_version
90+
admin_pass = var.db_password
91+
desired_security_ip_lists {
92+
group_name = "default"
93+
security_ip_list = "192.168.0.0/16"
94+
}
95+
}
96+
97+
# 创建ECS命令资源
98+
resource "alicloud_ecs_command" "run_tpcc_alicloud_ecs_command" {
99+
name = "commond_install"
100+
description = "commond_install_description"
101+
type = "RunShellScript"
102+
command_content = base64encode(local.ecs_command)
103+
timeout = 3600
104+
working_dir = "/root"
105+
}
106+
107+
# 创建ECS命令调用资源
108+
resource "alicloud_ecs_invocation" "run_tpcc_alicloud_ecs_invocation" {
109+
instance_id = [alicloud_instance.ecs_instance.id]
110+
command_id = alicloud_ecs_command.run_tpcc_alicloud_ecs_command.id
111+
depends_on = [alicloud_selectdb_db_instance.selectdb_instance]
112+
timeouts {
113+
create = "60m"
114+
}
115+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ------------------------------------------------------------------------------
2+
# 模块输出值
3+
#
4+
# 本文件定义了模块执行成功后返回给调用方的值
5+
# 这些输出可以被其他 Terraform 配置引用,或在 apply 命令结束后显示给用户
6+
# ------------------------------------------------------------------------------
7+
8+
# 输出ECS登录用户
9+
output "ecs_account" {
10+
description = "ECS登录用户"
11+
value = "root"
12+
}
13+
14+
# 输出ECS用户root密码
15+
output "ecs_login_password" {
16+
description = "ECS用户root密码"
17+
value = var.ecs_instance_password
18+
sensitive = true
19+
}
20+
21+
# 输出SelectDB登录用户
22+
output "selectdb_account" {
23+
description = "SelectDB登录用户"
24+
value = "admin"
25+
}
26+
27+
# 输出SelectDB用户admin密码
28+
output "selectdb_login_password" {
29+
description = "SelectDB用户admin密码"
30+
value = var.db_password
31+
sensitive = true
32+
}
33+
34+
# 输出SelectDB VPC地址
35+
output "selectdb_vpc_connection_string" {
36+
description = "SelectDB VPC地址"
37+
value = alicloud_selectdb_db_instance.selectdb_instance.instance_net_infos[1].connection_string
38+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ------------------------------------------------------------------------------
2+
# 模块输入变量
3+
#
4+
# 本文件定义了该 Terraform 模块所有可配置的输入变量
5+
# 每个变量都包含了详细的说明,以帮助用户正确配置模块
6+
# ------------------------------------------------------------------------------
7+
8+
# 指定ECS实例的规格型号
9+
variable "instance_type" {
10+
type = string
11+
default = "ecs.g8i.4xlarge"
12+
description = "ECS实例类型,建议选择配备 16 vCPU 64 GiB 配置的实例"
13+
}
14+
15+
# ECS服务器root账号密码
16+
variable "ecs_instance_password" {
17+
type = string
18+
sensitive = true
19+
description = "ECS服务器root账号密码"
20+
#default = ""
21+
validation {
22+
condition = can(regex("^[0-9A-Za-z_!@#$%^&*()_+\\-=\\+]+$", var.ecs_instance_password)) && length(var.ecs_instance_password) >= 8 && length(var.ecs_instance_password) <= 30
23+
error_message = "长度8-30,必须包含三项(大写字母、小写字母、数字、 !@#$%^&*()_+-=中的特殊符号)"
24+
}
25+
}
26+
27+
# SelectDB实例规格
28+
variable "instance_class" {
29+
type = string
30+
default = "selectdb.4xlarge"
31+
description = "SelectDB实例规格"
32+
}
33+
34+
# SelectDB内核版本
35+
variable "selectdb_engine_version" {
36+
type = string
37+
default = "4.0.4"
38+
description = "SelectDB内核版本"
39+
validation {
40+
condition = contains(["3.0.12", "4.0.4"], var.selectdb_engine_version)
41+
error_message = "无效的配置信息,请检查并重新输入"
42+
}
43+
}
44+
45+
# SelectDB admin账号密码
46+
variable "db_password" {
47+
type = string
48+
sensitive = true
49+
description = "SelectDB admin账号密码"
50+
#default = ""
51+
validation {
52+
condition = can(regex("^[0-9A-Za-z_!@#$%^&*()_+\\-=\\+]+$", var.db_password)) && length(var.db_password) >= 8 && length(var.db_password) <= 30
53+
error_message = "长度8-30,必须包含三项(大写字母、小写字母、数字、 !@#$%^&*()_+-=中的特殊符号)"
54+
}
55+
}

0 commit comments

Comments
 (0)