配置 Prometheus 伺服器監控和 Grafana 看板

Wi1dcard發表於2019-01-10

譯者序:Prometheus 是伺服器監控系統的後起之秀,可以和 Kubernetes 完美結合用於監控大量叢集和應用。Grafana 是一款資料視覺化看板,可指定多個資料來源執行查詢,將枯燥的資料轉化為多維度的皮膚。兩者均為開源專案,通過配置可實現直觀強大的監控、報警、分析系統,實屬運維神器。

硬廣:https://wi1dcard.cn/

效果預覽:


原文地址:https://www.scaleway.com/docs/configure-pr...

本文將介紹如何使用 Prometheus + Grafana 看板監控伺服器狀態。

Prometheus(普羅米修斯)是一款從 2012 年開始研發的彈性監控解決方案。該系統將其資料儲存至時序資料庫,且提供了多維度的資料模型和強大的查詢語言來生成被監控資源的報表。

要使用 Prometheus 和 Grafana 大約有五個步驟:

  • 準備伺服器環境
  • 下載並安裝 Node Exporter
  • 下載並安裝 Prometheus
  • 配置 Prometheus
  • 下載並安裝 Grafana

準備伺服器環境

在本教程內,我們使用 Ubuntu Xenial(16.04)系統來演示。

  1. 為了能夠讓 Prometheus 安全地執行在我們的伺服器上,我們首先要為 Prometheus 和 Node Exporter 建立一個不含登入許可權的使用者。可使用 --no-create-home 引數避免建立使用者根目錄,使用 --shell /usr/sbin/nologin 來禁止使用者開啟 Shell。

    sudo useradd --no-create-home --shell /usr/sbin/nologin prometheus
    sudo useradd --no-create-home --shell /bin/false node_exporter
  2. 建立目錄,用於儲存 Prometheus 可執行檔案以及其配置:

    sudo mkdir /etc/prometheus
    sudo mkdir /var/lib/prometheus
  3. 設定以上目錄的擁有者為 prometheus 使用者,確保 Prometheus 有許可權訪問這些資料夾。

    sudo chown prometheus:prometheus /etc/prometheus
    sudo chown prometheus:prometheus /var/lib/prometheus

下載並安裝 Node Exporter

由於 Prometheus 僅具備採集系統指標的功能,因此我們需要通過 Node Exporter 來擴充套件它的能力。Node Exporter 是一款收集系統 CPU、磁碟、記憶體用量資訊並將它們公開以供抓取的工具。

  1. 下載 Node Exporter 的最新版本。

    wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz

    譯者注:由於本文專案仍在持續更新,故請到 GitHub 檢視最新版本 Release 連結再下載。以下類同,不再贅述。

  2. 解壓壓縮包後,會發現一個名為 node_exporter-0.16.0.linux-amd64 的目錄,包含了可執行檔案、README 以及許可證檔案:

    tar xvf node_exporter-0.16.0.linux-amd64.tar.gz
  3. 複製其中的可執行檔案到 /usr/local/bin 目錄下,並將其擁有者設為上文建立的 node_exporter 使用者:

    sudo cp node_exporter-0.16.0.linux-amd64/node_exporter /usr/local/bin
    sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
  4. Node Exporter 剩餘的檔案已不需要,刪除即可:

    rm -rf node_exporter-0.16.0.linux-amd64.tar.gz node_exporter-0.16.0.linux-amd64
  5. 為了讓 Node Exporter 能夠跟隨系統自動啟動,我們需要建立一個 Systemd 服務。例如使用 Nano 編輯器建立該服務檔案:

    sudo nano /etc/systemd/system/node_exporter.service
  6. 將以下內容複製到檔案內,儲存並退出。

    [Unit]
    Description=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
  7. 在 Node Exporter 中,收集器(Collectors)用於蒐集系統資訊。預設情況下,一部分收集器已被開啟,你可以在 README 檔案中檢視具體列表。如果你想要使用某些特定的收集器,可以在以上檔案的 ExecStart 配置中進行定義。例如:

    ExecStart=/usr/local/bin/node_exporter --collectors.enabled meminfo,hwmon,entropy

    編輯後記得儲存。

  8. 重啟 Systemd 以便能夠使用新建立的服務:

    sudo systemctl daemon-reload
  9. 使用以下命令啟動 Node Exporter:

    sudo systemctl start node_exporter
  10. 確保其啟動成功:

    sudo systemctl status node_exporter

    你將會看到類似如下的輸出,分別展示已啟動狀態 active (running),以及主程式 ID(PID):

    ● node_exporter.service - Node Exporter
       Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: enabled)
       Active: active (running) since Mon 2018-06-25 11:47:06 UTC; 4s ago
     Main PID: 1719 (node_exporter)
       CGroup: /system.slice/node_exporter.service
               └─1719 /usr/local/bin/node_exporter
  11. 沒什麼問題的話,便可以開啟 Node Exporter 的開機自啟動了:

    sudo systemctl enable node_exporter

