Prometheus概述
Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社會也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。
https://prometheus.io
https://github.com/prometheus
Prometheus 特點:
• 多維數據模型:由度量名稱和鍵值對標識的時間序列數據
• PromSQL:一種靈活的查詢語言,可以利用多維數據完成復雜的查詢
• 不依賴分布式存儲,單個服務器節點可直接工作
• 基於HTTP的pull方式采集時間序列數據
• 推送時間序列數據通過PushGateway組件支持
• 通過服務發現或靜態配置發現目標
• 多種圖形模式及儀表盤支持(grafana)
Prometheus 組成及架構:
• Prometheus Server:收集指標和存儲時間序列數據,並提供查詢接口
• ClientLibrary:客戶端庫
• Push Gateway:短期存儲指標數據。主要用於臨時性的任務
• Exporters:采集已有的第三方服務監控指標並暴露metrics
• Alertmanager:告警
• Web UI:簡單的Web控制
Prometheus 部署
二進制部署:https://prometheus.io/docs/prometheus/latest/getting_started/
Docker部署:https://prometheus.io/docs/prometheus/latest/installation/
以下部署均在兩台機器上:
主機 |
IP地址 |
軟件 |
master |
192.168.1.128 |
prometheus+grafananode_exporter |
node |
192.168.1.129 |
node_exportermysql |
• 安裝Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz tar -xf prometheus-2.17.1.linux-amd64.tar.gz -C /usr/local/ cd /usr/local/ && mv prometheus-2.17.1.linux-amd64 prometheus && cd prometheus/
cat /usr/lib/systemd/system/prometheus.service [Unit] Description=https://prometheus.io/ [Service] Restart=on-failure ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml [Install] WantedBy=multi-user.target
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
netstat -lntp | grep prometheus
• 安裝node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz tar -xf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local cd /usr/local && mv node_exporter-0.18.1.linux-amd64 node_exporter cat /usr/lib/systemd/system/node_exporter.service [Unit] Description=Open node_exporter server daemon [Service] Restart=on-failure ExecStart=/usr/local/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service [Install] WantedBy=multi-user.target systemctl daemon-reload systemctl start node_exporter
systemctl enable node_exporter
ps -ef | grep node_exporter
• 安裝mysqld_exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/ cd /usr/local/ && mv mysqld_exporter-0.12.1.linux-amd64/ mysqld_exporter && cd mysqld_exporter/ mysqld_exporter需要連接到Mysql,所以需要Mysql的權限,我們先為它創建用戶並賦予所需的權限. mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY '123456'; mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost'; 創建.my.cnf文件 vim /usr/local/mysqld_exporter/.my.cnf [client] user=exporter password=123456 啟動服務 cd /usr/local/mysqld_exporter ./mysqld_exporter -config.my-cnf=.my.cnf & #啟動mysqld_exporter並后台運行
• 修改/添加配置文件prometheus.yml
vim prometheus.yml
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'. file_sd_configs: #動態發現 - targets: ['localhost:9090'] labels: instance: prometheus
refresh_interval: 5s #5秒加載一次
- job_name: 'master'
file_sd_configs:
- targets: ['192.168.1.128:9100'] #node_exporter安裝在本地,如安裝在其他機器使用IP即可,9100是node_exporter的端口號
labels:
instance: master_pro #名稱自定義,最好具有代表性
refresh_interval: 5s
- job_name: 'node' file_sd_configs: - targets: ['192.168.1.129:9100'] #node_exporter安裝在本地,如安裝在其他機器使用IP即可,9100是node_exporter的端口號 labels: instance: node1 #名稱自定義,最好具有代表性 refresh_interval: 5s
- job_name: 'mysql'
static_configs: #靜態發現
- targets: ['192.168.1.129:9104']
labels:
instance: db1
•檢查prometheus.yml配置是否有效
[root@k8s-master prometheus]# pwd /usr/local/prometheus
[root@k8s-master prometheus]# ./promtool check config prometheus.yml Checking prometheus.yml SUCCESS: 0 rule files found
[root@k8s-master prometheus]# ps -ef | grep prometheus
root 8740 1 0 4月09 ? 00:03:34 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml
root 12856 12012 0 16:45 pts/0 00:00:00 grep --color=auto prometheus
[root@k8s-master prometheus]# kill -hup 8740
•檢查prometheus.yml配置是否有效
安裝完成后,查看一下prometheus是否識別到相應監控,訪問
http://localhost:9090/targets Status->Targets頁面,如果可以看到Target的狀態已經變成UP,就是成功
Grafana部署
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-6.7.2-1.x86_64.rpm yum install grafana-enterprise-6.7.2-1.x86_64.rpm systemctl start grafana-server.service systemctl enable grafana-server.service
netstat -lntp | grep grafana-server
•添加data sources,點擊添加選擇prometheus即可
•添加配置信息,寫入prometheus的URL,點擊“Save&Test”提示綠色成功
配置grafana-node_exporter儀表版
•導入Prometheus儀表盤,import-dashboards
https://grafana.com/grafana/download
https://grafana.com/dashboards/8919
•進入儀表板查看
•導入grafana-mysqld_exporter儀表版,同上述方式一樣。
mysql_exporter:用於收集MySQL性能信息。
https://grafana.com/dashboards/7362
https://github.com/prometheus/mysqld_exporter
Altermanager監控告警
地址1:https://prometheus.io/download/
地址2:https://github.com/prometheus/alertmanager/releases
實現prometheus的告警,需要通過altermanager這個組件;在prometheus服務端寫告警規則,在altermanager組件配置郵箱
Alertmanager與Prometheus是相互分離的兩個組件。Prometheus服務器根據報警規則將警報發送給Alertmanager,然后Alertmanager將silencing、inhibition、aggregation等消息通過電子郵件、dingtalk和HipChat發送通知。
Alertmanager處理由例如Prometheus服務器等客戶端發來的警報。它負責刪除重復數據、分組,並將警報通過路由發送到正確的接收器,比如電子郵件、Slack、dingtalk等。Alertmanager還支持groups,silencing和警報抑制的機制。
-
安裝altermanager
wget https://github.com/prometheus/alertmanager/releases/download/v0.20.0/alertmanager-0.20.0.linux-amd64.tar.gz # 下載altermanager tar xvf alertmanager-0.20.0.linux-amd64.tar.gz -C /usr/local/ #解壓至指定文件夾 cd /usr/local/ && mv alertmanager-0.20.0.linux-amd64 alertmanager cd alertmanager/
-
編輯alertmanager.yml配置文件
smtp_auth_password填寫的是第三方登錄 郵箱的授權碼,非 郵箱 賬戶登錄密碼。
-
修改prometheus配置文件
vim /usr/local/prometheus/prometheus.yml
-
啟動alertmanager
./amtool check-config alertmanager.yml #檢查配置是否生效 ./alertmanager --config.file=alertmanager.yml & #根據配置文件啟動,后台運行
重啟prometheus
systemctl start prometheus
-
訪問http://loalhost:9090/alerts ,即可查看規則
-
查看報警郵件
更多更參考:
https://blog.csdn.net/liukuan73/article/details/78881008 、
https://blog.csdn.net/aixiaoyang168/article/details/98474494