Prometheus + Grafana
系统环境:CentOS Linux release 7.9.2009 (Core)
网络:2: ens33 inet 192.168.0.71/24
服务器端:192.168.0.71
客户端:192.168.0.71
1.1 运行用户创建
groupadd prometheus
useradd -g prometheus -m -d /opt/prometheus/ -s /sbin/nologin prometheus
1.2 prometheus server安装
可以访问 github.com 进行下载tar包
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.darwin-amd64.tar.gz
tar xzf prometheus-2.14.0.linux-amd64.tar.gz -C /opt/
cd /opt/prometheus-2.14.0.linux-amd64 && mv prometheus-2.14.0.linux-amd64 prometheus
1.3 prometheus配置语法校验
建议每次修改prometheus配置之后, 都进行语法校验, 以免导致 prometheus server无法启动.
./promtool check config prometheus.yml
1.4 测试启动Prometheus
此时采用默认配置启动 prometheus server 看下界面, 稍后介绍如何监控Linux 服务器.
./prometheus --config.file=prometheus.yml
http://192.168.0.71:9090/graph
1.6 设置prometheus系统服务,并配置开机启动
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention=60d
Restart=on-failure
[Install]
WantedBy=multi-user.target
Prometheus启动参数说明
–config.file – 指明prometheus的配置文件路径
–web.enable-lifecycle – 指明prometheus配置更改后可以进行热加载
–storage.tsdb.path – 指明监控数据存储路径
–storage.tsdb.retention --指明数据保留时间
修改启动文件权限,知道其他启动方式可以通过 shell 脚本启动。
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
设置开机启动
systemctl daemon-reload
systemctl enable prometheus.service
systemctl status prometheus.service
systemctl restart prometheus.service
2. Prometheus 配置监控其他Linux主机
2.1 node_exporter安装配置
运行用户添加
groupadd prometheus
useradd -g prometheus -m -d /usr/local/node_exporter/ -s /sbin/nologin prometheus
下载node_server 同样此安装包 github.com 也有
wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.darwin-amd64.tar.gz
tar -zxf node_exporter-1.3.1.darwin-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-1.3.1 /usr/local/node_exporter
系统服务配置 node_exporter
vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
chown prometheus:prometheus /usr/lib/systemd/system/node_exporter.service [Ubuntu系统路径不同:/lib/systemd/system/]
chown -R prometheus:prometheus /usr/local/node_exporter*
####启动 node_exporter 服务并设置开机启动
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
systemctl status node_exporter.service
systemctl restart node_exporter.service
systemctl start node_exporter.service
systemctl stop node_exporter.service
node_export 启动完毕后会开启一个 9100 tcp 端口:http://192.168.0.71:9100/metrics 可以手动访问测试
curl http://192.168.0.71:9100/metrics
客户端机器配置完成,现在配置服务端将客户端添加至 监控中。
[root@localhost ~]# cd /opt/prometheus/
[root@localhost prometheus]# ls
console_libraries consoles data LICENSE NOTICE prometheus prometheus.rules.yml prometheus.yml promtool sd_cfg
[root@localhost prometheus]# vim prometheus.yml # 这里展示的是不完全的配置文件
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
- job_name: 'Linux'
file_sd_configs:
- files: ['/opt/prometheus/sd_cfg/Linux.yml']
refresh_interval: 5s
配置 一个 Linux 群组:并创建一个对于的文件夹和文件。
[root@localhost prometheus]# cat sd_cfg/Linux.yml
- targets: ['localhost:9100']
labels:
name: Linux-node1
#[这里建议给每个主机打个有意义的标签,方便识别.]
- targets: ['192.168.0.102:9100']
labels:
name: Linux-node2
检查配置文件:
[root@localhost prometheus]# ./opt/prometheus/promtool check config prometheus.yml
Checking prometheus.yml
SUCCESS: 1 rule files found
Checking prometheus.rules.yml
SUCCESS: 1 rules found
http://192.168.0.71:9090/targets 浏览器打开服务器端的对于位置可以看到 自己添加的主机。
3 数据展示Grafana安装配置 可以直接下载 8.3版本 企业版,开源版下载不了
下载地址: https://grafana.com/grafana/download
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.3.2-1.x86_64.rpm
sudo yum install grafana-enterprise-8.3.2-1.x86_64.rpm
systemctl enable grafana-server.service
systemctl start grafana-server.service
打开浏览器测试:
http://192.168.0.71:3000
granafa默认端口为3000,可以在浏览器中输入http://localhost:3000/
granafa首次登录账户名和密码admin/admin,可以修改
配置数据源Data sources->Add data source -> Prometheus,输入prometheus数据源的信息,主要是输入name和url (直接粘贴:http://192.168.0.71:3000/datasources)
添加 Dashboard -> New Dashboard -> Import Dashboard -> 输入11074,导入Linux监控模板. 并配置数>据源为Prometheus,即上一步中的name
Grafana官方监控模板免费下载:https://grafana.com/grafana/dashboards/?search=influxdb
http://192.168.0.71:3000/dashboard/import 在这个页面可以自己上传json文件和复制粘贴 官网免费ID :10242, 15172