下載並安裝 Prometheus

  1. 下載並解壓 Prometheus 的最新版本。

    sudo apt-get update && apt-get upgrade
    wget https://github.com/prometheus/prometheus/releases/download/v2.2.1/prometheus-2.2.1.linux-amd64.tar.gz
    tar xfz prometheus-*.tar.gz
    cd prometheus-*

    該目錄內包含以下兩個可執行檔案:

    • prometheus - Prometheus 主程式
    • promtool

    以下兩個子目錄則分別包含了 Web 介面、示例配置以及許可證檔案:

    • console
    • console_libraries
  2. 複製可執行檔案到 /usr/local/bin/ 目錄。

    sudo cp ./prometheus /usr/local/bin/
    sudo cp ./promtool /usr/local/bin/
  3. 設定以上檔案的擁有者為上文建立的 prometheus 使用者。

    sudo chown prometheus:prometheus /usr/local/bin/prometheus
    sudo chown prometheus:prometheus /usr/local/bin/promtool
  4. 複製 consoleconsole_libraries 目錄到 /etc/prometheus

    sudo cp -r ./consoles /etc/prometheus
    sudo cp -r ./console_libraries /etc/prometheus
  5. 設定以上目錄及其子目錄和檔案的擁有者為 prometheus 使用者。

    sudo chown -R prometheus:prometheus /etc/prometheus/consoles
    sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
  6. 回到先前下載的目錄,刪除不再需要的原始檔案。

    cd .. && rm -rf prometheus-*

配置 Prometheus

在使用 Prometheus 之前,首先需要進行基本的配置。由此,我們需要建立一個名為 prometheus.yml 的配置檔案。

注意:Prometheus 的配置檔案使用 YAML 格式,嚴格禁止使用 Tabs。如果檔案內容格式不正確,Prometheus 將無法正常啟動。故編輯配置檔案時請留心。

  1. 在文字編輯器(例如 Nano)內編輯 prometheus.yml 檔案。

    Prometheus 的配置檔案分為三個部分:globalrule_filesscrape_configs

    global 部分內,可以找到一些通用配置:scrape_interval 用於設定 Prometheus 多久抓取一次目標(Targets),evaluation_interval 用於設定多久計算一次規則(Rules)。而規則用於控制儲存預先計算的資料,以及何時生成告警(Alert)。

    rule_files 部分內,包含了 Prometheus 載入的規則檔案路徑。

    配置檔案的最後一個部分名為 scrape_configs,包含了 Prometheus 監控的資源資訊。

    以上配置檔案看起來應當類似:

    global:
      scrape_interval:     15s
      evaluation_interval: 15s
    
    rule_files:
      # - "first.rules"
      # - "second.rules"
    
      scrape_configs:
        - job_name: 'prometheus'
          scrape_interval: 5s
          static_configs:
            - targets: ['localhost:9090']

    全域性 scrape_interval 設定為了適用於多數情況的 15 秒。

    我們目前還沒有任何規則檔案,所以 rule_files 部分已使用 # 註釋。

    scrape_configs 部分,我們定義了第一個匯出器(Exporter),用於 Prometheus 監控它自己。由於我們需要更加精確的 Prometheus 狀態資訊,我們將該任務(Job)的 scrape_interval 降低為 5 秒。static_configstargets 參數列示匯出器的監聽地址。在本例中是同一伺服器,所以我們使用 localhost 以及 Prometheus 自己的埠 9090

    Prometheus 將會抓取在 scrape_configs 內定義的匯出器,因此我們需要將 Node Exporter 新增至該檔案,就像上文中監控 Prometheus 自己一樣。

    將以下部分加入配置檔案即可:

    - job_name: 'node_exporter'
        scrape_interval: 5s
        static_configs:
            - targets: ['localhost:9100']

    如上,我們再次覆蓋了 scrape_interval 配置並設定為 5 秒。並且 Node Exporter 與 Prometheus 執行在統一伺服器,所以我們可以直接使用 localhost 以及 Node Exporter 的預設埠:9100

    若是從外部伺服器抓取資料,你需要使用遠端伺服器的 IP 地址替換 localhost

    欲知 Prometheus 全部配置項,請閱讀 官方配置文件

  2. 設定該配置檔案的擁有者為 prometheus 使用者。

    sudo chown prometheus:prometheus /etc/prometheus/prometheus.yml

好了,Prometheus 服務已經準備好大顯身手了!

