Prometheus部署、操作及Grafana展示

2023-05-16

目录

一、部署Prometheus(192.168.109.18)

1、环境准备工作

2、普罗米修斯的部署

2.1 上传 prometheus-2.37.0.linux-amd64.tar.gz 到 /opt 目录中,并解压

2.2 修改配置文件

2.3 配置系统启动文件,设置开机自启

2.4 开启prometheus,并访问网页验证

二、部署Exporters(192.168.109.20)

1、监控远程Linux主机(192.168.109.20)

1.1 上传 node_exporter-1.3.1.linux-amd64.tar.gz 到 /opt 目录中,并解压

1.2 配置启动文件,设置开机自启

1.3 启动node_exporter

1.4 修改Prometheus服务器的配置文件

1.5 访问prometheus服务器

2、监控远程MySQL

2.1 下载mysqld_exporter组件 

2.2 安装mysqld_exporter组件

2.3 安装mariadb数据库,并授权

2.4 创建一个mariadb配置文件

2.5 配置启动文件,启动mysql_exporter 

2.6 修改Prometheus服务器的配置文件

2.7 访问prometheus服务器

三、部署Grafana进行展示

1、下载安装Grafana(192.168.109.19)

2、配置数据源

3、导入模板

4、为数据源做数据展示

5、导入grafana监控面板

6、Grafana 图形显示 MySQL 监控数据

7、Grafana+onealert报警

7.1 在Grafana中配置Webhook URL

7.2 测试CPU负载告警

四、总结


一、部署Prometheus(192.168.109.18)

1、环境准备工作

服务器类型IP地址组件
Prometheus服务器192.168.109.18Prometheus、node_exporter
grafana服务器192.168.109.19Grafana
agent服务器192.168.109.20node_exporter

2、普罗米修斯的部署

prometheus下载地址:

https://prometheus.io/download/

2.1 上传 prometheus-2.37.0.linux-amd64.tar.gz 到 /opt 目录中,并解压

[root@prometheus ~]# cd /opt
[root@prometheus opt]# ls
cni  containerd  rh
[root@prometheus opt]# rz -E
rz waiting to receive.
[root@prometheus opt]# ls
cni  containerd  prometheus-2.37.0.linux-amd64.tar.gz  rh
[root@prometheus opt]# tar xf prometheus-2.37.0.linux-amd64.tar.gz 
[root@prometheus opt]# mv prometheus-2.37.0.linux-amd64 /usr/local/prometheus
[root@prometheus opt]# cd /usr/local/prometheus
[root@prometheus prometheus]# ls
console_libraries  consoles  LICENSE  NOTICE  prometheus  prometheus.yml  promtool

2.2 修改配置文件

cat /usr/local/prometheus/prometheus.yml | grep -v "^#"
global:					#用于prometheus的全局配置,比如采集间隔,抓取超时时间等
  scrape_interval: 15s			#采集目标主机监控数据的时间间隔,默认为1m
  evaluation_interval: 15s 		#触发告警生成alert的时间间隔,默认是1m
  # scrape_timeout is set to the global default (10s).
  scrape_timeout: 10s			#数据采集超时时间,默认10s

alerting:				#用于alertmanager实例的配置,支持静态配置和动态服务发现的机制
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

rule_files:				#用于加载告警规则相关的文件路径的配置,可以使用文件名通配机制
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:			#用于采集时序数据源的配置
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"		#每个被监控实例的集合用job_name命名,支持静态配置(static_configs)和动态服务发现的机制(*_sd_configs)

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:				#静态目标配置,固定从某个target拉取数据
      - targets: ["localhost:9090"]

2.3 配置系统启动文件,设置开机自启

[root@prometheus prometheus]# vim /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle
  
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

2.4 开启prometheus,并访问网页验证

systemctl start prometheus
systemctl enable prometheus

netstat -natp | grep :9090

浏览器访问:http://192.168.109.18:9090 ,访问到 Prometheus 的 Web UI 界面
点击页面的 Status -> Targets,如看到 Target 状态都为 UP,说明 Prometheus 能正常采集到数据
http://192.168.109.18:9090/metrics ,可以看到 Prometheus 采集到自己的指标数据

通过浏览器访问 http:// 服务器 IP:9090 就可以访问到 Prometheus 的主界面:

默认只监控了本机一台,点 Status→点 Targets→可以看到只监控了本机:

通过 http:// 服务器 IP:9090/metrics 可以查看到监控的数据:

