1. Prometheus搭建&使用
1.1 prometheus获取数据方式
通过HTTP协议从远程的机器(需安装exporter)收集数据并存储在本地的时序数据库上。
1.2 支持类型
Prometheus为了支持各种中间件以及第三方的监控提供了exporter,如:
- Node Exporter:用于收集主机级别的系统和硬件指标,如CPU、内存、磁盘、网络等。
- Docker Exporter:用于收集与Docker容器相关的指标,如容器的CPU、内存、网络等。
- MySQL Exporter:用于收集MySQL数据库的性能指标,如查询次数、连接数、慢查询等。
- Redis Exporter:用于收集Redis数据库的性能指标,如内存使用、连接数、命中率等。
- Apache Exporter:用于收集Apache Web服务器的指标,如请求数量、连接数、响应时间等。
- Nginx Exporter:用于收集Nginx Web服务器的指标,如请求数量、连接数、响应时间等。
- Kafka Exporter:用于收集Kafka消息队列的指标,如消息积压、吞吐量、延迟等。
- JMX Exporter:用于收集Java应用程序的JMX指标,如堆内存使用、线程数、GC时间等。
例如Node exporter主要通过读取Linux的/proc以及/sys目录下的系统文件获取操作系统运行状态,reids exporter通过Reids命令行获取指标,mysql exporter通过读取数据库监控表获取MySQL的性能数据。他们将这些异构的数据转化为标准的Prometheus格式,并提供HTTP查询接口。
2. Prometheus 安装&配置
下载网址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.48.0.linux-amd64.tar.gz
tar xf prometheus-2.48.0.linux-amd64.tar.gz
mv prometheus-2.48.0.linux-amd64 prometheus
cd prometheus
查看帮助信息
./prometheus --help
查看版本号
./prometheus --version
2.1 prometheus.yml 配置文件
[root@localhost prometheus]# cat prometheus.yml
# my global config
#global:全局配置部分,包含全局的配置选项,例如scrape_interval(数据采集间隔)和evaluation_interval(规则评估间隔)等。
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting: #告警配置部分,用于定义Prometheus的告警规则和告警通知方式。
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files: #规则文件配置部分,指定要加载的Prometheus规则文件的路径。
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs: #目标配置部分,定义了要监控的目标以及相应的抓取配置。
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus' #作业名称,用于标识一个目标组。
# metrics_path defaults to '/metrics' #目标提供的指标的路径,默认为/metrics。
# scheme defaults to 'http'.
static_configs: #静态目标配置,指定了目标的地址和其他相关信息。
- targets: ['localhost:9090']
2.2 prometheus直接启动启动
[root@localhost prometheus]# ./prometheus --config.file=prometheus.yml
常用启动参数:
- –config.file:指定Prometheus配置文件的路径。例如:./prometheus --config.file=/path/to/prometheus.yml
- –web.listen-address:指定Prometheus服务器监听的地址和端口。默认是localhost:9090。例
如:./prometheus --web.listen-address=:8080 - –web.enable-lifecycle:启用/禁用Prometheus服务器的生命周期管理接口,默认为false。例
如:./prometheus --web.enable-lifecycle - –storage.tsdb.path:指定Prometheus存储数据的路径。例如:./prometheus –
storage.tsdb.path=/path/to/data - –storage.tsdb.retention.time:指定Prometheus存储数据的保留时间。例如:./prometheus –
storage.tsdb.retention.time=30d - –log.level:指定Prometheus日志的级别。默认为info。例如:./prometheus --log.level=debug
- –query.timeout:配置PromQL查询的超时时间。默认为2分钟。例如:./prometheus –
query.timeout=5m
2.3 prometheus配置为系统服务
touch prometheus.service
vi prometheus.service
[Unit]
Description=Prometheus Server
After=network.target
[Service]
Type=simple
ExecStart=/path/to/prometheus ##实际执行文件地址
[Install]
WantedBy=default.target
将该文件拷贝到 systemd 服务目录中(例如 /etc/systemd/system/),并执行以下命令启动 Prometheus:
mv prometheus.service /etc/systemd/system/
sudo systemctl start prometheus
访问http://localhost:9090
3. 客户端配置
3.1 客户端安装node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
tar xf node_exporter-1.1.2.linux-amd64.tar.gz
mv node_exporter-1.1.2.linux-amd64 node_exporter
3.1 node_exporter添加到服务并启动
vi /etc/systemd/system/node_exporter.service
编辑node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/root/exporter/node_exporter/node_exporter --web.listen-address=:9100 #监听指定端口
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 启动node_exporter
systemctl daemon-reload
systemctl start node_exporter
3.1.3 promethues服务端配置文件添加监控项
修改配置文件
vi prometheus.yml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
#新增以下内容
- job_name: 'linux'
static_configs
- targets: ['192.168.140.128:9100'] # 多个用,分开
重启Prometheus服务
systemctl restart prometheus.service
访问http://localhost:9090
4. Grafana 可视化
下载安装
wget https://dl.grafana.com/oss/release/grafana-8.0.3-1.x86_64.rpm
yum install grafana-8.0.3-1.x86_64.rpm
启动&状态查看
systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
设置开机自启动
systemctl enable grafana-server
访问http://ip:3000/
初始用户名和密码都是admin
4.1 添加Prometheus数据源
4.2 创建Dashboard
导入模板
5.cAdvisor + Prometheus收集本机和docker容器数据
5.1 部署Cadvisor
拉取镜像
docker pull docker.io/google/cadvisor
docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:rw \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \
google/cadvisor:latest \
访问
http://IP:8080/metrics
http://IP:8080/containers
http://IP:8080/docker
prometheus.yml添加节点配置
- job_name: 'cadvisor'
scrape_interval: 5s
static_configs:
- targets: ['localhost:8080']
使用11600模板显示效果