监控工具 - Prometheus

 


监控工具 Prometheus

Prometheus

Prometheus是开源的系统监控和告警框架,相对Zabbix更灵活,模块间比较解耦,比如告警模块、代理模块等等都可以选择性配置。
Prometheus的服务端和客户端都是开箱即用,不需要进行安装。
Prometheus组件的主要模块包括Prometheus Server、Exporters、Pushgateway、PromQL、 Alertmanager及图形界面。

Prometheus组件大概的工作流程:

  • Prometheus Server定期从配置好的Jobs或Exporters中拉取metrics
  • 或接收Pushgateway组件发送的metrics
  • 或从其他的Prometheus Server中拉取metrics
    Prometheus Server在本地存储收集到的metrics,并运行已定义好的alert.rules,记录新的时间序列或向Alertrnanager推送警报。
    Alertrnanager根据配置文件,对接收到的警报进行处理,并发出告警。
    在图形界面中,可视化采集数据。

Prometheus组件特征

1.多维度数据模型 
- 存储时间序列的数据。 
- 时间序列数据通过metric名称和键值对的labels进行区分。 
- 所有metrics都能设置任意的多维标签。 
- 数据模型更灵活,不需要设置为以点分隔的字符串。 
- 可以对数据模型进行聚合、切割和切片操作。 
- 支持双精度浮点类型,标签可以设为Unicode字符。 

2.灵活而强大的查询语句 
提供了一种查询语言(Prometheus Query Language, PromQL),用户可以实时地查询和聚合时间序列数据。
在同一个查询语句中,可以对多个metrics进行乘法、加法、连接、取分数位等操作。 
PromQL查询语言支持标签值的字符串反向匹配以及正则表达式的反向匹配
- = :选择字符串完全相同的标签。 
-  != :选择字符串不相同的标签。 
- =~ :选择与正则表达式匹配的字符串的标签。 
- !~ :选择与正则表达式反向匹配的字符串的标签。 

3.易于管理 
Prometheus Server是一个单独的二进制文件,可以直接在本地工作,而不依赖于分布式存储。 

4.高效 
平均每个采样点仅占3.5B,并且一个Prometheus Server可以处理数百万个的监控指标metrics。 
使用拉取模式采集时间序列数据,这样不仅有利于本机测试,还可以避免有问题的服务器推送坏的metrics。 
可以采用push gateway的方式把时间序列数据推送至Prometheus Server端,也可以通过服务发现或静态配置获取监控的targets,有多种可视化图形界面,易于伸缩。 
需要指出的是,由于数据采集可能会有丢失,所以Prometheus组件不适用千对采集数据要求100%准确的情形。
但如果用于记录时间序列数据,Prometheus组件就具有很大的查询优势,
此外,Prometheus组件适用于微服务的体系架构。 

Grafana

Grafana组件是一个跨平台的开源的度量分析和可视化工具,可以查询和可视化展采集的数据,并根据设置的告警规则进行通知。
主要特点:
1.展示方式多样:具备快速、灵活的客户端图表展示功能,同时面板插件可以多方式可视化指标和日志,丰富的官方仪表盘插件,如热图、折线图、图表等。
2.数据源丰富:支持多种数据源,如Prometheus、 Elasticsearch等。
3.告警通知:支持以可视化的方式定义重要指标的告警规则,并不断计算,然后发送告警通知,在数据达到阙值时通过多种方式进行告警通知。
4.混合展示:在同一图表中可以混合使用不同的数据源,也可以基于每个查询指定数据源,甚至可以自定义数据源。
5.注释:使用来自不同数据源的丰富事件注释图表,将鼠标光标悬停在事件上会显示完整的事件元数据和标记。
6.过滤器:允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

Exporter

Exporter是Prometheus的一种数据采集组件的总称,负责从目标主机采集数据,并转化为Prometheus支持的数据格式。
官方Prometheus公司开发和维护众多的Exporter组件,包括Node、Nginx、Redis、MQ、mysqld、memcacbed、consul等数据采集Exporter

Node Exporter
与传统的数据采集组件不同,Node Exporter组件并不向中央数据服务器发送采集数据,而是等待中央数据服务器主动前来抓取。

  • 默认的采集数据抓取地址为“http://host_ip:9100/metrics”。 host_ip是指部署Node Exporter组件的主机IP地址。
  • 用于采集服务器主机层面的运行指标,包括主机的loadavg、 filesystem、meminfo等基础监控信息。
  • 部署在Kubemetes集群中的每个节点上,采集主机节点和容器相关的监控信息。

安装Prometheus

下载Prometheus

二进制包部署

  • Linux OS:CentOS Linux release 7.8.2003 (Core)
  • Prometheus:prometheus-2.42.0.linux-amd64.tar.gz

预备动作