二、部署Exporters(192.168.109.20)

1、监控远程Linux主机(192.168.109.20)

在远程 linux 主机(被监控端 agent)上安装 node_exporter 组件。

下载地址:

https://prometheus.io/download/

1.1 上传 node_exporter-1.3.1.linux-amd64.tar.gz 到 /opt 目录中,并解压

cd /opt/
tar xf node_exporter-1.3.1.linux-amd64.tar.gz
mv node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin

 

1.2 配置启动文件,设置开机自启

vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=mysqld_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstat

ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

1.3 启动node_exporter

systemctl start node_exporter
systemctl enable node_exporter

netstat -natp | grep :9100

浏览器访问:http://192.168.109.20:9100/metrics ,可以看到 Node Exporter 采集到的指标数据

 通过浏览器访问 http:// 被监控端 IP:9100/metrics 就可以查看到 node_exporter 在被监控端收集的监控信息:

 

1.4 修改Prometheus服务器的配置文件

回到 Prometheus 服务器的配置文件里添加被监控机器的配置段

vim /usr/local/prometheus/prometheus.yml
  - job_name: 'agent'
    static_configs:
    - targets: ['192.168.109.20:9100']

改完配置文件后,重启服务 

1.5 访问prometheus服务器

回到 web 管理界面→点 Status→点 Targets→可以看到多了一台监控目标

 

注:也可以在本机安装 node_exporter,使用上面的方式监控本机。

2、监控远程MySQL

在被管理机 agent上安装 mysqld_exporter 组件

下载地址:

https://prometheus.io/download/

2.1 下载mysqld_exporter组件 

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

2.2 安装mysqld_exporter组件

[root@agent opt]# tar xf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local
[root@agent opt]# mv /usr/local/mysqld_exporter-0.14.0.linux-amd64/ /usr/local/mysqld_exporter
[root@agent opt]# ls /usr/local/mysqld_exporter/
LICENSE  mysqld_exporter  NOTICE

2.3 安装mariadb数据库,并授权

yum install mariadb\* -y

ystemctl start mariadb
systemctl enable mariadb

#进入数据库
mysql

授权IP为192.168.109.20,因为不是prometheus服务器直接来找mariadb获取数据,而是prometheus服务器找mysql_exporter,然后mysql_exporter再找mariadb.所以这个IP指的是mysql_exporter的IP.
grant select,replication client,process ON *.* to 'mysql_monitor'@'192.168.109.20' identified by '123456';

flush privileges;

2.4 创建一个mariadb配置文件

创建一个mariadb配置文件,写上连接的用户名和密码(和上面的授权的用户名和密码要对应)

2.5 配置启动文件,启动mysql_exporter 

[root@agent ~]# vim /usr/lib/systemd/system/mysqld_exporter.service

