Prometheus -1-1-1

1.1Prometheus?是一個開源的系統監控和報警系統,現在已經加入到?CNCF?基金會,成為繼?k8s?之后

第二個在?CNCF?托管的項目,在?kubernetes?容器管理系統中,通常會搭配?prometheus?進行監控,同 時也支持多種?exporter?采集數據,還支持?pushgateway?進行數據上報,Prometheus?性能足夠支撐 上萬臺規模的集群。

下次有空多學一下relabel_configs,對k8s監控的服務發現很有幫助

prometheus?特點

1.多維度數據模型

每一個時間序列數據都由?metric?度量指標名稱和它的標簽?labels?鍵值對集合唯一確定:

這個?metric?度量指標名稱指定監控目標系統的測量特征(如:http_requests_total-?接收?http?請 求的總計數)。labbels?開啟了?Prometheus?的多維數據模型:對于相同的度量名稱,通過不同標簽列表 的結合,?會形成特定的度量維度實例。(例如:所有包含度量名稱為/api/tracks?的?http?請求,打上method=POST?的標簽,則形成了具體的?http?請求)。這個查詢語言在這些度量和標簽列表的基礎上進行過 濾和聚合。改變任何度量上的任何標簽值,則會形成新的時間序列圖。

2.靈活的查詢語言(PromQL)

可以對采集的?metrics?指標進行加法,乘法,連接等操作;?3.可以直接在本地部署,不依賴其他分布式存儲;

4.通過基于?HTTP?的?pull?方式采集時序數據;

5.可以通過中間網關?pushgateway?的方式把時間序列數據推送到?prometheus server?端;?6.可通過服務發現或者靜態配置來發現目標服務對象(targets)。?7.有多種可視化圖像界面,如?Grafana?等。

8.高效的存儲,每個采樣數據占?3.5 bytes?左右,300?萬的時間序列,30s?間隔,保留?60?天,消耗 磁盤大概?200G。

9.做高可用,可以對數據做異地備份,聯邦集群,部署多套?prometheus,pushgateway?上報數據

1.3 prometheus?組件

從上圖可發現,Prometheus?整個生態圈組成主要包括?prometheus server,Exporter,?pushgateway,alertmanager,grafana,Web ui?界面,Prometheus server?由三個部分組成,?Retrieval,Storage,PromQL

1.Retrieval?負責在活躍的?target?主機上抓取監控指標數據?

2.Storage?存儲主要是把采集到的數據存儲到磁盤中?

3.PromQL?是?Prometheus?提供的查詢語言模塊。

1.Prometheus Server:

用于收集和存儲時間序列數據。

2.Client Library:

客戶端庫,檢測應用程序代碼,當?Prometheus?抓取實例的?HTTP?端點時,客戶端庫會將所有跟蹤

的?metrics?指標的當前狀態發送到?prometheus server?端。?3.Exporters:

prometheus?支持多種?exporter,通過?exporter?可以采集?metrics?數據,然后發送到?prometheus server?端,所有向?promtheus server?提供監控數據的程序都可以被稱為?exporter

4.Alertmanager:

從?Prometheus server?端接收到?alerts?后,會進行去重,分組,并路由到相應的接收方,發出 報警,常見的接收方式有:電子郵件,微信,釘釘, slack?等。

5.Grafana:

監控儀表盤,可視化監控數據

6.pushgateway:

各個目標主機可上報數據到?pushgatewy,然后?prometheus server?統一從?pushgateway?拉取

數據。

1.4 prometheus?幾種部署模式

基本?HA?模式

基本的?HA?模式只能確保?Promthues?服務的可用性問題,但是不解決?Prometheus Server?之間 的數據一致性問題以及持久化問題(數據丟失后無法恢復),也無法進行動態的擴展。因此這種部署方式適 合監控規模不大,Promthues Server?也不會頻繁發生遷移的情況,并且只需要保存短周期監控數據的 場景。

基本?HA +?遠程存儲方案

在解決了?Promthues?服務可用性的基礎上,同時確保了數據的持久化,當?Promthues Server?發 生宕機或者數據丟失的情況下,可以快速的恢復。 同時?Promthues Server?可能很好的進行遷移。因 此,該方案適用于用戶監控規模不大,但是希望能夠將監控數據持久化,同時能夠確保?Promthues Server?的可遷移性的場景。

基本?HA +?遠程存儲?+?聯邦集群方案

Promthues?的性能瓶頸主要在于大量的采集任務,因此用戶需要利用?Prometheus?聯邦集群的特 性,將不同類型的采集任務劃分到不同的?Promthues?子服務中,從而實現功能分區。例如一個?Promthues Server?負責采集基礎設施相關的監控指標,另外一個?Prometheus Server?負責采集應用 監控指標。再有上層?Prometheus Server?實現對數據的匯聚。

