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版本下載
上傳到服務器
解壓
修改配置文件
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 安裝部署
下載地址
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了锤躁。
啟動命令 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 "count_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