[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mysqld_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstat

ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target

2.6 修改Prometheus服务器的配置文件

回到 Prometheus 服务器的配置文件里添加被监控的 mariadb 的配置段

vim /usr/local/prometheus/prometheus.yml
 35   - job_name: 'agent_mariadb'
 36     static_configs:
 37     - targets: ['192.168.109.20:9104']

改完配置文件之后,重启服务

2.7 访问prometheus服务器

回到 web 管理界面→点 Status→点 Targets→可以看到监控 mariadb 

 

三、部署Grafana进行展示

Grafana 是一个开源的度量分析和可视化工具,可以通过将采集的数据分析,查询,然后进行可视化的展示,并能实现报警。

下载地址

https://grafana.com/grafana/download/

1、下载安装Grafana(192.168.109.19)

#使用yum解决依赖关系  我这边直接上传软件包到opt
yum install -y grafana-7.4.0-1.x86_64.rpm
或
rpm -ivh /opt/grafana-7.3.6-1.x86_64.rpm

systemctl start grafana-server
systemctl enable grafana-server

netstat -natp | grep :3000

浏览器访问:http://192.168.109.19:3000 ,默认账号和密码为 admin/admin

通过浏览器访问 http:// grafana 服务器 IP:3000 就到了登录界面,使用默认的 admin 用户,admin 密码就可以登陆了。

2、配置数据源

下面我们把 Prometheus 服务器收集的数据做为一个数据源添加到 grafana,让 grafana 可以得到 Prometheus 的数据。

 

 

3、导入模板

点击prometheus_data,选择Dashboards

 

 

4、为数据源做数据展示

 

 

 

 自定义名称,点击保存

最后在dashboard可以查看到 

注:有多条数据的时候,可以在查询的键值后面加个大括号,括号里的条件表示只匹配当前的监控项。

5、导入grafana监控面板

浏览器访问:https://grafana.com/grafana/dashboards ,在页面中搜索 node exporter ,选择适合的面板,点击 Copy ID 或者 Download JSON

在 grafana 页面中,+ Create -> Import ,输入面板 ID 号或者上传 JSON 文件,点击 Load,即可导入监控面板

 

 ​​​

 

 

 

 

6、Grafana 图形显示 MySQL 监控数据

在 grafana 上修改配置文件,并下载安装 mysql 监控的 dashboard(包含相关 json 文件,这些 json 文件可以看作是开发人员开发的一个监控模板)。

在grafana图形化界面导入相关的json文件

用grafana服务器上的firefox浏览器打开,方便上传

 

点 import 导入后,报 prometheus 数据源找不到,因为这些 json 文件里默认要找的就是叫 Prometheus 的数据源,但我们前面建立的数据源却是叫 prometheus_data。

那么请自行把原来的 prometheus_data 源改名为 Prometheus 即可(注意:第一个字母 P 是大写)。然后再回去刷新一下,就有数据了。

7、Grafana+onealert报警

Prometheus 报警需要使用 alertmanager 这个组件,而且报警规则需要手动编写(对运维来说不友好)。所以我这里选用 grafana+onealert 报警。注意:实现报警前把所有机器时间同步再检查一遍。

登陆http://www.onealert.com/→注册帐户→登入后台管理

7.1 在Grafana中配置Webhook URL

1、在Grafana中创建Notification channel,选择类型为Webhook;
2、推荐选中Send on all alerts和Include image,Cloud Alert体验更佳;
3、将第一步中生成的Webhook URL填入Webhook settings Url;
URL格式:
http://api.aiops.com/alert/api/event/grafana/v1/7a2eb59ab2d24483847b17e74bd9b255/

4、Http Method选择POST;
5、Send Test&Save;

 在grafana增加通知通道

 

7.2 测试CPU负载告警

现在可以去设置一个报警来测试了(这里以我们前面加的 cpu 负载监控来做测试)

 保存后就可以测试了,如果 agent1上的 cpu 负载还没有到 0.3,你可以试试 0.1,或者运行一些程序把 agent1负载调大。

 最终的邮件报警效果: 

 

四、总结

报警不成功的可能原因

  1. 各服务器之间时间不同步,这样时序数据会出问题,也会造成报警出问题

  2. 必须写通知内容,留空内容是不会发报警的

  3. 修改完报警配置后,记得要点右上角的保存

  4. 保存配置后,需要由 OK 状态变为 alerting 状态才会报警(也就是说,你配置保存后,就已经是 alerting 状态是不会报警的)

  5. grafana 与 onealert 通信有问题

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Prometheus部署、操作及Grafana展示 的相关文章

随机推荐

  • Debian10搭建dhcp服务

    文章目录 1 安装dhcp服务2 设置网卡监听3 配置dhcp参数4 重启dhcp服务5 测试6 易错总结 1 安装dhcp服务 apt install y isc dhcp server 等待安装完成即可 xff08 这里有一个报错 xf
  • Debian10搭建ntp服务

    文章目录 1 所需设备2 任务描述3 安装ntp服务4 配置ntp服务器4 安装ntpdate客服端5 Debian10Client设置计划任务6 易错总结 1 所需设备 两台Debian10Debian10Server 网卡信息 xff1
  • win11安装的Ubuntu20.04子系统出现System has not been booted with systemd as init system (PID 1)问题的解决流程

    目录 一 前言 二 具体解决方法 第一步 xff1a 切换root用户至自己账号 第二步 xff1a 重新安装xrdp 第三步 xff1a 重新配置端口并启动xrdp 第四步 xff1a 打开远程连接窗口 第五步 xff1a 点击连接 xf
  • 方面级情感分析综述论文&论文+讲解+复现(ABSA)

    2022最新方面级别情感分析论文综述 A Survey on Aspect Based Sentiment Analysis Tasks Methods and Challenges 其中关于ASTE Data V2数据集的论文 1 论文地
  • 使用Go语言开发Qt界面

    Go 的 UI 库 Go 语言本身是没有 UI 库的 xff0c 不过有许多第三方的库支持将 Go 语言绑定到其他 UI 库 xff0c 比如 Qt GTK 参考地址 环境搭建 非 windows 或者需要参数说明的可以参考官方的wiki
  • GitHub AI 编程工具自动写代码神器Copilot插件体验

    简介 copilot 是一个基于 AI 的编程辅助工具 目前已经集成在了 vscode 中 xff0c 后续可能集成到更多平台和工具 xff0c 目前还是测试阶段 官网地址 https copilot github com 支持所有语言 c
  • WebStorm NodeJS

    按 Create New Project 選擇 Empty Project 選擇自己的Directory 作為Location Location 最尾是代表Project Name 改為Hello World 創建一個Javascript
  • wsl ubuntu22.04 conda环境安装labelImg解决xcb缺失问题

    labelImg 安装 pip install PyQt5 i https pypi tuna tsinghua edu cn simple pip install pyqt5 tools i https pypi tuna tsinghu
  • 7个大一C语言必学的程序 / C语言经典代码大全

    嗨 大家好 xff0c 这里是可莉 xff01 今天给大家带来的是7个C语言的经典基础代码 那一起往下看下去把 程序一 打印100到200之间的素数 include lt stdio h gt int main int i for i 61
  • 字符串转化为枚举类型

    需求 xff1a 通过配置文件中自定义传入枚举类型的值 span class token annotation punctuation 64 value span span class token punctuation span span
  • NAT和PAT的原理及配置

    文章目录 一 NAT1 NAT概述2 私有地址3 NAT工作原理4 NAT功能5 NAT包含4类地址6 NAT的实现方式 二 静态转换 xff08 Static Translation xff09 三 动态转换 xff08 Dynamic
  • Linux系统安装教程(手把手教学)

    文章目录 1 首先 xff0c 打开虚拟机 xff0c 点击新建虚拟机2 点击下一步 xff0c 再点击稍后安装3 操作系统选择Linux xff0c 版本选择CentOS7 64位4 命名虚拟机5 设置磁盘大小为100GB6 设置内存为4
  • NFS共享存储服务

    文章目录 引言一 NFS概述二 安装 nfs utils rpcbind 软件包三 NFS的特点四 实验步骤1 安装nfs和rpcbind软件2 设置共享目录3 启动 NFS服务并验证结果4 客户机中访问 NFS 共享资源4 1 手动挂载
  • 优化命令之Sar命令

    文章目录 引言一 sar简介1 sar命令常用格式2 常用选项3 常用参数 二 Sar常用性能数据三 CPU资源监控1 整体CPU使用统计 xff08 u xff09 2 各个CPU使用统计 P 3 将CPU使用情况保存到文件中 四 内存监
  • MySQL高级SQL语句

    文章目录 引言一 常用查询1 order by按关键字排序1 1 升序排序1 2 降序排序1 3 结合where进行条件过滤再排序1 4 多字段排序 2 and or判断2 1 and or 且与或的使用2 2 嵌套 多条件使用 3 dis
  • MongoDB搭建及基础操作

    文章目录 引言一 MongoDB概述1 什么是MongoDB2 MongoDB的特点3 MongoDB适用场景4 MongoDB概念解析 二 搭建MongoDB1 关闭系统防火墙和安全机制2 配置mongodb源仓库3 安装mongodb4
  • 【云原生之k8s】k8s之持久化存储PV、PVC

    文章目录 一 PV和PVC1 PV 概念2 PVC概念3 PV 与 PVC 之间的关系3 1 PV和PVC的生命周期3 2 一个PV从创建到销毁的具体流程3 3 三种回收策略3 4 查看pv pvc的定义方式 规格 4 两种PV的提供方式
  • react native 这样理解运行机制

    移动开发中 xff0c native开发性能和效果上无疑是最好的 但是在众多的情况下 xff0c native开发并不是最优的选择 当需求经常改动的时候 xff0c 当预算有限的时候 xff0c 当deadline很近的时候 xff0c n
  • Promethues原理详解

    目录 引言 一 Prometheus 概述 1 什么是Prometheus 2 Zabbix和Prometheus区别 3 Prometheus的特点 二 运维监控平台设计思路 三 Prometheus监控体系 1 系统层监控 xff08
  • Prometheus部署、操作及Grafana展示

    目录 一 部署Prometheus xff08 192 168 109 18 xff09 1 环境准备工作 2 普罗米修斯的部署 2 1 上传 prometheus 2 37 0 linux amd64 tar gz 到 opt 目录中 x