×
新网 > 虚机资讯 > 正文

Prometheus简单搭建

  • 作者:zccc
  • 来源:网络
  • 2020-09-01 15:03:16

系统运维 一.Prometheus介绍Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,拥有非常活跃的开

系统运维

一.Prometheus介绍
Prometheus(普罗米修斯)是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,拥有非常活跃的开发人员和用户社区。为强调开源及独立维护,Prometheus于2016年加入云原生云计算基金会(CNCF),成为继Kubernetes之后的第二个托管项目。
https://prometheus.io
https://github.com/prometheus

作为新一代的监控框架,Prometheus 具有以下特点:
• 多维数据模型:由度量名称和键值对标识的时间序列数据
• PromSQL:一种灵活的查询语言,可以利用多维数据完成复杂的查询
• 不依赖分布式存储,单个服务器节点可直接工作
• 基于HTTP的pull方式采集时间序列数据
• 推送时间序列数据通过PushGateway组件支持
• 通过服务发现或静态配置发现目标
• 多种图形模式及仪表盘支持
Prometheus适用于以机器为中心的监控以及高度动态面向服务架构的监控。

二.Prometheus部署
转载:https://blog.gmem.cc/prometheus-study-note
1.安装环境:
server1:centos7.6 主节点 190.168.3.250
server2:centos7.6 节点1 190.168.3.251

为您的平台下载最新版本的Prometheus,然后解压缩并运行它:

https://prometheus.io/download/

https://prometheus.io/docs/prometheus/latest/getting_started/

2.二进制安装
tar xvfz prometheus-2.15.0.linux-amd64.tar.gz
cd prometheus-2.15.0.linux-amd64
mv prometheus-2.15.0.linux-amd64 /usr/local/prometheus

启动
./prometheus --config.file=prometheus.yml

为了方便,加入到服务配置文件里
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target

重启服务
systemctl daemon-reload
systemctl start prometheus.service

3.容器安装
https://prometheus.io/docs/prometheus/latest/installation/
prometheus.yml通过运行以下命令将您从主机绑定:
docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \\
prom/prometheus
或者为配置使用额外的卷:
docker run -p 9090:9090 -v /prometheus-data \\
prom/prometheus --config.file=/prometheus-data/prometheus.yml

4.访问web
http://190.168.3.250:9090访问自己的状态页面
可以通过访问localhost:9090验证Prometheus自身的指标:190.168.3.250:9090/metrics

三.配置监控服务
1.配置Prometheus监控本身
全局配置文件简介
有关配置选项的完整,请参阅:https://prometheus.io/docs/prometheus/latest/configuration/configuration/
Prometheus以scrape_interval规则周期性从监控目标上收集数据,然后将数据存储到本地存储上。scrape_interval可以设定全局也可以设定单个metrics。
Prometheus以evaluation_interval规则周期性对告警规则做计算,然后更新告警状态。evaluation_interval只有设定在全局。

global:全局配置
alerting:告警配置
rule_files:告警规则
scrape_configs:配置数据源,称为target,每个target用job_name命名。又分为静态配置和服务发现

global:
默认抓取周期,可用单位ms、smhdwy #设置每15s采集数据一次,默认1分钟
[ scrape_interval: <duration> | default = 1m ]
默认抓取超时
[ scrape_timeout: <duration> | default = 10s ]
估算规则的默认周期 # 每15秒计算一次规则。默认1分钟
[ evaluation_interval: <duration> | default = 1m ]
和外部系统(例如AlertManager)通信时为时间序列或者警情(Alert)强制添加的标签列表
external_labels:
[ <labelname>: <labelvalue> ... ]

规则文件列表
rule_files:
[ - <filepath_glob> ... ]

抓取配置列表
scrape_configs:
[ - <scrape_config> ... ]

Alertmanager相关配置
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]

远程读写特性相关的配置
remote_write:
[ - <remote_write> ... ]
remote_read:
[ - <remote_read> ... ]

vi prometheus.yml
下面就是拉取自身服务采样点数据配置
scrape_configs:
别监控指标,job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上

