Skip to content

Commit 324fb8e

Browse files
committed
build-large-scale-low-cost-real-time-log-management-platform
1 parent 35fa15b commit 324fb8e

File tree

4 files changed

+387
-0
lines changed

4 files changed

+387
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Introduction
2+
3+
<!-- DOCS_DESCRIPTION_CN -->
4+
本示例用于实现解决方案[开源自建ELK上云指南:基于阿里云日志服务(SLS)构建低成本可扩展日志平台](https://www.aliyun.com/solution/tech-solution/build-large-scale-low-cost-real-time-log-management-platform), 涉及到专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、RAM 用户等资源的创建。
5+
<!-- DOCS_DESCRIPTION_CN -->
6+
7+
<!-- DOCS_DESCRIPTION_EN -->
8+
This example demonstrates the implementation of the solution [Build Large Scale Low Cost Realtime Log Management Platform](https://www.aliyun.com/solution/tech-solution/build-large-scale-low-cost-real-time-log-management-platform). It involves the creation, and deployment of resources such as Virtual Private Cloud (VPC), VSwitch, Elastic Compute Service (ECS), and RAM users.
9+
<!-- DOCS_DESCRIPTION_EN -->
10+
11+
<!-- BEGIN_TF_DOCS -->
12+
## Providers
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
17+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
18+
19+
## Modules
20+
21+
No modules.
22+
23+
## Resources
24+
25+
| Name | Type |
26+
|------|------|
27+
| [alicloud_ecs_command.run_command](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_command) | resource |
28+
| [alicloud_ecs_command.run_command_kibana](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_command) | resource |
29+
| [alicloud_ecs_invocation.invoke_script](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_invocation) | resource |
30+
| [alicloud_ecs_invocation.invoke_script_kibana](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_invocation) | resource |
31+
| [alicloud_instance.ecs_instance](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
32+
| [alicloud_instance.ecs_instance_kibana](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
33+
| [alicloud_log_machine_group.this](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/log_machine_group) | resource |
34+
| [alicloud_log_project.sls_project](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/log_project) | resource |
35+
| [alicloud_log_store.sls_log_store](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/log_store) | resource |
36+
| [alicloud_log_store_index.sls_index](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/log_store_index) | resource |
37+
| [alicloud_logtail_attachment.this](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/logtail_attachment) | resource |
38+
| [alicloud_logtail_config.this](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/logtail_config) | resource |
39+
| [alicloud_ram_access_key.ramak](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_access_key) | resource |
40+
| [alicloud_ram_user.ram_user](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_user) | resource |
41+
| [alicloud_ram_user_policy_attachment.attach_policy_to_user](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_user_policy_attachment) | resource |
42+
| [alicloud_security_group.security_group](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group) | resource |
43+
| [alicloud_security_group.security_group_kibana](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group) | resource |
44+
| [alicloud_security_group_rule.allow_kibana](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group_rule) | resource |
45+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/vpc) | resource |
46+
| [alicloud_vswitch.vswitch](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/vswitch) | resource |
47+
| [random_string.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
48+
| [alicloud_images.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/images) | data source |
49+
| [alicloud_zones.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/zones) | data source |
50+
51+
## Inputs
52+
53+
| Name | Description | Type | Default | Required |
54+
|------|-------------|------|---------|:--------:|
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_instance_type"></a> [instance\_type](#input\_instance\_type) | 实例类型 | `string` | `"ecs.e-c1m2.large"` | no |
57+
| <a name="input_instance_type_xlarge"></a> [instance\_type\_xlarge](#input\_instance\_type\_xlarge) | 高性能实例类型 | `string` | `"ecs.e-c1m2.xlarge"` | no |
58+
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | `"cn-hangzhou"` | no |
59+
<!-- END_TF_DOCS -->
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
4+
5+
data "alicloud_zones" "default" {
6+
available_disk_category = "cloud_essd"
7+
available_resource_creation = "VSwitch"
8+
available_instance_type = var.instance_type
9+
}
10+
11+
resource "random_string" "suffix" {
12+
length = 8
13+
lower = true
14+
upper = false
15+
numeric = false
16+
special = false
17+
}
18+
19+
locals {
20+
common_name = random_string.suffix.id
21+
}
22+
23+
resource "alicloud_vpc" "vpc" {
24+
cidr_block = "192.168.0.0/16"
25+
vpc_name = "vpc-${local.common_name}"
26+
}
27+
28+
resource "alicloud_vswitch" "vswitch" {
29+
vpc_id = alicloud_vpc.vpc.id
30+
cidr_block = "192.168.0.0/24"
31+
zone_id = data.alicloud_zones.default.zones.0.id
32+
vswitch_name = "vswitch-${local.common_name}"
33+
}
34+
35+
resource "alicloud_security_group" "security_group" {
36+
vpc_id = alicloud_vpc.vpc.id
37+
security_group_name = "sg-${local.common_name}"
38+
}
39+
40+
data "alicloud_images" "default" {
41+
name_regex = "^aliyun_3_x64_20G_alibase_.*"
42+
most_recent = true
43+
owners = "system"
44+
}
45+
46+
resource "alicloud_ram_user" "ram_user" {
47+
name = "create_by_solution-${local.common_name}"
48+
}
49+
50+
resource "alicloud_ram_access_key" "ramak" {
51+
user_name = alicloud_ram_user.ram_user.name
52+
depends_on = [
53+
alicloud_ram_user.ram_user
54+
]
55+
}
56+
57+
resource "alicloud_ram_user_policy_attachment" "attach_policy_to_user" {
58+
user_name = alicloud_ram_user.ram_user.name
59+
policy_type = "System"
60+
policy_name = "AliyunLogFullAccess"
61+
depends_on = [
62+
alicloud_ram_access_key.ramak
63+
]
64+
}
65+
66+
# the ECS instance which generate the log, and where LoongCollector is installed
67+
resource "alicloud_instance" "ecs_instance" {
68+
instance_name = "ecs-${local.common_name}"
69+
image_id = data.alicloud_images.default.images[0].id
70+
instance_type = var.instance_type
71+
system_disk_category = "cloud_essd"
72+
security_groups = [alicloud_security_group.security_group.id]
73+
vswitch_id = alicloud_vswitch.vswitch.id
74+
password = var.ecs_instance_password
75+
internet_max_bandwidth_out = 5
76+
}
77+
78+
resource "alicloud_ecs_command" "run_command" {
79+
name = "command-genlog-loongcollector-${local.common_name}"
80+
command_content = base64encode(<<EOF
81+
cat << EOT >> ~/.bash_profile
82+
export ROS_DEPLOY=true
83+
export ALIBABA_CLOUD_ACCESS_KEY_ID=${alicloud_ram_access_key.ramak.id}
84+
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=${alicloud_ram_access_key.ramak.secret}
85+
EOT
86+
87+
source ~/.bash_profile
88+
curl -fsSL https://help-static-aliyun-doc.aliyuncs.com/tech-solution/install-log-monitoring-alarming-0.1.sh|bash
89+
wget http://aliyun-observability-release-${var.region}.oss-${var.region}.aliyuncs.com/loongcollector/linux64/latest/loongcollector.sh -O loongcollector.sh
90+
chmod +x loongcollector.sh
91+
./loongcollector.sh install ${var.region}-internet
92+
EOF
93+
)
94+
working_dir = "/root"
95+
type = "RunShellScript"
96+
timeout = 3600
97+
}
98+
99+
resource "alicloud_ecs_invocation" "invoke_script" {
100+
instance_id = [alicloud_instance.ecs_instance.id]
101+
command_id = alicloud_ecs_command.run_command.id
102+
timeouts {
103+
create = "15m"
104+
}
105+
depends_on = [alicloud_instance.ecs_instance]
106+
}
107+
108+
resource "alicloud_log_project" "sls_project" {
109+
project_name = "sls-project-${local.common_name}"
110+
}
111+
112+
resource "alicloud_log_store" "sls_log_store" {
113+
logstore_name = "sls-logstore-${local.common_name}"
114+
project_name = alicloud_log_project.sls_project.project_name
115+
depends_on = [alicloud_log_project.sls_project]
116+
}
117+
118+
resource "alicloud_log_machine_group" "this" {
119+
identify_list = [alicloud_instance.ecs_instance.primary_ip_address]
120+
name = "lmg-${local.common_name}"
121+
project = alicloud_log_project.sls_project.project_name
122+
identify_type = "ip"
123+
}
124+
125+
resource "alicloud_logtail_config" "this" {
126+
project = alicloud_log_project.sls_project.project_name
127+
input_detail = <<EOF
128+
{
129+
"discardUnmatch": false,
130+
"enableRawLog": true,
131+
"fileEncoding": "utf8",
132+
"filePattern": "sls-monitor-test.log",
133+
"logPath": "/tmp",
134+
"logType": "common_reg_log",
135+
"maxDepth": 10,
136+
"topicFormat": "none"
137+
}
138+
EOF
139+
input_type = "file"
140+
logstore = alicloud_log_store.sls_log_store.logstore_name
141+
name = "lc-${local.common_name}"
142+
output_type = "LogService"
143+
}
144+
145+
resource "alicloud_logtail_attachment" "this" {
146+
project = alicloud_log_project.sls_project.project_name
147+
logtail_config_name = alicloud_logtail_config.this.name
148+
machine_group_name = alicloud_log_machine_group.this.name
149+
}
150+
151+
resource "alicloud_log_store_index" "sls_index" {
152+
project = alicloud_log_project.sls_project.project_name
153+
logstore = alicloud_log_store.sls_log_store.logstore_name
154+
full_text {}
155+
field_search {
156+
name = "content"
157+
type = "text"
158+
}
159+
}
160+
161+
resource "alicloud_security_group" "security_group_kibana" {
162+
vpc_id = alicloud_vpc.vpc.id
163+
security_group_name = "sg-kibana-${local.common_name}"
164+
}
165+
166+
resource "alicloud_security_group_rule" "allow_kibana" {
167+
type = "ingress"
168+
ip_protocol = "tcp"
169+
nic_type = "intranet"
170+
policy = "accept"
171+
port_range = "5601/5601"
172+
priority = 1
173+
security_group_id = alicloud_security_group.security_group_kibana.id
174+
cidr_ip = "0.0.0.0/0"
175+
}
176+
177+
# the ECS instance where Kibana is deployed
178+
resource "alicloud_instance" "ecs_instance_kibana" {
179+
instance_name = "ecs-kibana-${local.common_name}"
180+
image_id = data.alicloud_images.default.images[0].id
181+
instance_type = var.instance_type_xlarge
182+
system_disk_category = "cloud_essd"
183+
security_groups = [alicloud_security_group.security_group_kibana.id]
184+
vswitch_id = alicloud_vswitch.vswitch.id
185+
password = var.ecs_instance_password
186+
internet_max_bandwidth_out = 10
187+
}
188+
189+
resource "alicloud_ecs_command" "run_command_kibana" {
190+
name = "command-kibana-${local.common_name}"
191+
command_content = base64encode(<<EOF
192+
cat << EOT >> ~/.bash_profile
193+
export ROS_DEPLOY=true
194+
export ALIBABA_CLOUD_ACCESS_KEY_ID=${alicloud_ram_access_key.ramak.id}
195+
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=${alicloud_ram_access_key.ramak.secret}
196+
EOT
197+
198+
source ~/.bash_profile
199+
200+
# 安装Docker
201+
yum install -y yum-utils device-mapper-persistent-data lvm2
202+
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
203+
yum -y install docker-ce
204+
docker --version
205+
systemctl start docker
206+
systemctl enable docker
207+
208+
# 创建项目路径和用于存放数据的目录
209+
mkdir sls-kibana
210+
cd sls-kibana
211+
mkdir data
212+
chmod 777 data
213+
214+
# 在项目路径下创建.env文件
215+
cat << EOJ >> .env
216+
ES_PASSWORD=${var.ecs_instance_password}
217+
SLS_ENDPOINT=${var.region}.log.aliyuncs.com
218+
SLS_PROJECT=${alicloud_log_project.sls_project.project_name}
219+
# 需要提前创建RAM用户,且需要为RAM用户授予Logstore的查询权限
220+
# ECS RAM角色,请参见:https://help.aliyun.com/zh/ecs/user-guide/attach-an-instance-ram-role-to-an-ecs-instance
221+
# ECS RAM角色授权,请参见:https://help.aliyun.com/zh/sls/compatibility-between-log-service-and-elasticsearch#de61167fc0lqi
222+
SLS_ACCESS_KEY_ID=${alicloud_ram_access_key.ramak.id}
223+
SLS_ACCESS_KEY_SECRET=${alicloud_ram_access_key.ramak.secret}
224+
EOJ
225+
226+
# 在项目路径下创建docker-compose.yaml文件
227+
cat << EOK >> docker-compose.yaml
228+
services:
229+
es:
230+
image: sls-registry.cn-hangzhou.cr.aliyuncs.com/kproxy/elasticsearch:7.17.26
231+
environment:
232+
- "discovery.type=single-node"
233+
- "ES_JAVA_OPTS=-Xms2G -Xmx2G"
234+
- ELASTIC_USERNAME=elastic
235+
- ELASTIC_PASSWORD=${var.ecs_instance_password}
236+
- xpack.security.enabled=true
237+
volumes:
238+
- ./data:/usr/share/elasticsearch/data
239+
kproxy:
240+
image: sls-registry.cn-hangzhou.cr.aliyuncs.com/kproxy/kproxy:2.1.4
241+
depends_on:
242+
- es
243+
environment:
244+
- ES_ENDPOINT=es:9200
245+
- SLS_ENDPOINT=${var.region}.log.aliyuncs.com
246+
- SLS_PROJECT=${alicloud_log_project.sls_project.project_name}
247+
- SLS_ACCESS_KEY_ID=${alicloud_ram_access_key.ramak.id}
248+
- SLS_ACCESS_KEY_SECRET=${alicloud_ram_access_key.ramak.secret}
249+
kibana:
250+
image: sls-registry.cn-hangzhou.cr.aliyuncs.com/kproxy/kibana:7.17.26
251+
depends_on:
252+
- kproxy
253+
environment:
254+
- ELASTICSEARCH_HOSTS=http://kproxy:9201
255+
- ELASTICSEARCH_USERNAME=elastic
256+
- ELASTICSEARCH_PASSWORD=${var.ecs_instance_password}
257+
- XPACK_MONITORING_UI_CONTAINER_ELASTICSEARCH_ENABLED=true
258+
ports:
259+
- "5601:5601"
260+
# 这个服务组件是可选的,作用是自动创建kibana index pattern
261+
index-patterner:
262+
image: sls-registry.cn-hangzhou.cr.aliyuncs.com/kproxy/kproxy:2.1.4
263+
command: /usr/bin/python3 -u /workspace/create_index_pattern.py
264+
depends_on:
265+
- kibana
266+
environment:
267+
- KPROXY_ENDPOINT=http://kproxy:9201
268+
- KIBANA_ENDPOINT=http://kibana:5601
269+
- KIBANA_USER=elastic
270+
- KIBANA_PASSWORD=${var.ecs_instance_password}
271+
- SLS_ACCESS_KEY_ID=${alicloud_ram_access_key.ramak.id}
272+
- SLS_ACCESS_KEY_SECRET=${alicloud_ram_access_key.ramak.secret}
273+
EOK
274+
275+
# 启动Kibana
276+
docker compose up -d
277+
docker compose ps
278+
EOF
279+
)
280+
working_dir = "/root"
281+
type = "RunShellScript"
282+
timeout = 3600
283+
}
284+
285+
resource "alicloud_ecs_invocation" "invoke_script_kibana" {
286+
instance_id = [alicloud_instance.ecs_instance_kibana.id]
287+
command_id = alicloud_ecs_command.run_command_kibana.id
288+
timeouts {
289+
create = "15m"
290+
}
291+
depends_on = [alicloud_instance.ecs_instance_kibana]
292+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "ecs_login_address" {
2+
description = "生成日志的ECS实例的登录地址。通过此地址登录ECS后,在本地查看生成日志文件的命令为:tail -f /tmp/sls-monitor-test.log"
3+
value = format("https://ecs-workbench.aliyun.com/?from=ecs&instanceType=ecs&regionId=%s&instanceId=%s&resourceGroupId=", var.region, alicloud_instance.ecs_instance.id)
4+
}
5+
6+
output "sls_logsearch_url" {
7+
description = "SLS日志查询入口"
8+
value = format("https://sls.console.aliyun.com/lognext/project/%s/logsearch/%s?slsRegion=%s", alicloud_log_project.sls_project.project_name, alicloud_log_store.sls_log_store.logstore_name, var.region)
9+
}
10+
11+
output "kibana_management_url" {
12+
description = "Kibana管理界面入口,登录用户名为elastic,登录密码为您在配置时传入的密码"
13+
value = format("http://%s:5601", alicloud_instance.ecs_instance_kibana.public_ip)
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
variable "region" {
2+
type = string
3+
default = "cn-hangzhou"
4+
}
5+
6+
variable "instance_type" {
7+
type = string
8+
default = "ecs.e-c1m2.large"
9+
description = "实例类型"
10+
}
11+
12+
variable "instance_type_xlarge" {
13+
type = string
14+
default = "ecs.e-c1m2.xlarge"
15+
description = "高性能实例类型"
16+
}
17+
18+
variable "ecs_instance_password" {
19+
type = string
20+
sensitive = true
21+
description = "服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)"
22+
}

0 commit comments

Comments
 (0)