由于演示使用,所以我们配两台机器就可以了,过分点一台也可以
服务器类型 | 主机名 | IP地址 | 组件 |
---|---|---|---|
prometheus服务器 | node-135 | 192.168.17.135 | prometheus,node-exporter |
Grafana/mysql服务器/agent服务器 | node-136 | 192.168.17.136 | mysql-exporter,mariadb,Grafana,node-exporter |
在node-135部署Prometheus server
官网 https://prometheus.io/download/
我们选择LTS版本
上传prometheus tarball到服务器/usr/local/目录中并解压改名
[root@node-135 opt]# tar zxvf prometheus-2.45.0.linux-amd64.tar.gz
[root@node-135 opt]# mv prometheus-2.45.0.linux-amd64 /usr/local/prometheus
[root@node-135 prometheus]# cat 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=` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["192.168.17.135:9090"] #修改为静态IPV4地址
[root@node-135 prometheus]# cat /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
[root@node-135 prometheus]# systemctl start prometheus
[root@node-135 prometheus]# systemctl enable prometheus
Created symlink from /etc/systemd/system/multi-user.target.wants/prometheus.service to /usr/lib/systemd/system/prometheus.service.
[root@node-135 prometheus]# systemctl status prometheus
● prometheus.service - Prometheus Server
Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-08-24 11:20:02 CST; 15s ago
Docs: https://prometheus.io
Main PID: 1654 (prometheus)
CGroup: /system.slice/prometheus.service
└─1654 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/pr...
...
[root@node-135 prometheus]# ss -nltp|grep 9090
LISTEN 0 128 [::]:9090 [::]:* users:(("prometheus",pid=1654,fd=7))
http://192.168.17.135:9090
,访问到 Prometheus 的 Web UI 界面http://192.168.17.135:9090/metrics
,可以看到 Prometheus 采集到自己的指标数在远程linux主机node-137(被监控端agent)上安装node_exporter组件
下载地址 https://prometheus.io/download/
上传node_exporter tarball到服务器/usr/local/目录中并解压改名
[root@node-136 ~]# tar zxvf /root/node_exporter-1.6.1.linux-amd64.tar.gz -C /usr/local/
node_exporter-1.6.1.linux-amd64/
node_exporter-1.6.1.linux-amd64/NOTICE
node_exporter-1.6.1.linux-amd64/node_exporter
node_exporter-1.6.1.linux-amd64/LICENSE
[root@node-136 ~]# cd /usr/local/
[root@node-136 local]# mv node_exporter-1.6.1.linux-amd64 node_exporter
[root@node-136 local]# cd node_exporter
[root@node-136 node_exporter]# nohup ./node_exporter &
[1] 1623
[root@node-136 node_exporter]# nohup: ignoring input and appending output to ‘nohup.out’
[root@node-136 node_exporter]# ss -ltpn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=998,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1152,fd=13))
LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=1623,fd=3))
...
prometheus server node-135执行
[root@node-135 home]# cat /usr/local/prometheus/prometheus.yml
# my global config
...
static_configs:
- targets: ["192.168.17.136:9100"]
[root@node-135 home]# systemctl restart prometheus
回到 web 管理界面→点 Status→点 Targets→可以看到多了一台监控目标
参考 MySQL(一)
下载地址 https://prometheus.io/download/
[root@node-136 ~]# tar -zxvf mysqld_exporter-0.15.0.linux-amd64.tar.gz -C /usr/local/
mysqld_exporter-0.15.0.linux-amd64/
mysqld_exporter-0.15.0.linux-amd64/mysqld_exporter
mysqld_exporter-0.15.0.linux-amd64/NOTICE
mysqld_exporter-0.15.0.linux-amd64/LICENSE
[root@node-136 ~]# cd /usr/local/
[root@node-136 local]# mv mysqld_exporter-0.15.0.linux-amd64 mysqld_exporter
在mysql中创建监控用户,并赋权
create user 'exporter'@'localhost' IDENTIFIED BY '123456';
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT, RELOAD ON *.* TO 'exporter'@'localhost';
flush privileges;
创建配置文件并在后台启动组件
[root@node-136 local]# cat /usr/local/mysqld_exporter/mysqld_exporter.cnf
[client]
user=exporter
password=123456
[root@node-136 local]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/mysqld_exporter.cnf &
[1] 68377
[root@node-135 ~]# cat /usr/local/prometheus/prometheus.yml
# my global config
...
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["192.168.17.135:9090"]
- job_name: "agent"
static_configs:
- targets: ["192.168.17.136:9100"]
- job_name: "mysql"
static_configs:
- targets: ["192.168.17.136:9104"]
安装grafana用于监测结果的展示
安装参考 CHAPTER 3 Zabbix & Grafana
systemctl start grafana-server
systemctl enable grafana-server
netstat -natp | grep :3000
#账号密码默认为admin,admin
grafana默认配置文件目录 /etc/grafana/grafana.ini
浏览器访问:http://192.168.17.136:3000 ,默认账号和密码为 admin/admin