Prometheus 入門

翻譯 原文鏈接

Prometheus是一個監(jiān)控平臺,通過抓取目標上和metric相關(guān)的HTTP endpoint,收集被監(jiān)控目標的metrics。本文會指導(dǎo)你怎么安裝、配置prometheus,并通過prometheus監(jiān)控資源。你將會下載、安裝、運行prometheus;下載、安裝exporter or tools(輸出主機或服務(wù)的時間序列數(shù)據(jù))。我們介紹的第一個exporter是Node Exporter(輸出主機級別的metrics,比如CPU、內(nèi)存、磁盤)。

下載Prometheus

下載使用你平臺的最新版本的Prometheus,然后解壓:

curl -LO https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

prometheus服務(wù)是一個單一的二進制文件。我們運行這個二進制文件,通過可選擇項--help尋求幫助

./prometheus --help
usage: prometheus [<flags>]

The Prometheus monitoring server

. . .

在開始使用Prometheus,讓我們先配置它。

配置Prometheus

prometheus配置文件是yaml文件。prometheus安裝包里面有一個簡單的配置文件模版叫prometheus.yml,我們從這里開始。

為了直觀,下面的prometheus.yml內(nèi)容省去了大多注釋的內(nèi)容(通過#可以增加注釋)。

global:
  scrape_interval:     15s
  evaluation_interval: 15s

rule_files:
  # - "first.rules"
  # - "second.rules"

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']

在上述配置文件中有三個區(qū)塊:global, rule_files and scrape_configs

global控制prometheus服務(wù)的全局配置。當前配置文件配置了兩項。scrape_interval控制prometheus抓取目標metrics的頻率(每15s采集一次目標metrics),當然對個別的目標可以覆蓋該全局配置。evaluation_interval控制評估規(guī)則的頻率,prometheus使用規(guī)則產(chǎn)生新的時間序列數(shù)據(jù)或者產(chǎn)生警報。

rule_files塊制定了規(guī)則所在的位置,prometheus可以根據(jù)這個配置加載規(guī)則,當前我們還沒有配置任何規(guī)則。

scrape_configs控制prometheus監(jiān)控哪些資源。由于prometheus通過HTTP endpoint暴露的它本身的監(jiān)控數(shù)據(jù),prometheus也能夠監(jiān)控本身的健康情況。在默認的配置里有一個單獨的job,叫做prometheus,它采集prometheus服務(wù)本身輸出的時間序列數(shù)據(jù)。這個job包含了一個單獨的、靜態(tài)配置的目標:在端口9090上的localhost。prometheus默認會通過目標的/metrics路徑采集metrics。所以,默認的job通過URL:http://localhost:9090/metrics采集metrics。收集到的時間序列會詳述prometheus服務(wù)的狀態(tài)和性能。

For a complete specification of configuration options, see the configuration documentation.

開始運行Prometheus

使用我們的配置文件運行prometheus,cd到prometheus目錄,運行prometheus:

./prometheus --config.file=prometheus.yml

運行prometheus之后,你可以通過http://localhost:9090訪問prometheus的狀態(tài)頁面。你也可以通過本地訪問metrics endpoint:http://localhost:9090/metrics確認prometheus在工作,并在產(chǎn)生它自己的metrics。

使用表達式瀏覽器

我們首先展示prometheus自己采集自己的數(shù)據(jù),prometheus自帶展示界面。打開瀏覽器,訪問http://localhost:9090/graph,點擊Console按鈕

其中一個prometheus輸出的metric是http_requests_total(prometheus服務(wù)接收的http請求總數(shù))。在瀏覽器輸入http_requests_total,會返回許多不同的時間序列(每個序列按照時間排序)。所有的序列有著同一個metric namehttp_requests_total,不同的標簽;這些標簽標明不同類型的請求。如果我們僅僅對狀態(tài)碼200的請求感興趣,可以使用下面的查詢表達式查詢相應(yīng)的信息:

http_requests_total{code="200"}

為了計算序列的數(shù)量,可以使用如下查詢表達式:

count(http_requests_total)

For more about the expression language, see the expression language documentation.

使用繪圖界面

訪問http://localhost:9090/graph,點擊Graph按鈕。

輸入如下表達式,繪制每秒HTTP請求率:

rare(http_requests_total[1m])

安裝Node Exporter

收集本身的metrics不能很好的表現(xiàn)Prometheus的能力。所以,接下來我們使用Node Exporter監(jiān)控我們第一個資源Linux Host,這個樣例是監(jiān)控prometheus所在的host,不過你可以監(jiān)控任意一個host(只要prometheus能夠訪問)。你也可以使用VMI Exporter采集windows主機。

Download the latest release of the Node Exporter of Prometheus for your platform, then extract it:

curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*

Node Exporter是一個單獨的二進制文件,node_exporter,有一組可配置的收集器用于手機各種基于主機的metrics。默認,收集器會收集cpu、內(nèi)存、磁盤和其它metrics,暴露,使prometheus能夠抓取監(jiān)控數(shù)據(jù)。
啟動Node Exporter:

./node_exporter

Node Exporter的metrics可以通過訪問9100端口的/metrics路徑獲取。本例中,訪問地址:http://localhost:9100/metrics

現(xiàn)在我們需要配置Prometheus,使Prometheus能夠發(fā)現(xiàn)這個exporter。

配置prometheus監(jiān)控Host

我們將會配置prometheus抓取新的目標metrics。我們需要在prometheus.yml配置的scrape_configs區(qū)增加一個job。

- job_name: node
  static_configs:
    - targets: ['localhost:9100']

我們新的job叫做“node”。它抓取一個靜態(tài)的目標,在端口9100上的loclahost。你可以用hostname或者IP地址替換localhost。

現(xiàn)在我們需要重新加載Prometheus配置來激活這個新的job。reload配置有兩種方式:

  • send SIGHUP signal
kill -HUP <pid>
  • send a HTTP POST to the Prometheus web server
  • 使用API來reload prometheus需要在啟動prometheus時,開啟web.enable-lifecycle配置參數(shù)
  • --web.enable-lifecycle Enable shutdown and reload via HTTP request.
  • /prometheus --config.file=prometheus.yml --web.enable-lifecycle
curl -X POST http://localhost:9090/-/reload

訪問prometheus,在“Execute”按鈕旁邊有一個下拉框,在下拉框中可以看到prometheus采集的指標列表。在這個列表中能夠看到以node_開頭metrics,這些metrics就是Node Exporter收集的。比如通過node_cpu查看節(jié)點CPU使用率。

一個很有用的metric是up metric。up metric能夠標識目標的狀態(tài)。如果值是1,那么能夠成功的從目標抓取metrics;如果值是0,那么則標明從該目標抓取metrics失敗。在一定程度上能夠通過改指標判斷目標的狀態(tài)。當前只能看到兩個up metric,一個是關(guān)于Node Exporter的,一個是關(guān)于prometheus的。

總結(jié)

本文簡單介紹了如何安裝、配置prometheus來監(jiān)控資源信息。同時,也安裝了第一個exporter,介紹了時間序列的基本查詢方法。
You can find more documentation and guides to help you continue to learn more about Prometheus.

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

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

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