Prometheus + Grafana 在 CentOS 中搭建可視化性能監控平臺 Prometheus 部署 Prometheus + Grafana 附錄:問題

Prometheus

Prometheus ,由谷歌研發的一款開源的監控軟件。

Prometheus 是一款基於時序數據庫的開源監控告警系統,通過安裝在遠程機器上的 exporter 收集數據並通過HTTP協議從遠程的機器上獲取數據,最後存儲在本地的時序數據庫上。

Node exporter 是 Prometheus 最常用的一種 exporter,主要負責收集 Linux 的 /proc 以及 /sys 目錄下的系統文件獲取操作系統運行狀態。

另外還有一些其他的 exporter 分別用來監控不同的應用程序:

  • reids exporter:通過Reids命令行獲取指標
  • mysql exporter:通過讀取數據庫監控表獲取MySQL的性能數據

這些獲取到的監控數據,再通過 Grafana 實現可視化、圖形化、圖表化的信息展示。

這樣,我們在做性能測試、生產服務器監控的時候,就不用 Linux 命令,而且更直觀。

本篇文章演示如何在一臺 Centos 服務器上部署 Prometheus。

部署 Prometheus + Grafana

在 Centos 上部署 Prometheus + Grafana 包含以下 3 個大的步驟:

  1. 安裝 Prometheus,並部署成服務
  2. 安裝 Node exporter, 並部署成服務
  3. 安裝 Grafana

安裝 Prometheus

創建 Prometheus 用戶

$ useradd -m -s /bin/false prometheus

創建配置目錄

$ mkdir /etc/prometheus

$ mkdir /var/lib/prometheus

$ chown prometheus:prometheus /var/lib/prometheus/

下載最新版的 prometheus

  1. 通過 wget 命令下載

    # 切換到home目錄,將文件下載在這裏
    $ cd ~
    
    # 如果沒有 wget 命令則先安裝
    $ dnf install wget -y
    
    # 如果已經有了 wget 命令
    $ wget https://github.com/prometheus/prometheus/releases/download/v2.39.1/prometheus-2.39.1.linux-amd64.tar.gz
    

