Prometheus安裝配置

1 Prometheus安裝配置

1.1 下載

在 Prometheus 官網(wǎng) https://prometheus.io/download/#prometheus 獲取適用于 Linux 的 Prometheus 安裝包。

image.png

1.2 安裝

系統(tǒng)環(huán)境如下:

[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@centos ~]# uname -r
3.10.0-957.el7.x86_64

為了安全,不用 root 用戶啟動(dòng)相關(guān)服務(wù);自建 prometheus 用戶啟動(dòng)服務(wù)

groupadd prometheus
useradd -g prometheus -M -s /sbin/nologin prometheus

將安裝包 prometheus-2.47.1.linux-amd64.tar.gz 上傳至服務(wù)器 /opt/src 目錄下
解壓壓縮包

[root@centos ~]# cd /opt/src
[root@centos src]# tar -zxvf prometheus-2.47.1.linux-amd64.tar.gz -C /opt
[root@centos src]# cd /opt
[root@centos opt]# mv prometheus-2.47.1.linux-amd64/ prometheus
[root@centos opt]# mkdir -pv /opt/prometheus/data
[root@centos opt]# chown -R prometheus.prometheus /opt/prometheus

切換到解壓縮后的目錄,執(zhí)行 prometheus --version 命令查看是否正常

[root@centos ~]# cd /opt/prometheus/
[root@centos prometheus]# ./prometheus --version
prometheus, version 2.47.1 (branch: HEAD, revision: c4d1a8beff37cc004f1dc4ab9d2e73193f51aaeb)
  build user:       root@4829330363be
  build date:       20231004-10:31:16
  go version:       go1.21.1
  platform:         linux/amd64
  tags:             netgo,builtinassets,stringlabels

創(chuàng)建Systemd服務(wù)啟動(dòng)prometheus

[root@centos opt]# vim /etc/systemd/system/prometheus.service

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target

[Service]
User=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/prometheus \
  --config.file=/opt/prometheus/prometheus.yml \
  --storage.tsdb.path=/opt/prometheus/data
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target

在service文件里面,我們定義了啟動(dòng)的命令,定義了數(shù)據(jù)存儲(chǔ)在/opt/prometheus/data路徑下。

啟停命令

systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
systemctl status prometheus
systemctl reload prometheus

1.3 配置

Prometheus 通過抓取監(jiān)控目標(biāo)上的 HTTP 端點(diǎn)來收集指標(biāo),而且 Prometheus 本身也暴露 metrics 指標(biāo)接口,所以它也可以抓取并監(jiān)控其自身的運(yùn)行狀況。

1.3.1 收集Prometheus自身數(shù)據(jù)配置

配置文件 /opt/prometheus-2.47.1.linux-amd64\prometheus.yml

global:
  scrape_interval: 5s # 抓取頻率
  
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

上面配置了 Prometheus 每 5s 從自身抓取指標(biāo)。global 區(qū)域用于配置一些全局配置和默認(rèn)值,scrape_configs 部分是用來告訴 Prometheus 要抓取哪些目標(biāo)。

在實(shí)際情況下,抓取頻率間隔通常在 10 到 60 秒之間。

1.3.2 安裝配置 node_exporter

為監(jiān)控服務(wù)器 CPU , 內(nèi)存 , 磁盤 , I/O 等信息,需要在被監(jiān)控機(jī)器上安裝 node_exporter 服務(wù)。
首先我們需要從 node_exporter下載頁 下載我們需要安裝的版本。
node_exporter-1.6.1.linux-amd64.tar.gz
解壓并安裝 node_exporter 服務(wù):

[root@centos ~]# cd /opt/src
[root@centos src]# tar -zxvf node_exporter-1.6.1.linux-amd64.tar.gz -C /opt
[root@centos src]# cd /opt/
[root@centos opt]# mv node_exporter-1.6.1.linux-amd64/ node_exporter
[root@centos opt]# chown -R prometheus.prometheus /opt/node_exporter

創(chuàng)建Systemd服務(wù)啟動(dòng)node_exporter

[root@centos opt]# vim /etc/systemd/system/node_exporter.service

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/opt/node_exporter/node_exporter

