从零开始搭建监控系统 (二)
监控系统架构
部署
文件目录
root@chuango:/opt/prometheus# tree -L 1
.
├── docker-compose.yml
├── grafana_data
├── grafana.ini
├── prometheus_data
└── prometheus.yml
# grafana_data, prometheus_data 2个文件为空目录
docker-compose.yml
version: "3.7"
services:
node-exporter:
image: prom/node-exporter:latest
container_name: "node-exporter0"
ports:
- "9100:9100"
restart: always
prometheus:
image: prom/prometheus:latest
container_name: "prometheus0"
restart: always
deploy:
resources:
limits:
cpus: '0.03'
memory: 500M
reservations:
cpus: '0.025'
memory: 200M
ports:
- "9090:9090"
volumes:
- "./prometheus.yml:/etc/prometheus/prometheus.yml"
- "./prometheus_data:/prometheus"
grafana:
image: grafana/grafana:8.5.24
container_name: "grafana0"
ports:
- "3000:3000"
restart: always
deploy:
resources:
limits:
cpus: '0.03'
memory: 500M
reservations:
cpus: '0.025'
memory: 200M
volumes:
- "./grafana_data:/var/lib/grafana"
- "./grafana.ini:/etc/grafana/grafana.ini"
networks:
default:
external:
name: net-dcv2 # 与new_servers同一个网络, 方便使用网络别名通信
prometheus.yml
global:
scrape_interval: 15s
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'node-exporter'
scrape_interval: 5s
metrics_path: /metrics
static_configs:
- targets: ['192.168.1.113:9100'] # 本机ip:9100
grafana.ini
grafana.ini
文件比较特殊,会根据grafana的镜像版本进行更新, 可以通过先运行grafana的方式,将文件映射出来。
docker run -d -v grafana.ini:/etc/grafana/grafana.ini -p 3000:3000 grafana/grafana:8.5.24 grafana0
# 记得删除容器
docker stop grafana0 && docker rm grafana0
若使用 Mysql
作为存储, 需提前在 mysql
创建 grafana databases
REATE DATABASE IF NOT EXISTS grafana;
grafana.ini
# 需对一下配置进行修改
#################################### Server ####################################
[server]
# The public facing domain name used to access grafana from a browser
domain = 192.168.1.113 # 替换为主机ip, 发送邮件连接时的域名
#################################### Database ####################################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.
# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql # 使用mysql存储
host = 192.168.1.113:3306 # mysql的服务ip:port
;name = grafana
;user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = dc123456 # 用户名root的密码
#################################### SMTP / Emailing ##########################
[smtp] # 若使用发送邮件功能, 需提前开启SMTP,POPS协议功能
enabled = true # 若不使用 改为false
host = smtp.chuango.com:465
user = dev01@chuango.com
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = Feishu09091
;cert_file =
;key_file =
skip_verify = true
from_address = dev01@chuango.com
from_name = Grafana
部署
在该目录下执行docker-compose --compatibility up -d
,输出正常日志, 则部署监控系统成功
$ docker-compose logs
...
prometheus0 | ts=2023-05-18T08:14:35.302Z caller=repair.go:56 level=info component=tsdb msg="Found healthy block" mint=1684382402583 maxt=1684389600000 ulid=01H0PTH623JBN8
ZBNYW3NSWRWY
prometheus0 | ts=2023-05-18T08:14:36.095Z caller=head.go:587 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
prometheus0 | ts=2023-05-18T08:14:36.152Z caller=head.go:658 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=48.503163ms
prometheus0 | ts=2023-05-18T08:14:36.152Z caller=head.go:664 level=info component=tsdb msg="Replaying WAL, this may take a while"
prometheus0 | ts=2023-05-18T08:14:36.178Z caller=head.go:700 level=info component=tsdb msg="WAL checkpoint loaded"
prometheus0 | ts=2023-05-18T08:14:36.501Z caller=head.go:735 level=info component=tsdb msg="WAL segment loaded" segment=385 maxSegment=389
prometheus0 | ts=2023-05-18T08:14:36.919Z caller=head.go:735 level=info component=tsdb msg="WAL segment loaded" segment=386 maxSegment=389
prometheus0 | ts=2023-05-18T08:14:37.173Z caller=head.go:735 level=info component=tsdb msg="WAL segment loaded" segment=387 maxSegment=389
prometheus0 | ts=2023-05-18T08:14:37.389Z caller=head.go:735 level=info component=tsdb msg="WAL segment loaded" segment=388 maxSegment=389
prometheus0 | ts=2023-05-18T08:14:37.390Z caller=head.go:735 level=info component=tsdb msg="WAL segment loaded" segment=389 maxSegment=389
prometheus0 | ts=2023-05-18T08:14:37.390Z caller=head.go:772 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=25.972689ms wal_replay_duratio
n=1.211796899s wbl_replay_duration=145ns total_replay_duration=1.286326914s
prometheus0 | ts=2023-05-18T08:14:37.398Z caller=main.go:1026 level=info fs_type=EXT4_SUPER_MAGIC
prometheus0 | ts=2023-05-18T08:14:37.398Z caller=main.go:1029 level=info msg="TSDB started"
prometheus0 | ts=2023-05-18T08:14:37.399Z caller=main.go:1209 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
prometheus0 | ts=2023-05-18T08:14:37.417Z caller=main.go:1246 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuratio
n=18.30677ms db_storage=923ns remote_storage=1.017<C2><B5>s web_handler=316ns query_engine=789ns scrape=17.871741ms scrape_sd=104.021<C2><B5>s notify=760ns notify_sd=1.809<C2>
<B5>s rules=1.403<C2><B5>s tracing=3.31<C2><B5>s
登录并查看mysql
下相关表创建成功:
mysql> use grafana;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-----------------------------+
| Tables_in_grafana |
+-----------------------------+
| alert |
| alert_configuration |
| alert_configuration_history |
| alert_image |
| alert_instance |
| alert_notification |
| alert_notification_state |
| alert_rule |
| alert_rule_tag |
| alert_rule_version |
...
相关文档
从零开始搭建监控系统 (一)