1.5 prometheus?工作流程

1. Prometheus server?可定期從活躍的(up)目標主機上(target)拉取監控指標數據,目標主

機的監控數據可通過配置靜態?job?或者服務發現的方式被?prometheus server?采集到,這種方式默認的?pull?方式拉取指標;也可通過?pushgateway?把采集的數據上報到?prometheus server?中;還可通過 一些組件自帶的?exporter?采集相應組件的數據;

2.Prometheus server?把采集到的監控指標數據保存到本地磁盤或者數據庫;

3.Prometheus?采集的監控指標數據按時間序列存儲,通過配置報警規則,把觸發的報警發送到?alertmanager

4.Alertmanager?通過配置報警接收方,發送報警到郵件,微信或者釘釘等?

5.Prometheus?自帶的?web ui?界面提供?PromQL?查詢語言,可查詢監控數據

6.Grafana?可接入?prometheus?數據源,把監控數據以圖形化形式展示出

1.6 prometheus?如何更好的監控?k8s?

對于 Kubernetes?而言,我們可以把當中所有的資源分為幾類:

1、基礎設施層(Node):集群節點,為整個集群和應用提供運行時資源?2、容器基礎設施(Container):為應用提供運行時環境

3、用戶應用(Pod):Pod?中會包含一組容器,它們一起工作,并且對外提供一個(或者 一組)功能

4、內部服務負載均衡(Service):在集群內,通過?Service?在集群暴露應用功能,集群內 應用和應用之間訪問時提供內部的負載均衡

5、外部訪問入口(Ingress):通過?Ingress?提供集群外的訪問入口,從而可以使外部客戶 端能夠訪問到部署在?Kubernetes?集群內的服務

因此,在不考慮?Kubernetes?自身組件的情況下,如果要構建一個完整的監控體系,我們應該 考慮,以下?5?個方面:

1)、集群節點狀態監控:從集群中各節點的?kubelet?服務獲取節點的基本運行狀態;?

2)、集群節點資源用量監控:通過?Daemonset?的形式在集群中各個節點部署?Node Exporter?采集節點的資源使用情況;

3)、節點中運行的容器監控:通過各個節點中?kubelet?內置的?cAdvisor?中獲取個節點中所有容器的運行狀態和資源使用情況;

4)、從黑盒監控的角度在集群中部署?Blackbox Exporter?探針服務,檢測?Service?和?Ingress?的可用性;

5)、如果在集群中部署的應用程序本身內置了對?Prometheus?的監控支持,那么我們還應該找 到相應的?Pod?實例,并從該?Pod?實例中獲取其內部運行狀態的監控指標。

2安裝采集節點資源指標組件?node-exporter

node-exporter?是什么?

采集機器(物理機、虛擬機、云主機等)的監控指標數據,能夠采集到的指標包括?CPU,?內存,磁 盤,網絡,文件數等信息。

安裝?node-exporter?組件,在?k8s?集群的控制節點操作

# kubectl create ns monitor-sa

把課件里的?node-exporter.tar.gz?鏡像壓縮包上傳到?k8s?的各個節點,

手動解壓: ?docker load -i node-exporter.tar.gz

?docker load -i node-exporter.tar.gz

node-export.yaml?文件在課件,可自行上傳到自己?k8s?的控制節點,內容如下:?

# cat node-export.yaml

apiVersion: apps/v1

kind: DaemonSet

metadata:

? name: node-exporter

? namespace: monitor-sa

? labels:

? ? name: node-exporter

spec:

? selector:

? ? matchLabels:

? ? name: node-exporter

? template:

? ? metadata:

? ? ? labels:

? ? ? ? name: node-exporter

? ? spec:

? ? ? hostPID: true

? ? ? hostIPC: true

? ? ? hostNetwork: true

? ? ? containers:

? ? ? - name: node-exporter

? ? ? ? image: prom/node-exporter:v0.16.0

? ? ? ? ports:

? ? ? ? - containerPort: 9100

? ? ? ? resources:

? ? ? ? ? requests:

? ? ? ? ? ? cpu: 0.15

? ? ? ? securityContext:

? ? ? ? ? privileged: true

? ? ? ? args:

? ? ? ? - --path.procfs

? ? ? ? - /host/proc

? ? ? ? - --path.sysfs

? ? ? ? - /host/sys

? ? ? ? - --collector.filesystem.ignored-mount-points

? ? ? ? - '"^/(sys|proc|dev|host|etc)($|/)"'