# 添加prometheus账户
[root@200 ~]# cat /etc/passwd |grep prometheus
[root@200 ~]# 
[root@200 ~]# ll /home
total 4
drwx------. 17 anliven anliven 4096 Dec 26 18:25 anliven
[root@200 ~]# useradd prometheus && echo "prometheus:prometheus"|chpasswd && chage -M 99999 prometheus
[root@200 ~]# 
[root@200 ~]# cat /etc/passwd |grep prometheus
prometheus:x:1001:1001::/home/prometheus:/bin/bash
[root@200 ~]# 
[root@200 ~]# ll /home
total 4
drwx------. 17 anliven    anliven    4096 Dec 26 18:25 anliven
drwx------   3 prometheus prometheus   78 Feb  3 00:29 prometheus
[root@200 ~]# 

# 关闭防火墙
[root@200 prometheus]# systemctl stop firewalld.service
[root@200 prometheus]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@200 prometheus]# 
[root@200 prometheus]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead) since Fri 2023-02-03 00:59:47 CST; 23s ago
     Docs: man:firewalld(1)
 Main PID: 799 (code=exited, status=0/SUCCESS)

Feb 03 00:09:27 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION-STAGE-2' failed: iptables: No chain/target/match by that name.
Feb 03 00:09:27 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -F DOCKER-ISOLATION' failed: iptables: No chain/target/match by that name.
Feb 03 00:09:27 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -t filter -X DOCKER-ISOLATION' failed: iptables: No chain/target/match by that name.
Feb 03 00:09:30 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i br-1fd5464aebff -o br-1fd5464aebff -j DROP' failed: iptables: Bad rule (does a matching... that chain?).
Feb 03 00:09:31 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i br-8549ea0a041b -o br-8549ea0a041b -j DROP' failed: iptables: Bad rule (does a matching... that chain?).
Feb 03 00:09:33 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i br-8c0510e8d4d0 -o br-8c0510e8d4d0 -j DROP' failed: iptables: Bad rule (does a matching... that chain?).
Feb 03 00:09:36 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule (does a matching rule exist in that chain?).
Feb 03 00:09:37 200 firewalld[799]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w10 -D FORWARD -i docker0 -o docker0 -j DROP' failed: iptables: Bad rule (does a matching rule exist in that chain?).
Feb 03 00:59:45 200 systemd[1]: Stopping firewalld - dynamic firewall daemon...
Feb 03 00:59:47 200 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@200 prometheus]# 

验证和解压二进制包

# 验证和解压二进制包
[root@200 prometheus]# su - prometheus 
Last login: Fri Feb  3 00:31:19 CST 2023 on pts/0
[prometheus@200 ~]$ ls -l
total 88468
-rwxr-x--- 1 prometheus prometheus 90588428 Feb  3 00:32 prometheus-2.42.0.linux-amd64.tar.gz
[prometheus@200 ~]$ 
[prometheus@200 ~]$ sha256sum prometheus-2.42.0.linux-amd64.tar.gz 
422dab055ed9c7bcaff52b718705f9192c6fac0de6b7e78dd278e70ee2663dcc  prometheus-2.42.0.linux-amd64.tar.gz
[prometheus@200 ~]$ 
[prometheus@200 ~]$ tar -zxf prometheus-2.42.0.linux-amd64.tar.gz
[prometheus@200 ~]$ 
[prometheus@200 ~]$ ls -l
total 88468
drwxr-xr-x 4 prometheus prometheus      132 Feb  1 16:27 prometheus-2.42.0.linux-amd64
-rwxr-x--- 1 prometheus prometheus 90588428 Feb  3 00:32 prometheus-2.42.0.linux-amd64.tar.gz
[prometheus@200 ~]$ 
[prometheus@200 ~]$ cd prometheus-2.42.0.linux-amd64/
[prometheus@200 prometheus-2.42.0.linux-amd64]$ ls -l
total 223908
drwxr-xr-x 2 prometheus prometheus        38 Feb  1 16:23 console_libraries
drwxr-xr-x 2 prometheus prometheus       173 Feb  1 16:23 consoles
-rw-r--r-- 1 prometheus prometheus     11357 Feb  1 16:23 LICENSE
-rw-r--r-- 1 prometheus prometheus      3773 Feb  1 16:23 NOTICE
-rwxr-xr-x 1 prometheus prometheus 118789213 Feb  1 15:56 prometheus
-rw-r--r-- 1 prometheus prometheus       934 Feb  1 16:23 prometheus.yml
-rwxr-xr-x 1 prometheus prometheus 110468077 Feb  1 15:58 promtool
[prometheus@200 prometheus-2.42.0.linux-amd64]$ 

启动Prometheus

