1. 程式人生 > 其它 >Prometheus+grafana部署、使用,資料來源匯入展示

Prometheus+grafana部署、使用,資料來源匯入展示

一、Prometheus是什麼?

監控:資料採集,資料儲存,資料分析,資料展示,資料告警。

Prometheus本質上是一個度量資料的收集和分析工具,包含3個核心元件:

● 時間序列資料庫,用於儲存所有度量資料。

● 資料收集器,負責從外部來源拉取指標並將其推入資料庫。

● Web伺服器,為配置和查詢儲存的資料提供簡單的Web介面。

Prometheus的優勢:

配置靈活;

監控多樣性;

對雲原生的支援是所有監控方案裡最好的;

部署方便,不依賴外部元件,能適用企業各種場景;

採集精度高,可以精確到 1~5 秒;

支援很多實用的計算函式;

可以快速整合社群已經成熟 exporter;

能跟 Grafana 很好的結合,繪製各種高大上的圖;

可根據自身業務快速自定義 metrics;

Prometheus的架構:

  說明:

PromSQL,是用於從Prometheus中檢索指標的查詢語言。

AlertManager(告警管理),允許我們根據Prometheus取樣的指標定義告警(比如記憶體/ CPU使用率過高或請求時延達到峰值)。

Pushgateway(推送閘道器),允許應用和服務將指標推送到Prometheus,而非標準的由Prometheus主動拉取。

Service discocery(服務發現),Prometheus在一開始就被設計為只需極少配置即可完成初始安裝,以及適於在諸如Kubernetes之類的動態環境中執行。因此它可以對正在執行的服務進行自動發現,並嘗試對其應監視的內容作出最佳的猜測。

  

二、prometheus安裝

官方文件:https://prometheus.io/docs/   , https://grafana.com/docs/grafana/

下載地址:https://prometheus.io/download/   , https://grafana.com/grafana/download

實驗環境準備:

Prometheus伺服器

10.26.36.xx1

被監控端伺服器(node_exporter)

10.26.36.xx2

Grafana 伺服器

10.26.36.xx3

(Prometheus+Grafana 可部署在同一臺機器上)

同步時間(時間同步一定要確認一下)

yum install -y  ntpdate && ntpdate time.windows.com

關閉防火牆,SELinux

# systemctl stop firewalld && systemctl disable firewalld

# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && setenforce 0

https://prometheus.io/download/下載相應版本,官網提供的是二進位制版,解壓就能用,不需要編譯。

上傳prometheus包,並解壓

tar -zxvf prometheus-2.35.0.linux-amd64.tar.gz

mv prometheus-2.35.0.linux-amd64 /usr/local/prometheus

cd /usr/local/prometheus/

#檢視幫助

./prometheus -h

#啟動

./prometheus &

#也可指定配置檔案啟動

/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

#檢視相關程序及埠

ps -ef |grep prometheus

ss -anltp |grep 9090

netstat -nltp |grep prometheus

瀏覽器訪問驗證Prometheus,預設埠9090,直接輸入伺服器IP:9090即可。

檢視監控目標,Status-->Targets,預設只監控本機。

點選開啟http://localhost:9090/metrics可看到所有監控詳細資訊。(注意,localhost要改為實際IP)。

  

比如我想監控一個資訊,例如監控:process_cpu_seconds_total,可通過Table 或Graph來直觀的檢視相關資訊,在監控頁底部也有顯示被監控的目標。
process_cpu_seconds_total{instance="localhost:9090", job="prometheus"}

  

三、監控遠端主機

監控遠端主機需要下載並安裝node_exporter元件服務,安裝也特別簡單,將node_exporter元件安裝包上傳至要被監控的伺服器上,解壓后里面就一個啟動程式node_exporter,可以直接啟動,node_exporter預設埠9100。

tar -zxvf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/

cd /usr/local/