job_name: \'prometheus\'
覆盖global的采样点,拉取时间间隔5s
scrape_interval: 5s
static_configs: targets: [\'localhost:9090\']

最下面,静态配置监控本机,采集本机9090端口数据

注:每次修改配置完成,用promtool检测配置文件是否正确
[root@server1 prometheus]# ./promtool check config prometheus.yml

重启服务
可以用kill -hup 进程id 自动加载新配置文件

查看targets可以看到节点正常

也可以在这里搜寻收集来的数据

2.配置服务发现监控linux主机及相关服务
在server2 190.168.3.251上安装node_exporter
使用文档:https://prometheus.io/docs/guides/node-exporter/
GitHub:https://github.com/prometheus/node_exporter
exporter列表:https://prometheus.io/docs/instrumenting/exporters/

在server2安装节点采集器,mysql服务,nginx服务
tar zxf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter
cd /usr/local/node_exporter/
./node_exporter --help

服务发现,我们这里使用file_sd_configs,写监控配置文件,服务发现node节点

mkdir sd_config创建服务发现目录

写采集地址targets
[root@server1 prometheus]# vi sd_config/node.yml

方便管理服务,将宿主机节点监控采集node_exporter加入到服务配置文件里,

如果要监控节点的系统服务,需要在后面添加名单参数
--collector.systemd.unit-whitelist=".+" 从systemd中循环正则匹配单元
--collector.systemd.unit-whitelist="(docker|sshd|nginx).service" 白名单,收集目标

/usr/bin/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service

在http://190.168.3.250:9090/graph已经能看到节点采集到的宿主机信息,已经可以看到监控到了服务,值为1就是服务正常

补充:
也可以将之前190.168.3.250上监控本机的9090的静态采集改成文件服务发现的形式

[root@server1 prometheus]# vi sd_config/test.yml ,其中的加上了标签,添加采集地址,可以在targets里使用标签查找

四.使用grafana前端展示数据并监控docker服务

1.cAdvisor采集容器信息
使用cAdvisor(Container Advisor)用于收集正在运行的容器资源使用和性能信息。
docker run \\
--volume=/:/rootfs:ro \\
--volume=/var/run:/var/run:ro \\
--volume=/sys:/sys:ro \\
--volume=/var/lib/docker/:/var/lib/docker:ro \\
--volume=/dev/disk/:/dev/disk:ro \\
--publish=8080:8080 \\
--detach=true \\
--name=cadvisor \\

http://190.168.3.250:8080/访问web端口,已经采集到了容器的数据

2.Grafana

https://grafana.com/grafana/download

Grafana是一个开源的度量分析和可视化系统,Grafana支持查询普罗米修斯。自Grafana 2.5.0(2015-10-28)以来,包含了Prometheus的Grafana数据源。

在官网上下载https://grafana.com/grafana/download?platform=docker
我们这里用的是docker版的
docker run -d --name=grafana -p 3000:3000 grafana/grafana

跑起来后,190.168.3.250:3000访问web接口
密码账号都是admin,第一次登陆需要修改密码
添加数据源


已经有一个Prometheus数据源

3.添加监控宿主机模板

输入9276,导入

修改名字和数据库

完成,查看最后模板效果

4.查看docker监控
添加新job,修改采集配置文件prometheus.yml
[root@server1 prometheus]# vi prometheus.yml

检查配置文件后,重启主服务 ./promtool check config prometheus.yml

同样方法在Grafana下载模板,导入193
也可以在https://grafana.com/dashboards/下载别的模板

五.监控Mysql服务

MYSQL_exporter:用于收集MySQL性能信息。

下载https://github.com/prometheus/mysqld_exporter
监控模板https://grafana.com/dashboards/7362

我们在server2 190.168.3.251上配置,需要下载mysqld_exporter数据库采集和安装mariadb

登录mysql为exporter创建账号,以便可以采集数据:
mysql> CREATE USER \'exporter\'@\'localhost\' IDENTIFIED BY \'123456\';
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON . TO \'exporter\'@\'localhost\';

vi .my.cnf ,添加用户隐藏的数据库账号,prometheus监控mysql需要用这个账号采集

在server1 190.168.3.250修改采集配置文件
[root@server1 prometheus]# vi prometheus.yml

检查配置文件并重启服务,systemctl restart prometheus.service

[root@server2 mysql_exporter]# ./mysqld_exporter --config.my-cnf=.my.cnf

数据库信息已经采集到了
http://190.168.3.251:9104/metrics

导入模板后7362后

六.邮件告警
1.告警介绍
在Prometheus平台中,警报由独立的组件Alertmanager处理。通常情况下,我们首先告诉Prometheus Alertmanager所在的位置,然后在Prometheus配置中创建警报规则,最后配置Alertmanager来处理警报并发送给接收者(邮件,webhook、slack等)。

地址1:https://prometheus.io/download/
地址2:https://github.com/prometheus/alertmanager/releases
设置告警和通知的主要步骤如下:
第一步 部署Alertmanager
第二步 配置Prometheus与Alertmanager通信
第三步 在Prometheus中创建告警规则

2.配置Prometheus与Alertmanager通信

需要在官网下载以上的报警包

修改告警媒介,使用邮箱发送
[root@server1 alertmanager-0.16.0.linux-amd64]# vi alertmanager.yml

注意:上面的验证密码不是邮箱密码,是授权码

修改完检查配置文件alertmanager.yml是否正确,然后重启服务
./alertmanager --config.file alertmanager.yml
systemctl restart prometheus.service

修改prometheus连接告警模块,并设定告警规则
[root@server1 prometheus]# vi prometheus.yml

[root@server1 prometheus]# vi rules/test.yml
参照官网告警上写的

groups:

name: example # 报警规则组名称
rules: alert: InstanceDown
expr: up == 0
for: 1m #持续时间 , 表示持续一分钟获取不到信息,则触发报警
labels:
severity: page # 自定义标签
annotations:
summary: "Instance {{ $labels.instance }} down" # 自定义摘要
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes." # 自定义具体描述

3.测试邮箱告警
我们把server2 上的mysql监控关闭,测试告警
http://190.168.3.250:9090/alerts,已经触发

我们看邮箱,收到报警

新网虚拟主机

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading