1. 程式人生 > >使用 Prometheus + Grafana 對 Kubernetes 進行效能監控的實踐

使用 Prometheus + Grafana 對 Kubernetes 進行效能監控的實踐

本文由 網易雲 釋出

1 什麼是 Kubernetes?


Kubernetes 是 Google 開源的容器叢集管理系統,其管理操作包括部署,排程和節點叢集間擴充套件等。
 
如下圖所示為目前 Kubernetes 的架構圖,由 master 和 node 端構成,排程部署和擴充套件由 master 發起,node 協助 master 實現這些功能。
 

使用-1 
使用 Kubernetes 可以做到:
 
自動化容器的部署和複製;
隨時擴充套件或收縮容器規模;
將容器組織成組,並且提供容器間的負載均衡;
提供容器彈性,如果容器失效就替換它等等。

2 Prometheus + Grafana

 
針對 Kubernetes master 端,如何評估各個元件的效能呢?目前社群提供一種搭建便捷、實用性強的監控方案:Prometheus + Grafana。
 
Prometheus 是使用 Golang 開發的開源監控系統,被人稱為下一代監控系統,是為數不多的適合 Docker、Mesos 、Kubernetes 環境的監控系統之一 。
 
Grafana 是一個開源的圖表視覺化系統,簡言之,其特點在於圖表配置比較方便、生成的圖表漂亮。
 
Prometheus + Grafana 監控系統的組合中,前者負責取樣資料並存儲這些資料;後者則側重於形象生動的展示資料。
 
搭建好的這兩個長(下面)這個樣子,是不是感覺 grafana 的圖形化展示能力很強大呢?
 

使用-2 
prometheus 截圖
 
使用-3 
grafana 截圖
 
那麼它們要如何安裝和配置?下面就分別對這兩者進行個詳細的介紹。
 

3 Prometheus

 
概念
 
Prometheus 是源於 Google Borgmon 的一個系統監控和報警工具,用 Golang 語言開發。基本原理是通過 HTTP 協議週期性地抓取被監控元件的狀態(pull 方式),這樣做的好處是任意元件只要提供 HTTP 介面就可以接入監控系統,不需要任何 SDK 或者其他的整合過程。
 
這樣做非常適合虛擬化環境比如 VM 或者 Docker ,故其為為數不多的適合 Docker、Mesos 、Kubernetes 環境的監控系統之一,被很多人稱為下一代監控系統。
 
pull 方式


 
Prometheus 採集資料用的是 pull 也就是拉模型,通過 HTTP 協議去採集指標,只要應用系統能夠提供 HTTP 介面就可以接入監控系統,相比於私有協議或二進位制協議來說開發簡單。
 
push 方式
 
對於定時任務這種短週期的指標採集,如果採用 pull 模式,可能造成任務結束了 Prometheus 還沒有來得及採集的情況,這個時候可以使用加一箇中轉層,客戶端推資料到 Push Gateway 快取一下,由 Prometheus 從 push gateway pull 指標過來。
 
組成及架構
 
○ Prometheus server:主要負責資料採集和儲存,提供 PromQL 查詢語言的支援;
○ Push Gateway:支援臨時性 Job 主動推送指標的中間閘道器;
○ exporters:提供被監控元件資訊的 HTTP 介面被叫做 exporter ,目前網際網路公司常用的元件大部分都有 exporter 可以直接使用,比如 Varnish、Haproxy、Nginx、MySQL、Linux 系統資訊 (包括磁碟、記憶體、CPU、網路等等);
○ PromDash:使用 rails 開發的 dashboard,用於視覺化指標資料;
○ WebUI:9090 埠提供的圖形化功能;
○ alertmanager:實驗性元件、用來進行報警;
○ APIclients:提供 HTTPAPI 介面
 

使用-4 
安裝與配置
 
下載 Prometheus
 
在官網 https://prometheus.io/download/ 中選擇合適的版本下載,解壓。
 
配置檔案
 
配置 job 和每個 job 要收集的目標 metric 資料來源即可。配置檔案分為 job、targets 兩級,Kubernetes 的監控中主要配置 api-server 和 etcd 的 metrics 地址。
 
其中,api-server 和 etcd 的 metrics 地址為: {apiserver_ip}:{apiserver_port}/metrics、{etcd_ip1}:{etcd_port1}/metrics,… ,{etcd_ipX}:{etcd_portX}/metrics
 
#my global config
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.pull資料的間隔時間——預設 evaluation_interval: 15s # By default, scrape targets every 15 seconds.# scrape_timeout is set to the global default (10s).# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: ‘codelab-monitor’# Load and evaluate rules in this file every ‘evaluation_interval’ seconds.
rule_files:
#- “first.rules”# – “second.rules”
 