由於文章存在時效性,如果你要下最新的,請直接前往Prometheus 下載

  1. 你也可以直接下載 linux 版本

    鏈接:https://pan.baidu.com/s/1SIipv7igCfYoMWtmHJX8jA?pwd=iccg
    提取碼:iccg

     通過 sftp 上傳到 Centos。推薦使用 `mobaxterm`作爲 SSH 客戶端遠程連接,非常方便。[mobaxterm官網](https://mobaxterm.mobatek.net/)
    

解壓文件

$ tar -zxpvf prometheus-2.39.1.linux-amd64.tar.gz

# 複製 prometheus 和 promtool 到 /usr/local/bin 路徑
$ cd prometheus-2.39.1.linux-amd64

$ cp prometheus  /usr/local/bin

$ cp promtool  /usr/local/bin

# 將 prometheus.yml 複製到 /etc/prometheus/ 路徑
$ cp prometheus.yml /etc/prometheus/

開放 Prometheus 端口

$ firewall-cmd --add-port=9090/tcp --permanent

$ firewall-cmd --reload

創建 Prometheus 服務

創建服務文件,以便以服務方式運行 Prometheus

$ vi /etc/systemd/system/prometheus.service

將下面的內容拷貝進去

[Unit]

Description=Prometheus Time Series Collection and Processing Server

Wants=network-online.target

After=network-online.target

 
[Service]

User=prometheus

Group=prometheus

Type=simple

ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries


[Install]

WantedBy=multi-user.target

{% note warning flat %}
注意檢查一下開頭和結束的內容,拷貝可能會造成內容丟失。
{% endnote %}

啓動服務

# 重新加載
$ systemctl daemon-reload

# 啓動 Prometheus 服務
$ systemctl start prometheus

# 設置 Prometheus 開機啓動
$ systemctl enable prometheus

# 驗證 Prometheu s正在運行
$ systemctl status prometheus

# 查看端口 9090
netstat -tunlp |grep 9090

在瀏覽器打開 http://server-ip:9090, server-ip 改爲你 centos 的 ip。你將會看到如下界面。

安裝 Node Exporter

node exoorter 用於收集 linux 系統的 CPU、內存、網絡等信息。

創建用戶

$ useradd -m -s /bin/false node_exporter

下載解壓

  1. 下載
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz

{% note warning flat %}

由於文章存在時效性,需要最新版本,請前往node exoorter 下載

{% endnote %}

  1. 解壓並複製到 /usr/local/bin
$ tar -zxpvf node_exporter-1.4.0.linux-amd64.tar.gz

# 複製到 /usr/local/bin
$ cp node_exporter-1.4.0.linux-amd64/node_exporter /usr/local/bin

# 修改目錄權限
$ chown node_exporter:node_exporter /usr/local/bin/node_exporter

以服務方式運行

創建 service 配置文件

$ vi /etc/systemd/system/node_exporter.service

複製以下內容

[Unit]

Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target
 

[Service]

User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter

 
[Install]

WantedBy=multi-user.target

{% note warning flat %}
注意檢查一下開頭和結束的內容,拷貝可能會造成內容丟失。
{% endnote %}

啓動服務

# 重新加載
$ systemctl daemon-reload

# 啓動 Prometheus 服務
$ systemctl start node_exporter

# 設置 Prometheus 開機啓動
$ systemctl enable node_exporter

# 驗證 Prometheu s正在運行
$ systemctl status node_exporter

# 查看端口 9090
$ netstat -tunlp |grep 9100

開放 9100 端口

$ firewall-cmd --add-port=9100/tcp  --permanent
$ firewall-cmd --reload

修改 Prometheus 配置,加入 node_exporter

$ vi /etc/prometheus/prometheus.yml

添加如下內容

- job_name: 'node_exporter'

   static_configs:

   - targets: ['localhost:9100']

完整的 prometheus.yml

# 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: 15s  # scrape_timeout is set to the global default (10s).

# 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'

    # metrics_path defaults to '/metrics'

    # scheme defaults to 'http'.

    static_configs:

    - targets: ['localhost:9090']
    
  - job_name: 'node_exporter'

    static_configs:

    - targets: ['localhost:9100']

重啓 Prometheus 服務

$ systemctl restart prometheus

在瀏覽器打開http://server-ip:9100/metrics,會看到如下界面:

安裝 Grafana

下載安裝

進入 Grafana 的 [下載頁面](Download Grafana | Grafana Labs)

選擇你的服務器版本:

這裏我們選擇 CentOS 的命令:

$ wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.2.2-1.x86_64.rpm
$ sudo yum install grafana-enterprise-9.2.2-1.x86_64.rpm

啓動 Grafana 服務

# 啓動 Grafana 服務
$ systemctl start grafana-server

# 設爲開機啓動
$ systemctl enable grafana-server

開放3000端口

$ firewall-cmd --add-port=3000/tcp  --permanent
$ firewall-cmd --reload

訪問地址 訪問地址:http://server-ip:3000

默認的賬號密碼是 admin/admin

配置 Prometheus

進入 Grafana 後,添加數據源:

<img src="https://teststation.oss-cn-chengdu.aliyuncs.com/blog/image-20221030205020232.png" alt="配置Prometheus" />

配置顯示模板

最終就能看到配置後的結果。

後續進入的話,可以從菜單>Dashboards進入:

附錄:問題

如何修改 Grafana 的默認端口

如果你已經部署了其他程序,佔用了3000端口,那麼你可能需要修改一下 Grafana的 默認端口。

  1. 修改 Grafana 的配置文件
$ vi /etc/grafana/grafana.ini
  1. 保存退出後,重新啓動 Grafana 服務:
$ systemctl start grafana-server

Prometheus沒有數據

按照步驟安裝之後,發現 Prometheus 沒有數據,大概率是服務器時間設置問題。

查看服務器上的日期:

$ timedatectl

<img src="https://teststation.oss-cn-chengdu.aliyuncs.com/blog/image-20221030211857035.png" alt="服務器時間" style="zoom: 67%;" />

如果服務器時間與你手機的時間不一致,則需要同步一下時間:

# ntp 同步時間
$ ntpdate ntp1.aliyun.com

# 如果 ntpdate 命令不存在,需要安裝一下
$ yum install ntp

查看 Grafana 中 Prometheus 配置的時間:

如果你已經是 Asia/Shanghai就不需要修改。確保 Prometheus 的時區與服務器的時區一致,且時間是正確的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章