Prometheus簡(jiǎn)單搭建-創(chuàng)新互聯(lián)

一.Prometheus介紹
Prometheus(普羅米修斯)是一個(gè)最初在SoundCloud上構(gòu)建的監(jiān)控系統(tǒng)。自2012年成為社區(qū)開源項(xiàng)目,擁有非?;钴S的開發(fā)人員和用戶社區(qū)。為強(qiáng)調(diào)開源及獨(dú)立維護(hù),Prometheus于2016年加入云原生云計(jì)算基金會(huì)(CNCF),成為繼Kubernetes之后的第二個(gè)托管項(xiàng)目。
https://prometheus.io
https://github.com/prometheus

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比余江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式余江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋余江地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

作為新一代的監(jiān)控框架,Prometheus 具有以下特點(diǎn):
? 多維數(shù)據(jù)模型:由度量名稱和鍵值對(duì)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù)
? PromSQL:一種靈活的查詢語言,可以利用多維數(shù)據(jù)完成復(fù)雜的查詢
? 不依賴分布式存儲(chǔ),單個(gè)服務(wù)器節(jié)點(diǎn)可直接工作
? 基于HTTP的pull方式采集時(shí)間序列數(shù)據(jù)
? 推送時(shí)間序列數(shù)據(jù)通過PushGateway組件支持
? 通過服務(wù)發(fā)現(xiàn)或靜態(tài)配置發(fā)現(xiàn)目標(biāo)
? 多種圖形模式及儀表盤支持
Prometheus適用于以機(jī)器為中心的監(jiān)控以及高度動(dòng)態(tài)面向服務(wù)架構(gòu)的監(jiān)控。
Prometheus簡(jiǎn)單搭建

二.Prometheus部署
轉(zhuǎn)載:https://blog.gmem.cc/prometheus-study-note
1.安裝環(huán)境:
server1:centos7.6 主節(jié)點(diǎn) 190.168.3.250
server2:centos7.6 節(jié)點(diǎn)1 190.168.3.251

為您的平臺(tái)下載最新版本的Prometheus,然后解壓縮并運(yùn)行它:

https://prometheus.io/download/

https://prometheus.io/docs/prometheus/latest/getting_started/

2.二進(jìn)制安裝
tar xvfz prometheus-2.15.0.linux-amd64.tar.gz
cd prometheus-2.15.0.linux-amd64
mv prometheus-2.15.0.linux-amd64 /usr/local/prometheus

啟動(dòng)
./prometheus --config.file=prometheus.yml

為了方便,加入到服務(wù)配置文件里
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml

[Install]
WantedBy=multi-user.target

重啟服務(wù)
systemctl daemon-reload
systemctl start prometheus.service

3.容器安裝
https://prometheus.io/docs/prometheus/latest/installation/
prometheus.yml通過運(yùn)行以下命令將您從主機(jī)綁定:
docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
或者為配置使用額外的卷:
docker run -p 9090:9090 -v /prometheus-data \
prom/prometheus --config.file=/prometheus-data/prometheus.yml

4.訪問web
http://190.168.3.250:9090訪問自己的狀態(tài)頁面
可以通過訪問localhost:9090驗(yàn)證Prometheus自身的指標(biāo):190.168.3.250:9090/metrics

三.配置監(jiān)控服務(wù)
1.配置Prometheus監(jiān)控本身
全局配置文件簡(jiǎn)介
有關(guān)配置選項(xiàng)的完整,請(qǐng)參閱:https://prometheus.io/docs/prometheus/latest/configuration/configuration/
Prometheus以scrape_interval規(guī)則周期性從監(jiān)控目標(biāo)上收集數(shù)據(jù),然后將數(shù)據(jù)存儲(chǔ)到本地存儲(chǔ)上。scrape_interval可以設(shè)定全局也可以設(shè)定單個(gè)metrics。
Prometheus以evaluation_interval規(guī)則周期性對(duì)告警規(guī)則做計(jì)算,然后更新告警狀態(tài)。evaluation_interval只有設(shè)定在全局。

global:全局配置
alerting:告警配置
rule_files:告警規(guī)則
scrape_configs:配置數(shù)據(jù)源,稱為target,每個(gè)target用job_name命名。又分為靜態(tài)配置和服務(wù)發(fā)現(xiàn)

global:
默認(rèn)抓取周期,可用單位ms、smhdwy #設(shè)置每15s采集數(shù)據(jù)一次,默認(rèn)1分鐘
[ scrape_interval: <duration> | default = 1m ]
默認(rèn)抓取超時(shí)
[ scrape_timeout: <duration> | default = 10s ]
估算規(guī)則的默認(rèn)周期 # 每15秒計(jì)算一次規(guī)則。默認(rèn)1分鐘
[ evaluation_interval: <duration> | default = 1m ]
和外部系統(tǒng)(例如AlertManager)通信時(shí)為時(shí)間序列或者警情(Alert)強(qiáng)制添加的標(biāo)簽列表
external_labels:
[ <labelname>: <labelvalue> ... ]

規(guī)則文件列表
rule_files:
[ - <filepath_glob> ... ]

