1.普羅米修斯概述
Prometheus(由go語言(golang)開發)是一套開源的監控&報警&時間序列數 據庫的組合。適合監控docker容器。因為kubernetes(俗稱k8s)的流行帶動 了prometheus的發展。
2.時間序列數據
2.1什么是序列數據
時間序列數據(TimeSeries Data) : 按照時間順序記錄系統、設備狀態變化 的數據被稱為時序數據。
應用的場景很多, 如:
- 無人駕駛車輛運行中要記錄的經度,緯度,速度,方向,旁邊物體的距 離等等。每時每刻都要將數據記錄下來做分析。
- 某一個地區的各車輛的行駛軌跡數據
- 傳統證券行業實時交易數據
- 實時運維監控數據等
2.2時間序列數據特點
- 性能好
關系型數據庫對於大規模數據的處理性能糟糕。NOSQL可以比較好的處理 大規模數據,讓依然比不上時間序列數據庫。 - 存儲成本低
高效的壓縮算法,節省存儲空間,有效降低IO
Prometheus有着非常高效的時間序列數據存儲方法,每個采樣數據僅僅占 用3.5byte左右空間,上百萬條時間序列,30秒間隔,保留60天,大概花了 200多G(來自官方數據)
2.3Prometheus的主要特征
多維度數據模型 靈活的查詢語言 不依賴分布式存儲,單個服務器節點是自主的 以HTTP方式,通過pull模型拉去時間序列數據 也可以通過中間網關支持push模型 通過服務發現或者靜態配置,來發現目標服務對象 支持多種多樣的圖表和界面展示
2.4普羅米修斯原理架構圖
3.Prometheus搭建
服務器|ip地址
---|--|--
Prometneus服務器|192.168.134.131
被監控服務器|192.168.134.132
grafana服務器|192.168.134.133
3.1安裝prometheus
從 https://prometheus.io/download/ 下載相應版本,上傳到服務器上解壓安裝
[root@server ~]# ls
anaconda-ks.cfg prometheus-2.27.1.linux-amd64.tar.gz
[root@server ~]# tar xf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/
[root@server ~]# ls /usr/local/prometheus-2.27.1.linux-amd64/
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
[root@server ~]# mv /usr/local/prometheus-2.27.1.linux-amd64/ /usr/local/prometheus
直接使用默認配置文件啟動並確認端口9090
[root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[1] 18848
[root@server ~]# level=info ts=2021-06-02T09:22:01.560Z caller=main.go:388 msg="No time or size retention was set so using the default time retention" duration=15d
level=info ts=2021-06-02T09:22:01.560Z caller=main.go:426 msg="Starting Prometheus" version="(version=2.27.1, branch=HEAD, revision=db7f0bcec27bd8aeebad6b08ac849516efa9ae02)"
level=info ts=2021-06-02T09:22:01.560Z caller=main.go:431 build_context="(go=go1.16.4, user=root@fd804fbd4f25, date=20210518-14:17:54)"
level=info ts=2021-06-02T09:22:01.560Z caller=main.go:432 host_details="(Linux 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 server (none))"
level=info ts=2021-06-02T09:22:01.560Z caller=main.go:433 fd_limits="(soft=1024, hard=4096)"
level=info ts=2021-06-02T09:22:01.560Z caller=main.go:434 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2021-06-02T09:22:01.561Z caller=web.go:540 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2021-06-02T09:22:01.562Z caller=main.go:803 msg="Starting TSDB ..."
level=info ts=2021-06-02T09:22:01.562Z caller=tls_config.go:191 component=web msg="TLS is disabled." http2=false
level=info ts=2021-06-02T09:22:01.577Z caller=head.go:741 component=tsdb msg="Replaying on-disk memory mappable chunks if any"
level=info ts=2021-06-02T09:22:01.577Z caller=head.go:755 component=tsdb msg="On-disk memory mappable chunks replay completed" duration=3.827µs
level=info ts=2021-06-02T09:22:01.577Z caller=head.go:761 component=tsdb msg="Replaying WAL, this may take a while"
level=info ts=2021-06-02T09:22:01.578Z caller=head.go:813 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2021-06-02T09:22:01.578Z caller=head.go:818 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=11.651µs wal_replay_duration=945.393µs total_replay_duration=970.961µs
level=info ts=2021-06-02T09:22:01.578Z caller=main.go:828 fs_type=XFS_SUPER_MAGIC
level=info ts=2021-06-02T09:22:01.578Z caller=main.go:831 msg="TSDB started"
level=info ts=2021-06-02T09:22:01.578Z caller=main.go:957 msg="Loading configuration file" filename=/usr/local/prometheus/prometheus.yml
level=info ts=2021-06-02T09:22:11.588Z caller=main.go:988 msg="Completed loading of configuration file" filename=/usr/local/prometheus/prometheus.yml totalDuration=10.010120516s remote_storage=3.777µs web_handler=421ns query_engine=702ns scrape=10.009801277s scrape_sd=50.014µs notify=28.613µs notify_sd=8.716µs rules=3.397µs
level=info ts=2021-06-02T09:22:11.588Z caller=main.go:775 msg="Server is ready to receive web requests."
[root@server ~]# ss -antlp|grep 9090
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=18848,fd=7))
3.2prometheus界面
通過瀏覽器訪問http://服務器IP:9090就可以訪問到prometheus的主界面
默認只監控了本機一台,點Status --》點Targets --》可以看到只監控了本機
3.3主機數據展示
通過http://服務器IP:9090/metrics可以查看到監控的數據
在web主界面可以通過關鍵字查詢監控項
3.4監控遠程Linux主機
在遠程linux主機(被監控端agent)上安裝node_exporter組件
下載地址: https://prometheus.io/download/
[root@agent ~]# ls
anaconda-ks.cfg node_exporter-1.1.2.linux-amd64.tar.gz
[root@agent ~]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@agent ~]# mv /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter
里面就一個啟動命令node_exporter,可以直接使用此命令啟動,確認端口9100
[root@agent ~]# nohup /usr/local/node_exporter/node_exporter &
[1] 51186
[root@agent ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@agent ~]# ss -antlp|grep 9100
LISTEN 0 128 :::9100 :::* users:(("node_exporter",pid=51186,fd=3))
通過瀏覽器訪問http://被監控端IP:9100/metrics就可以查看到 node_exporter在被監控端收集的監控信息
回到prometheus服務器的配置文件里添加被監控機器的配置段
在主配置文件最后加上下面三行
[root@server ~]# tail /usr/local/prometheus/prometheus.yml
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'agent' # 取一個job名稱來代 表被監控的機器
static_configs:
- targets: ['192.168.134.132:9100'] # 這里改成被監控機器 的IP,后面端口接9100
改完配置文件后,重啟服務
[root@server ~]# pkill prometheus
[root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@server ~]# ss -antlp|grep 9090
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=51287,fd=8))
回到web管理界面 --》點Status --》點Targets --》可以看到多了一台監 控目標
4.Promethus監控Mysql數據庫
在被管理機agent上安裝mysqld_exporter組件
下載地址: https://prometheus.io/download/
[root@agent ~]# ls
anaconda-ks.cfg mysqld_exporter-0.13.0.linux-amd64.tar.gz node_exporter-1.1.2.linux-amd64.tar.gz nohup.out
安裝mysqld_exporter組件
[root@agent ~]# tar xf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
[root@agent ~]# mv /usr/local/mysqld_exporter-0.13.0.linux-amd64/ /usr/local/mysqld_exporter
[root@agent ~]# ls /usr/local/mysqld_exporter/
LICENSE mysqld_exporter NOTICE
安裝mariadb數據庫,並授權
[root@agent ~]# yum install mariadb\* -y
[root@agent ~]# systemctl enable --now mariadb
[root@agent ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
(注意:授權ip為localhost,因為不是prometheus服務器來直接找mariadb 獲取數據,而是prometheus服務器找mysql_exporter,mysql_exporter 再找mariadb。所以這個localhost是指的mysql_exporter的IP)
創建一個mariadb配置文件,寫上連接的用戶名與密碼(和上面的授權的用戶名 和密碼要對應)
[root@agent ~]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=123
啟動mysqld_exporter,確認端口(9104)
[root@agent ~]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
[root@agent ~]# ss -antlp|grep 9104
LISTEN 0 128 :::9104 :::* users:(("mysqld_exporter",pid=51787,fd=3))
回到prometheus服務器的配置文件里添加被監控的mariadb的配置段
[root@server ~]# tail /usr/local/prometheus/prometheus.yml
- targets: ['localhost:9090']
- job_name: 'agent'
static_configs:
- targets: ['192.168.134.132:9100']
- job_name: 'mariadb'
static_configs:
- targets: ['192.168.116.132:9104']
改完配置文件后,重啟服務
[root@server ~]# pkill prometheus
[root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
回到web管理界面 --》點Status --》點Targets --》可以看到監控 mariadb了
5.Promethus安裝Grafana可視化圖形工具
5.1什么是Grafana
Grafana是一個開源的度量分析和可視化工具,可以通過將采集的數據分 析,查詢,然后進行可視化的展示,並能實現報警。
網址: https://grafana.com/
5.2使用Grafana連接Prometheus
在grafana服務器上安裝grafana
下載地址:https://grafana.com/grafana/download
[root@grafana ~]# ls
anaconda-ks.cfg grafana-5.4.2-1.x86_64.rpm
[root@grafana ~]# yum -y install grafana-5.4.2-1.x86_64.rpm
啟動服務,確認端口(3000)
[root@grafana ~]# systemctl enable --now grafana-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
[root@grafana ~]# ss -antlp|grep 3000
LISTEN 0 128 :::3000 :::* users:(("grafana-server",pid=49313,fd=6))
通過瀏覽器訪問 http:// grafana服務器IP:3000就到了登錄界面,使用默 認的admin用戶,admin密碼就可以登陸了
第一次登錄會強制更改密碼
我們把prometheus服務器收集的數據做為一個數據源添加到 grafana,讓grafana可以得到prometheus的數據。
然后為添加好的數據源做圖形顯示
保存
最后在dashboard可以查看到
6.Promethus的Grafana圖形顯示MySQL監控數據
6.1在grafana上修改配置文件,並下載安裝mysql監控的dashboard(包含 相關json文件,這些json文件可以看作是開發人員開發的一個監控模板)
參考網址: https://github.com/percona/grafana-dashboards
在grafana配置文件里最后加上以下三行
[root@grafana ~]# tail -3 /etc/grafana/grafana.ini
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
下載配置模板
cd /var/lib/grafana/
git clone https://github.com/percona/grafana-dashboards.git
cp -r /var/lib/grafana/grafana-dashboards-master/dashboards/ /var/lib/grafana/
[root@grafana grafana]# ls
dashboards grafana.db plugins png sessions
[root@grafana grafana]# ls dashboards/
Advanced_Data_Exploration.json MySQL_InnoDB_Compression_Details.json Node_Summary.json
CPU_Utilization_Details.json MySQL_InnoDB_Details.json Node_Temperature_Details.json
Disk_Details.json MySQL_Instances_Compare.json NUMA_Details.json
HAProxy_Instance_Summary.json MySQL_Instances_Overview.json pmm-qan.json
Home_Dashboard.json MySQL_Instance_Summary.json PostgreSQL_Instances_Compare.json
Memory_Details.json MySQL_MyISAM_Aria_Details.json PostgreSQL_Instances_Overview.json
MongoDB_Cluster_Summary.json MySQL_MyRocks_Details.json PostgreSQL_Instance_Summary.json
MongoDB_InMemory_Details.json MySQL_Performance_Schema_Details.json Processes_Details.json
MongoDB_Instances_Compare.json MySQL_Query_Response_Time_Details.json Prometheus_Exporters_Overview.json
MongoDB_Instances_Overview.json MySQL_Replication_Summary.json Prometheus_Exporter_Status.json
MongoDB_Instance_Summary.json MySQL_Table_Details.json ProxySQL_Instance_Summary.json
MongoDB_MMAPv1_Details.json MySQL_TokuDB_Details.json PXC_Galera_Cluster_Summary.json
MongoDB_ReplSet_Summary.json MySQL_User_Details.json PXC_Galera_Nodes_Compare.json
MongoDB_WiredTiger_Details.json MySQL_Wait_Event_Analyses_Details.json PXC_Galera_Node_Summary.json
MySQL_Amazon_Aurora_Details.json Network_Details.json VictoriaMetrics_Agents_Overview.json
MySQL_Command_Handler_Counters_Compare.json Nodes_Compare.json VictoriaMetrics.json
MySQL_Group_Replication_Summary.json Nodes_Overview.json
重啟grafana服務
[root@grafana grafana]# systemctl restart grafana-server.service
6.2在grafana圖形界面導入相關json文件
7.Promethus的Grafana+onealert實現報警功能
7.1先在onealert里添加grafana應用(申請onealert賬號)
7.2在Grafana中配置Webhook URL
在Grafana中創建Notification channel,選擇類型為Webhook
現在可以去設置一個報警來測試了(這里以我們前面加的cpu負載監控來做測試)
模擬cpu負載
cat /dev/urandom | md5sum