架構(gòu)班的小伙伴作業(yè)看這里哦:(學(xué)習(xí)杰哥視頻的作業(yè)第35天)
1、安裝配置promethues和alertmanager,實(shí)現(xiàn)對k8s的監(jiān)控,并將監(jiān)控數(shù)據(jù)展示到grafana
前言
本篇文章介紹k8s集群中部署prometheus、grafana、alertmanager,并且配置prometheus的動態(tài)、靜態(tài)服務(wù)發(fā)現(xiàn),實(shí)現(xiàn)對容器、物理節(jié)點(diǎn)、service、pod等資源指標(biāo)監(jiān)控,并在Grafana的web界面展示prometheus的監(jiān)控指標(biāo),然后通過配置自定義告警規(guī)則,通過alertmanager實(shí)現(xiàn)qq、釘釘、微信報警,文章內(nèi)容較多,大概1.5萬以上字?jǐn)?shù),可以先關(guān)注和轉(zhuǎn)發(fā),在慢慢學(xué)習(xí)。
prometheus簡介
Prometheus是一個開源的系統(tǒng)監(jiān)控和報警系統(tǒng),現(xiàn)在已經(jīng)加入到CNCF基金會,成為繼k8s之后第二個在CNCF托管的項(xiàng)目,在kubernetes容器管理系統(tǒng)中,通常會搭配prometheus進(jìn)行監(jiān)控,同時也支持多種exporter采集數(shù)據(jù),還支持pushgateway進(jìn)行數(shù)據(jù)上報,Prometheus性能足夠支撐上萬臺規(guī)模的集群。
prometheus特點(diǎn)
1.多維度數(shù)據(jù)模型
1.1 時間序列數(shù)據(jù)由metrics名稱和鍵值對來組成
1.2 可以對數(shù)據(jù)進(jìn)行聚合,切割等操作
1.3 所有的metrics都可以設(shè)置任意的多維標(biāo)簽。
2.靈活的查詢語言(PromQL)
可以對采集的metrics指標(biāo)進(jìn)行加法,乘法,連接等操作;
3.可以直接在本地部署,不依賴其他分布式存儲;
4.通過基于HTTP的pull方式采集時序數(shù)據(jù);
5.可以通過中間網(wǎng)關(guān)pushgateway的方式把時間序列數(shù)據(jù)推送到prometheus server端;
6.可通過服務(wù)發(fā)現(xiàn)或者靜態(tài)配置來發(fā)現(xiàn)目標(biāo)服務(wù)對象(targets)。
7.有多種可視化圖像界面,如Grafana等。
8.高效的存儲,每個采樣數(shù)據(jù)占3.5 bytes左右,300萬的時間序列,30s間隔,保留60天,消耗磁盤大概200G。
prometheus組件介紹
1.Prometheus Server: 用于收集和存儲時間序列數(shù)據(jù)。
2.Client Library: 客戶端庫,檢測應(yīng)用程序代碼,當(dāng)Prometheus抓取實(shí)例的HTTP端點(diǎn)時,客戶端庫會將所有跟蹤的metrics指標(biāo)的當(dāng)前狀態(tài)發(fā)送到prometheus server端。
3.Exporters: prometheus支持多種exporter,通過exporter可以采集metrics數(shù)據(jù),然后發(fā)送到prometheus server端
4.Alertmanager: 從 Prometheus server 端接收到 alerts 后,會進(jìn)行去重,分組,并路由到相應(yīng)的接收方,發(fā)出報警,常見的接收方式有:電子郵件,微信,釘釘, slack等。
5.Grafana:監(jiān)控儀表盤
6.pushgateway: 各個目標(biāo)主機(jī)可上報數(shù)據(jù)到pushgatewy,然后prometheus server統(tǒng)一從pushgateway拉取數(shù)據(jù)。
prometheus架構(gòu)圖

? ? ? ? Prometheus整個生態(tài)圈組成主要包括prometheus server,Exporter,pushgateway,alertmanager,grafana,Web ui界面,Prometheus server由三個部分組成,Retrieval,Storage,PromQL
1 Retrieval:負(fù)責(zé)在活躍的target主機(jī)上抓取監(jiān)控指標(biāo)數(shù)據(jù)
2 Storage:存儲主要是把采集到的數(shù)據(jù)存儲到磁盤中
3 PromQL:是Prometheus提供的查詢語言模塊。
prometheus 二進(jìn)制安裝:
1 下載:https://prometheus.io/download/
# tar xvf prometheus-2.11.1.linux-amd64.tar.gz
# ln -sv /usr/local/src/prometheus-2.11.1.linux-amd64 /usr/local/prometheus
# cd /usr/local/prometheus
2 創(chuàng)建prometheus啟動腳本
# vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/usr/local/prometheus/
ExecStart=/usr/local/prometheus/prometheus --
config.file=/usr/local/prometheus/prometheus.yml
[Install]
WantedBy=multi-user.target
3 啟動prometheus服務(wù)
# systemctl daemon-reload
# systemctl restart prometheus
# systemctl enable prometheus
4 安裝node exporter
4.1 收集各k8s node節(jié)點(diǎn)上的監(jiān)控指標(biāo)數(shù)據(jù),監(jiān)聽端口為9100
# tar xvf node_exporter-0.18.1.linux-amd64.tar.gz
# ln -sv /usr/local/src/node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter
# cd /usr/local/node_exporter
4.2 創(chuàng)建node exporter啟動腳本
# vim /etc/systemd/system/node-exporter.service
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/usr/local/node_exporter/node_exporter
[Install]
WantedBy=multi-user.target
4.3 啟動node exporter服務(wù)
# systemctl daemon-reload
# systemctl restart node-exporter
# systemctl enable node-exporter
5 prometheus采集node 指標(biāo)數(shù)據(jù)
5.1 配置prometheus通過node exporter采集 監(jiān)控指標(biāo)數(shù)據(jù)
prometheus配置文件:prometheus.yml
global:
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'promethues-node'
static_configs:
- targets: ['192.168.7.110:9100','192.168.7.111:9100']
systemctl restart prometheus
安裝grafana
1 調(diào)用prometheus的數(shù)據(jù),進(jìn)行更專業(yè)的可視化
# rpm -hiv grafana-7.0.5-1.x86_64.rpm
2 配置文件
# vim /etc/grafana/grafana.ini
[server]
# Protocol (http, https, socket)
protocol = http
# The ip address to bind to, empty will bind to all interfaces
http_addr = 0.0.0.0
# The http port to use
http_port = 3000
3 啟動grafana
# systemctl start grafana-server.servicesystemctl enable grafana-server.service
在web界面添加prometheus數(shù)據(jù)源及模板



