在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    重磅!DIY的Prometheus主備方案,全網(wǎng)唯一 生產(chǎn)未上,測(cè)試先行

    重磅!DIY的Prometheus主備方案,全網(wǎng)唯一 生產(chǎn)未上,測(cè)試先行

    寫在開篇

    關(guān)于prometheus的高可用方案,經(jīng)過筆者不斷的研究、對(duì)比、和搜索,發(fā)現(xiàn)不管是官方、還是各大搜索引擎搜索出來的方案,都不符合筆者的需求。因此,筆者自己設(shè)計(jì)了一套prometheus主備的方案。該方案是一個(gè)很low的方案,但經(jīng)過不斷的實(shí)踐、驗(yàn)證,最后發(fā)現(xiàn)還挺實(shí)用。關(guān)于本方案,筆者以后還會(huì)找機(jī)會(huì)用go或者python開發(fā)一個(gè)帶UI界面的prometheus主備管理器,讓它的功能更加完善,做到更自動(dòng)化和智能化。Prometheus是監(jiān)控領(lǐng)域的新啟之秀,潛力非常大,K8S內(nèi)置對(duì)它直接支持,直接有提供exporter,而K8S又是未來基礎(chǔ)架構(gòu)的主力軍。而監(jiān)控方面,prometheus成為未來的主力軍是勝券在握,把它玩透了你絕對(duì)不吃虧。好了,前戲有點(diǎn)多了。敬請(qǐng)大家的關(guān)注、點(diǎn)贊、轉(zhuǎn)發(fā)。下面的正式進(jìn)入主題?。。?/p>

    • DIY的prometheus主備方案架構(gòu)圖

    方案說明

  • 兩臺(tái)主機(jī)(master和slave)分別部署keepalived,讓master主機(jī)接管VIP,注意:keepalived建議配置成非搶占模式
  • 之所以采用VIP的原因如下:
    • 為了方便日常訪問Prometheus頁面和Alertmanager頁面,在主備切換時(shí),可無需更換訪問ip。
    • 上層可視化應(yīng)用(如grafana)通過VIP來對(duì)接Prometheus的數(shù)據(jù)源,當(dāng)主備切換時(shí),無需在grafana上修改對(duì)應(yīng)的數(shù)據(jù)源。
  • 日常由master主機(jī)處于工作狀態(tài),在master中,啟動(dòng)Promethues和Alertmanager組件,啟動(dòng)webhook腳本(告警消息推送腳本,用于將告警推送到其他平臺(tái))。
  • slave主機(jī)備用狀態(tài),在slave中,需要啟動(dòng)Promethues組件(用于拉取指標(biāo)數(shù)據(jù)),啟動(dòng)Alertmanager組件(用于接收警報(bào)消息),這里注意,webhook腳本需處于停止?fàn)顟B(tài)(不進(jìn)行告警推送到其他平臺(tái))。這樣做是為了規(guī)避推送重復(fù)告警的問題,雖然Alertmanager有自身的去重告警功能,但這樣的設(shè)計(jì)根本就沒有告警重復(fù),已經(jīng)將重復(fù)扼殺在搖籃里了。
  • 在接入監(jiān)控對(duì)象時(shí)(部署對(duì)應(yīng)的exporter),切記,僅需要在master上做配置即可, slave定期從master拉取配置文件(包括主配置文件、警報(bào)規(guī)則文件等),定期和master保持配置同步。
  • master和slave的配置保持同步,意味著兩邊都會(huì)拉取被監(jiān)控對(duì)象的監(jiān)控指標(biāo)數(shù)據(jù)。監(jiān)控指標(biāo)的拉取、警報(bào)的觸發(fā)兩臺(tái)均一起工作,但告警的推送只有master在負(fù)責(zé),slave不負(fù)責(zé)告警的推送,如果master不可用了,就需要將slave上的webhook腳本手動(dòng)拉起來,由slave上的webhook腳本接管告警推送的任務(wù)。
  • 配置文件同步的做法是采用最原始、最簡(jiǎn)單、最粗暴的辦法,master和slave的配置文件同步方案如下:
  • Master主機(jī):

    • master提供配置文件下載服務(wù),由python自帶的SimpleHTTPServer模塊實(shí)現(xiàn),且需要在prometheus或alertmanager規(guī)范安裝路徑下(如/usr/local/prometheus)進(jìn)行SimpleHTTPServer模塊的啟動(dòng),拉起后,默認(rèn)的監(jiān)聽端口是8000。
    • master檢測(cè)配置文件變化情況,如達(dá)到條件則觸發(fā)備份和打包新的配置目錄。在master上,設(shè)計(jì)了一個(gè)保存通知?jiǎng)幼鞯奈募otice_slave.action,配置發(fā)生變化寫入1,配置沒有發(fā)生變化寫入0。同時(shí),該檢測(cè)腳本作為常駐進(jìn)程在后臺(tái)運(yùn)行。

    Slave主機(jī):

    • slave從master下載通知?jiǎng)幼鞯奈募otice_slave.action,根據(jù)狀態(tài)碼(1和0)來決定接下來的動(dòng)作,如果是1,則:從master下載配置壓縮包、備份原有配置目錄、解壓新下載后的配置壓縮包、熱重啟相關(guān)組件(prometheus、alertmanger),如果是0則什么都不做。

    對(duì)于配置文件的同步,也是有兩種實(shí)現(xiàn)方式的,要么是推,要么是拉,筆者的這個(gè)方案里是后者,筆者目前之所以折騰零散的shell腳本來去做高可用的管理,是為了能快速解決需求,因此才做了這個(gè)簡(jiǎn)陋的方案,筆者的原則是:困難的事情簡(jiǎn)單做,簡(jiǎn)單的事情咱不做(開玩笑哈!?。。?。 當(dāng)然,筆者以后會(huì)通過Go或者Python打造一個(gè)管理Promtheus主備的工具,且是帶UI的管理工具,敬請(qǐng)期待推出!我不造車,我只造零件。


    一、規(guī)劃和規(guī)范

    1. 設(shè)備規(guī)劃(本示例為測(cè)試環(huán)境)

    角色

    物理IP

    VIP

    安裝組件

    告警推送方式

    master

    192.168.11.146

    192.168.11.203(當(dāng)前接管)

    prometheus、alertmanager(均拉起)

    webhook方式,腳本拉起

    slave

    192.168.11.147

    prometheus、alertmanager(均拉起)

    webhook方式,腳本不拉起(備用)

    2. 統(tǒng)一安裝路徑規(guī)范

    master和slave主機(jī)的標(biāo)準(zhǔn)安裝路徑均為:/usr/local/,筆者安裝組件后的環(huán)境如下:

    /usr/local/keepalived (注意:建議keepalived配置成非搶占模式)/usr/local/prometheus/usr/local/alertmanager

    至于安裝路徑的規(guī)范,請(qǐng)自行根據(jù)實(shí)際情況敲定。

    3. prometheus組件配置文件和目錄規(guī)范

    • 所有配置文件統(tǒng)一標(biāo)準(zhǔn)路徑:/usr/local/prometheus/conf/
    • prometheus主配置文件:/usr/local/prometheus/conf/prometheus.yml
    • 業(yè)務(wù)粒度,一個(gè)業(yè)務(wù)對(duì)應(yīng)一個(gè)目錄,業(yè)務(wù)下不同的監(jiān)控對(duì)象都放在對(duì)應(yīng)的業(yè)務(wù)目錄下:/usr/local/prometheus/conf/business

    特別說明1:請(qǐng)自行在prometheus組件的安裝目錄下創(chuàng)建conf目錄,并將默認(rèn)的prometheus.yml配置文件移動(dòng)進(jìn)去

    特別說明2:請(qǐng)自行在prometheus組件的配置文件統(tǒng)一標(biāo)準(zhǔn)路徑(./conf)下創(chuàng)建業(yè)務(wù)目錄business

    特別說明3:業(yè)務(wù)目錄下,又包含兩個(gè)目錄:job和rule,job用于存放監(jiān)控拉取配置文件,rule用于存放警報(bào)規(guī)則配置文件

    配置文件目錄和業(yè)務(wù)目錄規(guī)劃示范,如下:

    /usr/local/prometheus/ # 這是規(guī)范的安裝路徑/usr/local/prometheus/conf/ # 這是規(guī)范的配置目錄/usr/local/prometheus/conf/prometheus.yml # 這是主配置文件/usr/local/prometheus/conf/business 這是按業(yè)務(wù)粒度規(guī)劃的業(yè)務(wù)根目錄# 如下是業(yè)務(wù)A的規(guī)劃案例:/usr/local/prometheus/conf/business/a_business/ 這是業(yè)務(wù)a的目錄/usr/local/prometheus/conf/business/a_business/job/oracle.yml 這是業(yè)務(wù)a下拉取oracle監(jiān)控配置數(shù)據(jù)的yml配置文件/usr/local/prometheus/conf/business/a_business/rule/oracle.rules 這是業(yè)務(wù)a下oracle的警報(bào)規(guī)則rules配置文件

    特別說明:上述對(duì)業(yè)務(wù)A的配置文件規(guī)劃案例非常重要,請(qǐng)務(wù)必參照此規(guī)范。

    4. alertmanager組件配置文件和目錄規(guī)范

    關(guān)于Alertmanager組件的配置文件,相對(duì)來說沒prometheus那么復(fù)雜,主要的規(guī)劃還是在prometeus中

    • alertmanager的主配置文件統(tǒng)一標(biāo)準(zhǔn)路徑放在prometeheus的conf中:/usr/local/prometheus/conf/alertmanager.yml

    5. 備份路徑規(guī)范

    在master主機(jī)上,會(huì)自動(dòng)備份原有的conf配置文件目錄

    • Prometheus組件

    統(tǒng)一備份路徑為:/usr/local/prometheus/backup/

    • Alertmanager組件

    不涉及到備份

    6. 日志目錄規(guī)范

    在master主機(jī)和slave主機(jī)上運(yùn)行的腳本日志均統(tǒng)一存放在指定目錄

    • Prometheus組件

    統(tǒng)一日志目錄:/usr/local/prometheus/logs/

    • AlertManager組件

    統(tǒng)一日志目錄:/usr/local/alertmanager/logs/


    二、組件安裝部署

    注意:master和slave均需要安裝如下組件

    • keepalived高可用組件
    • prometheus監(jiān)控組件
    • alertmanager警報(bào)組件

    因組件的安裝部署不是本文的主題,所以筆者在這里就不再撰寫安裝步驟,在此省略了哈,請(qǐng)自行安裝好即可。


    三、prometheus配置文件目錄同步部署

    說明1:均需要在master和slave上部署文件同步相關(guān)腳本

    說明2:以下的每一步操作,請(qǐng)均進(jìn)入到“/usr/local/prometheus/”目錄下進(jìn)行操作(此目錄是之前已經(jīng)定為安裝規(guī)范的目錄),如您的規(guī)范目錄和筆者的不同,請(qǐng)進(jìn)入到您自己的規(guī)范目錄下。

    說明3:以下涉及的腳本,涉及的目錄:conf、backup、logs、cfmd5,請(qǐng)自行在規(guī)范的目錄下進(jìn)行創(chuàng)建即可。

    1. master部署配置文件下載服務(wù)

  • 通過python拉起簡(jiǎn)單的Http服務(wù),默認(rèn)監(jiān)聽端口為8000,創(chuàng)建腳本startPromconfSyncApi.sh
  • startPromconfSyncApi.sh腳本內(nèi)容如下:

    #!/bin/shnohup /usr/bin/python -m SimpleHTTPServer > /dev/null &

    運(yùn)行配置文件下載服務(wù)的腳本

    sh startPromconfSyncApi.sh

    拉起http服務(wù)腳本后查看端口

    [root@prosvr-master prometheus]# netstat -tulnp | grep 8000tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 1293/python [root@prosvr-master prometheus]#

  • 創(chuàng)建配置文件變化檢查腳本startTarPackConf.sh
  • 注意,請(qǐng)?jiān)谝?guī)范的安裝路徑/usr/local/prometheus/下面創(chuàng)建startTarPackConf.sh腳本,以及創(chuàng)建目錄cfmd5

    startTarPackConf.sh腳本內(nèi)容:

    #!/bin/shtime_log=`date “+%Y-%m-%d %H:%M:%S”`echo “${time_log} 配置檢查器啟動(dòng)”task_wait_sec=4find ./conf -type f -print0 | xargs -0 md5sum > ./cfmd5/cfmd5.listwhile truedo time_bak=`date “+%Y%m%d%H%M%S”` time_log=`date “+%Y-%m-%d %H:%M:%S”` md5sum -c ./cfmd5/cfmd5.list > ./cfmd5/check_cfmd5.log md5ret=`cat ./cfmd5/check_cfmd5.log | grep “FAILED” | wc -l` while true do if [ ${md5ret} -gt 0 ] then echo “${time_log} 配置文件發(fā)生變化,觸發(fā)備份和打包壓縮” mv ./conf.tar.gz ./backup/conf.tar.gz_bak_${time_bak} tar -zcf conf.tar.gz conf/ echo 1 > ./notice_slave.action curl -X POST http://127.0.0.1:9090/-/reload break else echo 0 > ./notice_slave.action break fi done find ./conf -type f -print0 | xargs -0 md5sum > ./cfmd5/cfmd5.list sleep ${task_wait_sec}done

    腳本實(shí)現(xiàn)說明:很簡(jiǎn)單,就是遞歸搜索conf目錄下的所有配置文件且生成md5值保存在./cfmd5/cfmd5.list,并使用md5sum -c實(shí)時(shí)檢查./cfmd5/cfmd5.list中的文件md5值是否有變化,且將結(jié)果輸出到./cfmd5/check_cfmd5.log,再通過cat ./cfmd5/check_cfmd5.log進(jìn)行過濾”FAILED”并統(tǒng)計(jì),只要出現(xiàn)有”FAILED”,就認(rèn)為配置文件有發(fā)生過變化,要么是增加了,要么是現(xiàn)有的配置文件做了修改。統(tǒng)計(jì)的結(jié)果保存在md5ret變量,判斷條件就是md5ret結(jié)果大于0就觸發(fā)北方和打包壓縮配置目錄,同時(shí)master中的配置文件發(fā)生變化后,也會(huì)自動(dòng)觸發(fā)熱重啟。接著將狀態(tài)碼1寫入到./notice_slave.action文件中,如果沒有變化,將狀態(tài)碼就是0。notice_slave.action文件是存儲(chǔ)狀態(tài)變化的(在這里就干脆叫通知文件吧!)。

    關(guān)于通知文件(notice_slave.action)設(shè)計(jì)的詳細(xì)說明:對(duì)slave端來講,由slave主動(dòng)拉取這個(gè)通知文件并讀取結(jié)果,如果是1就觸發(fā)拉取master打包好的壓縮目錄并解壓,且繼續(xù)熱重啟相應(yīng)的組件,如果是0就啥也不干。

    關(guān)于參數(shù)task_wait_sec=4,任務(wù)等待時(shí)間,master目前配置的是4秒,也就是每隔4秒檢測(cè)一次。對(duì)于slave端,也有一個(gè)pull_wait_sec=2參數(shù)(目前是2秒),也就是每隔2秒拉取一次通知文件,并做判斷。這里要注意,slave的pull_wait_sec拉取時(shí)間一定要小于master的task_wait_sec時(shí)間,別問為什么,自己思考去。

    拉起配置文件變化檢查腳本

    # 拉起nohup sh ./startTarPackConf.sh >> ./logs/tar_pack.log &

    查看進(jìn)程

    [root@prosvr-master prometheus]# ps -aux | grep tarroot 2473 0.0 0.3 113284 848 pts/1 S 09:48 0:02 sh start_tar_pack_conf.sh

    拉起后會(huì)作為后臺(tái)進(jìn)程常駐,需要停止的話,查看pid,然后kill掉即可。

  • 創(chuàng)建啟動(dòng)prometheus組件,腳本startPrometheusSvr.sh
  • startPrometheusSvr.sh內(nèi)容:

    #!/bin/shnohup ./prometheus –storage.tsdb.retention.time=180d –web.enable-lifecycle –config.file=./conf/prometheus.yml –log.level=warn –log.format=json >> ./logs/prometheus_run_status.log &

    數(shù)據(jù)保留周期是180天,請(qǐng)根據(jù)實(shí)際情況調(diào)整,其他參數(shù)的意義請(qǐng)自行help

    在上面腳本的啟動(dòng)參數(shù)中,也可以通過參數(shù)–storage.tsdb.path=”data/”修改本地?cái)?shù)據(jù)存儲(chǔ)的路徑,不指定的話,時(shí)序數(shù)據(jù)默認(rèn)是在prometheus的data目錄下,如需修改數(shù)據(jù)的存儲(chǔ)路徑,建議存放在性能好(SSD、高端磁盤陣列)、容量大的目錄中。

    接著啟動(dòng)prometheus

    # 拉起[root@prosvr-master prometheus]# sh startPrometheusSvr.sh

    查看進(jìn)程,檢查是否啟動(dòng)成功

    [root@prosvr-master prometheus]# ps -aux | grep prometheusroot 1201 0.0 30.1 1100628 66840 pts/0 Sl 08:45 0:22 ./prometheus –web.enable-lifecycle –config.file=./conf/prometheus.yml –log.level=warn –log.format=json

  • 為了方便熱重啟操作,創(chuàng)建腳本hot_restart_prometheus.sh,當(dāng)修改了配置文件后,就手動(dòng)執(zhí)行這個(gè)腳本進(jìn)行熱重啟
  • #!/bin/shcurl -X POST http://127.0.0.1:9090/-/reload

    平滑重啟示例

    [root@prosvr-master prometheus]# sh hot_restart_prometheus.sh

    在日常維護(hù)中,當(dāng)配置文件發(fā)生了變更就可以利用該腳本進(jìn)行平滑重啟

  • 創(chuàng)建匯總啟動(dòng)腳本start_all.sh
  • #!/bin/shsh ./startPromconfSyncApi.shnohup sh ./startTarPackConf.sh >> ./logs/tar_pack.log &sh ./startPrometheusSvr.sh

    以后就不用一個(gè)一個(gè)腳本拉起了,直接拉起該腳本即可一并啟動(dòng)

  • 為了增加安全性,僅允許slave主機(jī)訪問配置文件拉取服務(wù)的端口(筆者這里是8000端口)
  • 在tcp協(xié)議中禁止所有的ip訪問本機(jī)的8000端口,僅允許slave主機(jī)(192.168.11.147)訪問本機(jī)的8000端口(注意按順序執(zhí)行)

    iptables -I INPUT -p tcp –dport 8000 -j DROPiptables -I INPUT -s 192.168.11.147 -p tcp –dport 8000 -j ACCEPT

    查看規(guī)則

    [root@prosvr-master prometheus]# iptables -nvLChain INPUT (policy ACCEPT 9 packets, 744 bytes) pkts bytes target prot opt in out source destination 8 552 ACCEPT tcp — * * 192.168.11.147 0.0.0.0/0 tcp dpt:8000 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000

    保存規(guī)則

    注意:使用此命令的規(guī)則位置可以是任意的,此方式保存的規(guī)則在重啟機(jī)器后無法自動(dòng)生效,需要使用命令iptables-restart恢復(fù)),筆者這里是保存在了/etc/sysconfig/my-iptable-rule-script

    # 保存[root@prosvr-master prometheus]# iptables-save > /etc/sysconfig/my-iptable-rule-script# 查看[root@prosvr-master prometheus]# cat /etc/sysconfig/my-iptable-rule-script# Generated by iptables-save v1.4.21 on Mon May 30 10:37:12 2022*filter:INPUT ACCEPT [49:4408]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [27:4840]-A INPUT -s 192.168.11.147/32 -p tcp -m tcp –dport 8000 -j ACCEPT-A INPUT -p tcp -m tcp –dport 8000 -j DROPCOMMIT# Completed on Mon May 30 10:37:12 2022

    手動(dòng)清空規(guī)則,模擬規(guī)則丟失后,從文件中加載

    # 查看[root@prosvr-master prometheus]# iptables -nvLChain INPUT (policy ACCEPT 122 packets, 12944 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp — * * 192.168.11.147 0.0.0.0/0 tcp dpt:8000 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 86 packets, 14400 bytes) pkts bytes target prot opt in out source destination [root@prosvr-master prometheus]# # 手動(dòng)清空[root@prosvr-master prometheus]# iptables -F# 清空后查看[root@prosvr-master prometheus]# iptables -nvLChain INPUT (policy ACCEPT 12 packets, 1056 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 7 packets, 1080 bytes) pkts bytes target prot opt in out source destination [root@prosvr-master prometheus]# # 使用iptables-restore命令還原iptables-save命令所備份的iptables配置[root@prosvr-master prometheus]# iptables-restore < /etc/sysconfig/my-iptable-rule-script # 還原后查看[root@prosvr-master prometheus]# iptables -nvLChain INPUT (policy ACCEPT 34 packets, 2992 bytes) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp — * * 192.168.11.147 0.0.0.0/0 tcp dpt:8000 0 0 DROP tcp — * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8000Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 18 packets, 2480 bytes) pkts bytes target prot opt in out source destination [root@prosvr-master prometheus]#

    特別說明:預(yù)防規(guī)則重啟后丟失的辦法還有一種,就是將配置規(guī)則寫入到啟動(dòng)文件中,如/etc/rc.local,筆者之前將規(guī)則保存在/etc/sysconfig/my-iptable-rule-script文件中,那么就可以將恢復(fù)的命令 iptables-restore < /etc/sysconfig/my-iptable-rule-script 寫入到/etc/rc.local中

    特別注意:如果您的環(huán)境和筆者不一樣,請(qǐng)自行更改為您自己的IP和端口即可。

  • 創(chuàng)建匯總停止腳本stop_all.sh
  • #!/bin/shps -aux | grep SimpleHTTPServer | grep -v grep | awk ‘{print $2}’ | xargs killps aux | grep startTarPackConf.sh | grep -v grep | awk ‘{print $2}’ | xargs killps -aux | grep -v grep | grep prometheus | awk ‘{print $2}’ | xargs kill

    日常維護(hù)中,如有需要停止全部服務(wù)的需求,運(yùn)行該腳本即可。


    2. slave部署配置文件拉取服務(wù)

    特別說明:在slave主機(jī)上,請(qǐng)?jiān)谝?guī)范的目錄下創(chuàng)建conf、logs、backup目錄

  • 將首次安裝完成的prometheus.yml文件移動(dòng)到當(dāng)前的conf目錄下
  • mv ./prometheus.yml ./conf/

    之所以要移動(dòng)slave本身的prometheus.yml,是因?yàn)橐獜膍aster同步過來已經(jīng)規(guī)劃好的conf配置目錄,拉取后會(huì)覆蓋slave上的conf目錄,以后都以master的配置為主

  • 準(zhǔn)備啟動(dòng)prometheus組件的腳本 腳本/usr/local/prometheus/startPrometheusSvr.sh,代碼:
  • #!/bin/shnohup ./prometheus –storage.tsdb.retention.time=180d –web.enable-lifecycle –config.file=./conf/prometheus.yml –log.level=warn –log.format=json >> ./logs/prometheus_run_status.log &

    數(shù)據(jù)保留周期是180天,請(qǐng)根據(jù)實(shí)際情況調(diào)整,其他參數(shù)的意義請(qǐng)自行help

    在上面腳本的啟動(dòng)參數(shù)中,也可以通過參數(shù)–storage.tsdb.path=”data/”修改本地?cái)?shù)據(jù)存儲(chǔ)的路徑,不指定的話,時(shí)序數(shù)據(jù)默認(rèn)是在prometheus的data目錄下,如需修改數(shù)據(jù)的存儲(chǔ)路徑,建議存放在性能好(SSD、高端磁盤陣列)、容量大的目錄中。

  • 接著啟動(dòng)prometheus,并檢查是否啟動(dòng)成功
  • [root@prosvr-slave prometheus]# sh startPrometheusSvr.sh[root@prosvr-slave prometheus]# ps -aux | grep prometheusroot 5107 3.7 16.0 768960 35456 pts/0 Sl 17:18 0:00 ./prometheus –web.enable-lifecycle –config.file=./conf/prometheus.yml –log.level=warn –log.format=jsonroot 5114 0.0 0.4 112812 976 pts/0 R+ 17:18 0:00 grep –color=auto prometheusYou have new mail in /var/spool/mail/root[root@prosvr-slave prometheus]# netstat -tulnp | grep prometheustcp6 0 0 :::9090 :::* LISTEN 5107/./prometheus [root@prosvr-slave prometheus]#

  • 準(zhǔn)備從master拉取配置文件目錄的腳本startUpdateSyncConf.sh,代碼:
  • #!/bin/shtime_log=`date “+%Y-%m-%d %H:%M:%S”`echo “${time_log} 配置更新器啟動(dòng)”pull_wait_sec=2while truedo wget http://192.168.11.146:8000/notice_slave.action -O notice_slave.action > /dev/null 2>&1 status=`cat ./notice_slave.action` if [ ${status} -eq 1 ] then time_bak=`date “+%Y%m%d%H%M%S”` time_log=`date “+%Y-%m-%d %H:%M:%S”` echo “${time_log} 從master下載配置壓縮包文件” wget http://192.168.11.146:8000/conf.tar.gz -O conf.tar.gz echo “${time_log} 備份原有的配置目錄” mv ./conf ./backup/conf_bak_${time_bak} echo “${time_log} 解壓下載后的配置壓縮包” tar -zxf conf.tar.gz echo “${time_log} 熱重啟prometheus服務(wù)” curl -X POST http://127.0.0.1:9090/-/reload fi sleep ${pull_wait_sec}done

    pull_wait_sec參數(shù)控制每隔時(shí)間工作一次,首先會(huì)從master中拉取通知文件notice_slave.action,并讀取里面的結(jié)果,如果是1,則說明master上的配置文件有變化,接著會(huì)執(zhí)行一系列操作。如果是0,則什么也不做。

  • 創(chuàng)建熱重啟的腳本hot_restart_prometheus.sh
  • #!/bin/shcurl -X POST http://127.0.0.1:9090/-/reload

    日常維護(hù)中,在slave主機(jī)上,如有必要時(shí)方便手動(dòng)執(zhí)行熱重啟

  • 創(chuàng)建匯總啟動(dòng)腳本start_all.sh
  • #!/bin/shnohup sh startUpdateSyncConf.sh > ./logs/update_sync.log &sleep 3sh ./startPrometheusSvr.sh

    日常維護(hù)中,如需一次性拉起服務(wù),執(zhí)行該腳本即可

  • 創(chuàng)建匯總停止腳本stop_all.sh
  • #!/bin/shps -aux | grep startUpdateSyncConf.sh | grep -v grep | awk ‘{print $2}’ | xargs killps -aux | grep -v grep | grep prometheus | awk ‘{print $2}’ | xargs kill

    日常維護(hù)中,如需一次性停止服務(wù),執(zhí)行該腳本即可


    四、監(jiān)控mysql案例

    prometheus如何監(jiān)控mysql?很簡(jiǎn)單,只需要在運(yùn)行mysql的主機(jī)上安裝mysqld_exporter,mysqld_exporter的用途是啥?說白了它就是用來收集mysql數(shù)據(jù)庫(kù)相關(guān)指標(biāo)的程序(官方就有,而且是go語言寫的),mysqld_exporter啟動(dòng)后默認(rèn)監(jiān)聽9104端口(當(dāng)然啟動(dòng)時(shí)可以通過相應(yīng)參數(shù)進(jìn)行更改),且它連接上數(shù)據(jù)庫(kù)采集相應(yīng)指標(biāo),并等著prometheus服務(wù)器來拉取。所以,需要在mysql中創(chuàng)建一個(gè)專門用于采集監(jiān)控指標(biāo)的數(shù)據(jù)庫(kù)賬號(hào),讓mysqld_exporter通過這個(gè)賬號(hào)去登錄數(shù)據(jù)庫(kù)采集指標(biāo),且這個(gè)賬號(hào)要有相關(guān)權(quán)限(合適的權(quán)限即可)。所以的合適,請(qǐng)自行根據(jù)實(shí)際情況決定要給什么權(quán)限,如果是生產(chǎn)環(huán)境,一般的原則是:最小原則、夠用就好。

  • mysql測(cè)試環(huán)境信息
    • 以下是筆者的mysql測(cè)試環(huán)境

    數(shù)據(jù)庫(kù)

    操作系統(tǒng)

    IP

    mysql8.0

    centos7

    192.168.11.150

    說明:本篇只講解如何使用Prometheus監(jiān)控MySQL,MySQL本身的安裝過程不在本篇范圍內(nèi),請(qǐng)自行將MySQL安裝好。

  • 下載mysqld_exporter
  • wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.14.0/mysqld_exporter-0.14.0.linux-amd64.tar.gz

  • 登錄mysql創(chuàng)建用于采集指標(biāo)數(shù)據(jù)的專有賬戶
  • # 創(chuàng)建賬號(hào)mysql> create user ‘exporter_user’@’localhost’ identified by ‘Root.123456’ with max_user_connections 3;Query OK, 0 rows affected (0.06 sec)# 授權(quán)mysql> grant process, replication client, select on *.* to ‘exporter_user’@’localhost’;Query OK, 0 rows affected (0.00 sec)# 刷新權(quán)限mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)# 查看權(quán)限mysql> show grants for exporter_user@localhostG; *************************** 1. row ***************************Grants for exporter_user@localhost: GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO `exporter_user`@`localhost`1 row in set (0.00 sec)ERROR: No query specified# 查看賬號(hào)mysql> select user,host from mysql.user;+——————+———–+| user | host |+——————+———–+| exporter | localhost | # 這個(gè),筆者曾經(jīng)創(chuàng)建過的,不管它了| exporter_user | localhost | # 這個(gè)是剛剛創(chuàng)建好的,就用這個(gè)啦!| mysql.infoschema | localhost || mysql.session | localhost || mysql.sys | localhost || root | localhost || ttr1 | localhost |+——————+———–+7 rows in set (0.00 sec)mysql>

    關(guān)于mysql的數(shù)據(jù)庫(kù)賬號(hào)權(quán)限的授權(quán)和回收的知識(shí),筆者以后會(huì)出一個(gè)專題,專門深入淺出的剖析,敬請(qǐng)大家的關(guān)注!

  • 部署mysqld_exporter
  • # 下載完成后,解壓,并移動(dòng)到規(guī)定的目錄下(目錄可自定義哈)tar -zxf mysqld_exporter-0.14.0.linux-amd64.tar.gz [root@mysql8db ~]# mkdir /usr/local/exporter/[root@mysql8db ~]# mv mysqld_exporter-0.14.0.linux-amd64 /usr/local/exporter/mysqld_exporter[root@mysql8db ~]# cd /usr/local/exporter/mysqld_exporter/[root@mysql8db mysqld_exporter]# lltotal 14824-rw-r–r– 1 3434 3434 11357 Mar 5 00:30 LICENSE-rwxr-xr-x 1 3434 3434 15163162 Mar 5 00:25 mysqld_exporter # 這個(gè)就是可執(zhí)行程序-rw-r–r– 1 3434 3434 65 Mar 5 00:30 NOTICE

  • 創(chuàng)建連接mysql的配置文件并啟動(dòng)mysqld_exporter
  • # 創(chuàng)建連接mysql的配置文件[root@mysql8db mysqld_exporter]# cat > exporter_conn_mysql.conf user=exporter_user> password=Root.123456> EOF# 查看創(chuàng)建好的配置文件[root@mysql8db mysqld_exporter]# cat exporter_conn_mysql.conf [client]user=exporter_userpassword=Root.123456[root@mysql8db mysqld_exporter]#

  • 啟動(dòng)mysqld_exporter
  • # 為了方便啟動(dòng),創(chuàng)建一個(gè)啟動(dòng)腳本[root@mysql8db mysqld_exporter]# cat > start_mysqld_exporter.sh EOF[root@mysql8db mysqld_exporter]## 查看創(chuàng)建好的啟動(dòng)腳本 [root@mysql8db mysqld_exporter]# cat start_mysqld_exporter.sh nohup ./mysqld_exporter –config.my-cnf=./exporter_conn_mysql.conf &[root@mysql8db mysqld_exporter]# # 開始啟動(dòng)[root@mysql8db mysqld_exporter]# sh start_mysqld_exporter.sh # 啟動(dòng)后查看相關(guān)端口(默認(rèn)的端口為9104)[root@mysql8db mysqld_exporter]# netstat -tulnp | grep mysqltcp6 0 0 :::33060 :::* LISTEN 1916/mysqld tcp6 0 0 :::3306 :::* LISTEN 1916/mysqld tcp6 0 0 :::9104 :::* LISTEN 2073/./mysqld_expor # 這個(gè)就是啦![root@mysql8db mysqld_exporter]# [root@mysql8db mysqld_exporter]#

    說明:咦!–config.my-cnf這個(gè)參數(shù)我咋知道的?當(dāng)然是可以使用help啦!這樣 ./mysqld_exporter –help 就可以知道有哪些選項(xiàng)啦!

    還有一個(gè)奇怪的問題,怎么只有ipv6在監(jiān)聽?沒有IPV4?其實(shí)不是啦!centos7以上,都是ipv6優(yōu)先的原則,對(duì)ipv6的支持默認(rèn)是開啟的,ipv4其實(shí)也是有在監(jiān)聽的啦!

  • 啟動(dòng)后,通過瀏覽器訪問指標(biāo)頁面
    • 暴露的HTTP服務(wù)地址(http://192.168.11.150:9104/metrics)

    看到這些指標(biāo)了嗎?Prometheus服務(wù)端會(huì)周期性的從Exporter暴露的HTTP服務(wù)地址(通常是/metrics)拉取監(jiān)控樣本數(shù)據(jù)。

    指標(biāo)內(nèi)容簡(jiǎn)單說明:

    • HELP:用于解釋當(dāng)前指標(biāo)的含義
    • TYPE:說明當(dāng)前指標(biāo)的數(shù)據(jù)類型

    比如下面的一個(gè)指標(biāo)

    # HELP mysql_up Whether the MySQL server is up. # MySQL服務(wù)器是否啟動(dòng)# TYPE mysql_up gauge # 指標(biāo)的數(shù)據(jù)類型是gauge,測(cè)量、檢測(cè)的意思,也有儀表盤的意思?mysql_up 1 # mysql_up反應(yīng)當(dāng)前的狀態(tài),當(dāng)前的值為1,說明是啟動(dòng)的,也可能為0(停止?fàn)顟B(tài))

    筆者去把Mysql給停止了,再次刷新指標(biāo)頁面,查看這個(gè)指標(biāo),發(fā)現(xiàn)確實(shí)變成了0

  • 查看采集過程輸出的相關(guān)信息
  • 剛剛是通過nohup將mysqld_exporter程序丟入到后臺(tái)啟動(dòng)的,所以相關(guān)的輸出信息默認(rèn)是會(huì)寫入nohup.out文件中

    [root@mysql8db mysqld_exporter]# tailf nohup.out ts=2022-05-21T13:40:01.735Z caller=mysqld_exporter.go:277 level=info msg=”Starting mysqld_exporter” version=”(version=0.14.0, branch=HEAD, revision=ca1b9af82a471c849c529eb8aadb1aac73e7b68c)”ts=2022-05-21T13:40:01.735Z caller=mysqld_exporter.go:278 level=info msg=”Build context” (gogo1.17.8,userroot@401d370ca42e……

    到此為止,在Mysql服務(wù)器主機(jī)上部署mysqld_exporter的任務(wù)算是大功告成。

    接下來回到Prometheus Master中繼續(xù)以下的操作

  • 在Prometheus Master中配置從mysqld_exporter收集監(jiān)控?cái)?shù)據(jù)
  • 在Master中的prometheus主配置文件中的scrape_configs配置項(xiàng)添加基于文件的自動(dòng)發(fā)現(xiàn)job

    一定要注意:只需在master上做相關(guān)配置,slave主機(jī)會(huì)定時(shí)拉取master的配置目錄和master保持同步,且slave的配置發(fā)生變更還會(huì)自動(dòng)熱重啟使其生效,也就是說slave不用你操心,你只需管理好你的master就好。

    再羅嗦一次:以下操作僅在master上操作。

    在主配置文件/usr/local/prometheus/conf/prometheus.yml中添加測(cè)試業(yè)務(wù)A的job

    說明:下面的job_name為prometheus_server是拉取prometheus本身的指標(biāo)數(shù)據(jù)(也就是監(jiān)控其本身),IP地址是指向VIP:192.168.11.203,指向VIP這是建議的做法。

    scrape_configs: – job_name: ‘prometheus_server’ static_configs: – targets: [‘192.168.11.203:9090’] – job_name: ‘測(cè)試業(yè)務(wù)A’ file_sd_configs: – files: – ‘./business/test_bus_a/job/*.yml’ refresh_interval: 1s

    參數(shù)說明:

    • ‘測(cè)試業(yè)務(wù)A’的job_name:定義自發(fā)現(xiàn)的采集任務(wù)名稱,按業(yè)務(wù)的維度進(jìn)行定義名稱,筆者這里叫“測(cè)試業(yè)務(wù)A”
    • file_sd_configs:這是基于文件的自動(dòng)發(fā)現(xiàn),即,下面這塊配置都是和file_sd_configs有關(guān)的,詳細(xì)說明如下:

    file_sd_configs: # 指定這是基于文件的自動(dòng)發(fā)現(xiàn) – files: – ‘./business/test_bus_a/job/*.yml’ # 指定自動(dòng)發(fā)現(xiàn)配置文件的路徑,這里表示在該路徑下發(fā)現(xiàn)所有.yml格式的配置文件 refresh_interval: 1s # 自動(dòng)發(fā)現(xiàn)間隔,時(shí)間默認(rèn)是5秒,筆者這里設(shè)置了1秒。

  • 在規(guī)劃好的業(yè)務(wù)目錄(business)下創(chuàng)建對(duì)應(yīng)的業(yè)務(wù)文件夾:test_bus_a,以及在業(yè)務(wù)目錄test_bus_a下面創(chuàng)建job目錄,并進(jìn)入job目錄創(chuàng)建mysql.yml(該名稱可自定義,也可叫mon_mysql.yml或者其他,主要你喜歡就好?。┰趍ysql.yml中定義拉取mysql的監(jiān)控指標(biāo)數(shù)據(jù)
    • ./conf/business/test_bus_a/job/mysql.yml的內(nèi)容如下:

    – targets: – ‘192.168.11.150:9104’ labels: ip: ‘192.168.11.150’ monitype: ‘mysql’ project: ‘測(cè)試業(yè)務(wù)A’ business: ‘測(cè)試業(yè)務(wù)A’

    參數(shù)說明:

    • targets:拉取目標(biāo),這里指向mysql服務(wù)器的IP地址,mysqld_exporter的端口是9104
    • labels:這是標(biāo)簽,標(biāo)簽的主要作用是可以通過指定的標(biāo)簽查詢指定的數(shù)據(jù)。

    標(biāo)簽的作用:Prometheus中存儲(chǔ)的數(shù)據(jù)為時(shí)間序列,是由Metric的名字和一系列的標(biāo)簽(鍵值對(duì))唯一標(biāo)識(shí)的, 不同的標(biāo)簽代表不同的時(shí)間序列,即通過指定標(biāo)簽查詢指定數(shù)據(jù)。不同的標(biāo)簽代表不同的時(shí)間序列,即通過指定標(biāo)簽查詢指定數(shù)據(jù)。指標(biāo)+標(biāo)簽實(shí)現(xiàn)了查詢條件的作用,可以指定不同的標(biāo)簽過濾不同的數(shù)據(jù)。

    在Prometheus UI中對(duì)應(yīng)的Labels信息如下圖可見:

    假設(shè)有個(gè)需求,需要知道被監(jiān)控的mysql服務(wù)器所在的機(jī)房位置,那么就可以增加一個(gè)自定義標(biāo)簽,如下:

    • ./conf/business/test_bus_a/job/mysql.yml

    – targets: – ‘192.168.11.150:9104’ labels: ip: ‘192.168.11.150’ monitype: ‘mysql’ project: ‘測(cè)試業(yè)務(wù)A’ business: ‘測(cè)試業(yè)務(wù)A’ region: ‘廣州機(jī)房’

    在Prometheus UI中可以看到:

    • 不管你是用VIP、還是master、slave的物理IP去訪問UI,結(jié)果都一樣的,不信你試試。

    自定義標(biāo)簽的主要應(yīng)用場(chǎng)景:有了這些標(biāo)簽可以針對(duì)特定的標(biāo)簽去查詢,比如筆者在上面的假設(shè)需求中,需要定義一個(gè)根據(jù)自定義標(biāo)簽region作為標(biāo)識(shí)機(jī)房位置??偠灾?,添加的標(biāo)簽越多,查詢的維度就會(huì)越細(xì)。

    在UI界面中的Graph面板中使用PromQL表達(dá)式查詢特定監(jiān)控指標(biāo)的監(jiān)控?cái)?shù)據(jù),如下查詢“mysql_up”指標(biāo),如下圖:

    PromQL是Prometheus自定義的一套強(qiáng)大的數(shù)據(jù)查詢語言,除了使用監(jiān)控指標(biāo)作為查詢關(guān)鍵字以為,還內(nèi)置了大量的函數(shù),幫助用戶進(jìn)一步對(duì)時(shí)序數(shù)據(jù)進(jìn)行處理。例如使用rate()函數(shù),可以計(jì)算在單位時(shí)間內(nèi)樣本數(shù)據(jù)的變化情況即增長(zhǎng)率,通過PromQL我們可以非常方便的對(duì)數(shù)據(jù)進(jìn)行查詢,過濾,以及聚合,計(jì)算等操作。通過這些豐富的表達(dá)式語句,監(jiān)控指標(biāo)不再是一個(gè)單獨(dú)存在的個(gè)體,而是一個(gè)個(gè)能夠表達(dá)出正式業(yè)務(wù)含義的語言。當(dāng)然,關(guān)于更多的PromQL知識(shí),以后筆者會(huì)慢慢分享,本篇的重點(diǎn)是主備架構(gòu),可別跑題了呢!


    五、監(jiān)控主機(jī)案例

    為了能夠采集到主機(jī)的運(yùn)行指標(biāo)如CPU, 內(nèi)存,磁盤等信息??梢允褂肗ode Exporter。Node Exporter同樣采用Golang編寫,并且不存在任何的第三方依賴,只需要下載,解壓即可運(yùn)行。

  • 下載node_exporter并解壓以及部署到指定目錄
  • wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gztar -zxf node_exporter-1.3.1.linux-amd64.tar.gzmv node_exporter-1.3.1.linux-amd64 /usr/local/exporter/node_exportercd /usr/local/exporter/node_exporter/

  • 可通過help查看一堆啟動(dòng)參數(shù)
  • [root@mysql8db node_exporter]# ./node_exporter –helpusage: node_exporter []Flags: -h, –help Show context-sensitive help (also try –help-long and –help-man). –collector.bcache.priorityStats Expose expensive priority stats. –collector.cpu.guest Enables metric node_cpu_guest_seconds_total –collector.cpu.info Enables metric cpu_info……

  • 啟動(dòng)node_exporter
  • # 丟入后臺(tái)運(yùn)行[root@mysql8db node_exporter]# nohup ./node_exporter &# 查看監(jiān)聽的端口[root@mysql8db node_exporter]# netstat -tulnp | grep node_exportetcp6 0 0 :::9100 :::* LISTEN 1935/./node_exporte [root@mysql8db node_exporter]# # 通過nohup丟入后臺(tái)運(yùn)行,相關(guān)的輸出會(huì)追加到nohup.out文件中,必要時(shí)可查看該文件診斷相關(guān)問題[root@mysql8db node_exporter]# tailf nohup.out ts=2022-06-04T00:45:58.822Z caller=node_exporter.go:115 level=info collector=thermal_zonets=2022-06-04T00:45:58.822Z caller=node_exporter.go:115 level=info collector=timets=2022-06-04T00:45:58.822Z caller=node_exporter.go:115 level=info collector=timexts=2022-06-04T00:45:58.822Z caller=node_exporter.go:115 level=info collector=udp_queues……

    筆者沒啥特殊的需求,所以無需額外在給定啟動(dòng)參數(shù),直接丟入后臺(tái)運(yùn)行即可,默認(rèn)的監(jiān)聽端口是9100

  • 通過瀏覽器查看node_exporter暴露的指標(biāo)
  • 在prometheus的master服務(wù)器中配置從node_exporter收集監(jiān)控?cái)?shù)據(jù)
  • 假設(shè)筆者的這臺(tái)主機(jī)是測(cè)試業(yè)務(wù)b(test_bus_b)的一臺(tái)主機(jī),請(qǐng)按照之前的業(yè)務(wù)目錄規(guī)范,在規(guī)范的業(yè)務(wù)目錄(conf/business)下創(chuàng)建業(yè)務(wù)文件夾test_bus_b。

    再次羅嗦一次:僅需在master上做配置即可,slave會(huì)定時(shí)拉取masetr的配置目錄,不要去管slave,OK?一個(gè)大男人那么羅嗦,真的很惹人討厭?。?/p>

    在主配置文件中添加業(yè)務(wù)B的job

    • 主配置文件:/usr/local/prometheus/conf/prometheus.yml

    scrape_configs: – job_name: ‘prometheus_server’ static_configs: – targets: [‘192.168.11.203:9090’] – job_name: ‘測(cè)試業(yè)務(wù)A’ file_sd_configs: – files: – ‘./business/test_bus_a/job/*.yml’ refresh_interval: 1s – job_name: ‘測(cè)試業(yè)務(wù)B’ # 這是新增加的測(cè)試業(yè)務(wù)B file_sd_configs: – files: – ‘./business/test_bus_b/job/*.yml’ refresh_interval: 1s

    在規(guī)范的業(yè)務(wù)目錄(conf/business)下創(chuàng)建業(yè)務(wù)文件夾test_bus_b,然后創(chuàng)建host.yml,增加targets(拉取目標(biāo))的配置

    [root@prosvr-master business]# cd /usr/local/prometheus/conf/business/[root@prosvr-master business]# mkdir test_bus_b[root@prosvr-master business]# cd test_bus_b/[root@prosvr-master business]# mkdir job[root@prosvr-master business]# cd job/[root@prosvr-master job]# cat host.yml – targets: – ‘192.168.11.150:9100’ labels: ip: ‘192.168.11.150’ monitype: ‘linux-centos7’ project: ‘測(cè)試業(yè)務(wù)B’ business: ‘測(cè)試業(yè)務(wù)B’ region: ‘深圳機(jī)房’

    在Prometheus UI中查看新增的測(cè)試業(yè)務(wù)B

    • 不管你是用VIP、還是master、slave的物理IP去訪問UI,結(jié)果都一樣的,不信你試試。

    非常不錯(cuò),只要檢測(cè)到配置文件發(fā)生變化,master會(huì)自動(dòng)熱重啟,slave也會(huì)自動(dòng)拉取配置目錄然后熱重啟,非常的省心、省力。自我感覺這個(gè)DIY的主備方案幾乎接近完美,雖然沒有用很高大上的語言、工具去實(shí)現(xiàn),但筆者的這個(gè)思路自我感覺是非常的不錯(cuò),這難道就是傳說中的自我感覺良好?當(dāng)然,筆者以后會(huì)通過Go或者Python打造一個(gè)管理Promtheus主備的工具,且是帶UI的管理工具,敬請(qǐng)期待推出!我不造車,我只造零件。


    六、AlertManager警報(bào)組件配置

    說明:基于二進(jìn)制包的alertmanager組件請(qǐng)自行在master和slave中安裝部署到規(guī)范的目錄,之后繼續(xù)下面的步驟。

  • 移動(dòng)master和slave上的警報(bào)組件的主配置文件alertmanager.yml
    • 在master和slave服務(wù)器上,alertmanager組件的二進(jìn)制包解壓到規(guī)范的目錄后,將警報(bào)的主配置文件“alertmanager.yml”移動(dòng)到prometheus組件的conf目錄下

    # 移動(dòng)后(也就是用mv命令移動(dòng)),查看如下:[root@prosvr-master conf]# pwd/usr/local/prometheus/conf[root@prosvr-master conf]# lltotal 12-rw-r–r– 1 3434 3434 348 Jun 4 13:26 alertmanager.yml # 警報(bào)組件的主配置文件已經(jīng)也在prometheus組件下的conf目錄drwxr-xr-x 4 root root 42 Jun 4 09:18 business-rw-r–r– 1 3434 3434 1033 Jun 4 12:27 prometheus.yml[root@prosvr-master conf]#

    特別注意:上述操作,在master和slave上都要操作,且在slave服務(wù)器移動(dòng)alertmanager.yml配置文件后,往后就可以不用去管slave上的alertmanager.yml配置文件了,主要的配置變更都在master上進(jìn)行就好,如果有變更,slave會(huì)自動(dòng)拉取配置目錄。通常警報(bào)組件的alertmanager.yml配置文件一旦配置好后,改動(dòng)的頻率比較少。

    特別說明:之所以這么設(shè)計(jì),有兩個(gè)好處:1)配置的變更都在同一個(gè)目錄下進(jìn)行;2)利用了現(xiàn)有master和slave的配置目錄同步能力

  • 在master上配置警報(bào)的主配置文件/usr/local/prometheus/conf/alertmanager.yml
    • 在警報(bào)組件中配置告警消息發(fā)往的接口地址, 讓其可以調(diào)用接口,配置方式很簡(jiǎn)單,只需要指定一下接口地址即可

    global: resolve_timeout: 5mroute: group_by: […] group_wait: 1s group_interval: 1s repeat_interval: 1000d receiver: ‘web.hook’receivers:- name: ‘web.hook’ webhook_configs: – url: ‘http://127.0.0.1:5001/webhook’ send_resolved: true

    上述配置中,主要包含兩大部分,路由(route)和接收器(receivers),所有的告警信息都會(huì)從配置中的頂級(jí)路由(route)進(jìn)入路由樹,根據(jù)路由規(guī)則將告警信息發(fā)送給相應(yīng)的接收器。本篇主要是配置接收器,使用webhook的方式,假設(shè)是將告警消息推送到第三方平臺(tái)。當(dāng)然,在本篇僅為示例,打印出來而已。

  • 在master和slave上創(chuàng)建Alertmanager組件啟動(dòng)腳本
    • 注意:該步驟一定要進(jìn)入到/usr/local/alertmanager/目錄下進(jìn)行操作
    • 創(chuàng)建腳本,名稱:startAlertManagerSvr.sh

    #!/bin/shnohup ./alertmanager –config.file=/usr/local/prometheus/conf/alertmanager.yml >> ./logs/alert.log &[root@prosvr-master alertmanager]#

    注意:startAlertManagerSvr.sh腳本在masetr和slave中都要?jiǎng)?chuàng)建,且–config.file使用絕對(duì)路徑指向alertmanager.yml

    通過該腳本拉起alertmanager組件

    sh startAlertManagerSvr.sh

    注意:master和slave都要拉起

    啟動(dòng)后,通過VIP或master和slave的物理IP都可以訪問到警報(bào)的頁面,筆者這里是使用VIP進(jìn)行訪問,如下圖:

  • 關(guān)聯(lián)Prometheus與Alertmanager
  • 注意:僅在master上配置即可,因?yàn)閟lave會(huì)從master上拉取

    • /usr/local/prometheus/conf/prometheus.yml

    alerting: alertmanagers: – static_configs: – targets: – 192.168.11.203:9093

    筆者這里通過VIP跟Alertmanager組件通信,當(dāng)prometheus中的警報(bào)規(guī)則觸發(fā)了告警后,告警消息就會(huì)發(fā)送到警報(bào)組件監(jiān)聽的9093端口,由alertmanager組件進(jìn)行處理

  • 配置警報(bào)規(guī)則文件的自動(dòng)發(fā)現(xiàn)
  • 注意:僅在master上配置即可

    • /usr/local/prometheus/conf/prometheus.yml

    rule_files: – “./business/test_bus_a/rule/*.rules” – “./business/test_bus_b/rule/*.rules”

  • 配置mysql的警報(bào)規(guī)則,當(dāng)mysql掛掉后,使其觸發(fā)警報(bào)
  • 注意:僅在master上配置即可

    • /usr/local/prometheus/conf/business/test_bus_a/rule/mysql.rules

    groups: – name: mysql-alert rules: – alert: “MysqlDown” expr: mysql_up{job=”測(cè)試業(yè)務(wù)A”}==0 for: 1m labels: annotations: summary: “MySQL數(shù)據(jù)庫(kù)服務(wù):{{ $labels.ip }}發(fā)生停止告警” description: “測(cè)試業(yè)務(wù)A的環(huán)境MySQL數(shù)據(jù)庫(kù)服務(wù):{{ $labels.ip }}已停止,當(dāng)前UP狀態(tài)值為:{{ $value }},已觸發(fā)告警條件:mysql_up = 0,持續(xù)時(shí)間:1m。” alertLevel: 5

    上面的案例很簡(jiǎn)單,expr是表達(dá)式,該表達(dá)式是說:如果mysql_up指標(biāo)的值等于0那么就觸發(fā)該警報(bào)

    可以通過promtool工具檢查警報(bào)規(guī)則配置文件是否有誤

    [root@prosvr-master prometheus]# ./promtool check rules ./conf/business/test_bus_a/rule/mysql.rules Checking ./conf/business/test_bus_a/rule/mysql.rules SUCCESS: 1 rules found

    配置文件發(fā)生了變更后,master會(huì)自動(dòng)熱重啟,slave會(huì)自動(dòng)拉取配置并熱重啟,直接在UI界面上可以查看到該規(guī)則

  • 筆者在這里假設(shè)用python編寫了一個(gè)最簡(jiǎn)單的webhook接口,讓其可以接收來自alertmanager的警報(bào)消息,然后打印出來
  • 特別說明:只需在master上編寫的webhook接口腳本,并且也放在規(guī)范的conf目錄下:/usr/local/prometheus/conf/webhook.py,該API腳本會(huì)被slave拉取到

    • webhook.py簡(jiǎn)單的API代碼如下:

    import jsonfrom flask import Flask, requestapp = Flask(__name__)@app.route(‘/webhook’, methods=[‘POST’])def webhook(): data = json.loads(request.data) print(data)if __name__ == ‘__main__’: app.run(‘0.0.0.0’, 5001)

    特別說明1:此示例API只是演示使用,請(qǐng)根據(jù)實(shí)際情況編寫相關(guān)代碼,本實(shí)例僅僅只是打印出來,并沒有將告警推送到其他平臺(tái),如釘釘、郵件、或其他告警收斂平臺(tái)。

    特別說明2:如果您也想在您的測(cè)試環(huán)境將筆者的這個(gè)webhook.py跑起來,請(qǐng)自行安裝python的flask庫(kù),筆者不在本篇講解python的相關(guān)知識(shí)。python編程筆者后續(xù)會(huì)專門抽時(shí)間作為專題給大家分享,敬請(qǐng)您的關(guān)注。

  • 創(chuàng)建webhook API的啟動(dòng)腳本
  • 說明:在master和slave都要?jiǎng)?chuàng)建startWebHook.sh腳本

    #!/bin/shnohup python ./conf/webhook.py >> ./logs/webhook.log &

  • 僅在master上啟動(dòng)webhook API
  • [root@prosvr-master prometheus]# sh startWebHook.sh

    特別注意:千萬不要在slave上啟動(dòng)webhook API,具體原因在本篇的最前面已經(jīng)有過解釋(避免告警重復(fù)推送),只有當(dāng)master不可用了,slave才拉起webhook API腳本進(jìn)行承擔(dān)告警推送的任務(wù)。

  • 模擬mysql故障,驗(yàn)證告警是否可以正常觸發(fā),驗(yàn)證由webhook是否可以正常接收
    • 在master上用tailf命令實(shí)時(shí)監(jiān)測(cè)./logs/webhook.log

    [root@prosvr-master prometheus]# tailf ./logs/webhook.log * Serving Flask app “webhook” (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)

    登錄Mysql主機(jī),停掉mysql

    [root@mysql8db ~]# cat stop_mysql.sh #!/bin/bash/usr/local/mysql8/bin/mysqladmin -S /usr/local/mysql8/mysql.sock -uroot -pRoot.123456 shutdown[root@mysql8db ~]# [root@mysql8db ~]# sh stop_mysql.sh mysqladmin: [Warning] Using a password on the command line interface can be insecure.[root@mysql8db ~]#

    沒過多久,webhook api就接收到了告警消息

    同樣,在Alertmanager告警頁面中,也能看到告警消息

    寫在最后

    到目前為止,該DIY的prometheus主備方案的全程搭建過程就已經(jīng)完結(jié)了,期間涉及到很多知識(shí)點(diǎn)都還沒有去深入的剖析,比如:PromQL,Metric類型,告警的分組、抑制、靜默等等知識(shí)點(diǎn)。本篇的核心主題是把這個(gè)DIY的方案給搞起來,后續(xù)筆者會(huì)逐一分享更多關(guān)于prometheus的技能點(diǎn),敬請(qǐng)大家的關(guān)注。謝謝!感謝您的關(guān)注,望多多轉(zhuǎn)發(fā)、點(diǎn)贊。謝謝!

    重磅!DIY的Prometheus主備方案,全網(wǎng)唯一。生產(chǎn)未上,測(cè)試先行。

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年6月13日 18:04
    下一篇 2022年6月13日 18:04

    相關(guān)推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息