抓取配置列表
scrape_configs:
[ - <scrape_config> ... ]

Alertmanager相關(guān)配置
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]

遠(yuǎn)程讀寫特性相關(guān)的配置
remote_write:
[ - <remote_write> ... ]
remote_read:
[ - <remote_read> ... ]

vi prometheus.yml
下面就是拉取自身服務(wù)采樣點(diǎn)數(shù)據(jù)配置
scrape_configs:
別監(jiān)控指標(biāo),job名稱會(huì)增加到拉取到的所有采樣點(diǎn)上,同時(shí)還有一個(gè)instance目標(biāo)服務(wù)的host:port標(biāo)簽也會(huì)增加到采樣點(diǎn)上

  • job_name: 'prometheus'
    覆蓋global的采樣點(diǎn),拉取時(shí)間間隔5s
    scrape_interval: 5s
    static_configs:
    • targets: ['localhost:9090']

最下面,靜態(tài)配置監(jiān)控本機(jī),采集本機(jī)9090端口數(shù)據(jù)

注:每次修改配置完成,用promtool檢測(cè)配置文件是否正確
[root@server1 prometheus]# ./promtool check config prometheus.yml

重啟服務(wù)
可以用kill -hup 進(jìn)程id 自動(dòng)加載新配置文件

查看targets可以看到節(jié)點(diǎn)正常
Prometheus簡(jiǎn)單搭建
也可以在這里搜尋收集來的數(shù)據(jù)
Prometheus簡(jiǎn)單搭建

2.配置服務(wù)發(fā)現(xiàn)監(jiān)控linux主機(jī)及相關(guān)服務(wù)
在server2 190.168.3.251上安裝node_exporter
使用文檔:https://prometheus.io/docs/guides/node-exporter/
GitHub:https://github.com/prometheus/node_exporter
exporter列表:https://prometheus.io/docs/instrumenting/exporters/

在server2安裝節(jié)點(diǎn)采集器,mysql服務(wù),nginx服務(wù)
tar zxf node_exporter-0.17.0.linux-amd64.tar.gz
mv node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter
cd /usr/local/node_exporter/
./node_exporter --help

服務(wù)發(fā)現(xiàn),我們這里使用file_sd_configs,寫監(jiān)控配置文件,服務(wù)發(fā)現(xiàn)node節(jié)點(diǎn)
Prometheus簡(jiǎn)單搭建

mkdir sd_config創(chuàng)建服務(wù)發(fā)現(xiàn)目錄
Prometheus簡(jiǎn)單搭建

寫采集地址targets
[root@server1 prometheus]# vi sd_config/node.yml
Prometheus簡(jiǎn)單搭建

方便管理服務(wù),將宿主機(jī)節(jié)點(diǎn)監(jiān)控采集node_exporter加入到服務(wù)配置文件里,
Prometheus簡(jiǎn)單搭建

如果要監(jiān)控節(jié)點(diǎn)的系統(tǒng)服務(wù),需要在后面添加名單參數(shù)
--collector.systemd.unit-whitelist=".+" 從systemd中循環(huán)正則匹配單元
--collector.systemd.unit-whitelist="(docker|sshd|nginx).service" 白名單,收集目標(biāo)

/usr/bin/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service

在http://190.168.3.250:9090/graph已經(jīng)能看到節(jié)點(diǎn)采集到的宿主機(jī)信息,已經(jīng)可以看到監(jiān)控到了服務(wù),值為1就是服務(wù)正常

Prometheus簡(jiǎn)單搭建

補(bǔ)充:
也可以將之前190.168.3.250上監(jiān)控本機(jī)的9090的靜態(tài)采集改成文件服務(wù)發(fā)現(xiàn)的形式
Prometheus簡(jiǎn)單搭建

[root@server1 prometheus]# vi sd_config/test.yml ,其中的加上了標(biāo)簽,添加采集地址,可以在targets里使用標(biāo)簽查找
Prometheus簡(jiǎn)單搭建
Prometheus簡(jiǎn)單搭建

四.使用grafana前端展示數(shù)據(jù)并監(jiān)控docker服務(wù)

1.cAdvisor采集容器信息
使用cAdvisor(Container Advisor)用于收集正在運(yùn)行的容器資源使用和性能信息。
docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \

http://190.168.3.250:8080/訪問web端口,已經(jīng)采集到了容器的數(shù)據(jù)

Prometheus簡(jiǎn)單搭建

2.Grafana

https://grafana.com/grafana/download

Grafana是一個(gè)開源的度量分析和可視化系統(tǒng),Grafana支持查詢普羅米修斯。自Grafana 2.5.0(2015-10-28)以來,包含了Prometheus的Grafana數(shù)據(jù)源。

在官網(wǎng)上下載https://grafana.com/grafana/download?platform=docker
我們這里用的是docker版的
docker run -d --name=grafana -p 3000:3000 grafana/grafana

跑起來后,190.168.3.250:3000訪問web接口
密碼賬號(hào)都是admin,第一次登陸需要修改密碼
添加數(shù)據(jù)源
Prometheus簡(jiǎn)單搭建
Prometheus簡(jiǎn)單搭建
已經(jīng)有一個(gè)Prometheus數(shù)據(jù)源
Prometheus簡(jiǎn)單搭建