? ? ? ? volumeMounts:

? ? ? ? - name: dev

? ? ? ? ? mountPath: /host/dev

? ? ? ? - name: proc

? ? ? ? ? mountPath: /host/proc

? ? ? ? - name: sys

? ? ? ? ? mountPath: /host/sys

? ? ? ? - name: rootfs

? ? ? ? ? mountPath: /rootfs

? ? ? tolerations:

? ? ? - key: "node-role.kubernetes.io/master"

? ? ? ? operator: "Exists"

? ? ? ? effect: "NoSchedule"

? ? ? volumes:

? ? ? ? - name: proc

? ? ? ? ? hostPath:

? ? ? ? ? ? path: /proc

? ? ? ? - name: dev

? ? ? ? ? hostPath:

? ? ? ? ? ? path: /dev

? ? ? ? - name: sys

? ? ? ? ? hostPath:

? ? ? ? ? ? path: /sys

? ? ? ? - name: rootfs

? ? ? ? ? hostPath:

? ? ? ? ? ? path: /

kubectl apply -f node-export.yaml

#查看?node-exporter?是否部署成功

kubectl get pods -n monitor-sa

NAME? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE

node-exporter-5dh6b? 1/1? ? Running? 0? ? ? ? ? 11m

node-exporter-qd9f7? 1/1? ? Running? 0? ? ? ? ? 11m

通過?node-exporter?采集數據

curl http://主機?ip:9100/metrics

#node-export?默認的監聽端口是?9100,可以看到當前主機獲取到的所有監控數據

curl http://192.168.172.163:9100/metrics | grep node_cpu_seconds

顯示?192.168.172.163?主機?cpu?的使用情況

curl http://192.168.172.163:9100/metrics | grep node_cpu_seconds

? % Total? ? % Received % Xferd? Average Speed? Time? ? Time? ? Time? Current

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Dload? Upload? Total? Spent? ? Left? Speed

100 78237? 100 78237? ? 0? ? 0? 10.5M? ? ? 0 --:--:-- --:--:-- --:--:-- 12.4# HELP node_cpu_seconds_total Seconds the cpus spent in each mode.

M

# TYPE node_cpu_seconds_total counter

node_cpu_seconds_total{cpu="0",mode="idle"} 64776.09

node_cpu_seconds_total{cpu="0",mode="iowait"} 16.41

node_cpu_seconds_total{cpu="0",mode="irq"} 0

node_cpu_seconds_total{cpu="0",mode="nice"} 0.63

node_cpu_seconds_total{cpu="0",mode="softirq"} 121.24#HELP:解釋當前指標的含義,上面表示在每種模式下?node?節點的?cpu?花費的時間,以?s?為單位?#TYPE:說明當前指標的數據類型,上面是?counter?類型?node_cpu_seconds_total{cpu="0",mode="idle"}?:

cpu0?上?idle?進程占用?CPU?的總時間,CPU?占用時間是一個只增不減的度量指標,從類型中也可以 看出?node_cpu?的數據類型是?counter(計數器)

counter?計數器:只是采集遞增的指標

curl http://192.168.172.163:9100/metrics | grep node_load

# HELP node_load1 1m load average.

# TYPE node_load1 gauge

node_load1 0.1

node_load1?

該指標反映了當前主機在最近一分鐘以內的負載情況,系統的負載情況會隨系統資源的

使用而變化,因此?node_load1?反映的是當前狀態,數據可能增加也可能減少,從注釋中可以看出當前 指標類型為?gauge(標準尺寸)

gauge?標準尺寸:統計的指標可增加可減少

3.在?k8s?集群中安裝?Prometheus server?服務

3.1創建?sa?賬號

#在?k8s?集群的控制節點操作,創建一個?sa?賬號

kubectl create serviceaccount monitor -n monitor-sa

#把?sa?賬號?monitor?通過?clusterrolebing?綁定到?clusterrole?上

kubectl create clusterrolebinding monitor-clusterrolebinding -n monitor-sa --clusterrole=cluster-admin --serviceaccount=monitor-sa:monitor

#執行上面授權也會報錯,那就需要下面的授權命令:

kubectl create clusterrolebinding monitor-clusterrolebinding-1 -n monitor-sa -- clusterrole=cluster-admin --user=system:serviceaccount:monitor:monitor-sa

3.2?創建數據目錄

#在 god64?作節點創建存儲數據的目錄:?

]# mkdir /data?

]# chmod 777 /data/

3.3?安裝?prometheus?服務

以下步驟均在?

k8s?集群的控制節點操作:

創建一個?configmap?存儲卷,用來存放?prometheus?配置信息?prometheus-cfg.yaml ,k8s?的控制節點

