文章目錄
前言
安裝,配置和使用簡單的Prometheus實例。您將在本地下載並運行Prometheus,對其進行配置以抓取自身和示例應用程序,然后使用查詢,規則和圖形使用收集的時間序列數據。
一、什么是Prometheus(普羅米修斯)?
Prometheus是最初在SoundCloud上構建的開源系統監視和警報工具包 。自2012年成立以來,許多公司和組織都采用了Prometheus,該項目擁有非常活躍的開發人員和用戶社區。現在,它是一個獨立的開源項目,並且獨立於任何公司進行維護。為了強調這一點並闡明項目的治理結構,Prometheus在2016年加入了 Cloud Native Computing Foundation,這是繼Kubernetes之后的第二個托管項目。
1.1特征
- prometheus server:主要用於抓取數據和存儲時序數據,另外還提供查詢和 Alert Rule 配置管理
- client libraries:用於對接 Prometheus Server,檢測應用程序代碼可以查詢和上報數據
- push gateway:用於批量,短期的監控數據的匯總節點,主要用於業務數據匯報等
- exporters:各種匯報exporter,例如node_exporter,mysql_exporter,HAProxy,StatsD,Graphite
- alertmanager:告警通知管理
1.2架構
下圖說明了Prometheus的體系結構及其某些生態系統組件:

Prometheus直接或通過中間推送網關從已檢測作業中刪除指標,以用於短期作業。它在本地存儲所有報廢的樣本,並對這些數據運行規則,以匯總和記錄現有數據中的新時間序列,或生成警報。Grafana或其他API使用者可以用來可視化收集的數據。
二、下載Prometheus
個人喜好在根目錄下創建一個文件夾把軟件下載過去
創建文件夾
# mkdir -p /data/source
# cd /data/source/
官網下載鏈接
# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
- 1
- 2
- 3
- 4
- 5
- 6
三、安裝
3.1下載最新版本的Prometheus后
解壓縮到/usr/local/share/prometheus #tar -zxvf prometheus-2.23.0.linux-amd64.tar.gz -C /usr/local/share/ 進入文件夾 #cd /usr/local/share/ 給他起個短一點的名字 #mv prometheus-2.23.0.linux-amd64 prometheus
- 1
- 2
- 3
- 4
- 5
- 6
Prometheus服務器是一個稱為prometheus的二進制文件。我們可以運行二進制文件,並通過傳遞–help標志來查看有關其選項的幫助。
查看幫助
#./prometheus --help usage: prometheus [<flags>] The Prometheus monitoring server Flags: -h, --help Show context-sensitive help (also try --help-long and --help-man). --version Show application version. --config.file="prometheus.yml" Prometheus configuration file path. --web.listen-address="0.0.0.0:9090"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
*在啟動Prometheus之前,先對其進行配置。
3.2配置Prometheus
Prometheus配置為YAML。Prometheus下載在一個名為的文件中帶有一個示例配置,prometheus.yml這是一個入門的好地方。
刪除了示例文件中的大多數注釋,以使其更加簡潔(注釋以開頭的行#)。
配置如下:
global: #global塊控制Prometheus服務器的全局配置 scrape_interval: 15s #scrape_interval控制,Prometheus多久刮一次目標。 evaluation_interval: 15s #evaluation_interval選項控制Prometheus多久評估一次規則。 alerting: alertmanagers: - static_configs: - targets: rule_files: #rule_files塊指定希望Prometheus服務器加載的任何規則的位置。 # - "first_rules.yml" # - "second_rules.yml" scrape_configs: #scrape_configs控制Prometheus監視哪些資源。 - job_name: 'prometheus' static_configs: - targets: ['localhost:9090']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.3運行普羅米修斯
要使用我們新創建的配置文件啟動Prometheus,轉到包含Prometheus二進制文件的目錄並運行:
./prometheus --config.file=prometheus.yml
- 1
運行log
理論上來說這樣就算是安裝完成了,但這樣太簡陋了。因為每次啟動 Prometheus server 都需要手動執行命令:
# /usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml
- 1
這實在是太不方便了!應該把它配置成服務,用 systemd 來管理。
四、添加到systemd來管理
4.1先創建一個名為 prometheus 的用戶:
# adduser prometheus
- 1
把目錄 /usr/local/share/prometheus/ 的所有者設置為 prometheus 用戶:
# chown -R prometheus:prometheus /usr/local/share/prometheus/
- 1
然后創建文件 /etc/systemd/system/prometheus.service,內容如下:
[Unit] Description=Prometheus Server Documentation=https://prometheus.io/docs/introduction/overview/ After=network.target [Service] User=prometheus Restart=on-failure WorkingDirectory=/usr/local/share/prometheus/ ExecStart=/usr/local/share/prometheus/prometheus --config.file=/usr/local/share/prometheus/prometheus.yml [Install] WantedBy=multi-user.target
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
好了,現在可以通過 systemd 來控制 Prometheus 服務了,
重新載入systemctl服務:
# systemctl daemon-reload
- 1
啟動服務:
# systemctl start prometheus
- 1
再把服務配置為開機時啟動:
# systemctl enable prometheus
- 1
檢查一下服務的狀態:
# systemctl status prometheus
- 1

五、web端
現在可以在瀏覽器上瀏覽到有關其自身的狀態頁。
還可以通過導航到自己的指標終結點:http:// localhost:9090 / metrics來驗證Prometheus是否正在提供有關自身的指標。
5.1用瀏覽器查看數據
讓我們嘗試查看Prometheus收集的有關自身的一些數據。要使用Prometheus的內置表達式瀏覽器,請導航至 http:// localhost:9090 / graph並在“圖形”選項卡中選擇“控制台”視圖。
正如您可以從http:// localhost:9090 / metrics收集的那樣,稱為Prometheus導出的有關其自身的一個指標 promhttp_metric_handler_requests_total(/metricsPrometheus服務器已處理的請求總數)。繼續並將其輸入到表達式控制台中:
promhttp_metric_handler_requests_total
- 1

Graphite InfluxDB Kapacitor OpenTSDB Nagios Sensu
六、使用NODE EXPORTER監視LINUX主機指標
Node Exporter公開了各種與硬件和內核相關的指標。
在localhost配置為從運行的Node Exporter刮取指標的Prometheus實例上啟動
安裝並運行節點導出器
Prometheus Node Exporter是一個單個靜態二進制文件,可以通過tarball安裝。從Prometheus下載頁面下載后,將其解壓縮並運行:
# wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz tar xvfz node_exporter-*.*-amd64.tar.gz cd node_exporter-*.*-amd64 ./node_exporter
- 1
- 2
- 3
- 4
您應該看到類似以下的輸出,表明節點導出器正在運行,並且在端口9100上公開了指標:
節點導出器指標
安裝並運行節點導出器后,可以通過對/metrics端點進行cURL驗證來導出指標:
http://localhost:9100/metrics
- 1
您應該看到如下輸出:
配置您的Prometheus實例
需要正確配置本地運行的Prometheus實例,才能訪問Node Exporter指標。以下prometheus.yml示例配置文件將通過以下命令告訴Prometheus實例從Node Exporter進行抓取以及抓取頻率localhost:9100:
本節說明如何下載和安裝Grafana,如何在基於RPM的Linux系統上啟動並運行該服務,以及安裝軟件包的詳細信息。
6.1下載並安裝
# wget <rpm package url>
- 1
# rpm -Uvh <local rpm package>
- 1
6.2啟動服務器
grafana-server將以grafana用戶身份啟動該過程,該過程是在軟件包安裝期間創建的。systemd命令在大多數情況下都可以使用,但是某些較舊的Linux系統可能需要init.d。安裝程序應提示您輸入正確的命令。
如果安裝了.rpm軟件包,則可以使用systemd或啟動服務器init.d。如果安裝了二進制.tar.gz文件,則需要執行二進制文件。
6.2.1用systemd啟動服務器
要啟動服務並驗證服務已啟動:
# systemctl daemon-reload # systemctl start grafana-server # systemctl status grafana-server
- 1
- 2
- 3

配置Grafana服務器以在啟動時啟動:
# systemctl enable grafana-server
