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

本文由  網易雲 發佈。node

 

1 什麼是 Kubernetes?web

 
Kubernetes 是 Google 開源的容器集羣管理系統,其管理操做包括部署,調度和節點集羣間擴展等。
 
以下圖所示爲目前 Kubernetes 的架構圖,由 master 和 node 端構成,調度部署和擴展由 master 發起,node 協助 master 實現這些功能。
 docker

 
使用 Kubernetes 能夠作到:
 
自動化容器的部署和複製;
隨時擴展或收縮容器規模;
將容器組織成組,而且提供容器間的負載均衡;
提供容器彈性,若是容器失效就替換它等等。數據庫

2 Prometheus + Grafana

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


 

 
prometheus 截圖
 

 
grafana 截圖後端


 
那麼它們要如何安裝和配置?下面就分別對這二者進行個詳細的介紹。
 api

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 接口
 

 
安裝與配置緩存


 
下載 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 的狀態信息。架構

 


 

○ 在頁面 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 內容,而後進行繪圖

 


 

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。
 


 

添加 dashboard
 
添加 dashboard,經過名字區分不一樣的 dashboard 便可。
 

 


 

○爲 dashboard 添加監控 panel

 
常見的 panel 是 graph, 其中 metric 配置爲核心配置。Query 中使用的是 Prometheus query 語言,一個 panel 中能夠添加 n 多的 query,以圖形化方式顯示。(因此爲了視覺美觀和直觀,建議圖中的線條不要太多)


 

 

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 計算平均延遲時間比較相似。

 

來自: 網易雲-共創雲上精彩世界

 

 

瞭解 網易雲 :
網易雲官網:https://www.163yun.com/
新用戶大禮包:https://www.163yun.com/gift
網易雲社區:https://sq.163yun.com/

相關文章
相關標籤/搜索