prometheus +pushgatewa +grafana的安裝部署與測試

prometheus搭建教程

主要概念

prometheus 主要功能是負責數(shù)據(jù)的手機存儲, 手機的來源是各種exporter。 比如mysql 有mysql exporter 叠荠,服務器性能指標的exporter 等等。

因此為了能夠監(jiān)控到某些東西,如主機的CPU 使用率,我們需要使用到 Exporter曾沈。Prometheus 周期性的從 Exporter 暴露的HTTP 服務地址(通常是/metrics)拉取監(jiān)控樣本數(shù)據(jù)。

prometheus安裝部署

訪問官網(wǎng)

https://prometheus.io/download/

選擇linux版本下載

https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz

上傳到服務器

解壓

修改配置文件

prometheus.yml文件

my global config
global: # global是一些常規(guī)的全局配置鸥昏,這里只列出了兩個參數(shù):
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. #每15s采集一次數(shù)據(jù)
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.  #每15s做一次告警檢測
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration 
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093


# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files: # rule_files指定加載的告警規(guī)則文件塞俱,告警規(guī)則放到下面來介紹
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus" # 
這是prometheus本機的一個監(jiān)控節(jié)點,可以繼續(xù)擴展加入其它需要被監(jiān)控的節(jié)點吏垮,例如:

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    #可以看到targets可以并列寫入多個節(jié)點障涯,用逗號隔開,機器名+端口號膳汪,端口號主要是exporters的端口唯蝶,在這里9100其實是node_exporter的默認端口。配置完成后遗嗽,prometheus就可以通過配置文件識別監(jiān)控的節(jié)點粘我,持續(xù)開始采集數(shù)據(jù),prometheus基礎(chǔ)配置也就搭建好了痹换。

    static_configs:
      - targets: ["localhost:9090"]  # 啟動的端口

啟動命令

nohup ./prometheus --config.file=prometheus.yml > ./prometheus.log 2>&1 &

exporter 安裝部署

下載地址

https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz

nohup ./node_exporter > node_exporter.log 2>&1 &

ps -ef |grep node_exporter

檢查 是否正常啟動

打開網(wǎng)頁 http://10.50.51.30:9100/metrics

配置node_exporter的自啟動

vi /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
ExecStart= /usr/local/node_exporter-1.4.0/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

grafana安裝部署

grafana官方下載地址:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.3.6.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-9.3.6.linux-amd64.tar.gz

參考教程

https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

啟動命令

nohup ./grafana-server &

需要配置郵箱信息

在grafana目錄下創(chuàng)建目錄config征字,在里面創(chuàng)建文件grafana.ini

#################################### SMTP / Emailing ##########################
# 配置郵件服務器
[smtp]
enabled = true
# 發(fā)件服務器
host = smtp.qq.com:465
# smtp賬號
user = 2469278741@qq.com
# smtp 授權(quán)碼
password = 123456
# 發(fā)信郵箱
from_address = 2469278741@qq.com
# 發(fā)信人
from_name = zhiweiliao

需要配置數(shù)據(jù)源文件 conf/ provisioning /datasource.yml

# config file version
apiVersion: 1

deleteDatasources:  #如果之前存在name為Prometheus,orgId為1的數(shù)據(jù)源先刪除
- name: Prometheus
  orgId: 1

datasources:  #配置Prometheus的數(shù)據(jù)源
- name: Prometheus
  type: prometheus
  access: proxy
  orgId: 1
  url: http://prometheus:9090  #在相同的docker compose下晴音,可以直接用prometheus服務名直接訪問
  basicAuth: false
  isDefault: true
  version: 1
  editable: true

打開頁面

http://10.50.51.30:3000/

跳過用戶名密碼訪問

添加prometheus數(shù)據(jù)源

點擊右側(cè)小齒輪圖標 ==》add data source

選擇prometheus 填入url http://localhost:9090 點擊save&test成功

測試查詢

點擊explorer 小圖標

選擇頂部 explore 右邊的 下拉框里的 prometheus

metric里選擇 go_gc_duration_seconds

label filters instance

localhost:9090 點擊左上角的runQuery 就有圖表數(shù)據(jù)出來了

pushgateway安裝部署

有些指標是能通過拉取來實現(xiàn)的,但是有些數(shù)據(jù)是事件觸發(fā)的缔杉,或者我們想推送到prometheus怎么辦這個時候就需要pushgateway了锤躁。

下載地址:https://github.com/prometheus/pushgateway/releases/download/v1.5.1/pushgateway-1.5.1.linux-amd64.tar.gz

啟動命令 nohup ./pushgateway &

查看端口

netstat -apn | grep 9091

查看pushgateway頁面

打開pushgateway的web頁面,http://10.50.51.30:9091或详,發(fā)現(xiàn)Metrics欄沒有任何數(shù)據(jù)系羞。因為此時還沒有客戶端推送數(shù)據(jù)給pushgateway

修改 prometheus server 配置文件霸琴,定義一個job

在prometheus server的prometheus.yml文件中定義個job椒振,然后tagets指向pushgateway所在的ip和9091端口:

[root@ip-10-50-51-30 prometheus-2.42.0.linux-amd64]# ps -ef|grep prometheus
root     25157 22948  0 Feb20 pts/0    00:02:01 ./prometheus --config.file=prometheus.yml
root     25310 22948  2 14:35 pts/0    00:00:00 grep --color=auto prometheus
[root@ip-10-50-51-30 prometheus-2.42.0.linux-amd64]# kill -9 25157
[root@ip-10-50-51-30 prometheus-2.42.0.linux-amd64]# nohup ./prometheus --config.file=promeths.yml > ./prometheus.log 2>&1 & 
ps -ef|grep prometheus

編輯采集腳本采集主機數(shù)據(jù),然后推送給pushgateway

vim pushgateway.sh

#!/bin/bash

instance_name=`hostname -f | cut -d'.' -f1` 截取主機名



vim pushgateway.sh                          #編寫pushgateway腳本采集數(shù)據(jù)
#!/bin/bash
instance_name=`hostname -f | cut -d'.' -f1` #截取主機名
if [ $instance_name == "localhost" ];then
   echo "Must FQDN hostname"                #要求主機名不能是localhost梧乘,不要主機名區(qū)別不了
   exit 1
fi
label="count_netstat_wait_connections"                              #定義一個key
count_netstat_wait_connections=`netstat -an| grep -i wait| wc -l`   #定義values

#推送數(shù)據(jù)給pushgateway
echo "$label $count_netstat_wait_connections" | curl --data-binary @- http://10.50.51.30:9091/metrics/job/${instance_name}

echo "labelcount_netstat_wait_connections" | curl --data-binary @- http://10.50.51.30:9091/metrics/job/${instance_name}

然后給腳本授權(quán)執(zhí)行

再打開prometheus或者grafana進行查看就可以了

http://10.50.51.30:9090/graph?

count_netstat_wait_connections

換成用java 進行推送

java 推送pushgateway

方案1 推送到gateway

推送的話 數(shù)據(jù)量太大了 澎迎,其實是http請求庐杨,每次都以http請求進行發(fā)送 物理機的還好,

用戶的數(shù)據(jù)量太大了夹供。 目前來看 用戶的數(shù)據(jù)是grpc實時上報的灵份,udp是定時上報的。

方案2 寫到redis中

方案3 寫到本地日志中

方案4 prometheus拉取的時候取消費kafka

取了大量的時候 返回

邊緣節(jié)點有prometheus 為什么么要集中到中心節(jié)點 總結(jié)領(lǐng)導sb

和同時討論 prometheus 是適合存儲監(jiān)控指標哮洽,不適合記錄每一個記錄填渠, 他時候定時的記錄監(jiān)控目標的瞬間狀態(tài),但是 你要讓他存儲完整的記錄 鸟辅,他有translog 嗎氛什,他的存儲是通過拉取的方式 就不適合當做數(shù)據(jù)庫取用!7肆埂G姑肌!H髯骸9迕铡!树绩!

---------------------------------------------------完結(jié)撒花-------------------------------------------

docker部署

待續(xù)

參考

官網(wǎng): https://prometheus.io/download/

csdn: https://blog.csdn.net/weixin_44352521/article/details/127947313

         https://blog.csdn.net/MssGuo/article/details/127599745

java推送 https://blog.csdn.net/qq_21389711/article/details/125183313

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末萨脑,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子饺饭,更是在濱河造成了極大的恐慌渤早,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瘫俊,死亡現(xiàn)場離奇詭異鹊杖,居然都是意外死亡,警方通過查閱死者的電腦和手機扛芽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門骂蓖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人川尖,你說我怎么就攤上這事登下。” “怎么了叮喳?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵被芳,是天一觀的道長。 經(jīng)常有香客問我馍悟,道長畔濒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任锣咒,我火速辦了婚禮侵状,結(jié)果婚禮上赞弥,老公的妹妹穿的比我還像新娘。我一直安慰自己壹将,他們只是感情好嗤攻,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著诽俯,像睡著了一般妇菱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上暴区,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天闯团,我揣著相機與錄音,去河邊找鬼仙粱。 笑死房交,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的伐割。 我是一名探鬼主播候味,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼隔心!你這毒婦竟也來了白群?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤硬霍,失蹤者是張志新(化名)和其女友劉穎帜慢,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唯卖,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡粱玲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了拜轨。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抽减。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖橄碾,靈堂內(nèi)的尸體忽然破棺而出卵沉,到底是詐尸還是另有隱情,我是刑警寧澤堪嫂,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布偎箫,位于F島的核電站木柬,受9級特大地震影響皆串,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜眉枕,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一恶复、第九天 我趴在偏房一處隱蔽的房頂上張望怜森。 院中可真熱鬧,春花似錦谤牡、人聲如沸副硅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恐疲。三九已至,卻和暖如春套么,著一層夾襖步出監(jiān)牢的瞬間培己,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工胚泌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留省咨,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓玷室,卻偏偏與公主長得像零蓉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子穷缤,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容