一、介紹
1.什么是Prometheus?
普羅米修斯是一個開源的系統監控及報警工具,在2016年加入了 Cloud Native Computing Foundation,是繼Kubernetes之后的第二個托管項目。
2.Prometheus的特征有什么?
- 具有由metric名稱和鍵值對標示的時間序列數據的多位數據模型
- 有一個靈活的查詢語言promQL
- 不依賴分布式存儲,只和本地磁盤有關
- 通過HTTP來拉取(pull)時間序列數據
- 也支持推送(push)方式添加時間序列數據
- 多種圖形和儀表盤支持
3.Prometheus的組件都有哪些?來張官方圖:

- Prometheus Server 用於定時抓取數據指標(metrics)、存儲時間序列數據(TSDB)
- Jobs/exporte 收集被監控端數據並暴露指標給Prometheus
- Pushgateway 監控端的數據會用push的方式主動傳給此組件,隨后被Prometheus 服務定時pull此組件數據即可
- Alertmanager 報警組件,可以通過郵箱、微信等方式
- Web UI 用於多樣的UI展示,一般為Grafana
- 還有一些例如配置自動發現目標的小組件和后端存儲組件
4.什么時候使用Prometheus
- 監控的對象動態可變,無法預先配置的時候
- Prometheus 是專為雲環境(k8s/docker)提供的監控工具
- 想要更直觀更簡單的直接觀察某項指標的數據變化時
5.看到一個寫的非常不錯的關Prometheus存儲的文章
https://www.cnblogs.com/zqj-blog/p/12205063.html
二、搭建
1.安裝Prometheus
官網下載地址:https://prometheus.io/download/ 選擇自己所需版本即可
## 解壓安裝
tar zxf prometheus-2.22.0.linux-amd64.tar.gz -C /opt/vfan/
mv prometheus-2.22.0.linux-amd64 prometheus-2.22.0
cd prometheus-2.22.0/
## 可以通過--help或--version查看服務啟動參數和版本等
./prometheus --help
./prometheus --version
## 啟動服務,並指定配置文件
nohup ./prometheus --config.file="prometheus.yml" &> /dev/null &
## 查看端口占用情況(默認9090)
[root@VM-0-10-centos prometheus-2.22.0]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=28673,fd=14))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1306,fd=3))
LISTEN 0 128 :::80 :::* users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4))
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=11771,fd=10))
docker run \
-p 9090:9090 \
-v /path/to/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
查看默認prometheus.yml文件:vim 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']
目前只在監控Prometheus本機
可以登錄普羅米修斯(服務器ip:9090)web界面,Status—>Rules下查看目前正在監控的目標

可以看到獲取監控信息的終點是 本機ip+端口+/metrics:

也可以查看監控圖形:Graph—>選擇監控項—>Execute

這種圖形界面顯然不太直觀,所以引入Grafana。
2.安裝node-exporter插件,添加監控機器
下載鏈接:https://prometheus.io/download/ 選擇自己所需版本即可
## 解壓安裝
tar zxf node_exporter-1.0.1.linux-amd64.tar.gz -C /opt/vfan/
mv node_exporter-1.0.1.linux-amd64 node_exporter
cd node_exporter/
## 可以查看服務啟動參數
./node_exporter --help
--web.listen-address=":9100" #可以指定監聽端口
--collector.ntp.server="127.0.0.1" #可以指定ntp server
## 直接執行即可,--web.listen-address參數可以指定監聽端口,默認9100。
nohup ./node_exporter --web.listen-address=":9100" &> /dev/null &
[root@VM-0-10-centos node_exporter]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=28673,fd=14))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1306,fd=3))
LISTEN 0 128 :::9100 :::* users:(("node_exporter",pid=26134,fd=3))
LISTEN 0 128 :::80 :::* users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4))
LISTEN 0 128
prometheus.yaml中添加node_exporter配置
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'] - job_name: 'node_demo1' static_configs: - targets: ['localhost:9100']
然后重啟普羅米修斯服務,重啟后再次查看監控目標:

已經開始監控新的node
3.安裝Grafana
下載鏈接:https://grafana.com/grafana/download
wget https://dl.grafana.com/oss/release/grafana-7.2.2.linux-amd64.tar.gz
## 解壓安裝 tar zxf grafana-7.2.2.linux-amd64.tar.gz -C /opt/vfan/ cd grafana-7.2.2 ## 查看啟動參數 ./grafana-server --help ## 啟動服務,默認端口3000 nohup ./grafana-server &> /dev/null & [root@VM-0-10-centos conf]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=28673,fd=14)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=1306,fd=3)) LISTEN 0 128 :::9100 :::* users:(("node_exporter",pid=26134,fd=3)) LISTEN 0 128 :::80 :::* users:(("httpd",pid=31980,fd=4),("httpd",pid=31851,fd=4),("httpd",pid=30055,fd=4),("httpd",pid=21050,fd=4),("httpd",pid=14509,fd=4),("httpd",pid=12678,fd=4),("httpd",pid=12676,fd=4),("httpd",pid=9731,fd=4),("httpd",pid=9678,fd=4),("httpd",pid=2718,fd=4),("httpd",pid=1430,fd=4)) LISTEN 0 128 :::3000 :::* users:(("grafana-server",pid=31050,fd=10)) LISTEN 0 128
Grafana默認的配置文件為:vim grafana-7.2.2/conf/defaults.ini;主要有監聽端口、日志路徑、默認登錄帳號密碼等
[server] # Protocol (http, https, h2, socket) protocol = http # The ip address to bind to, empty will bind to all interfaces http_addr = # The http port to use http_port = 3000 # The public facing domain name used to access grafana from a browser domain = localhost [security] # disable creation of admin user on first start of grafana disable_initial_admin_creation = false # default admin user, created on startup admin_user = admin # default admin password, can be changed before first start of grafana, or in profile settings admin_password = admin
現在可以通過ip+端口方式來訪問Grafana:

第一次登陸會強制性修改密碼,修改后即可進入
4.配置Grafana,增加可視化模板
第一步:添加數據源

選擇Prometheus,只需將URL修改為Prometheus服務地址,其余默認即可(也可自行修改):

可以將Prometheus服務及Grafana服務的監控模板導入進去:

但要注意,導入Grafana的模板后,要在Prometheus.yml增加Grafana的監控:vim prometheus.yml
scrape_configs: - job_name: 'grafana' static_configs: - targets: ['localhost:3000']
點擊保存,保存后查看數據源:

查看剛剛導入的模板,已經形成監控圖形:

至此,Prometheus+Grafana基本組件搭建完成。
三、配置Grafana模板,配合Prometheus使用
1、監控系統指標
前提條件:
- 被監控的主機系統上已經安裝node_exporter
- Prometheus.yml中已經添加此主機的Job
也就是以上第二步的第2點
前提條件准備完畢后,我們可以找一些實用且直觀的模板來直接套用,不僅可以節省時間成本,實際效果也相當不錯,如果有什么地方不能滿足自己的需求,還可以在此基礎上修改:
前往Grafana的官網下載Dashboard模板:https://grafana.com/grafana/dashboards

選擇Prometheus,再根據關鍵字搜索
(1).點進去一個node_exporter的模板,可以查看樣圖,然后直接下載JSON文件

(2).點擊加號—>import—>Upload JSON file

(3).模板導入后,即可進行監控

2、監控mysql服務各項指標
(1).Prometheus官網提供了mysqld的metric指標采集插件,可以直接下載:https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
## 解壓即可 tar zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz
(2).下載安裝完畢后,啟動前,需要在mysql中創建一個Prometheus收集數據的賬號:
mysql> create user 'promethues'@'localhost' IDENTIFIED BY 'promethues1'; Query OK, 0 rows affected (0.00 sec) mysql> grant select,replication client,process on *.* to 'promethues'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
注意:這里的的localhost不是指mysqld服務的ip,是指mysqld_exporter的ip,因為promethues服務來找mysqld獲取數據時,是先找到mysqld_exporter,然后mysqld_exporter再去mysqld獲取數據。所以要保證mysqld_exporter的ip可以連接mysqld服務。
(3).在mysqld_exporter組件中配置mysql信息
創建一個保存mysql用戶名密碼的文件:vim mysqld_exporter/.my.cnf
[client] user=promethues password=promethues1
(4).啟動mysqld_exporter組件,配置promethues.yml,並指定mysql賬號信息文件
## 可以查看一些啟動信息 ./mysqld_exporter --help ## 啟動,指定端口號,默認9104,指定連接mysql的用戶文件 nohup ./mysqld_exporter --web.listen-address=":9104" --config.my-cnf=".my.cnf" &> /dev/null &
## 添加以下配置:vim prometheus.yml - job_name: 'mysqld' static_configs: - targets: ['localhost:9104']
(5).並在Grafana添加mysqld模板
然后還是前往Grafana查找自己喜歡的模板:

以上只是簡單演示了兩個比較常用的插件,普羅米修斯官方還有許多插件可供使用。大家可慢慢研究。下文也將介紹Prometheus監控K8S集群的手段。