# 启动Prometheus
[prometheus@200 prometheus-2.42.0.linux-amd64]$ ./prometheus --help    # 显示帮助
[prometheus@200 prometheus-2.42.0.linux-amd64]$ 
[prometheus@200 prometheus-2.42.0.linux-amd64]$ nohup ./prometheus > nohup-prometheus.log 2>&1 &  # 启动并后台运行
[1] 3335
[prometheus@200 prometheus-2.42.0.linux-amd64]$ 
[prometheus@200 prometheus-2.42.0.linux-amd64]$ ls -l
total 223912
drwxr-xr-x 2 prometheus prometheus        38 Feb  1 16:23 console_libraries
drwxr-xr-x 2 prometheus prometheus       173 Feb  1 16:23 consoles
drwxrwxr-x 4 prometheus prometheus        70 Feb  3 00:44 data
-rw-r--r-- 1 prometheus prometheus     11357 Feb  1 16:23 LICENSE
-rw-rw-r-- 1 prometheus prometheus      2912 Feb  3 00:44 nohup-prometheus.log
-rw-r--r-- 1 prometheus prometheus      3773 Feb  1 16:23 NOTICE
-rwxr-xr-x 1 prometheus prometheus 118789213 Feb  1 15:56 prometheus
-rw-r--r-- 1 prometheus prometheus       934 Feb  1 16:23 prometheus.yml
-rwxr-xr-x 1 prometheus prometheus 110468077 Feb  1 15:58 promtool
[prometheus@200 prometheus-2.42.0.linux-amd64]$ 
[prometheus@200 prometheus-2.42.0.linux-amd64]$ cat nohup-prometheus.log 
nohup: ignoring input
ts=2023-02-02T16:44:46.602Z caller=main.go:512 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2023-02-02T16:44:46.602Z caller=main.go:556 level=info msg="Starting Prometheus Server" mode=server version="(version=2.42.0, branch=HEAD, revision=225c61122d88b01d1f0eaaee0e05b6f3e0567ac0)"    # 正在启动
ts=2023-02-02T16:44:46.602Z caller=main.go:561 level=info build_context="(go=go1.19.5, platform=linux/amd64, user=root@c67d48967507, date=20230201-07:53:32)"
ts=2023-02-02T16:44:46.602Z caller=main.go:562 level=info host_details="(Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 200 (none))"
ts=2023-02-02T16:44:46.602Z caller=main.go:563 level=info fd_limits="(soft=4096, hard=4096)"
ts=2023-02-02T16:44:46.602Z caller=main.go:564 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2023-02-02T16:44:46.607Z caller=web.go:561 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090    # 开始监听端口
ts=2023-02-02T16:44:46.610Z caller=main.go:993 level=info msg="Starting TSDB ..."    # 启动时序数据库
ts=2023-02-02T16:44:46.759Z caller=tls_config.go:232 level=info component=web msg="Listening on" address=[::]:9090
ts=2023-02-02T16:44:46.760Z caller=tls_config.go:235 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2023-02-02T16:44:46.761Z caller=head.go:564 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2023-02-02T16:44:46.761Z caller=head.go:608 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=5.472µs
ts=2023-02-02T16:44:46.761Z caller=head.go:614 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2023-02-02T16:44:46.763Z caller=head.go:685 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2023-02-02T16:44:46.763Z caller=head.go:722 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=20.611µs wal_replay_duration=1.847888ms wbl_replay_duration=224ns total_replay_duration=1.899968ms
ts=2023-02-02T16:44:46.765Z caller=main.go:1014 level=info fs_type=XFS_SUPER_MAGIC
ts=2023-02-02T16:44:46.765Z caller=main.go:1017 level=info msg="TSDB started"    # 时序数据库已启动
ts=2023-02-02T16:44:46.765Z caller=main.go:1197 level=info msg="Loading configuration file" filename=prometheus.yml
ts=2023-02-02T16:44:46.768Z caller=main.go:1234 level=info msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=2.098466ms db_storage=4.152µs remote_storage=3.496µs web_handler=474ns query_engine=3.168µs scrape=1.20586ms scrape_sd=34.434µs notify=34.487µs notify_sd=9.332µs rules=3.655µs tracing=20.168µs
ts=2023-02-02T16:44:46.768Z caller=main.go:978 level=info msg="Server is ready to receive web requests."    # 服务已启动
ts=2023-02-02T16:44:46.768Z caller=manager.go:974 level=info component="rule manager" msg="Starting rule manager..."
[prometheus@200 prometheus-2.42.0.linux-amd64]$ 

http://192.168.56.200:9090/

信息关注点

  • 主配置文件:global(全局配置)、alerting(告警配置)、rule_files(告警规则)、scrape_configs(抓取配置)
  • 自带UI界面的使用
  • PromQL的使用
  • Grafana界面、模版获取与导入
  • Prometheus配置自动发现
  • 重新加载配置文件:--web.enable-lifecycle参数、file_sd_configs配置
  • Alertmanager:安装启动、配置文件、访问地址、告警规则
  • 常用exporter的安装配置与调试: node_exporter、mysqld_exporter、redis_exporter等

一些默认端口

9093   alermanager
9090   Prometheus
3000   grafana

9100   node_exporter
9121   redis_exporter
......

参考信息

posted @   Anliven  阅读(15)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示