# Prometheus Server怎么安裝
## 目錄
1. [Prometheus簡介](#prometheus簡介)
2. [安裝前準備](#安裝前準備)
- [系統要求](#系統要求)
- [環境檢查](#環境檢查)
3. [Linux系統安裝](#linux系統安裝)
- [二進制包安裝](#二進制包安裝)
- [Docker安裝](#docker安裝)
- [源碼編譯安裝](#源碼編譯安裝)
4. [Windows系統安裝](#windows系統安裝)
5. [macOS系統安裝](#macos系統安裝)
6. [配置詳解](#配置詳解)
- [主配置文件](#主配置文件)
- [服務發現配置](#服務發現配置)
7. [啟動與驗證](#啟動與驗證)
8. [系統服務管理](#系統服務管理)
- [Systemd配置](#systemd配置)
- [Supervisor配置](#supervisor配置)
9. [數據存儲與維護](#數據存儲與維護)
10. [安全配置](#安全配置)
11. [高可用部署](#高可用部署)
12. [常見問題排查](#常見問題排查)
13. [最佳實踐](#最佳實踐)
14. [附錄](#附錄)
---
## Prometheus簡介
Prometheus是由SoundCloud開發的開源監控系統,現已成為CNCF畢業項目。它具有以下核心特性:
- 多維數據模型(時間序列由metric名稱和key/value標簽組成)
- 靈活的查詢語言PromQL
- 不依賴分布式存儲,單個服務器節點自治
- 基于HTTP的pull方式采集時序數據
- 支持通過中間網關進行push時間序列
- 支持服務發現和靜態配置
- 多種圖形和儀表板支持
典型架構圖:
```mermaid
graph TD
P[Prometheus Server] -->|拉取| E[Exporters]
P -->|拉取| A[Application]
P -->|拉取| S[Service Discovery]
P -->|告警| AM[Alertmanager]
G[Grafana] -->|查詢| P
最低配置:
推薦配置:
# 檢查系統版本
cat /etc/os-release
# 檢查CPU和內存
free -h
lscpu
# 檢查磁盤空間
df -h
# 檢查網絡連通性
ping -c 4 prometheus.io
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
./prometheus --version
├── prometheus # 主程序
├── promtool # 配置檢查工具
├── consoles/ # 控制臺模板
├── console_libraries/ # 控制臺庫
└── prometheus.yml # 主配置文件
docker pull prom/prometheus
mkdir /etc/prometheus
docker run -d \
-p 9090:9090 \
-v /etc/prometheus:/etc/prometheus \
prom/prometheus
wget https://golang.org/dl/go1.17.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.17.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
git clone https://github.com/prometheus/prometheus.git
cd prometheus
make build
wget https://github.com/prometheus/prometheus/releases/download/v2.30.3/prometheus-2.30.3.windows-amd64.zip -OutFile prometheus.zip
Expand-Archive -Path prometheus.zip -DestinationPath C:\Prometheus
cd C:\Prometheus
.\prometheus.exe
brew install prometheus
brew services start prometheus
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
- 'alert.rules'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
| 配置項 | 說明 | 默認值 |
|---|---|---|
| scrape_interval | 抓取間隔 | 1m |
| evaluation_interval | 規則評估間隔 | 1m |
| scrape_timeout | 抓取超時時間 | 10s |
| external_labels | 外部標簽 | {} |
./prometheus --config.file=prometheus.yml
curl http://localhost:9090/-/healthy
http://<server-ip>:9090
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries
[Install]
WantedBy=multi-user.target
--storage.tsdb.retention.time=15d
promtool tsdb clean --older-than 30d --output-dir /tmp/clean_data
web:
cert_file: /path/to/cert.pem
key_file: /path/to/key.pem
htpasswd -c /etc/prometheus/web_auth admin
典型HA架構:
graph TB
P1[Prometheus 1] --> R[Remote Storage]
P2[Prometheus 2] --> R
AM[Alertmanager] --> P1
AM --> P2
journalctl -u prometheus -f
./promtool check config prometheus.yml
”`
注:本文檔實際約3000字,要達到7300字需要擴展以下內容: 1. 每個安裝方法的詳細步驟和截圖 2. 配置文件的完整參數說明 3. 性能調優指南 4. 與各類導出器的集成案例 5. 詳細的故障排查手冊 6. 安全加固的完整方案 7. 實際生產環境部署案例 需要繼續擴展哪些部分可以告訴我。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。