mv node_exporter-1.3.1.linux-amd64/  node_exporter

#啟動

nohup /usr/local/node_exporter/node_exporter &

#檢視程序及埠

ps -ef |grep node_exporter

netstat -nltp |grep 9100

通過瀏覽器訪問 http://被監控端IP:9100/metrics 就可以檢視到 node_exporter在被監控端採集的監控資料資訊。

回到Prometheus伺服器的配置檔案裡新增被監控機器的配置段。在主配置檔案最後加上下面三行。

vim /usr/local/prometheus/prometheus.yml

- job_name: 'node1'       # 取一個job名稱來代表被監控的機器

static_configs:

- targets: ['10.26.36.xx2:9100']       # 這裡改成被監控機器的IP,後面埠接9100

改完配置檔案並儲存後,重啟prometheus服務。

ps -ef |grep prometheus

kill -9 <程序ID>

nohup /usr/local/prometheus/prometheus &

#啟動方式還可以追加指定預設配置檔案

/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

回到prometheus Web管理介面 --》點Status --》點Targets --》可以看到多了一臺監 控目標。

  

四、監控MySQL資料庫

監控mysql資料庫需要下載並安裝mysqld_exporter元件服務,安裝也特別簡單,將mysqld_exporter元件安裝包上傳至要被監控的mysql伺服器上,解壓后里面就一個啟動程式mysqld_exporter,這裡先不要啟動,mysqld_exporter預設埠9104。

監控mysql的前提是你必須已經有現成的或者已經安裝好了mysql服務。

Mysql安裝可參考:https://www.cnblogs.com/panw/p/16297877.html

#解壓mysqld_exporter

tar -zxvf mysqld_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/

cd /usr/local/

mv mysqld_exporter-0.14.0.linux-amd64/ mysqld_exporter

注意:這裡還不能直接啟動mysqld_exporter,因為是要監控mysql資料庫,所以需要先登入mysql,建立一個能夠讓mysqld_exporter元件可訪問的賬戶密碼來採集資料。Prometheus再去拉取mysqld_exporter的資料進行監控。

#登入mysql,也可設定免密登入

mysql -u root -p

#建立一個可查詢、複製許可權的mysqld_exporter使用者,去使用mysql本機的localhost連線訪問採集資料

grant select,replication client,process on *.* to "mysqld_monitor"@"localhost" identified by "123456";

flush privileges;

建立好可訪問mysql許可權的使用者後(這裡使用者為mysqld_monitor),還需要再建立一個讓mysqld_exporter元件去讀取mysql服務採集資料的配置檔案,這裡建立一個隱藏的 .my.cnf檔案,檔案中指定客戶端的使用者和密碼。

cat > /usr/local/mysqld_exporter/.my.cnf << EOF

[client]

user=mysqld_monitor

password=123456

EOF

到這裡我們就可以去啟動mysqld_exporter元件服務了,要注意啟動時指定上我們剛才建立的.my.cnf配置檔案。

#啟動

nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &

#檢視程序及埠

ps -ef |grep mysqld_exporter

ss -anltp |grep mysqld_exporter

netstat -nltp |grep 9104

回到Prometheus伺服器的配置檔案裡新增被監控機器的配置段。在主配置檔案最後加上下面三行。

vim /usr/local/prometheus/prometheus.yml

- job_name: 'mysql'       # 取一個job名稱來代表被監控的目標

static_configs:

- targets: ['10.26.36.xx2:9104']       # 這裡改成被監控mysql的IP,後面埠接9104

改完配置檔案並儲存後,重啟prometheus服務。

ps -ef |grep prometheus

kill -9 <程序ID>

#啟動方式追加指定預設配置檔案

/usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

回到prometheus Web管理介面 --》點Status --》點Targets --》可以看到多了一臺mysql57的監控目標。

  這個時候我們就可以來監控mysql的相關資訊了,比如我想監控一個mysql_global_status_aborted_clients,可通過Table 或Graph來直觀的檢視相關資訊,在監控頁底部也有顯示被監控的目標。

