Prometheus 監控
Prometheus 監控系統的架構包括以下組件:
Prometheus Server:
????????Prometheus 服務器是監控系統的核心組件,負責收集、存儲和處理指標數據。它定期從各種數據源(如 Exporter、Agent 等)拉取指標數據,并將其存儲在本地的時序數據庫中。Prometheus 服務器還提供了強大的查詢語言(PromQL),用于對數據進行實時查詢和分析。
Exporter:
????????Exporter 是用于從不同數據源中收集指標數據的組件。Prometheus 社區提供了許多官方 Exporter,如 Node Exporter(用于收集主機級別的系統指標)、Blackbox Exporter(用于進行健康檢查)、MySQL Exporter(用于監控 MySQL 數據庫)等。此外,還有許多第三方 Exporter 可用于監控各種不同類型的應用程序和服務。
Push Gateway:
????????Push Gateway 是一個中間件組件,用于接收短暫性作業(如批處理任務、臨時服務等)生成的指標數據,并將其暫時存儲在內存中,以便被 Prometheus 服務器拉取。這使得 Prometheus 能夠監控短暫性作業,而無需這些作業一直在線。
Alertmanager:
????????Alertmanager 是用于處理和發送警報的組件。它與 Prometheus 集成,負責管理警報規則、接收來自 Prometheus 服務器的警報通知,并根據配置的策略進行處理,如抑制重復警報、分組、靜默等,然后將警報發送到各種通知渠道(如電子郵件、Slack 等)。
存儲:
????????Prometheus 使用本地磁盤存儲時序數據庫,以存儲收集到的指標數據。這使得 Prometheus 能夠快速高效地查詢歷史數據,并支持靈活的數據保留策略。此外,Prometheus 還支持與遠程存儲系統(如 InfluxDB、Google Cloud Storage 等)集成,以實現長期存儲。
Grafana:
????????盡管 Grafana 不是 Prometheus 的一部分,但它經常與 Prometheus 一起使用,用于創建儀表盤、圖表和報表,以實時展示和分析監控數據。Grafana 提供了豐富的可視化功能,使用戶能夠直觀地了解系統的運行狀態和性能指標。
整個 Prometheus 監控系統的架構設計靈活且模塊化,可以根據具體的需求和場景進行定制和擴展。通過合理配置和組合各種組件,可以構建一個高效、穩定且功能豐富的監控解決方案,幫助用戶實時監控和管理其 IT 基礎架構和應用程序。
準備
- ??? 兩臺虛擬機
192.168.100.125 Prometheus端192.168.100.126 node端
- ??? 開放防火墻端口
?更改防火墻
?? 在兩臺機器執行,請運行以下命令以開放端口
firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=22/tcp --permanentfirewall-cmd --zone=public --add-port=3000/tcp --permanentfirewall-cmd --zone=public --add-port=9115/tcp --permanentfirewall-cmd --zone=public --add-port=9090/tcp --permanentfirewall-cmd --zone=public --add-port=9100/tcp --permanentfirewall-cmd --reload
- 1、打開 SELinux 的配置文件
vi /etc/selinux/config
- 2、在打開的配置文件中,找到
SELINUX
這一行。這一行可能設置為enforcing
(強制模式)或permissive
(寬容模式)。如果你想要將 SELinux 設置為寬容模式,確保這一行的值是permissive
:
SELINUX=permissive
- 3、保存并關閉文件。如果你使用的是 vi 編輯器,按Esc,然后輸入:wq并按Enter來保存并退出。
- 4、為了讓新的 SELinux 配置生效,你需要重啟你的系統:
reboot
- 5、或者,你也可以嘗試使用setenforce命令臨時將 SELinux 設置為寬容模式,但這只是臨時的,重啟后 SELinux 會恢復到配置文件中的設置:
setenforce 0
一、Prometheus 部署
Prometheus:
????????Prometheus 是一種開源的系統監控和警報工具包。它最初由 SoundCloud 開發,現已成為 CNCF(云原生計算基金會)的一部分。Prometheus 具有多維數據模型和靈活的查詢語言,可實時收集和存儲各種系統指標,如 CPU 使用率、內存使用率、磁盤空間、網絡流量等。它支持通過 HTTP 等多種方式進行數據抓取,并能夠對數據進行長期存儲和分析。
以下在 Prometheus 端執行
- 1、下載wget
yum install wget
- 2、在線下載
wget https://github.com/prometheus/prometheus/releases/download/v2.37.2/prometheus-2.37.2.linux-amd64.tar.gz或者更快--wget https://githubfast.com/prometheus/prometheus/releases/download/v2.37.2/prometheus-2.37.2.linux-amd64.tar.gz
- 3、解壓到/usr/local/,再重命名
tar -xzvf prometheus-2.37.2.linux-amd64.tar.gz -C /usr/localcd /usr/localmv prometheus-2.37.2.linux-amd64 prometheus
- 4、查看Prometheus 版本
cd /usr/local/prometheus./prometheus --version
????????檢查 prometheus.yml 格式的命令(一定要在/usr/local/prometheus 目錄下執行)
cd /usr/local/prometheus./promtool check config prometheus.yml
- 5、創建 prometheus 本地 TSDB 數據存儲目錄
mkdir -p /var/lib/prometheus
- 6、使用systemctl 管理 Prometheus
vi /usr/lib/systemd/system/prometheus.service[Unit]Description=PrometheusDocumentation=https://prometheus.io/After=network.target[Service]Type=simpleUser=rootExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.enable-lifecycleExecReload=/bin/kill -HUP $MAINPIDKillMode=processRestart=on-failure[Install]WantedBy=multi-user.target
- 7、設置 Prometheus 開機啟動
systemctl enable prometheussystemctl start prometheus
- 8、查看prometheus 服務狀態
systemctl status prometheus
- 9、訪問Prometheus 的網頁界面
瀏覽器輸入 http://你的 ip 地址:9090
二、node-exporter 部署
Node Exporter:
????????Node Exporter 是 Prometheus 生態系統中的一個組件,用于收集主機級別的系統指標。它是一個輕量級的代理,定期收集主機的 CPU、內存、磁盤、網絡等指標,并將其暴露為 Prometheus 可以拉取的格式。Node Exporter 使得 Prometheus 能夠監控和記錄服務器的性能指標,從而實現對整個基礎架構的實時監控和分析。
以下在 node 端執行
- 1、給被監控的機器下載和解壓 node-exporter
yum install wget -y
wget https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
tar -zvxf node_exporter-1.4.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local
mv node_exporter-1.4.0.linux-amd64 node_exporter如果下載慢試試wget https://githubfast.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
- 2、systemctl 管理 node_exporter
vi /usr/lib/systemd/system/node_exporter.service[Unit]Description=node_exporterDocumentation=https://prometheus.io/After=network.target[Service]Type=simpleUser=rootExecStart=/usr/local/node_exporter/node_exporterExecReload=/bin/kill -HUP $MAINPIDKillMode=processRestart=on-failure[Install]WantedBy=multi-user.target
- 3、設置開機啟動
systemctl enable node_exportersystemctl start node_exporter
以下在 prometheus 端執行
- 4、在prometheus 主機添加 node 節點監控
????????在 prometheus Server 配置文件中添加被監控的機器
vi /usr/local/prometheus/prometheus.yml添加- job_name: "node1"static_configs:- targets: ['被監控的計算機IP:9100']
- 5、檢查prometheus.yml 格式(一定要在 /usr/local/prometheus 目錄下執行)
cd /usr/local/prometheus/usr/local/prometheus/promtool check config prometheus.yml
- 6、熱加載 prometheus 配置
curl -X POST http://127.0.0.1:9090/-/reload
- 7、訪問Prometheus 的網頁界面,查看 node 節點已經被監控
- ?8、查看http metrics 采集指標
????????http://被監控的計算機 IP:9100/metrics,查看從 exporter 具體能抓到的數據
- ?9、node_exporter 的 PromQL 查詢語句
一些 PromQL 如下
- 1、獲取系統信息
node_uname_info
- 2、獲取系統 uptime 時間
sum(time() - node_boot_time_seconds)by(instance)
- 3、系統啟動時間
node_boot_time_seconds
- 4、系統當前時間
time()
- 5、CPU 核數
count(node_cpu_seconds_total{mode='system'}) by (instance)
- 6、計算 CPU 使用率
(1 - sum(rate(node_cpu_seconds_total{mode="idle"}[1m])) by (instance) / sum(rate(node_cpu_seconds_total[1m])) by (instance) ) * 100
- 7、計算內存使用率
(1- (node_memory_Buffers_bytes + node_memory_Cached_bytes + node_memory_MemFree_bytes) / node_memory_MemTotal_bytes) * 100
- 8、查看節點總內存
node_memory_MemTotal_bytes/1024/1024/1024
- 9、計算磁盤使用率
(1 - node_filesystem_avail_bytes{fstype=~"ext4|xfs"} /node_filesystem_size_bytes{fstype=~"ext4|xfs"}) * 100
- 10、磁盤 IO
????????磁盤讀 IO 使用
sum by (instance) (rate(node_disk_reads_completed_total[5m]))
????????磁盤寫 IO 使用
sum by (instance) (rate(node_disk_writes_completed_total[5m]))
- 11、網絡帶寬
????????下行帶寬
sum by(instance) (irate(node_network_receive_bytes_total{device!~"bond.*?|lo"}[5m]))
????????上行帶寬
sum by(instance) (irate(node_network_transmit_bytes_total{device!~"bond.*?|lo"}[5m]))
三、blackbox_exporter 探針
Blackbox Exporter:
????????Blackbox Exporter 也是 Prometheus 生態系統中的一個組件,專門用于對網絡服務進行探測和監控。它可以執行 HTTP、TCP、ICMP 等類型的健康檢查,并記錄響應時間、狀態碼等信息。Blackbox Exporter 可以用于監控 Web 服務、數據庫、消息隊列等各種網絡應用,并及時發現并報告它們的健康狀態和可用性問題。
部署 blackbox_exporter
- 1、給被監控的計算機下載 blackbox_exporter
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gztar -zvxf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
mv blackbox_exporter-0.22.0.linux-amd64 blackbox_exporter下載慢嘗試:wget https://githubfast.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
- 2、查看 blackbox_exporter 版本信息
cd /usr/local/blackbox_exporter./blackbox_exporter --version
- 3、systemctl 管理 blackbox_exporter
vi /usr/lib/systemd/system/blackbox_exporter.service[Unit]Description=blackbox_exporterAfter=network.target[Service]User=rootType=simpleExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.ymlExecReload=/bin/kill -HUP $MAINPIDKillMode=processRestart=on-failure[Install]WantedBy=multi-user.target
- 4、啟動、開機啟動 blackbox_exporter
systemctl start blackbox_exporter && systemctl enable blackbox_exporterps -ef | grep blackbox_exporter
- 5、http訪問測試(blackbox_exporter 默認監聽 9115 端口)
http://被監控的計算機IP:9115
- ?6、blackbox_exporter 配置文件
????????blackbox_exporter 的配置文件無特殊需求使用默認配置即可
cat /usr/local/blackbox_exporter/blackbox.yml
以下在 Prometheus 端操作
????????在 prometheus.yml 中添加 blackbox_exporter 的配置, 要注意 yml 文件的語法規范
vi /usr/local/prometheus/prometheus.yml
????????ICMP 監控主機存活狀態的配置
#icmp ping 監控- job_name: crawler_statusmetrics_path: /probeparams:module: [icmp]static_configs:- targets: ['223.5.5.5','114.114.114.114']labels:instance: node_statusgroup: 'icmp-node'relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 安裝blackbox_expoter的ip地址:9115
????????TCP 監控端口的配置
#監控tcp端口- job_name: tcp_portmetrics_path: /probeparams:module: [tcp_connect]file_sd_configs:- files: ['/usr/local/prometheus/conf.d/tcp_port/*.yml']refresh_interval: 10srelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 安裝blackbox_expoter的ip地址:9115
????????HTTP GET 監控的配置
# http get 監控- job_name: http_getmetrics_path: /probeparams:module: [http_2xx]file_sd_configs:- files: ['/usr/local/prometheus/conf.d/http_get/*.yml']refresh_interval: 10srelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 安裝blackbox_expoter的ip地址:9115
上面三段配置都在寫 prometheus.yml 里面
編輯 tcp 監控 targets 文件
????????上一個配置指定了配置文件,在這里新建文件
mkdir -p /usr/local/prometheus/conf.d/tcp_portvi /usr/local/prometheus/conf.d/tcp_port/tcp_port.yml
????????寫入并改寫要監控的 ip 和端口號
- targets: ['192.168.100.126:80','192.168.100.126:22']labels:group: 'tcp port'
編輯 http_get 監控 targets 文件
mkdir -p /usr/local/prometheus/conf.d/http_getvi /usr/local/prometheus/conf.d/http_get/http_get.yml
????????寫入要監控的內容
- targets:- http://192.168.100.126:80/labels:name: 'http_get'
????????重新啟動 Prometheus
systemctl restart prometheus
四、配置 Prometheus Rule 告警規則
- 1、創建rule 告警目錄
mkdir -p /usr/local/prometheus/rules/
- 2、編輯rule 配置文件
vi /usr/local/prometheus/rules/rules.ymlgroups:- name: http_status_coderules:- alert: probe_http_status_codeexpr: probe_http_status_code != 200for: 1mlabels:severity: criticalannotations:summary: "{{ $labels.instance }} 狀態碼異常"description: "{{ $labels.instance }} 網站訪問異常!!! (value: {{ $value }})"- name: icmp_ping_statusrules:- alert: icmp_ping_statusexpr: probe_icmp_duration_seconds{phase="rtt"} == 0for: 1mlabels:severity: criticalannotations:summary: "主機 {{ $labels.instance }} ICMP異常"description: "{{ $labels.instance }} ICMP異常!!!(value: {{ $value }})"value: '{{ $value }}'##延遲高- name: link_delay_highrules:- alert: link_delay_highexpr: probe_icmp_duration_seconds{phase="rtt"} >0.005for: 1mlabels:severity: criticalannotations:summary: " {{ $labels.instance }} 延遲高!"description: "{{ $labels.instance }} 延遲高!!!(value: {{ $value }})"
- 3、檢查 rule 文件格式(一定要在/usr/local/prometheus/rules 目錄下執行)
cd /usr/local/prometheus/rules/usr/local/prometheus/promtool check rules rules.yml
- 4、在Prometheus 主機配置文件中引入 rule 告警目錄
vi /usr/local/prometheus/prometheus.yml
找到 rule_files 那一行,改為
rule_files: ['/usr/local/prometheus/rules/*.yml']
- 5、重新啟動 Prometheus
systemctl restart prometheus
- 6、訪問Prometheus 前端頁面查看 Rules
- ?7、查看 Alerts
五、安裝 Grafana
Grafana:
????????Grafana 是一款開源的數據可視化和監控平臺,可以與 Prometheus 等多種數據源集成。它提供了豐富的圖表、儀表盤和警報功能,使用戶能夠直觀地展示和分析監控數據。Grafana 支持靈活的查詢語言和可定制的儀表盤布局,可以滿足各種不同場景下的監控需求,并為用戶提供實時的數據可視化和分析能力。
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.2.2-1.x86_64.rpmyum install grafana-enterprise-9.2.2-1.x86_64.rpm
- 1、啟動Grafana 服務
systemctl enable grafana-serversystemctl start grafana-server
訪問 地址:http://Prometheus:3000
默認賬號密碼:admin/admin第一次登錄后會要求更改密碼
- ?2、配置 Prometheus
?????????進入 Grafana 后,添加數據源:
- 3、配置顯示模板
?最終就能看到配置后的結果
?后續進入的話,可以從菜單>Dashboards 進入
Grafana dashboards官網地址
Grafana dashboards | Grafana Labs