一 、Prometheus 源码安装和启动配置
普罗米修斯下载网址:https://prometheus.io/download/
监控集成器下载地址:http://www.coderdocument.com/docs/prometheus/v2.14/instrumenting/exporters_and_integrations.html
1.实验环境
IP | 角色 | 系统 |
---|---|---|
172.16.11.7 | Prometheus 服务端 | CentOS 7 |
172.16.11.8 | node_exporter 客户端 | CentOS 7 |
2.下载prometheus
[root@prometheus ~]# cd /usr/local/
[root@prometheus local]# wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz
[root@prometheus local]# tar xf prometheus-2.25.0.linux-amd64.tar.gz
[root@prometheus local]# mv prometheus-2.25.0.linux-amd64/ prometheus
查看版本号
[root@prometheus prometheus]# ./prometheus --version
查看帮助文档
[root@prometheus prometheus]# ./prometheus --help
3.prometheus.yml 配置解释
cat /usr/local/prometheus/prometheus.yml
# my global config
global:
# 默认情况下,每15s拉取一次目标采样点数据。
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
# 每15秒评估一次规则。默认值为每1分钟。
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:
# job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
- job_name: 'prometheus'
# 覆盖global的采样点,拉取时间间隔5s
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
4.启动服务
#启动服务
[root@prometheus prometheus]# ./prometheus --config.file=prometheus.yml
# 指定配置文件
--config.file="prometheus.yml"
# 默认指定监听地址端口,可修改端口
--web.listen-address="0.0.0.0:9090"
# 最大连接数
--web.max-connections=512
# tsdb数据存储的目录,默认当前data/
--storage.tsdb.path="data/"
# premetheus 存储数据的时间,默认保存15天
--storage.tsdb.retention=15d
# 通过命令热加载无需重启 curl -XPOST 172.16.11.7:9090/-/reload
--web.enable-lifecycle
# 可以启用 TLS 或 身份验证 的配置文件的路径
--web.config.file=""
启动选项更多了解:./prometheus --help
5.访问:http://172.16.11.7:9090
6.查看暴露指标
访问 http://172.16.11.7:9090/metrics
7.将Prometheus配置为系统服务
进入systemd目录下
cd /usr/lib/systemd/system
创建文件:vim prometheus.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090
[Install]
WantedBy=multi-user.target
生效系统system文件
systemctl daemon-reload
启动服务
systemctl start prometheus
二、客户端,配置服务发现监控linux主机及相关服务
172.16.11.8操作
1.安装node_exporter
监控Linux 安装常用node_exporter
cd /usr/local/
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
2.启动node_exporter,并添加到服务
(1)直接启动
cd /usr/local/node_exporter && ./node_exporter &
# 启动后会监听9100端口
(2)添加为服务方式启动
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
这里选择(2)添加为服务方式启动
systemctl daemon-reload
systemctl start node_exporter
三 、服务端配置文件添加监控项
172.16.11.7操作
cd /usr/local/prometheus
vim 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:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['172.16.11.7:9090']
- job_name: 'linux'
static_configs:
- targets: ['172.16.11.8:9100']
重启prometheus
[root@prometheus ~]# systemctl restart prometheus.service
重启之后,再次刷新查看
四 、监控mysql(mysqld-exporter)
172.16.11.8操作
1.下载跟配置
cd /usr/local
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
cd /usr/local/mysqld_exporter && vim .my.cnf
2.启动mysqld-exporter
cd /usr/local/mysqld_exporter
./mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
启动后会监听9104端口
3.配置文件添加监控项后重启
172.16.11.7Prometheus 服务端操作
cd /usr/local/prometheus
vim prometheus.yml
- job_name: 'mysql'
static_configs:
- targets: ['172.16.11.8:9104']
重启普罗米修斯
systemctl restart prometheus.service
五 、监控节点的其它系统服务
172.16.11.8操作
如果要监控节点的系统服务,需要在后面添加名单参数
–collector.systemd.unit-whitelist=“.+” 从systemd中循环正则匹配单元
–collector.systemd.unit-whitelist=“(docker|sshd|nginx).service” 白名单,收集目标
#监控客户端,docker服务,nginx服务,sshd
vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=https://prometheus.io
[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service
[Install]
WantedBy=multi-user.target
重启服务node_exporter
systemctl daemon-reload
systemctl restart node_exporter
六 、Grafana 展示 Prometheus 数据
1.快速下载安装Grafana
172.16.11.7操作
wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.4.3-1.x86_64.rpm
yum install -y initscripts fontconfig
yum install -y grafana-7.4.3-1.x86_64.rpm
systemctl start grafana-server.service
systemctl status grafana-server.service
启动后访问地址:ip:3000
初始用户名和密码都是admin
2.添加Prometheus数据源
Configuration -> Data Sources ->add data source -> Prometheus
3.新增Dashboard Linux基础数据展示
Create -> import
4.导入模板8919
5.选择数据源
点击lmport
6.查看Dashboard
Dashboards ->Manage
七 、MySQL数据展示
1 设置数据源
2.导入已经画好的dashboard,数据源选择刚刚创建好的mysql数据源即可
导入画好的dashboard,可在官网下载
点击访问mysql数据源
这里我选择第一个
7991数字是上面官网复制过来的
粘贴,点击load
选择Mysql源
七 、监控Redis(redis_exporter)
1.安装redis_exporter
172.16.11.8操作
cd /usr/local
wget https://github.com/oliver006/redis_exporter/releases/download/v0.15.0/redis_exporter-v0.15.0.linux-amd64.tar.gz
tar -xvf redis_exporter-v0.15.0.linux-amd64.tar.gz
2.启动redis_exporter
172.16.11.8操作
默认redis_exporter端口为9121
cd /usr/local
./redis_exporter redis//172.16.11.8:6379 &
3.prometheus配置文件中加入redis监控并重启
172.16.11.7操作
vim /usr/local/prometheus/prometheus.yml
- job_name: 'Redis'
static_configs:
- targets: ['172.16.11.8:9121']
systemctl restart prometheus
4.grafana导入画好的dashboard
流程可以参考 (七 、MySQL数据展示)
模板改成 redis的就行了