0x00 概述

prometheus+grafana是目前较为流行的开源监控解决方案,prometheus丰富的exporter加上grafana灵活的配置以及炫酷的仪表盘界面基本满足系统的日常监控需求,搭建一个完整的监控系统需要安装以下程序

  • prometheus:负责数据存储和数据查询
  • grafana:负责数据展示
  • exporter:负责数据收集,不同的场景会有不同的exporter,基本上常用的场景都有相应的exporter,本文安装了node-exporter作为示例,node-expoerter负责收集主机相关信息
  • alertmanager:负责预警通知

另外本文会介绍两种安装方式,二进制程序安装和docker安装,读者可根据需要自行选择安装方式

如果只是想快速搭建prometheus环境用于技术研究,建议用docker方式进行安装

0x01 配置环境信息

如果是新服务器,需要设置hostname和hosts信息,如果是在原有服务器上安装可跳过该步骤

[root]# hostnamectl set-hostname promethues
[root]# su
hostname
cat <<EOF >> /etc/hosts
127.0.0.1 promethues
EOF

0x02 安装prometheus

  • 安装程序
[root]
➜ wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
➜ tar -xvf prometheus-2.27.1.linux-amd64.tar.gz
➜ cd prometheus-2.27.1.linux-amd64
➜ cp prometheus /usr/sbin/
➜ cp promtool /usr/sbin/
➜ mkdir -p /etc/prometheus
➜ cp prometheus.yml /etc/prometheus/
  • 测试
➜ ./prometheus --config.file=/etc/prometheus/prometheus.yml
...
level=info ts=2021-05-30T04:13:51.272Z caller=main.go:775 msg="Server is ready to receive web requests".

当出现Server is ready to receive web requests表示服务启动,访问http://localhost:9090

  • 设置systemctl启动

上面的启动方式存在几个问题

  • 前台启动,ctrl+C或者退出就关闭
  • 需要手动指定配置文件

可以设置成systemctl启动方式,通过systemctl进行管理,创建文件

➜ mkdir -p /data/prometheus
➜ cat > /usr/lib/systemd/system/prometheus.service <<-EOF
[Unit]
Description=Prometheus
After=network-online.target
[Service]
ExecStart=/usr/sbin/prometheus --config.file=/etc/prometheus/prometheus.yml --web.read-timeout=5m  --storage.tsdb.retention=30d --storage.tsdb.path=/data/prometheus --query.timeout=2m
User=root
[Install]
WantedBy=multi-user.target
EOF
➜ systemctl daemon-reload #重载配置
➜ systemctl enable prometheus

相关配置信息

  • config.file:指定配置文件
  • web.read-timeout:请求链接的最大等待时间,防止太多的空闲链接占用资源
  • storage.tsdb.retention:数据保存时间d代表天
  • storage.tsdb.path:数据保存路径
  • query.timeout:查询超时
启动
➜ systemctl start prometheus.service
➜ systemctl status prometheus.service
● prometheus.service - Prometheus
   Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-05-30 13:32:07 CST; 9s ago
 Main PID: 13419 (prometheus)

访问http://localhost:9090登录系统

0x03 安装grafana

  • 安装
[root]
wget https://dl.grafana.com/oss/release/grafana-7.5.7-1.x86_64.rpm
yum install grafana-7.5.7-1.x86_64.rpm
  • 启动
systemctl start grafana-server
  • 配置文件位置
/etc/grafana/grafana.ini

访问http://localhost:3000测试系统

0x04 安装 node-expoerter

  • 安装
[root]
➜ wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
➜ tar -xvf node_exporter-1.1.2.linux-amd64.tar.gz
➜ cd node_exporter-1.1.2.linux-amd64
➜ cp node_exporter /usr/sbin/
➜ cat > /usr/lib/systemd/system/node-exporter.service <<-EOF
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
[Service]
ExecStart=/usr/sbin/node_exporter
User=root
[Install]
WantedBy=multi-user.target
EOF
➜ systemctl daemon-reload #重载配置
➜ systemctl enable node-exporter
  • 启动
[root]
➜ systemctl start node-exporter
➜ systemctl status node-exporter
● node-exporter.service - Prometheus Node Exporter
   Loaded: loaded (/usr/lib/systemd/system/node-exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-05-30 14:13:40 CST; 6s ago
 Main PID: 15598 (node_exporter)

如果出现错误,可以查看系统日志文件/var/log/messages,如果启动成功可以打开http://localhost:9100/metrics查看

0x07 安装alertmanager

  • 安装
[root]
➜ wget https://github.com/prometheus/alertmanager/releases/download/v0.22.1/alertmanager-0.22.1.linux-amd64.tar.gz
➜ tar -xvf alertmanager-0.22.1.linux-amd64.tar.gz
➜ cd alertmanager-0.22.1.linux-amd64
➜ cp alertmanager /usr/sbin/
➜ mkdir -p /etc/alertmanager/
➜ cp alertmanager.yml /etc/alertmanager/
➜ cp amtool /usr/sbin/
➜ cat > /usr/lib/systemd/system/alertmanager.service <<-EOF
[Unit]
Description=Alertmanager
After=network-online.target
[Service]
ExecStart=/usr/sbin/alertmanager --config.file=/etc/alertmanager/alertmanager.yml
User=root
[Install]
WantedBy=multi-user.target
EOF
➜ systemctl daemon-reload #重载配置
➜ systemctl enable alertmanager
➜ systemctl start alertmanager
➜ systemctl status alertmanager #查看启动状态

登录http://localhost:9093验证安装是否成功

0x07 docker安装

以上所有程序都支持docker安装,相比二进制程序安装,docker更为简单基本上都是一行命令运行,推荐使用docker方式进行安装

  • 安装prometheus
docker run -p 9090:9090 --name='prometheus' -d prom/prometheus
  • 安装grafana
docker run -p 3000:3000 --name='grafana' -d grafana/grafana
  • 安装node_exporter

node-exporter是负责监视主机,因此不大建议部署在容器中,但node_exporter本身也是支持在容器中运行

docker run -p 9100:9100 --name="node_exporter" --net="host" --pid="host" -v "/:/host:ro,rslave" -d prom/node-exporter --path.rootfs=/host
  • 安装 alertmanager
docker run -d -p 9093:9093 -v /you/path/alert.yml:/etc/alertmanager/alertmanager.yml --name="alertmanager" -d prom/alertmanager

其中/you/path/alert.ymlw为预警规则配置文件,配置规则完整配置可以参考这里下面是一个使用qq邮件服务器作为邮箱预警的配置样例,后面的章节会详细介绍,这里就不进行展开

global:
  # The smarthost and SMTP sender used for mail notifications.
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_from: '896442328@qq.com'
  smtp_auth_username: '896442328@qq.com'
  smtp_auth_password: '*****'
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 5s
  group_interval: 5s
  repeat_interval: 5m
  receiver: 'email'
receivers:
<li>name: 'email'</li>
  email_configs:
  - to: 'jianfeng.zheng@definesys.com'
    send_resolved: true
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']
,
Trackback

no comment untill now

Add your comment now