mysql_global_status_aborted_clients{instance="10.26.36.xx2:9104", job="mysql57"}

模擬測試把mysql服務停掉,再看prometheus監控狀態。

這裡測試發現,單獨停掉mysql服務,prometheus監控並不會發出告警,而只有停掉mysqld_exporter元件服務,監控才會發起告警,並沒有達到我監控mysql的實際效果,有待考究....

  

經過多番測試發現,單獨停掉mysql服務並不會發出告警,而是無法再去採集mysql相關資料。

當再次啟動mysql服務後,資料採集恢復正常。

 五、prometheus配置引數詳解

 待補充...

 六、Grafana是什麼?

Grafana 是一款開源視覺化和分析軟體,它允許你查詢、視覺化、提醒和探索您的指標,無論這些指標儲存在哪裡。Grafana 支援數十種資料庫,我們可以建立一個儀表盤來視覺化全部。

Grafana 還提供報警,直觀地定義閥值,並通過 Slack、 PagerDuty 和其他平臺獲得通知。Grafana 還提供了多種選項來檢視我們的資料,從熱力圖到直方圖,從圖形到地理地圖。 Grafana 有大量的視覺化選項可以幫助我們更好地理解資料。

七、Grafana 安裝

https://grafana.com/grafana/download下載相應版本,官網提供的是二進位制版,解壓就能用,不需要編譯。

上傳grafana包,並解壓

tar -zxvf grafana-enterprise-8.3.1.linux-amd64.tar.gz -C /usr/local/

mv  /usr/local/grafana-8.3.1/   /usr/local/grafana

cd /usr/local/grafana/

#檢視幫助

./bin/grafana-cli -h

#啟動

nohup /usr/local/grafana/bin/grafana-server &

#檢視程序及埠

ps -ef |grep grafana-server

ss -anltp |grep 3000

netstat -nltp |grep 3000

通過執行上述命令即可完成安裝,Grafana 執行在埠 3000。預設的使用者名稱和密碼均為“admin” 

瀏覽器訪問驗證grafana,預設埠3000,直接輸入伺服器IP:3000即可。首次登入要求強制修改密碼。

   

八、Grafana使用,模板Import

當我們進入儀表板,我們需要新增一個數據源,在我們的例子中是 Prometheus。我們只需要提供 Prometheus URL 並點選儲存和測試按鈕。如果我們看到一個成功的提示框,說明資料來源正在工作,那麼我們就可以開始了。

1)首先在左邊選單找到‘’setting設定‘’按鈕 →‘’Data sources’點選進去。

 2)然後在展示介面點選‘’Add data source‘’按鈕,進入新增資料來源的頁面。

  

3)在資料來源新增的介面選擇你的資料來源型別,這裡用的是prometheus資料來源,點選“Select”新增。

  

4)設定資料來源中的一些引數及資料來源服務地址,主要是設定url,其他可以預設。

  

5)設定好後點擊“save & test”儲存即可。提示data source is working,就代表新增成功了。

  

6)然後點選左側的加號“+”符號,然後在裡面點選“Import”匯入按鈕。

   

7)輸入模版的ID,可以去官網找你喜歡的模版,然後複製code在這裡新增即可。

我們先去官網選取我們需要的模板

注:Dashbod可以根據自定義面板結構,但是官方有很多大佬提供的非常優質監控模板,真香!所以我們這裡直接採用Import模板。也可以Download JSON匯入模板。

官方模板地址:https://grafana.com/grafana/dashboards

選取我們需要的模板,這裡使用的是 1860 和 893,這些是我們匯入儀表板的唯一ID。

  

8)模板匯入後,會進行資料來源的選擇,這裡選擇prometheus,然後點選Import按鈕就成功匯入了。

  

9)最終的效果展示