#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=` to any timeseries scraped from this config.
– job_name: ‘etcd-server-v2’# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 10s #給每個job設定pull資料的間隔時間
# metrics_path defaults to ‘/metrics’# scheme defaults to ‘http’.
static_configs:
– targets: [‘{etcd_ip1}:{etcd_port1}’,'{etcd_ip2}:{etcd_port2}’, … , ‘{etcd_ipX}:{etcd_portX}’]

– job_name: ‘apiserver’# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 10s
#metrics_path defaults to ‘/metrics’# scheme defaults to ‘http’.
static_configs:
– targets: [‘{apiserver_ip}:{apiserver_port}’]
 
啟動方式
 
原始碼啟動——當前採用這個方式:直接啟動。 引數裡面指定配置檔案路徑、監聽埠號。
nohup ./prometheus -config.file=prometheus.yml -web.listen-address “:9090” -log.level=debug 2>&1 >> run.log &
 
啟動成功後的效果
 
○ 訪問監控頁面: http://{host_ip}:9090/(WebUI——9090 埠提供圖形化功能), status–>targets 可以看到 job 下面各個 metric 的狀態資訊。
 

使用-5 
○ 在頁面 http://{host_ip}:9090/graph 中輸入針對該 target 的 Prometheus query 語句即可實時繪圖,但是趨勢圖不能儲存,使用起來不便。
 
如以下 query 語句,用於計算名為 apiserver、scheduler 和 controller-manager 的 job 在 20s 內平均 cpu 使用率:rate(process_cpu_seconds_total{job=~”apiserver|scheduler|controller-manager”}[20s]),Prometheus query 語法見第 5 節。
 
以上請求得到的圖形可以認為是以下兩個步驟的組合:
 
○ 根據 http 請求:http://{host_ip}:9090/api/v1/query?query=rate(process_cpu_seconds_total{job=~”apiserver|scheduler|controller-manager”}[20s]) 後得到的 json 資料(包括 job 名稱、時間資訊和 cpu 使用率資訊等)
 
○ 解析 json 內容,然後進行繪圖
 
使用-6 

4 Grafana

 
Grafana 是一個開源的圖表視覺化系統,與 Kibana 類似,能夠對後端的資料進行實時展示,簡單地說圖表配置比較方便、生成的圖表比較漂亮。它一般和一些時間序列資料庫進行配合來展示資料,例如:Graphite、OpenTSDB、InfluxDB 和 Elasticsearch 等。
 
安裝與配置
 
下載 Grafana:
 
在官網 http://grafana.org/download/ 選擇合適版本下載、解壓。
 
啟動方式:
 
○ 原始碼啟動(當前採用這個方式)
 
配置檔案在 ./conf/defaults.ini, 比如預設的監聽埠是 3000,data、log 之類的路徑等,我們這邊均使用預設配置。
nohup ./bin/grafana-server -homepath ./ 2>&1 >> run.log &
 
○ docker 啟動
 
docker run –name grafana \ -d \
-p 3000:3000
-v $DATAPATH:/var/lib/grafana
grafana/grafana

啟動成功後效果
 
訪問頁面 http://{host_ip}:3000/ ,預設情況下管理員的賬號和密碼均為 admin,登入即可。
 
使用指南
 
○ 新增資料來源 datasource
 
新增 prometheus 地址,作為一個數據源,資料來源型別選擇的是 prometheus。當前 prometheus 內包含了3個 job(一個 Kubernetes、一個 etcd、一個 prometheus 自身), 其中 etcd 的 job 裡面又有四個 target,在 grafana 中稱其為 instance。
 

使用-7 
新增 dashboard
 
新增 dashboard,通過名字區分不同的 dashboard 即可。
 
使用-8 
○ 為 dashboard 新增監控 panel
 
常見的 panel 是 graph, 其中 metric 配置為核心配置。Query 中使用的是 Prometheus query 語言,一個 panel 中可以新增 n 多的 query,以圖形化方式顯示。(所以為了視覺美觀和直觀,建議圖中的線條不要太多)
 
使用-9 

5 Prometheus Query 語言

 
○ prometheus 的查詢語法基礎:https://prometheus.io/docs/querying/basics/
○ prometheus 查詢語法的操作符:https://prometheus.io/docs/querying/operators/
○ prometheus 的函式:https://prometheus.io/docs/querying/functions

○ prometheus 官方最佳實踐:https://prometheus.io/docs/practices/histograms/ ,其中有個例子和 Kubernetes 計算平均延遲時間比較類似。

感興趣的朋友可以點選這裡瞭解網易雲端計算基礎服務,包括容器服務物件儲存等。

瞭解 網易雲