安装 Prometheus

1. 下载 Prometheus 二进制文件

Prometheus-Github

2. 配置 Prometheus

解压,安装

1
2
tar -xzvf tar_name.tar.gz
mv tar_name /opt/prometheus

tar_name 是下载的文件名

配置system服务

1
vim /etc/systemd/system/prometheus.service
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/opt/prometheus/prometheus --web.listen-address="0.0.0.0:9090" --config.file=/opt/prometheus/prometheus.yml --storage.tsdb.retention.time=90d --web.enable-lifecycle --web.external-url=http://PrometheusIP:9090 --storage.tsdb.path=/opt/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target

----web.listen-address: web面板的绑定地址端口
–config.file: 配置文件路径
–storage.tsdb.retention.time 数据保留时间
–web.enable-lifecycle: 修改配置后可以用 curl -X POST http://localhost:9090/-/reload 来实现配置热加载

修改配置文件中 scrape_interval: 15s 来配置询问被控端的间隔

启动 Prometheus

1
2
3
systemctl daemon-reload
systemctl enable prometheus
systemctl restart prometheus

通过以下地址即可访问Prometheus web 面板

1
http://localhost:9090

安装 Grafana

1. 安装 Grafana

Grafana

2. 启动 Grafana

1
2
3
systemctl daemon-reload
systemctl enable grafana-server
systemctl start grafana-server

3.修改配置

如果是通过deb或者rpm包安装

1
vim /etc/grafana/grafana.ini

如果单独配置的建议在启动参数上加上--config来指定对应的配置文件

编辑时要删掉前导注释符号

配置中指定好http_addrhttp_port,重启 Grafana

1
systemctl restart grafana-server

4.添加数据源

访问web面板,左侧边栏:

Connections > Data sources > Add data source > Prometheus
Connection一栏中填入

1
http://localhost:9090

点击Save & test,如图即为成功
Save & test

被监控主机安装 Node exporter

此部分均在每个被监控主机上操作

1. 下载&配置 Node exporter

Node exporter-github

1
2
tar -xvzf tar_name.tar.gz
mv tar_name/node_exporter /usr/local/bin

配置system服务

1
vim /etc/systemd/system/node_exporter.service
1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/bin/node_exporter --web.listen-address=0.0.0.0:9100
Restart=on-failure
[Install]
WantedBy=multi-user.target
1
2
3
systemctl daemon-reload
systemctl enable node_exporter
systemctl restart node_exporter

Prometheus 添加监控主机

回到主服务器,打开 Prometheus 的配置文件 /opt/prometheus/prometheus.yml

找到scrape_config部分

1
2
3
4
5
6
7
8
9
10
11
12
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["ip:port"]
labels:
instance: server1
- targets: ["ip:port"]
labels:
instance: server2
- targets: ["ip:port"]
labels:
instance: server3

targets中填对应的ip端口,labels为各种标签,这里设置了instance值来替代默认的ip:port

重载配置

1
curl -X POST http://localhost:9090/-/reload

导入面板

我直接选择使用别人做好的模板,ID:22403

Node Information

数据非常齐全