Prometheus+Grafana 服务器监控

Prometheus+Grafana 服务器监控

一、部署Prometheus

1、环境准备工作

服务器 IP地址 组件

Prometheus服务器 192.168.16.131 Prometheus、Grafana

agent服务器 192.168.16.132 node_exporter

agent服务器 192.168.16.133 node_exporter

可以到 github 下载最新版本

服务器 ip 部署组件
Prometheus服务器 192.168.16.131 Prometheus、Grafana
agent服务器 192.168.16.132 node_exporter
agent服务器 192.168.16.133 node_exporter

2、puometheus部署

2.1上传prometheus到opt目录下,并解压

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">tar zxf prometheus-2.54.1.linux-amd64.tar.gz
mv prometheus-2.54.1.linux-amd64 /usr/local/prometheus
cd /usr/local/prometheus/
ls</pre>

[图片上传失败...(image-3e720d-1726713228528)]

2.2修改prometheus配置文件

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n31" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">vim 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).

scrape_timeout: 10s #数据采集超时时间,默认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=<job_name> to any timeseries scraped from this config.

  • job_name: "prometheus"
    static_configs:
  • targets: ["localhost:9090"]</pre>

[图片上传失败...(image-d40323-1726713228528)]

2.3配置系统启动文件,设置开机自启

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">[root@prometheus prometheus]# vim /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</pre>

2.4开启prometheus,并访问网页验证,启动命令:systemctl start prometheus.service

[图片上传失败...(image-400f2d-1726713228528)]

二、部署Exporter(192.168.16.132、192.168.16.133)

1、监控远程Linux主机192.168.226.129

  1. 1.上传node_exporter到/usr/local目录,并解压

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">tar zxf node_exporter-1.8.2.linux-amd64.tar.gz
mv node_exporter-1.8.2.linux-amd64 /usr/local/bin/</pre>

  1. 2.配置系统启动文件,设置开机自启,ExecStart 修改为你的路径

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">vim /etc/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter
After=network.target

[Service]
User=nobody
Type=simple
ExecStart=/usr/local/bin/node_exporter-1.8.2.linux-amd64/node_exporter

[Install]
WantedBy=multi-user.target</pre>

  1. 3.启动服务 systemctl start node_exporter

  2. 4.查看服务状态 systemctl status node_exporter

[图片上传失败...(image-181733-1726713228528)]

  1. 5.修改 192.168.16.131 prometheus 服务器的配置文件,添加如下内容,多个服务节点在数组内添加

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n56" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> vim prometheus.yml

  • job_name: "nodes"
    static_configs:
  • targets: ["192.168.226.129:9100","192.168.16.133:9100"]</pre>

[图片上传失败...(image-eeba48-1726713228528)]

改完配置文件后,重启 prometheus 服务 systemctl restart prometheus.service

[图片上传失败...(image-3c4cb2-1726713228528)]

三、部署Grafana进行展示

1、下载安装Grafana(192.168.16.131)https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1,选择对应系统的包

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="text" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">rpm -ivh grafana-enterprise-11.2.0-1.x86_64.rpm

systemctl start grafana-server
systemctl enable grafana-server

netstat -natp | grep :3000</pre>

浏览器访问:http://192.168.16.131:3000 ,默认账号和密码为 admin/admin

2、配置数据源

下面我们把 Prometheus 服务器收集的数据做为一个数据源添加到 grafana,让 grafana 可以得到 Prometheus 的数据

[图片上传失败...(image-8a6517-1726713228528)]

[图片上传失败...(image-b38f18-1726713228528)]

后续配置参考 https://blog.csdn.net/qq_45088125/article/details/126275157 进行配置

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容