[Install]
WantedBy=default.target

啟動(dòng) node_exporter 服務(wù):

systemctl daemon-reload
systemctl enable node_exporter
systemctl start node_exporter
systemctl status node_exporter

修改 prometheus 的配置文件/srv/prometheus/prometheus.yml,增加如下內(nèi)容:

scrape_configs:
...
- job_name: 'node'
    scrape_interval: 10s
    static_configs:
      - targets: ['localhost:9100','192.168.1.100:9100']

重啟 Prometheus 服務(wù):

systemctl reload prometheus

1.4 查看監(jiān)控目標(biāo)

當(dāng)啟動(dòng) Prometheus 后,我們可以檢查下它是否正確的抓取了配置的目標(biāo),可以在瀏覽器中訪問 http://<host-ip>:9090/targets 來查看所有的抓取目標(biāo)列表

image.png

如果我們配置的抓取本身的 prometheus 這個(gè)任務(wù)顯示的綠色的 UP 狀態(tài),證明 Prometheus 已經(jīng)正常抓取自身的監(jiān)控指標(biāo)了。

如果在抓取過程中出現(xiàn)任何問題(DNS 解析失敗、連接超時(shí)等等錯(cuò)誤),抓取目標(biāo)都會(huì)顯示為 DOWN,同時(shí)還有一條錯(cuò)誤消息,提供有關(guān)抓取失敗的相關(guān)信息,可以幫助我們快速發(fā)現(xiàn)錯(cuò)誤配置或不健康的目標(biāo)。

2 Grafana安裝配置

Grafana 用來展示 Prometheus 的監(jiān)控指標(biāo)

2.1 下載

到Grafana的官網(wǎng)進(jìn)行下載
grafana-enterprise-10.2.0.linux-amd64.tar.gz

2.2 安裝

[root@centos opt]# cd /opt/src/
[root@centos src]# tar -zxvf grafana-enterprise-10.2.0.linux-amd64.tar.gz -C /opt
[root@centos src]# cd /opt/
[root@centos opt]# mv grafana-10.2.0/ grafana
[root@centos opt]# mkdir -pv /data/grafana/data
[root@centos opt]# mkdir -pv /data/grafana/log
[root@centos opt]# mkdir -pv /data/grafana/plugins
[root@centos opt]# mkdir -pv /data/grafana/conf/provisioning
[root@centos opt]# chown -R prometheus.prometheus /opt/grafana
[root@centos opt]# chown -R prometheus.prometheus /data/grafana
[root@centos opt]# chmod 777 /data/grafana/*

2.3 配置

修改 /opt/grafana/conf/defaults.ini 文件,配置為上面新建的數(shù)據(jù)目錄。

data = /data/grafana/data
logs = /data/grafana/log
plugins = /data/grafana/plugins
provisioning = /data/grafana/conf/provisioning

2.4 把 grafana-server 添加到 systemd

新增 grafana-server.service文件,使用systemd來管理grafana服務(wù)

vim /etc/systemd/system/grafana-server.service

[Unit]
Description=Grafana
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=notify
ExecStart=/opt/grafana/bin/grafana-server \
    --config=/opt/grafana/conf/defaults.ini \
    --homepath=/opt/grafana
Restart=on-failure

[Install]
WantedBy=multi-user.target

啟停并設(shè)置開機(jī)啟動(dòng)命令

#重載配置:
systemctl daemon-reload
#設(shè)置開機(jī)啟動(dòng)
systemctl enable grafana-server
#啟動(dòng)服務(wù)
systemctl start grafana-server
#關(guān)閉服務(wù)
systemctl stop grafana-server
#查看服務(wù)狀態(tài)
systemctl status grafana-server

2.5 打開 Grafana 的 web 頁面

在瀏覽器中訪問 http://<host-ip>:3000
默認(rèn)的賬號(hào)密碼 admin/admin

image.png

2.6 添加數(shù)據(jù)源

image.png

2.7 導(dǎo)入Grafana模板,數(shù)據(jù)展示

如要使用其他的模板,請(qǐng)到grafana的官網(wǎng)去查找 https://grafana.com/grafana/dashboards/

image.png

image.png
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容