3.添加監(jiān)控宿主機(jī)模板
Prometheus簡(jiǎn)單搭建

輸入9276,導(dǎo)入
Prometheus簡(jiǎn)單搭建
修改名字和數(shù)據(jù)庫
Prometheus簡(jiǎn)單搭建
完成,查看最后模板效果
Prometheus簡(jiǎn)單搭建

4.查看docker監(jiān)控
添加新job,修改采集配置文件prometheus.yml
[root@server1 prometheus]# vi prometheus.yml
Prometheus簡(jiǎn)單搭建
檢查配置文件后,重啟主服務(wù) ./promtool check config prometheus.yml

同樣方法在Grafana下載模板,導(dǎo)入193
也可以在https://grafana.com/dashboards/下載別的模板
Prometheus簡(jiǎn)單搭建

五.監(jiān)控mysql服務(wù)

mysql_exporter:用于收集MySQL性能信息。

下載https://github.com/prometheus/mysqld_exporter
監(jiān)控模板https://grafana.com/dashboards/7362

我們?cè)趕erver2 190.168.3.251上配置,需要下載mysqld_exporter數(shù)據(jù)庫采集和安裝mariadb

登錄mysql為exporter創(chuàng)建賬號(hào),以便可以采集數(shù)據(jù):
mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON . TO 'exporter'@'localhost';

vi .my.cnf ,添加用戶隱藏的數(shù)據(jù)庫賬號(hào),prometheus監(jiān)控mysql需要用這個(gè)賬號(hào)采集
Prometheus簡(jiǎn)單搭建

Prometheus簡(jiǎn)單搭建

在server1 190.168.3.250修改采集配置文件
[root@server1 prometheus]# vi prometheus.yml
Prometheus簡(jiǎn)單搭建

檢查配置文件并重啟服務(wù),systemctl restart prometheus.service

[root@server2 mysql_exporter]# ./mysqld_exporter --config.my-cnf=.my.cnf
Prometheus簡(jiǎn)單搭建

數(shù)據(jù)庫信息已經(jīng)采集到了
http://190.168.3.251:9104/metrics
Prometheus簡(jiǎn)單搭建

導(dǎo)入模板后7362后
Prometheus簡(jiǎn)單搭建

六.郵件告警
1.告警介紹
在Prometheus平臺(tái)中,警報(bào)由獨(dú)立的組件Alertmanager處理。通常情況下,我們首先告訴Prometheus Alertmanager所在的位置,然后在Prometheus配置中創(chuàng)建警報(bào)規(guī)則,最后配置Alertmanager來處理警報(bào)并發(fā)送給接收者(郵件,webhook、slack等)。

地址1:https://prometheus.io/download/
地址2:https://github.com/prometheus/alertmanager/releases
設(shè)置告警和通知的主要步驟如下:
第一步 部署Alertmanager
第二步 配置Prometheus與Alertmanager通信
第三步 在Prometheus中創(chuàng)建告警規(guī)則
Prometheus簡(jiǎn)單搭建

2.配置Prometheus與Alertmanager通信
Prometheus簡(jiǎn)單搭建
需要在官網(wǎng)下載以上的報(bào)警包

修改告警媒介,使用郵箱發(fā)送
[root@server1 alertmanager-0.16.0.linux-amd64]# vi alertmanager.yml
Prometheus簡(jiǎn)單搭建
注意:上面的驗(yàn)證密碼不是郵箱密碼,是授權(quán)碼
Prometheus簡(jiǎn)單搭建

修改完檢查配置文件alertmanager.yml是否正確,然后重啟服務(wù)
./alertmanager --config.file alertmanager.yml
systemctl restart prometheus.service

修改prometheus連接告警模塊,并設(shè)定告警規(guī)則
[root@server1 prometheus]# vi prometheus.yml
Prometheus簡(jiǎn)單搭建

[root@server1 prometheus]# vi rules/test.yml
參照官網(wǎng)告警上寫的
Prometheus簡(jiǎn)單搭建

groups:

  • name: example # 報(bào)警規(guī)則組名稱
    rules:
    • alert: InstanceDown
      expr: up == 0
      for: 1m #持續(xù)時(shí)間 , 表示持續(xù)一分鐘獲取不到信息,則觸發(fā)報(bào)警
      labels:
      severity: page # 自定義標(biāo)簽
      annotations:
      summary: "Instance {{ $labels.instance }} down" # 自定義摘要
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes." # 自定義具體描述

3.測(cè)試郵箱告警
我們把server2 上的mysql監(jiān)控關(guān)閉,測(cè)試告警
http://190.168.3.250:9090/alerts,已經(jīng)觸發(fā)
Prometheus簡(jiǎn)單搭建

我們看郵箱,收到報(bào)警
Prometheus簡(jiǎn)單搭建

當(dāng)前題目:Prometheus簡(jiǎn)單搭建-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article46/cosjeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)網(wǎng)站建設(shè)、服務(wù)器托管外貿(mào)建站、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化