服务搭建篇(一) 搭建基于prometheus + node_exporter + grafana + Alertmanager 的监控报警系统 , 保姆级教程

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Alertmanager处理客户端应用程序(如Prometheus服务器)发送的警报。它负责重复数据删除、分组,并将它们路由到正确的接收器集成,如电子邮件、PagerDuty或OpsGenie。它还负责静音和抑制警报

一. 介绍

1.Prometheus:

一个监控系统,集成在项目中可以对项目运行的一些情况进行监控,并将监控的数据存储在文件中,并可以通过自带的页面端查询到这些数据。


2.node_exporter:

一个监控系统,集成在服务器中,可以监控主机的运行状况,可以将node_exporter集成到Prometheus的管理端,通过Prometheus提供的页面查询到相应的数据


3.grafana:

一个显示页面,比Prometheus自带的更好看,更形象,可以通过给grafana配置Prometheus的数据源,让grafana显示Prometheus的数据。当然除了Prometheus,它也支持其他的数据源


4.Alertmanager

Alertmanager处理客户端应用程序(如Prometheus服务器)发送的警报。它负责重复数据删除、分组,并将它们路由到正确的接收器集成,如电子邮件、PagerDuty或OpsGenie。它还负责静音和抑制警报


安装机器为 : Centos 7.6


二. 防火墙设置

1.将所有机器的防火墙关闭

systemctl stop firewalld.service

2.关闭防火墙开机自启动

systemctl disable firewalld.service

1.安装prometheus

1.1.下载prometheus

wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz

1.2.解压

tar -xzvf prometheus-2.8.1.linux-amd64.tar.gz

1.3.修改名称(非必要)

mv prometheus-2.8.1.linux-amd64/ prometheus/

1.4.创建prometheus.service文件

vim /etc/systemd/system/prometheus.service

1.5.增加配置

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
User=root
Restart=on-failure
#Change this line if you download the
#Prometheus on different path user
ExecStart=/home/prometheus/prometheus/prometheus \
--config.file=/home/prometheus/prometheus/prometheus.yml \
--storage.tsdb.path=/home/prometheus/prometheus/data
[Install]
WantedBy=multi-user.target


1.6.重新载入配置(修改了prometheus.service文件之后执行)

systemctl daemon-reload

1.7.启动

systemctl start prometheus

1.8.验证

输入 netstat -plntu |grep 9090 或者 访问ip+9090

1.9其他命令

重启 : systemctl restart prometheus
查看状态 :systemctl status prometheus
停止: systemctl stop prometheus
开机自启动 : systemctl enable prometheus


2.安装 node_exporter

2.1.下载

wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz

2.2.解压

tar -xzvf node_exporter-0.17.0.linux-amd64.tar.gz

2.3.修改名称(非必要)

mv node_exporter-0.17.0.linux-amd64 node_exporter

2.4.创建node_exporter.service

vim /etc/systemd/system/node_exporter.service

2.5.添加配置

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=root
ExecStart=/home/software/node_exporter/node_exporter
[Install]
WantedBy=default.target


2.6.重新载入设置

systemctl daemon-reload

2.7.启动

systemctl start node_exporter

2.8 验证

netstat -plntu |grep 9100

或者

systemctl status node_exporter

2.9 将node_exporter添加到Prometheus服务器

vim prometheus/prometheus.yml

2.10 在’scrape_config’行下,通过复制粘贴下面的配置添加新的job_name node_exporter

一定要注意yml的格式 ! ! !

- job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']

2.11 重启promethues

systemctl restart prometheus

2.12 其他命令

重启 : systemctl restart node_exporter
查看状态 :systemctl status node_exporter
停止: systemctl stop node_exporter
开机自启动 : systemctl enable node_exporter


2.13 测试prometheus + node_exporter

如下图所示 , 输入node_memory_MemTotal_bytes , 点击执行按钮

8c6098e695d3dcab1097dcbbfaa4f3ef_b8c90e2c83ab485fae6954a5723d9ccf.png

通过http://ip:9100/metrics可以获得node_exporter原始数据,如下所示

eeeabc44d359638d6d17e753d33cb902_77926a1fa5e5412280d1cbb2b71b44a3.png


3.安装grafana