安裝?prometheus server鏡像?prometheus-2-2-1.tar.gz,上傳到?k8s?的工作節點

cat prometheus-deploy.yaml?

apiVersion: apps/v1

kind: Deployment

metadata:

? name: prometheus-server

? namespace: monitor-sa

? labels:

? ? app: prometheus

spec:

? replicas: 1

? selector:

? ? matchLabels:

? ? ? app: prometheus

? ? ? component: server

? ? #matchExpressions:

? ? #- {key: app, operator: In, values: [prometheus]}

? ? #- {key: component, operator: In, values: [server]}

? template:

? ? metadata:

? ? ? labels:

? ? ? ? app: prometheus

? ? ? ? component: server

? ? ? annotations:

? ? ? ? prometheus.io/scrape: 'false'

? ? spec:

? ? ? nodeName: god64

? ? ? serviceAccountName: monitor

? ? ? containers:

? ? ? - name: prometheus

? ? ? ? image: prom/prometheus:v2.2.1

? ? ? ? imagePullPolicy: IfNotPresent

? ? ? ? command:

? ? ? ? ? - prometheus

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

? ? ? ? ? - --storage.tsdb.path=/prometheus? #舊數據存儲目錄

? ? ? ? ? - --storage.tsdb.retention=720h? ? #何時刪除舊數據,默認為 15 天。

? ? ? ? ? - --web.enable-lifecycle? ? ? ? ? #開啟熱加載

? ? ? ? ports:

? ? ? ? - containerPort: 9090

? ? ? ? ? protocol: TCP

? ? ? ? volumeMounts:

? ? ? ? - mountPath: /etc/prometheus/prometheus.yml

? ? ? ? ? name: prometheus-config

? ? ? ? ? subPath: prometheus.yml

? ? ? ? - mountPath: /prometheus

? ? ? ? ? name: prometheus-storage-volume

? ? ? volumes:

? ? ? ? - name: prometheus-config

? ? ? ? ? configMap:

? ? ? ? ? ? name: prometheus-config

? ? ? ? ? ? items:

? ? ? ? ? ? - key: prometheus.yml

? ? ? ? ? ? ? path: prometheus.yml

? ? ? ? ? ? ? mode: 0644

? ? ? ? - name: prometheus-storage-volume

? ? ? ? ? hostPath:

? ? ? ? ? ? path: /data

? ? ? ? ? ? type: Directory

工作 節點我們創建了數據目錄/data,你在?k8s?集群的哪個節點創建/data,就讓?pod?調度到哪個節點。

kubectl apply -f prometheus-deploy.yaml

#查看?prometheus?是否部署成功

kubectl get pods -n monitor-sa

NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE

node-exporter-5dh6b? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4d1h

node-exporter-qd9f7? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4d1h

prometheus-server-5bc47cc46d-6wn4t? 1/1? ? Running? 0? ? ? ? ? 2m49s

給?prometheus pod?創建一個?service

cat prometheus-svc.yaml

apiVersion: v1

kind: Service

metadata:

? name: prometheus

? namespace: monitor-sa

? labels:

? ? app: prometheus

spec:

? type: NodePort

? ports:

? ? - port: 9090

? ? ? targetPort: 9090

? ? ? protocol: TCP

? selector:

? ? app: prometheus

? ? component: server

kubectl apply -f prometheus-svc.yaml

#查看?service?在物理機映射的端口

kubectl get svc -n monitor-sa

NAME? ? ? ? TYPE? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? ? ? ? AGE

prometheus? NodePort? 10.106.198.175? <none>? ? ? ? 9090:31994/TCP? 28s

通過上面可以看到?service?在宿主機上映射的端口是?30009,這樣我們訪問?k8s?集群的控制節點的?ip:30009,就可以訪問到?prometheus?的?web ui?界面了

#訪問?prometheus web ui?界面 火狐瀏覽器輸入如下地址:

http://192.168.172.164:31994/graph

kubectl get pods -n monitor-sa -o wide

NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? ? IP? ? ? ? ? ? ? ? NODE? ? ? NOMINATED NODE? READINESS GATES

node-exporter-5dh6b? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4d4h? 192.168.172.164 ? ??

node-exporter-qd9f7? ? ? ? ? ? ? ? ? 1/1? ? Running? 0? ? ? ? ? 4d4h? 192.168.172.163 ? ?

prometheus-server-5bc47cc46d-6wn4t? 1/1? ? Running? 0? ? ? ? ? 143m? 10.244.209.134 ? ?

curl -X POST?http://10.244.209.134:9090/-/reload

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容