執行 Prometheus

  1. 直接使用以下命令啟動 Prometheus 即可,該命令將會以 prometheus 使用者的身份執行。

    sudo -u prometheus /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

    接著,你會看到一些狀態輸出,以及服務已啟動的資訊:

    level=info ts=2018-04-12T11:56:53.084000977Z caller=main.go:220 msg="Starting Prometheus" version="(version=2.2.1, branch=HEAD, revision=bc6058c81272a8d938c05e75607371284236aadc)"
    level=info ts=2018-04-12T11:56:53.084463975Z caller=main.go:221 build_context="(go=go1.10, user=root@149e5b3f0829, date=20180314-14:15:45)"
    level=info ts=2018-04-12T11:56:53.084632256Z caller=main.go:222 host_details="(Linux 4.4.127-mainline-rev1 #1 SMP Sun Apr 8 10:38:32 UTC 2018 x86_64 scw-041406 (none))"
    level=info ts=2018-04-12T11:56:53.084797692Z caller=main.go:223 fd_limits="(soft=1024, hard=65536)"
    level=info ts=2018-04-12T11:56:53.09190775Z caller=web.go:382 component=web msg="Start listening for connections" address=0.0.0.0:9090
    level=info ts=2018-04-12T11:56:53.091908126Z caller=main.go:504 msg="Starting TSDB ..."
    level=info ts=2018-04-12T11:56:53.102833743Z caller=main.go:514 msg="TSDB started"
    level=info ts=2018-04-12T11:56:53.103343144Z caller=main.go:588 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
    level=info ts=2018-04-12T11:56:53.104047346Z caller=main.go:491 msg="Server is ready to receive web requests."
  2. 開啟瀏覽器,輸入 http://IP.OF.YOUR.SERVER:9090 便能夠訪問到 Prometheus 的 Web 頁面了。如果一切正常,我們需要先暫時在命令列按下 Ctrl + C 結束程式。

    如果啟動 Prometheus 服務時有錯誤資訊輸出,請再次確認配置檔案是否存在語法錯誤。錯誤資訊將會指明應當如何檢查。

  3. 好了,Prometheus 已經能夠正常工作,但它還沒有跟隨系統啟動。接下來我們再次建立一個 Systemd 服務檔案來告知系統開機啟動:

    sudo nano /etc/systemd/system/prometheus.service

    該檔案將會指明使用 prometheus 使用者執行 Prometheus,並指定其配置檔案的路徑。

  4. 複製以下內容並貼上,儲存後退出文字編輯器。

    [Unit]
      Description=Prometheus Monitoring
      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
      ExecReload=/bin/kill -HUP $MAINPID
    
    [Install]
      WantedBy=multi-user.target
  5. 過載 systemd 後才能使用新建立的服務:

    sudo systemctl daemon-reload

    開啟 Prometheus 服務,實現開機自啟:

    sudo systemctl enable prometheus
  6. 啟動 Prometheus:

    sudo systemctl start prometheus

搞定,我們成功安裝 Prometheus 用來監控伺服器,Prometheus 服務已經可以正常訪問了。

Prometheus Web 端

Prometheus 內建一個簡易的 Web 服務,可通過 http://your.server.ip:9000 訪問。通過它能夠查詢其收集到的資料。

我們可以使用它驗證 Prometheus 服務的執行狀態:

此外,還可以查詢被收集的資料:

此 Web 頁面十分輕量,如果你不僅僅只想測試一下效果,Prometheus 團隊建議使用類似 Grafana 的工具來替代它。

安裝 Grafana

  1. 下載並安裝 Grafana。

    wget https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb
    sudo apt-get install -y adduser libfontconfig
    sudo dpkg -i grafana_5.4.2_amd64.deb
  2. 使用 systemd 開啟 Grafana 的開機自啟動。

    sudo systemctl daemon-reload && sudo systemctl enable grafana-server && sudo systemctl start grafana-server

    Grafana 已經開始執行,我們可以通過 http://your.server.ip:3000 訪問。預設的使用者名稱和密碼是 admin / admin

  3. 現在你需要建立一個資料來源(Data Source),也就是 Prometheus:

    • 點選 Grafana Logo 開啟側邊欄。
    • 在側邊欄內,點選「Data Sources」。
    • 選擇「Add New」。
    • 選擇「Prometheus」作為資料來源。
    • 設定 Prometheus 服務的 URL(在本例中為:http://localhost:9090/)。
    • 點選 「Add」即可測試連線並儲存為新的資料來源。

    如上配置應當類似:

  4. 現在你可以建立第一個看板(Dashboard)用於展示 Prometheus 收集到的資訊了。你也可以從共享看板集合匯入一些現成的看板。

    如下是一個例子看板,它查詢了節點伺服器的 CPU 使用量並展示在 Grafana 內:

在本教程中,我們安裝了 Prometheus 服務以及兩個資料匯出器供 Prometheus 抓取,並配置了由 Peometheus 提供資料的 Grafana 看板。不要猶豫,快去看看 PrometheusGrafana 的官方文件吧。

譯者推薦

擴充閱讀:

推薦看板:

本作品採用《CC 協議》,轉載必須註明作者和本文連結

Former WinForm and PHP Engineer. Now focus on #DevSecOps and global networking.

相關文章