3.1.下载

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.0.3-1.x86_64.rpm
或者 直接访问
https://dl.grafana.com/enterprise/release/grafana-enterprise-9.0.3-1.x86_64.rpm下载

3.2.使yum安装

sudo yum install grafana-enterprise-9.0.3-1.x86_64.rpm


3.3.启动

systemctl start grafana-server


3.4.验证

访问http://ip:3000


3.5.整合prometheus

登录grafana , 用户名密码默认 admin

1bc44c1161ef5b763bda2e0071313b58_8a103efd863e4bda99b9d8abc884d898.png

b436ead42075e8756d52a5a9e8fb3747_ded6e91936b24b6cba346b086e458d2d.png

点击即可


f473b3bff9b47a2549e8b8e0794b7690_ff9d468c122545b3ae01f83cbff54914.png

ddff7c30d49343960dc169d1197efb62_32b0f672c36b42c180b0365f3b390878.png

配置之后点击 save & test 即可

81490d6a36be010d39085f6c28b78617_9fc09f1afaa34df29d2b97bfb82e1b13.png

随便选择一个模板 , 点击导入即可

822a46ed051055bd565018604eb5197d_3d2cbcc2445d40a8b636a4a97972b4e8.png

点击这个地方就可以看到数据了

0d6481aac11bdcb1ff157768cfd38224_e06640f416294ad5a0db44175dfda038.png



3.6.监听多节点

  - job_name: 'cluster-134'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'cluster-135'
    static_configs:
      - targets: ['192.168.154.135:9100']

然后换个主题

062002ce34b96c864f7d8774f6272369_3f35bf2b16ab47f69d188f6b146218a6.png


上传一个json文件 , 可以在这个网址找 : https://grafana.com/grafana/dashboards/

也可以用这个 :https://grafana.com/grafana/dashboards/11074-node-exporter-for-prometheus-dashboard-en-v20201010/

98274f7f3c590c25acbd2e202a8158a0_e90cb677eb024332b3f7d3488d33d448.png


上传之后需要选择数据库 , 就选上面配置好的就行

3c32761e2849ffe361a28a54bf588613_8bd1a1ad2707448f93aecea4f4e3ac37.png

然后保存

caaded4f92b2deca1140ff3ca3825bc0_4f0dda90ac654a6c894dd84159aab61f.png


然后回到首页 , 点一下刚刚配置好的

dc5367c858a97fed135ee8969b35197b_ccd7203f97ae423eb8895237e68c547a.png

就可以切换查看不同机器的运行状态

b0c343e0f0f2188ba65f2416c98c024c_3291ae55515b40eaa750ca0e4a84d1b0.png



3.7其他命令

开机自启动 : systemctl enable grafana-server
停止 : systemctl stop grafana-server
查看状态 : systemctl status grafana-server


4.安装Alertmanager

4.1.下载

wget https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz

4.2.解压

tar xvf alertmanager-0.21.0.linux-amd64.tar.gz

4.3.修改文件名

mv alertmanager-0.21.0.linux-amd64 alertmanager

4.4. 配置启动文件

[Unit]
Description=alertmanager
Documentation=https://prometheus.io/
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
User=root
ExecStart=/home/software/alertmanager/alertmanager --storage.path=/home/software/alertmanager/data/ \
--config.file=/home/software/alertmanager/alertmanager.yml
Restart=always
RestartSec=1
[Install]
WantedBy=default.target

4.5.启动

systemctl start alertmanager
1
4.6. prometheus.yml
alerting:
  alertmanagers:
  - static_configs:
    - targets: ["192.168.154.134:9093"]
rule_files:
   - "rule/*.yml"


4.7.host.yml

在prometheus.yml同一层新建rule文件夹 , 然后再rule文件夹下新建host.yml(文件夹的名称和.yml的名称可以随便修改) , 配置以下内容


