前言
Prometheus
Prometheus 是有 SoundCloud 开发的开源监控零碎和时序数据库,基于 Go 语言开发。通过基于 HTTP 的 pull 形式采集时序数据,通过服务发现或动态配置去获取要采集的指标服务器,反对多节点工作,反对多种可视化图表及仪表盘。
贴一下官网提供的架构图:
Pormetheus 几个次要模块有,Server,Exporters,Pushgateway,PromQL,Alertmanager,WebUI等,次要逻辑如下:
- Prometheus server 定期从动态配置的 targets 或者服务发现的 targets 拉取数据。
- 当新拉取的数据大于配置内存缓存区时,Prometheus 会将数据长久化到磁盘(如果应用 remote storage 将长久化到云端)。
- Prometheus 配置 rules,而后定时查问数据,当条件触发时,会将 alert 推送到配置的 Alertmanager。
- Alertmanager 收到正告时,会依据配置,聚合、去重、降噪等操作,最初发送正告。
- 能够应用 API,Prometheus Console 或者 Grafana 查问和聚合数据。
Grafana
Grafana 是一个开源的度量剖析及可视化套件。通过拜访数据库(如InfluxDB、Prometheus),展现自定义图表。
Exporter
Exporter 是 Prometheus 推出的针对服务器状态监控的 Metrics 工具。目前开发中常见的组件都有对应的 exporter 能够间接应用。常见的有两大类,一种是社区提供的,蕴含数据库,音讯队列,存储,HTTP服务,日志等,比方 node_exporter,mysqld_exporter等;还有一种是用户自定义的 exporter,能够基于官网提供的 Client Library 创立本人的 exporter 程序。
每个 exporter 的一个实例被称为 target,Prometheus 通过轮询的形式定期从这些 target 中获取样本数据。
原理简介
装置数据收集器 node-exporter
装置 node-exporter
cd /optwget https://github.com/prometheus/node_exporter/releases/download/v1.4.0-rc.0/node_exporter-1.4.0-rc.0.linux-amd64.tar.gztar xvf node_exporter-1.4.0-rc.0.linux-amd64.tar.gzmv node_exporter-1.4.0-rc.0.linux-amd64 node_exportermv node_exporter /usr/local/
运行如下命令测试 node-exporter 收集器启动状况,失常状况下会输入服务端口。
/usr/local/node_exporter/node_exporter
增加到零碎服务
vim /etc/systemd/system/node_exporter.service
增加如下内容
[Unit]Description=mysqld_exporterAfter=network.target[Service]ExecStart=/usr/local/node_exporter/node_exporterRestart=on-failure[Install]WantedBy=multi-user.target
加载并重启服务
# 加载配置systemctl daemon-reload# 启动服务systemctl restart node_exporter.service# 查看服务状态systemctl status node_exporter.service# 配置开机启动systemctl enable node_exporter.service
查看数据收集状况
从新起一个终端,查看数据收集状况。也能够在浏览器中查看。
curl http://127.0.0.1:9100/metrics
装置 prometheus 和 grafana
装置 docker&docker-compose
本文介绍的装置办法是基于 docker-compose 的,所以须要先装置相干 docker 环境。相干办法能够见笔者的其余文章,本文中不做具体介绍。
装置 prometheus 和 grafana
能够间接 clone 这个我的项目来疾速搭建:
https://github.com/FX-Max/docker-install-everything/tree/master/prometheus
该我的项目是笔者弄的一个应用 docker-compose 搭建软件开发常见服务的我的项目,大家感觉有帮忙,能够帮忙点个 star,感激。
依据理论状况,批改 prometheus.yml 文件中的内容,将ip批改为下面装置了 node-exporter 的服务器ip即可。
而后在该目录下执行 docker-compose up -d
即可,docker ps
查看服务启动状况。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6f360e9ab242 grafana/grafana "/run.sh" 25 hours ago Up 25 hours 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp grafana97b92b65aca6 prom/prometheus "/bin/prometheus --c…" 25 hours ago Up 21 hours 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp prometheus3f5906f07bf6 prom/pushgateway "/bin/pushgateway" 25 hours ago Up 25 hours 0.0.0.0:9091->9091/tcp, :::9091->9091/tcp pushgatewayf556168c1b8b prom/alertmanager "/bin/alertmanager -…" 25 hours ago Up 25 hours 0.0.0.0:9093->9093/tcp, :::9093->9093/tcp alertmanager
docker-compose.yml 内容:
version: "3"services: prometheus: image: prom/prometheus container_name: prometheus user: root# restart: always ports: - "9090:9090" volumes: - ./conf/prometheus:/etc/prometheus - ./data/prometheus/prometheus_db:/prometheus command: - '--config.file=/etc/prometheus/prometheus.yml' - '--storage.tsdb.path=/prometheus' - '--web.console.libraries=/usr/share/prometheus/console_libraries' - '--web.console.templates=/usr/share/prometheus/consoles' networks: - net-prometheus grafana: image: grafana/grafana container_name: grafana user: root# restart: always ports: - "3000:3000" volumes: #- ./conf/grafana:/etc/grafana - ./data/prometheus/grafana_data:/var/lib/grafana depends_on: - prometheus networks: - net-prometheus pushgateway: image: prom/pushgateway container_name: pushgateway user: root# restart: always ports: - "9091:9091" volumes: - ./data/prometheus/pushgateway_data:/var/lib/pushgateway alertmanager: image: prom/alertmanager hostname: alertmanager container_name: alertmanager user: root# restart: always ports: - "9093:9093" volumes: - ./data/prometheus/alertmanager_data:/var/lib/alertmanagernetworks: net-prometheus:
prometheus.yml 内容:
global: scrape_interval: 5s evaluation_interval: 5s external_labels: monitor: 'dashboard'alerting: alertmanagers: - static_configs: - targets: - "alertmanager:9093"rule_files: #- 'alert.rules'scrape_configs: - job_name: 'prometheus' scrape_interval: 5s static_configs: - targets: ['prometheus:9090'] - job_name: node static_configs: - targets: ['192.168.0.103:9100','pushgateway:9091'] - job_name: 'mysql-131' static_configs: - targets: ['192.168.0.131:9104'] labels: instance: mysql
查看 prometheus
拜访 http://127.0.0.1:9090/targets
,成果如下,下面咱们通过 node_exporter 收集的节点状态是 up 状态。
配置 Grafana
拜访 http://127.0.0.1:3000
,登录 Grafana,默认的账号密码是 admin:admin,首次登录须要批改默认明码。
依照如下增加 data sources,将 prometheus 增加到 data sources 中。
增加 prometheus 服务地址,此处因为服务是基于 docker-compose 构建的,没有填写ip,间接填写服务名即可。
增加监控模版
输出官网模版 id,1860,点击 load。而后依照下图抉择确认即可。
导入胜利后,会主动跳转到监控面板页面,如下图。
结语
本文简略介绍了 prometheus + grafana 服务搭建流程,初步跑通了整个服务。当然它还有很多性能,后续笔者会开新的文章来分享。
参考文档
官网模板库:https://grafana.com/grafana/dashboards/
node 模板:https://grafana.com/grafana/dashboards/1860
MySQL 模板:https://grafana.com/grafana/dashboards/7362
docker 搭建 prometheus&grafana:https://blog.51cto.com/keep11/4261521