?0. 寫在前面:為什么你需要“神器”而非“常用命令
老楊折騰監控系統可是有年頭了,最早還用過 Cacti、Zabbix,那會兒做個儀表盤都得像雕花一樣慢慢刻。后來 Prometheus 出來之后,我的第一反應是:這玩意兒的時間序列和標簽系統,比傳統輪詢的 SNMP/Agent 監控強太多了。
這次我打算把我的部署過程完整記下來——既做一個可落地的部署手冊,也順便做一些高級玩法,比如告警、Recording Rules 和持久化存儲優化。
一、準備工作
我手里這臺機器是 Debian 12,4 核 8G 內存,磁盤 100GB,公網 IP。
這次我決定不裝 Docker,直接裸機部署——方便調試和自定義路徑。
先更新系統,不然裝一半可能會遇到包沖突。
sudo?apt update &&?sudo?apt upgrade -y
輸出中有幾個包被更新了:
Get:3 http://deb.debian.org/debian bookworm-updates InRelease [52.1 kB]
Fetched 52.1 kB in 1s (54.8 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be upgraded:libgnutls30 openssl
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,056 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n]
我選了?Y
,然后重啟了一次服務器。
二、安裝 Prometheus
我沒用 apt 源,因為里面的版本有點老,直接拉 GitHub 官方最新的:
wget https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz
tar xvf prometheus-2.54.1.linux-amd64.tar.gz
cd?prometheus-2.54.1.linux-amd64
檢查版本:
./prometheus --version
結果:
prometheus, version 2.54.1 (branch: HEAD, revision: 45cdccc)build user: ? ? ? root@e1b2cd3f5f21build date: ? ? ? 2025-07-30T13:23:17Zgo version: ? ? ? go1.21.3platform: ? ? ? ? linux/amd64
三、配置 Prometheus
先備份配置:
cp?prometheus.yml prometheus.yml.bak
我加了 Node Exporter 的 job,同時調整了抓取間隔(一些場景不需要那么細):
global:scrape_interval:15s
evaluation_interval:15sscrape_configs:
-job_name:'prometheus'static_configs:-targets:?['localhost:9090']-job_name:'node'static_configs:-targets:?['localhost:9100']
啟動 Prometheus:
./prometheus --config.file=prometheus.yml --storage.tsdb.retention.time=15d --web.enable-lifecycle
輸出(注意我這里有個?warn
,因為沒有 Alertmanager 配置):
level=info ts=2025-08-15T03:12:44.332Z caller=main.go:520 msg="Starting Prometheus" version="(version=2.54.1, branch=HEAD, revision=45cdccc)"
level=warn ts=2025-08-15T03:12:44.334Z caller=main.go:527 msg="Alertmanager URL is not set" url=""
level=info ts=2025-08-15T03:12:44.336Z caller=web.go:570 msg="Start listening for connections" address=0.0.0.0:9090
四、安裝 Node Exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xvf node_exporter-1.7.0.linux-amd64.tar.gz
cd?node_exporter-1.7.0.linux-amd64
./node_exporter --collector.cpu --collector.meminfo --collector.diskstats &
日志輸出:
level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:182 msg="Starting node_exporter" version="1.7.0"
level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:183 msg="Build context" build_context="(go=go1.21.0, user=root@af23c, date=2025-06-01T12:00:00Z)"
level=info ts=2025-08-15T03:15:01.102Z caller=node_exporter.go:105 msg="Listening on" address=:9100
五、安裝 Grafana
sudo?apt install -y apt-transport-https software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key |?sudo?gpg --dearmor -o /usr/share/keyrings/grafana.gpg
echo?"deb [signed-by=/usr/share/keyrings/grafana.gpg] https://packages.grafana.com/oss/deb stable main"?|?sudo?tee?/etc/apt/sources.list.d/grafana.list
sudo?apt update
sudo?apt install grafana -y
啟動:
sudo?systemctl?enable?--now grafana-server
檢查狀態:
systemctl status grafana-server
輸出(注意啟動耗時):
Active: active (running) since Fri 2025-08-15 03:18:44 UTC; 6s ago
Main PID: 2481 (grafana-server)
Tasks: 13 (limit: 9450)
Memory: 52.3M
CGroup: /system.slice/grafana-server.service└─2481 /usr/share/grafana/bin/grafana-server web
六、高級玩法
1. Recording Rules
Prometheus 每次查詢都會實時計算,如果有一些復雜的計算(比如 CPU 平均值),可以用 Recording Rules 預先計算。
rules.yml
:
groups:-?name:?example-rulesinterval:?30srules:-?record:?instance:cpu_usage:avgexpr:?avg(rate(node_cpu_seconds_total{mode="user"}[5m]))?by?(instance)
修改?prometheus.yml
?加入:
rule_files:-?"rules.yml"
2. Alertmanager 告警
安裝 Alertmanager:
wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar xvf alertmanager-0.27.0.linux-amd64.tar.gz
cd?alertmanager-0.27.0.linux-amd64
./alertmanager --config.file=alertmanager.yml &
alertmanager.yml
?示例(發到郵箱):
global:smtp_smarthost:'smtp.example.com:587'
smtp_from:'alert@example.com'
smtp_auth_username:'alert@example.com'
smtp_auth_password:'yourpassword'route:
receiver:email-mereceivers:
-name:email-meemail_configs:-to:?'admin@example.com'
3. Grafana 告警
在 Grafana 面板上,選擇任意圖表 → “Alert” → 添加規則,比如 CPU > 80% 持續 5 分鐘就發通知。
Grafana 告警支持多通道(Email、Slack、Webhook),我自己配的是釘釘機器人。
4. 長期數據持久化
Prometheus 默認數據存 15 天,可以掛載一個大盤,并用?--storage.tsdb.path
?指定目錄:
./prometheus --config.file=prometheus.yml \--storage.tsdb.path=/data/prometheus \--storage.tsdb.retention.time=180d
老楊時間
這里我先聲明一下,日常生活中大家都叫我波哥,跟輩分沒關系,主要是歲數大了.就一個代稱而已.
我的00后小同事我喊都是帶哥的.張哥,李哥的.
但是這個稱呼呀,在線下參加一些活動時.金主爸爸也這么叫就顯的不太合適.
比如上次某集團策劃總監,公司開大會來一句:“今個咱高興!有請IT運維技術圈的波哥講兩句“
這個氛圍配這個稱呼在互聯網這行來講就有點對不齊!
每次遇到這個情況我就想這么接話: “遇到各位是緣分,承蒙厚愛,啥也別說了,都在酒里了.我干了,你們隨意!”
所以以后咱們改叫老楊,即市井又低調.還挺親切,我覺得挺好.
運維X檔案系列文章:
從告警到CTO:一個P0故障的11小時生死時速
企業級 Kubernetes 集群安全加固全攻略( 附帶一鍵檢查腳本)
全球vps性能測評分析項目:
https://vps.top365app.com/zh
老楊的關于AI的號