容器監(jiān)控與報(bào)警:
容器監(jiān)控的實(shí)現(xiàn)方對(duì)比虛擬機(jī)或者物理機(jī)來(lái)說(shuō)比大的區(qū)別杆怕,比如容器在k8s環(huán)境中可以任意橫向擴(kuò)容與縮容,那么
就需要監(jiān)控服務(wù)能夠自動(dòng)對(duì)新創(chuàng)建的容器進(jìn)行監(jiān)控壳贪,當(dāng)容器刪除后又能夠及時(shí)的從監(jiān)控服務(wù)中刪除陵珍,而傳統(tǒng)的
zabbix的監(jiān)控方式需要在每一個(gè)容器中安裝啟動(dòng)agent,并且在容器自動(dòng)發(fā)現(xiàn)注冊(cè)方面并沒有比好的實(shí)現(xiàn)方式
Prometheus:
k8s的早期版本基于組件heapster實(shí)現(xiàn)對(duì)pod和node節(jié)點(diǎn)的監(jiān)控功能违施,但是從k8s 1.8版本開始使用metrics API的方
式監(jiān)控互纯,并在1.11版本 正式將heapster替換,后期的k8s監(jiān)控主要是通過(guò)metrics Server提供核心監(jiān)控指標(biāo)磕蒲,比如
Node節(jié)點(diǎn)的CPU和內(nèi)存使用率伟姐,其他的監(jiān)控交由另外一個(gè)組件Prometheus 完成收苏。
prometheus簡(jiǎn)介:
https://prometheus.io/docs/ #官方文檔
https://github.com/prometheus #github地址
Prometheus是基于go語(yǔ)言開發(fā)的一套開源的監(jiān)控亿卤、報(bào)警和時(shí)間序列數(shù)據(jù)庫(kù)的組合愤兵,是由SoundCloud公司開發(fā)的 開源監(jiān)控系統(tǒng),Prometheus是CNCF(Cloud Native Computing Foundation,云原生計(jì)算基金會(huì))繼kubernetes 之 后畢業(yè)的第二個(gè)項(xiàng)目,prometheus在容器和微服務(wù)領(lǐng)域中得到了廣泛的應(yīng)用排吴,其特點(diǎn)主要如下:
6.1.2:prometheus系統(tǒng)架構(gòu)圖:
6.1.3:prometheus 二進(jìn)制安裝:
https://prometheus.io/download/ #官方二進(jìn)制下載地址秆乳,監(jiān)聽端口為9090 6.1.3.1:二進(jìn)制方式安裝:
使用key-value的多維度格式保存數(shù)據(jù) 數(shù)據(jù)不使用MySQL這樣的傳統(tǒng)數(shù)據(jù)庫(kù),而是使用時(shí)序數(shù)據(jù)庫(kù)钻哩,目前是使用的TSDB 支持第三方dashboard實(shí)現(xiàn)更高的圖形界面屹堰,如grafana(Grafana 2.5.0版本及以上) 功能組件化 不需要依賴存儲(chǔ),數(shù)據(jù)可以本地保存也可以遠(yuǎn)程保存 服務(wù)自動(dòng)化發(fā)現(xiàn) 強(qiáng)大的數(shù)據(jù)查詢語(yǔ)句功(PromQL,Prometheus Query Language)
prometheus server:主服務(wù)街氢,接受外部http請(qǐng)求扯键,收集、存儲(chǔ)與查詢數(shù)據(jù)等 prometheus targets: 靜態(tài)收集的目標(biāo)服務(wù)數(shù)據(jù) service discovery:動(dòng)態(tài)發(fā)現(xiàn)服務(wù) prometheus alerting:報(bào)警通知 pushgateway:數(shù)據(jù)收集代理服務(wù)器(類似于zabbix proxy) data visualization and export: 數(shù)據(jù)可視化與數(shù)據(jù)導(dǎo)出(訪問(wèn)客戶端)
prometheus 二進(jìn)制安裝:
https://prometheus.io/download/ #官方二進(jìn)制下載地址珊肃,監(jiān)聽端口為9090
安裝鏡像
root@master:~# docker pull prom/prometheus
運(yùn)行
root@master:~# docker run --rm -p 9090:9090 prom/prometheus
下載鏡像
root@master:~# docker pull grafana/grafana:6.2.2
解壓
root@master:/usr/local/src# tar xf prometheus-2.13.0.linux-amd64.tar.gz
做個(gè)軟連接
root@master:/usr/local/src# ln -sv /usr/local/src/prometheus-2.13.0.linux-amd64 /usr/local/prometheus
創(chuàng)建prometheus啟動(dòng)腳本:
root@master:/usr/local/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
啟動(dòng)服務(wù)
root@master:/usr/local/prometheus# systemctl start prometheus
root@master:/usr/local/prometheus# systemctl enable prometheus
查看端口
root@master:/usr/local/prometheus# ss -tnl | grep 9090
LISTEN 0 128 *:9090 *:*
訪問(wèn)prometheus web界面:
node exporter:
收集各k8s node節(jié)點(diǎn)上的監(jiān)控指標(biāo)數(shù)據(jù)荣刑,監(jiān)聽端口為9100
二進(jìn)制方式安裝node exporter:
解壓
root@node1:/usr/local/src tar xf node_exporter-0.18.1.linux-amd64.tar.gz
root@node1:/usr/local/src ln -sv /usr/local/src/node_exporter-0.18.1.linux-amd64 /usr/local/node_exporter
啟動(dòng)
root@node1:/usr/local/node_exporter# ./node_exporter
訪問(wèn)
創(chuàng)建node exporter啟動(dòng)腳本:
root@node1:/usr/local/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
拷貝到另一個(gè)node節(jié)點(diǎn)
root@node1:/usr/local/node_exporter# scp /etc/systemd/system/node-exporter.service 192.168.200.207:/etc/systemd/system/
root@node1:/usr/local/node_exporter# systemctl start node-exporter
systemctl enable node-exporter
修改主節(jié)點(diǎn)
root@master:~# vim /usr/local/src/prometheus-2.13.0.linux-amd64/prometheus.yml
23 - job_name: 'prometheus'
24 static_configs:
25 - targets: ['localhost:9090']
26
27 - job_name: 'prometheus-node_exporter'
28 static_configs:
29 - targets: ['192.168.200.206:9100','192.168.200.207:9100']
重啟服務(wù)
root@master:~# systemctl restart prometheus
Grafana:
https://grafana.com/docs/
調(diào)用prometheus的數(shù)據(jù),進(jìn)行更專業(yè)的可視化
安裝這個(gè)自動(dòng)解決依賴關(guān)系
apt --fix-broken install -y
然后安裝
root@master2:~# dpkg -i grafana_6.4.2_amd64.deb
打開短褲
root@master2:~# vim /etc/grafana/grafana.ini
38 http_port = 3000
root@master2:~# systemctl restart grafana-server
查看監(jiān)聽端口
root@master2:~# ss -tnl | grep 3000
LISTEN 0 128 *:3000 *:*
驗(yàn)證圖形信息:
餅圖插件未安裝伦乔,需要提前安裝
https://grafana.com/grafana/plugins/grafana-piechart-panel
安裝插件
grafana-cli plugins install grafana-piechart-panel
重啟服務(wù)
root@master2:~# service grafana-server restart