Prometheus 是什么?
Prometheus是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社區也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。類似的產品還有influxdb, 如果用於監控用途是通常會配合grafana進行數據展示和報警。
Prometheus 的優點
- 非常少的外部依賴,安裝使用超簡單
- 已經有非常多的系統集成, 例如:Docker、StatsD、HAProxy、Nginx、JMX等
- 服務自動化發現
- 直接集成到代碼
- 設計思想是按照分布式、微服務架構來實現的
Prometheus 的特性
- 自定義多維度的數據模型
- 非常高效的存儲 平均一個采樣數據占 ~3.5 bytes左右,320萬的時間序列,每30秒采樣,保持60天,消耗磁盤大概228G。
- 強大的查詢語句
- 輕松實現強大的數據可視化
- 簡單的操作,每一個Server都是一個獨立的實現,僅僅依賴本地存儲。
- 精確的告警功能。
- 豐富的客戶端library
更詳細的說明和幫助,可以參考prometheus官網: https://prometheus.io/
Prometheus的架構
Prometheus使用的場景
如官網描述的一樣,prometheus主要是在當我們需要單純記錄以時間進行分片的數據時使用,無論是系統級別的監控,或是我們的高度動態的服務架構。針對微服務場景,prometheus也能很好的支持多為數據的收集和查詢。
當然,prometheus也有局限性,如果我們的監控或系統,需要百分百的准確性就不太適合。
需要了解更多關於prometheus的知識,強烈建議好好閱讀官網的文檔。
Prometheus的安裝
本文的重點是如何在CentOS 7中安裝和配置Prometheus,我們要按生產級的要求來部署Prometheus。以下就開始我們的部署工作, 按步驟進行操作。
基礎准備
以root或具有sudo權限的用戶登錄CentOS系統,執行以下命令升級系統(非必要):
yum update -y
禁用SELinux
vim /etc/sysconfig/selinux
將SELINUX=enforcing
改為ELINUX=disabled
, 然后重啟系統reboot
。
同步時區( 設置系統時區為上海)
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
下載安裝包
cd /data/software/
wget https://github.com/prometheus/prometheus/releases/download/v2.11.1/prometheus-2.11.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.11.1.linux-amd64.tar.gz
創建執行用戶
useradd --no-create-home --shell /bin/false prometheus
創建執行目錄
mkdir /etc/prometheus
mkdir /var/lib/prometheus
修改目錄權限(更改所有者)
chown prometheus:prometheus /etc/prometheus
chown prometheus:prometheus /var/lib/prometheus
復制文件到執行目錄
cd /data/software/prometheus-2.11.1.linux-amd64
cp prometheus /usr/local/bin/
cp promtool /usr/local/bin/
# 設置權限(更改所有者)
chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool
cp -r consoles /etc/prometheus
cp -r console_libraries /etc/prometheus
chown -R prometheus:prometheus /etc/prometheus/consoles
chown -R prometheus:prometheus /etc/prometheus/console_libraries
添加配置文件
mkdir /etc/prometheus/
cp prometheus.xml /etc/prometheus/ or vim /etc/systemd/system/prometheus.service
chown prometheus:prometheus /etc/prometheus/prometheus.yml
當然,如果沒有模板文件的話,也可以新創建一個,具體配置如下:
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# 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:
# - "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'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
創建服務
vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
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
啟動服務
# reload服務配置
systemctl daemon-reload
# 啟動服務
systemctl start prometheus
# 查看服務狀態
systemctl status prometheus
如果一切正常應該可以看到如下輸出, 說明服務以及正常啟動。
[root@ ~]# systemctl status prometheus
● prometheus.service - Prometheus
Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor prese t: disabled)
Active: active (running) since Mon 2019-07-15 17:43:53 CST; 1h 34min ago
Main PID: 22807 (prometheus)
CGroup: /system.slice/prometheus.service
└─22807 /usr/local/bin/prometheus --config.file /etc/prometheus/pr...
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)"
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)"
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.661Z...)"
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.663Z...."
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...IC
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...d"
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...ml
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.695Z...90
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...ml
Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...."
Hint: Some lines were ellipsized, use -l to show in full.
設置服務開機啟動
systemctl enable prometheus
也可以試一下重啟系統,看服務會不會自動開啟。
防火牆設置
因為我們的配置文件中配置的端口為9090
, 所以如果你的系統有設置防火牆,這需要將這個端口開放出來。
firewall-cmd --zone=public --add-port=9090/tcp --permanent
# 重啟防火牆服務
systemctl reload firewalld
訪問頁面
因為我是在虛擬機里安裝的,直接在宿主機瀏覽器中輸入: http://192.168.56.2:9090
,如果出現以下頁面這表示服務正常開啟。
安裝exporter
exporter是prometheus用來收集數據的客戶端工具,在prometheus的官網上https://prometheus.io/download/ 可以下載到很多服務的exporter采集器。以下是我們經常用到的一些采集器:
Name | Description | URL |
---|---|---|
Node Exporter | 針對服務器進行監控的探測器(探針) | https://github.com/prometheus/node_exporter |
Alert Manager | 報警相關 | https://github.com/prometheus/alertmanager |
Mysql Exporter | MySQL服務監控相關的探測器 | https://github.com/prometheus/mysqld_exporter |
Blackbox Exporter | 黑盒監控解決方案,其允許用戶通過:HTTP、HTTPS、DNS、TCP以及ICMP的方式對網絡進行探測 | https://github.com/prometheus/blackbox_exporter |
jmx_exporter | JMX服務的探測器 | https://github.com/prometheus/jmx_exporter |
具體的可以到我們使用時再行在https://prometheus.io/download/或github中尋找需要的插件進行安裝配置,這里我們就簡單介紹幾個常用的插件的安裝和使用。
node_exporter
node_exporter是一個服務器端agent,負責采集服務器基礎監控項。下面我們就來一步一步安裝node_exporter收集所在服務器的性能。
下載安裝包
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
tar -zvxf node_exporter-0.18.1.linux-amd64.tar.gz
添加執行用戶
useradd -rs /bin/false nodeusr
復制執行文件
cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/
創建系統服務
vim /etc/systemd/system/node_exporter.service
內容如下:
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=nodeusr
Group=nodeusr
Type=simple
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
啟動服務
systemctl daemon-reload
systemctl start node_exporter
查看服務狀態,如果正常的話會顯示如下:
[root@ software]# systemctl status node_exporter
● node_exporter.service - Node Exporter
Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2019-07-15 20:10:48 CST; 20min ago
Main PID: 30530 (node_exporter)
CGroup: /system.slice/node_exporter.service
└─30530 /usr/local/bin/node_exporter
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - sockstat" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - stat" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - textfile" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - time" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - timex" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - uname" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - vmstat" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - xfs" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - zfs" source="node_exporter.go:104"
Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg="Listening on :9100" source="node_exporter.go:170"
設置服務開機自啟動
systemctl enable node_exporter
開啟防火牆
如果防火牆服務有開啟的話,就需要開通端口: 9100
firewall-cmd --zone=public --add-port=9100/tcp --permanent
systemctl restart firewalld
查看服務
在宿主機瀏覽器輸入http://192.168.56.2:9100/metrics
將node_exporter加入prometheus的配置
vim /etc/prometheus/prometheus.yml
在prometheus.xml中的scrape配置項中加入以下內容:
- job_name: 'node_exporter_centos'
scrape_interval: 5s
static_configs:
- targets: ['192.168.56.2:9100']
最終的配置文件如下:
然后重新啟動prometheus服務。
systemctl restart prometheus
prometheus中查看node_exporter的數據
在宿主機的瀏覽器中輸入prometheus的地址, http://192.168.56.2:9090/graph
Alert Manager
下載二進制安裝包
export VERSION=0.18.0
curl -LO https://github.com/prometheus/alertmanager/releases/download/v$VERSION/alertmanager-$VERSION.darwin-amd64.tar.gz
tar xvf alertmanager-$VERSION.darwin-amd64.tar.gz