上一部分说了如何搭建Prometheus+granfana服务,下面来说说如何对Prometheus一些常用配置方法,prometheus有多种配置服务器发现模式,这里主要说的是在服务端进行配置写死的,还可以配置成自发现模式。
官方expoter下载地址
下载各种expoter,https://prometheus.io/download/
一、监控服务器
1、下载及解压安装包
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
mkdir /usr/local/prometheus/
tar -xvf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local/prometheus/
cd /usr/local/prometheus/
mv /usr/local/prometheus/node_exporter-1.0.0-rc.0.linux-amd64 /usr/local/prometheus/node_exporter
2、创建用户
groupadd prometheus
useradd -g prometheus prometheus -d /usr/local/prometheus/
chown -R prometheus.prometheus node_exporter
3、创建node_exporter.service的 systemd unit 文件
vi /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
4、启动服务
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
5、增加服务端prometheus.yml配置的job
- job_name: '192.168.1.171'
static_configs:
- targets: ['192.168.1.171:9100']
labels:
instance: sys1
6、加载配置
curl -X POST http://localhost:9090/-/reload
7、granfana添加dashborad
导入8919号dashborad
一、监控jvm
1、下载exporter
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.12.0/jmx_prometheus_javaagent-0.12.0.jar
2、改名并移动到指定目录
mkdir /usr/local/prometheus/jmx_exporter
mv jmx_prometheus_javaagent-0.12.0.jar /usr/local/prometheus/jmx_exporter/jmx_prometheus_javaagent.jar
cd /usr/local/prometheus/jmx_exporter
3、创建exporter规则文件
vi /usr/local/prometheus/jmx_exporter/jmx_exporter.yml
rules:
- pattern: ".*"
4、所需监控java引用随exporter一起启动,此例中,9888为exporter输出端口
nohup java -javaagent:/usr/local/prometheus/jmx_exporter/jmx_prometheus_javaagent.jar=9888:/usr/local/prometheus/jmx_exporter/jmx_exporter.yml -jar gateway_zuul_service-20200408_1759.jar &
5、增加服务端prometheus.yml配置的job
- job_name: 'jmx'
static_configs:
- targets: ['192.168.1.171:9888']
6、加载配置
curl -X POST http://localhost:9090/-/reload
7、granfana添加dashborad
导入3457号dashborad,结合项目实际情况,调整granfana收集数据的标签,
最终效果:
三、监控mysql
1、下载exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
mkdir /usr/local/prometheus/
tar -xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/prometheus/
cd /usr/local/prometheus/
mv /usr/local/prometheus/mysqld_exporter-0.12.1.linux-amd64/ /usr/local/prometheus/mysqld_exporter
2、改名并移动到指定目录
mv mysqld_exporter-0.11.0.linux-amd64 /usr/local/mysqld_exporter
cd /usr/local/prometheus/mysqld_exporter
3、登录mysql为exporter创建账号并授权(如果用root,可以不做这步)
# 创建数据库用户。
mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '*******';
# 可查看主从运行情况查看线程,及所有数据库。
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
4、创建mysql配置文件、运行时可免密码连接数据库:vi /etc/my.cnf
[client]
user=root
password=******
5、创建mysqld_exporter.service的 systemd unit 文件
vi /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
#Description=mysqld_exporter
Documentation=https://prometheus.io/
#After=network.target
[Service]
#Type=simple
#User=prometheus
ExecStart=/usr/local/prometheus/mysqld_exporter/mysqld_exporter --config.my-cnf=/etc/my.cnf
Restart=on-failure
[Install]
WantedBy=multi-user.target
6、启动服务
systemctl daemon-reload
systemctl enable mysqld_exporter.service
systemctl start mysqld_exporter.service
8、增加服务端prometheus.yml配置的job
- job_name: 'mysql'
metrics_path: /metrics
static_configs:
- targets: ['192.168.1.173:9104']
labels:
instance: mysql
9、加载配置
curl -X POST http://localhost:9090/-/reload
10、granfana添加dashborad
导入7362号dashborad
四、监控redis
1、下载redis_exporter
wget https://github.com/oliver006/redis_exporter/releases/download/v1.5.2/redis_exporter-v1.5.2.linux-amd64.tar.gz
tar -xvf redis_exporter-v1.5.2.linux-amd64.tar.gz -C /usr/local/prometheus/
cd /usr/local/prometheus/
mv /usr/local/prometheus/redis_exporter-v1.5.2.linux-amd64/ /usr/local/prometheus/redis_exporter
2、下载redis_exporter.service的 systemd unit 文件
vi /usr/lib/systemd/system/redis_exporter.service
[Unit]
Description=redis_exporter
Documentation=https://github.com/oliver006/redis_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/redis_exporter/redis_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
3、修改服务端配置文件,
vi /data/prometheus/prometheus.yml
```
```
- job_name: 'redis'
metrics_path: /metrics
static_configs:
- targets: ['192.168.1.173:9121']
labels:
instance: redis
4、启动服务
systemctl daemon-reload
systemctl enable redis_exporter.service
systemctl start redis_exporter.service
5、查看metrics是否可以获取
curl http://192.168.1.173:9121/metrics
6、granfana添加dashborad
导入11442号dashborad
五、监控zookeeper
1、修改服务端配置文件,vi /data/prometheus/prometheus.yml
- job_name: 'zookeeper'
metrics_path: /metrics
static_configs:
- targets: ['192.168.1.172:8090']
labels:
instance: zookeeper
2、下载zookeeper_exporter
wget https://github.com/dabealu/zookeeper-exporter/releases/download/v0.1.5/zookeeper-exporter-v0.1.5-linux.tar.gz
tar -xvf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local/prometheus/
cd /usr/local/prometheus/
mv /usr/local/prometheus/zookeeper-exporter-v0.1.5-\$\{OS\}/ /usr/local/prometheus/zookeeper-exporter
3、下载zookeeper_exporter.service的 systemd unit 文件
vi /usr/lib/systemd/system/zookeeper_exporter.service
[Unit]
Description=zookeeper_exporter
Documentation=https://github.com/dabealu/zookeeper-exporter/releases
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/zookeeper-exporter/zookeeper-exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
4、启动服务
systemctl daemon-reload
systemctl enable zookeeper_exporter.service
systemctl start zookeeper_exporter.service
5、查看metrics是否可以获取
curl http://192.168.1.172:8080/metrics
6、granfana添加dashborad
导入11442号dashborad