groups:
- name: host
  rules:
  - alert: PrometheusTargetMissing
    expr: up == 0
    for: 1m
    labels:
      severity: critical
    annotations:
      summary: 服务器宕机 (instance {{ $labels.instance }})
      description: "服务器宕机,或者node exporter未启动\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - alert: HostOutOfDiskSpace
    expr: (node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes < 5 and ON (instance, device, mountpoint) node_filesystem_readonly == 0
    for: 10s
    labels:
      severity: warning
    annotations:
      summary: 主机磁盘空间不足 (instance {{ $labels.instance }})
      description: "主机磁盘空间不足 (剩余 < 10% )\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - alert: HostHighCpuLoad
    expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[2m])) * 100) > 80
    for: 0m
    labels:
      severity: warning
    annotations:
      summary: CPU使用率过高! (instance {{ $labels.instance }})
      description: "CPU使用率超过 > 80%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"
  - alert: HostOutOfMemory
    expr: node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100 < 10
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: 内存使用率过高 (instance {{ $labels.instance }})
      description: "内存使用率过高 (剩余< 10% )\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}"

4.8.alertmanager.yml

配置接收报警的邮箱如果是qq邮箱 , 那么就需要开通相关服务

bfc91c68d0bbe8e6d6b456fd655a599d_a5631dd70c864dcfaee01ee074ff46c1.png


global:
  smtp_smarthost: 'smtp.163.com:25'
  smtp_from: '2788956780@xxx.com'
  smtp_auth_username: '2788956780@qq.com'
  smtp_auth_password: 'xxxxxxxx'
route:
  group_by: ['alert']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1h
  receiver: 'email-me'
receivers:
- name: 'email-me'
  email_configs:
  - to: 2788956780@qq.com
    send_resolved: true

重启prometheus和alertmanager即可


4.9.验证

我这里有两台机器 134 和135 ,关掉135 一分钟后即可收到报警


4.10.其他命令

重启 : systemctl restart alertmanager
查看状态 :systemctl status alertmanager
停止: systemctl stop alertmanager
开机自启动 : systemctl enable alertmanager


相关文章
|
3月前
|
JavaScript 前端开发 网络协议
​Node.js 教程(一) 基本概念与基本使用
​Node.js 教程(一) 基本概念与基本使用
|
3月前
|
Web App开发 缓存 JavaScript
【安装指南】nodejs下载、安装与配置详细教程
这篇博文详细介绍了 Node.js 的下载、安装与配置过程,为初学者提供了清晰的指南。读者通过该教程可以轻松完成 Node.js 的安装,了解相关配置和基本操作。文章首先介绍了 Node.js 的背景和应用场景,随后详细说明了下载安装包、安装步骤以及配置环境变量的方法。作者用简洁明了的语言,配以步骤图示,使得读者能够轻松跟随教程完成操作。总的来说,这篇文章为初学者提供了一个友好的入门指南,使他们能够顺利开始使用 Node.js 进行开发。
213 1
【安装指南】nodejs下载、安装与配置详细教程
|
4月前
|
Prometheus Cloud Native Java
微服务框架(二十三)Prometheus + Grafana 安装、配置及使用
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 安装、配置及使用 本系列文章中所使用的框架版本为Spring ...
|
4天前
|
Prometheus 监控 Cloud Native
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
Prometheus+Grafana+NodeExporter 打造一款出色的监控系统,帅呆了!
26 2
|
3月前
|
存储 Prometheus Cloud Native
Grafana 系列 - 统一展示 -2-Prometheus 数据源
Grafana 系列 - 统一展示 -2-Prometheus 数据源
|
3月前
|
JSON Prometheus Cloud Native
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
Grafana 系列 - 统一展示 -3-Prometheus 仪表板
|
3月前
|
Prometheus Kubernetes Cloud Native
「译文」使用 Prometheus 和 Grafana 实现 SLO
「译文」使用 Prometheus 和 Grafana 实现 SLO
|
3月前
|
SQL 数据可视化 数据库连接
Grafana图表工具Graph使用教程-通过Grafana绘制折线图
Grafana图表工具Graph使用教程-通过Grafana绘制折线图
|
4月前
|
Prometheus 监控 Cloud Native
微服务框架(二十二)Prometheus + Grafana 可视化监控
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 可视化监控的介绍,下篇为Prometheus + Grafana...
|
4月前
|
Prometheus 监控 Cloud Native
微服务框架(十九)Spring Boot 可视化监控 Prometheus + Grafana
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Spring Boot 通过 micrometer 的监控门面,实现Prometheus + G...