上一部分說了如何搭建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