1.1Prometheus?是一個開源的系統(tǒng)監(jiān)控和報警系統(tǒng),現(xiàn)在已經(jīng)加入到?CNCF?基金會,成為繼?k8s?之后
第二個在?CNCF?托管的項目,在?kubernetes?容器管理系統(tǒng)中,通常會搭配?prometheus?進行監(jiān)控,同 時也支持多種?exporter?采集數(shù)據(jù),還支持?pushgateway?進行數(shù)據(jù)上報,Prometheus?性能足夠支撐 上萬臺規(guī)模的集群。
下次有空多學一下relabel_configs,對k8s監(jiān)控的服務發(fā)現(xiàn)很有幫助
prometheus?特點
1.多維度數(shù)據(jù)模型
每一個時間序列數(shù)據(jù)都由?metric?度量指標名稱和它的標簽?labels?鍵值對集合唯一確定:
這個?metric?度量指標名稱指定監(jiān)控目標系統(tǒng)的測量特征(如:http_requests_total-?接收?http?請 求的總計數(shù))。labbels?開啟了?Prometheus?的多維數(shù)據(jù)模型:對于相同的度量名稱,通過不同標簽列表 的結(jié)合,?會形成特定的度量維度實例。(例如:所有包含度量名稱為/api/tracks?的?http?請求,打上method=POST?的標簽,則形成了具體的?http?請求)。這個查詢語言在這些度量和標簽列表的基礎(chǔ)上進行過 濾和聚合。改變?nèi)魏味攘可系娜魏螛撕炛担瑒t會形成新的時間序列圖。
2.靈活的查詢語言(PromQL)
可以對采集的?metrics?指標進行加法,乘法,連接等操作;?3.可以直接在本地部署,不依賴其他分布式存儲;
4.通過基于?HTTP?的?pull?方式采集時序數(shù)據(jù);
5.可以通過中間網(wǎng)關(guān)?pushgateway?的方式把時間序列數(shù)據(jù)推送到?prometheus server?端;?6.可通過服務發(fā)現(xiàn)或者靜態(tài)配置來發(fā)現(xiàn)目標服務對象(targets)。?7.有多種可視化圖像界面,如?Grafana?等。
8.高效的存儲,每個采樣數(shù)據(jù)占?3.5 bytes?左右,300?萬的時間序列,30s?間隔,保留?60?天,消耗 磁盤大概?200G。
9.做高可用,可以對數(shù)據(jù)做異地備份,聯(lián)邦集群,部署多套?prometheus,pushgateway?上報數(shù)據(jù)
1.3 prometheus?組件
從上圖可發(fā)現(xiàn),Prometheus?整個生態(tài)圈組成主要包括?prometheus server,Exporter,?pushgateway,alertmanager,grafana,Web ui?界面,Prometheus server?由三個部分組成,?Retrieval,Storage,PromQL
1.Retrieval?負責在活躍的?target?主機上抓取監(jiān)控指標數(shù)據(jù)?
2.Storage?存儲主要是把采集到的數(shù)據(jù)存儲到磁盤中?
3.PromQL?是?Prometheus?提供的查詢語言模塊。
1.Prometheus Server:
用于收集和存儲時間序列數(shù)據(jù)。
2.Client Library:
客戶端庫,檢測應用程序代碼,當?Prometheus?抓取實例的?HTTP?端點時,客戶端庫會將所有跟蹤
的?metrics?指標的當前狀態(tài)發(fā)送到?prometheus server?端。?3.Exporters:
prometheus?支持多種?exporter,通過?exporter?可以采集?metrics?數(shù)據(jù),然后發(fā)送到?prometheus server?端,所有向?promtheus server?提供監(jiān)控數(shù)據(jù)的程序都可以被稱為?exporter
4.Alertmanager:
從?Prometheus server?端接收到?alerts?后,會進行去重,分組,并路由到相應的接收方,發(fā)出 報警,常見的接收方式有:電子郵件,微信,釘釘, slack?等。
5.Grafana:
監(jiān)控儀表盤,可視化監(jiān)控數(shù)據(jù)
6.pushgateway:
各個目標主機可上報數(shù)據(jù)到?pushgatewy,然后?prometheus server?統(tǒng)一從?pushgateway?拉取
數(shù)據(jù)。
1.4 prometheus?幾種部署模式
基本?HA?模式
基本的?HA?模式只能確保?Promthues?服務的可用性問題,但是不解決?Prometheus Server?之間 的數(shù)據(jù)一致性問題以及持久化問題(數(shù)據(jù)丟失后無法恢復),也無法進行動態(tài)的擴展。因此這種部署方式適 合監(jiān)控規(guī)模不大,Promthues Server?也不會頻繁發(fā)生遷移的情況,并且只需要保存短周期監(jiān)控數(shù)據(jù)的 場景。
基本?HA +?遠程存儲方案
在解決了?Promthues?服務可用性的基礎(chǔ)上,同時確保了數(shù)據(jù)的持久化,當?Promthues Server?發(fā) 生宕機或者數(shù)據(jù)丟失的情況下,可以快速的恢復。 同時?Promthues Server?可能很好的進行遷移。因 此,該方案適用于用戶監(jiān)控規(guī)模不大,但是希望能夠?qū)⒈O(jiān)控數(shù)據(jù)持久化,同時能夠確保?Promthues Server?的可遷移性的場景。
基本?HA +?遠程存儲?+?聯(lián)邦集群方案
Promthues?的性能瓶頸主要在于大量的采集任務,因此用戶需要利用?Prometheus?聯(lián)邦集群的特 性,將不同類型的采集任務劃分到不同的?Promthues?子服務中,從而實現(xiàn)功能分區(qū)。例如一個?Promthues Server?負責采集基礎(chǔ)設(shè)施相關(guān)的監(jiān)控指標,另外一個?Prometheus Server?負責采集應用 監(jiān)控指標。再有上層?Prometheus Server?實現(xiàn)對數(shù)據(jù)的匯聚。
1.5 prometheus?工作流程
1. Prometheus server?可定期從活躍的(up)目標主機上(target)拉取監(jiān)控指標數(shù)據(jù),目標主
機的監(jiān)控數(shù)據(jù)可通過配置靜態(tài)?job?或者服務發(fā)現(xiàn)的方式被?prometheus server?采集到,這種方式默認的?pull?方式拉取指標;也可通過?pushgateway?把采集的數(shù)據(jù)上報到?prometheus server?中;還可通過 一些組件自帶的?exporter?采集相應組件的數(shù)據(jù);
2.Prometheus server?把采集到的監(jiān)控指標數(shù)據(jù)保存到本地磁盤或者數(shù)據(jù)庫;
3.Prometheus?采集的監(jiān)控指標數(shù)據(jù)按時間序列存儲,通過配置報警規(guī)則,把觸發(fā)的報警發(fā)送到?alertmanager
4.Alertmanager?通過配置報警接收方,發(fā)送報警到郵件,微信或者釘釘?shù)?
5.Prometheus?自帶的?web ui?界面提供?PromQL?查詢語言,可查詢監(jiān)控數(shù)據(jù)
6.Grafana?可接入?prometheus?數(shù)據(jù)源,把監(jiān)控數(shù)據(jù)以圖形化形式展示出
1.6 prometheus?如何更好的監(jiān)控?k8s?
對于 Kubernetes?而言,我們可以把當中所有的資源分為幾類:
1、基礎(chǔ)設(shè)施層(Node):集群節(jié)點,為整個集群和應用提供運行時資源?2、容器基礎(chǔ)設(shè)施(Container):為應用提供運行時環(huán)境
3、用戶應用(Pod):Pod?中會包含一組容器,它們一起工作,并且對外提供一個(或者 一組)功能
4、內(nèi)部服務負載均衡(Service):在集群內(nèi),通過?Service?在集群暴露應用功能,集群內(nèi) 應用和應用之間訪問時提供內(nèi)部的負載均衡
5、外部訪問入口(Ingress):通過?Ingress?提供集群外的訪問入口,從而可以使外部客戶 端能夠訪問到部署在?Kubernetes?集群內(nèi)的服務
因此,在不考慮?Kubernetes?自身組件的情況下,如果要構(gòu)建一個完整的監(jiān)控體系,我們應該 考慮,以下?5?個方面:
1)、集群節(jié)點狀態(tài)監(jiān)控:從集群中各節(jié)點的?kubelet?服務獲取節(jié)點的基本運行狀態(tài);?
2)、集群節(jié)點資源用量監(jiān)控:通過?Daemonset?的形式在集群中各個節(jié)點部署?Node Exporter?采集節(jié)點的資源使用情況;
3)、節(jié)點中運行的容器監(jiān)控:通過各個節(jié)點中?kubelet?內(nèi)置的?cAdvisor?中獲取個節(jié)點中所有容器的運行狀態(tài)和資源使用情況;
4)、從黑盒監(jiān)控的角度在集群中部署?Blackbox Exporter?探針服務,檢測?Service?和?Ingress?的可用性;
5)、如果在集群中部署的應用程序本身內(nèi)置了對?Prometheus?的監(jiān)控支持,那么我們還應該找 到相應的?Pod?實例,并從該?Pod?實例中獲取其內(nèi)部運行狀態(tài)的監(jiān)控指標。
2安裝采集節(jié)點資源指標組件?node-exporter
node-exporter?是什么?
采集機器(物理機、虛擬機、云主機等)的監(jiān)控指標數(shù)據(jù),能夠采集到的指標包括?CPU,?內(nèi)存,磁 盤,網(wǎng)絡(luò),文件數(shù)等信息。
安裝?node-exporter?組件,在?k8s?集群的控制節(jié)點操作
# kubectl create ns monitor-sa
把課件里的?node-exporter.tar.gz?鏡像壓縮包上傳到?k8s?的各個節(jié)點,
手動解壓: ?docker load -i node-exporter.tar.gz
?docker load -i node-exporter.tar.gz
node-export.yaml?文件在課件,可自行上傳到自己?k8s?的控制節(jié)點,內(nèi)容如下:?
# 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?采集數(shù)據(jù)
curl http://主機?ip:9100/metrics
#node-export?默認的監(jiān)聽端口是?9100,可以看到當前主機獲取到的所有監(jiān)控數(shù)據(jù)
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?節(jié)點的?cpu?花費的時間,以?s?為單位?#TYPE:說明當前指標的數(shù)據(jù)類型,上面是?counter?類型?node_cpu_seconds_total{cpu="0",mode="idle"}?:
cpu0?上?idle?進程占用?CPU?的總時間,CPU?占用時間是一個只增不減的度量指標,從類型中也可以 看出?node_cpu?的數(shù)據(jù)類型是?counter(計數(shù)器)
counter?計數(shù)器:只是采集遞增的指標
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?
該指標反映了當前主機在最近一分鐘以內(nèi)的負載情況,系統(tǒng)的負載情況會隨系統(tǒng)資源的
使用而變化,因此?node_load1?反映的是當前狀態(tài),數(shù)據(jù)可能增加也可能減少,從注釋中可以看出當前 指標類型為?gauge(標準尺寸)
gauge?標準尺寸:統(tǒng)計的指標可增加可減少
3.在?k8s?集群中安裝?Prometheus server?服務
3.1創(chuàng)建?sa?賬號
#在?k8s?集群的控制節(jié)點操作,創(chuàng)建一個?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
#執(zhí)行上面授權(quán)也會報錯,那就需要下面的授權(quán)命令:
kubectl create clusterrolebinding monitor-clusterrolebinding-1 -n monitor-sa -- clusterrole=cluster-admin --user=system:serviceaccount:monitor:monitor-sa
3.2?創(chuàng)建數(shù)據(jù)目錄
#在 god64?作節(jié)點創(chuàng)建存儲數(shù)據(jù)的目錄:?
]# mkdir /data?
]# chmod 777 /data/
3.3?安裝?prometheus?服務
以下步驟均在?
k8s?集群的控制節(jié)點操作:
創(chuàng)建一個?configmap?存儲卷,用來存放?prometheus?配置信息?prometheus-cfg.yaml ,k8s?的控制節(jié)點
安裝?prometheus server鏡像?prometheus-2-2-1.tar.gz,上傳到?k8s?的工作節(jié)點
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? #舊數(shù)據(jù)存儲目錄
? ? ? ? ? - --storage.tsdb.retention=720h? ? #何時刪除舊數(shù)據(jù),默認為 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
工作 節(jié)點我們創(chuàng)建了數(shù)據(jù)目錄/data,你在?k8s?集群的哪個節(jié)點創(chuàng)建/data,就讓?pod?調(diào)度到哪個節(jié)點。
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?創(chuàng)建一個?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?集群的控制節(jié)點的?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