认识普罗米修斯
1、prometheus介绍
Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合, 由go语言开发。
适合监控容器平台, 因为kubernetes(俗称k8s)的流行带动了prometheus的发展。
PS:文档使用prometheus监控物理服务器。
prometheus的官方网站: https://prometheus.io/
2、时序数据库介绍
数据库分类:
- 关系型:mysql,oracle,sql server,sybase,db2,access等
- 非关系型(nosql):
- key-value:memcache redis etcd
- 文档型:mongodb elasticsearch
- 列式:hbase
- 时序:prometheus
- 图形数据库:Neo4j
时间序列数据(TimeSeries Data):按照时间顺序记录系统、设备状态变化的数据被称为时序数据。
3、时序数据主要的特点
-
- 数据带有时间属性,且数据量随着时间递增;
- 大都为插入操作较多且无更新的需求,插入数据多,每秒钟插入数据可到达千万甚至是上亿条分析过去时序数据可以做成多纬度报表,揭示其趋势性、规律性、异常性;
- 分析时序数据趋势可以做大数据分析,机器学习,实现预测和预警;
- 能够按照条件筛选数据, 也可以按照时间范围统计,聚合,展示数据。
4、常见应用场景
- 无人驾驶车辆运行中要记录的经度,纬度,速度,方向,旁边物体的距离等等。每时每刻都要将数据记录下来做分析;
- 某一个地区的各车辆的行驶轨迹数据;
- 传统证券行业实时交易数据;
- 实时运维监控数据等。
prometheus主要特性
1、Prometheus的主要特性有
1. 多维度数据模型
2. 灵活的查询语言
3. 不依赖分布式存储,单个服务器节点是自主的
4. 以HTTP方式,通过pull模型拉去时间序列数据
5. 也可以通过中间网关支持push模型
6. 通过服务发现或者静态配置, 来发现目标服务对象
7. 支持多种多样的图表和界面展示
2、pormetheus原理架构图
prometheus监控
1、实验环境准备
需要准备三台主机,修改其主机名,互绑IP
server,agent1,grafana
- [root@server ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.100.50 server.example.com server
- 192.168.100.10 agent.example.com agent
- 192.168.100.50 grafana.example.com grafana
-
- [root@server ~]# scp /etc/hosts root@192.168.100.10:/etc/hosts
- [root@server ~]# scp /etc/hosts root@192.168.100.30:/etc/hosts
复制代码
2、安装prometheus
下载地址:https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)
- [root@server ~]# tar xf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/
- [root@server ~]# mv /usr/local/prometheus-2.5.0.linux-amd64/ /usr/local/prometheus
复制代码
3、配置文件说明
- [root@server ~]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
- 2:global: # 全局配置段
- 3: scrape_interval: 15s # 每15s抓取(采集)数据一次
- 4: evaluation_interval: 15s # 每15秒计算一次规则
- 8:alerting: # Alertmanager报警相关
- 9: alertmanagers:
- 10: - static_configs:
- 11: - targets:
- 12:
- 15:rule_files: # 规则文件列表
- 19:
- 21:scrape_configs: # 抓取的配置文件(也就是监控的实例) 23: - job_name: 'prometheus' # 监控的实例名称
- 28: static_configs:
- 29: - targets: ['localhost:9090'] # 监控的实例IP与端口,在这里为监控服务器本身
复制代码
4、直接使用默认配置文件启动, 建议加&后台符号,验证9090端口
- [root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
- [root@server ~]# netstat -ntlup |grep :9090
- tcp6 0 0 :::9090 :::* LISTEN 64950/prometheus
复制代码
prometheus界面
通过浏览器访问http://服务器IP:9090就可以访问到prometheus的主界面
点Status --》点Targets --》可以看到只监控了本机 (默认只监控了本机一台)
通过http://服务器IP:9090/metrics可以查看到监控的数据
说明:这里的metrics你可以类比成zabbix里的监控项
在web主界面可以通过关键字查询metrics, 并显示图形
虽然prometheus服务器通过9090端口能监控一些metrics,但像cpu负载等这些linux常见的监控项却没有,需要node_exporter组件;node_exporter组件可以安装在本机或远程linux主机上。
监控远程linux主机
在远程linux主机(被监控端agent1)上安装node_exporter组件
下载地址: https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)
安装node_exporter组件
- [root@agent ~]# ls
- anaconda-ks.cfg node_exporter-1.8.2.linux-amd64.tar.gz
- [root@agent ~]# tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/
-
- [root@agent ~]# cd /usr/local/
- [root@agent local]# ls
- bin etc games include lib lib64 libexec node_exporter-1.8.2.linux-amd64 sbin share src
- [root@agent local]# mv node_exporter-1.8.2.linux-amd64/ node_exporter
复制代码
启动node_exporter, 并验证端口
- [root@agent local]# nohup /usr/local/node_exporter/node_exporter &
- nohup命令: 如果把启动node_exporter的终端给关闭,那么进程也可能会随之关闭。nohup命令可以挂
- 起在后台,除非杀掉相关进程,否则不会随终端关闭而关闭进程。
- 查看端口9100是否启动
- [root@agent local]# ss -anlt
- State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
- LISTEN 0 4096 *:9100 *:*
- LISTEN 0 128 [::]:22 [::]:*
- [root@agent local]#
复制代码
浏览器访问http://被监控端IP:9100/metrics就可以查看到node_exporter在被监控端收集的 metrics
回到prometheus服务器的配置文件里添加被监控机器的配置段
- [root@server ~]# cd /usr/local/prometheus/
- [root@server prometheus]# ls
- console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
- [root@server prometheus]# vim prometheus.yml
-
- - job_name: "agent"
- static_configs:
- - targets: ["192.168.100.10:9100"]
-
- [root@server prometheus]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
- 2:global:
- 3: scrape_interval: 15s
- 4: evaluation_interval: 15s
- 8:alerting:
- 9: alertmanagers:
- 10: - static_configs:
- 11: - targets:
- 12:
- 15:rule_files:
- 19:
- 21:scrape_configs:
- 23: - job_name: "prometheus"
- 28: static_configs:
- 29: - targets: ["localhost:9090"]
- 30: - job_name: "agent" # 最后加上这三行,取一个job名称来代表被监控的机器
- 31: static_configs:
- 32: - targets: ["192.168.100.10:9100"] # 这里改成被监控机器的IP,后面端口接9100
复制代码
改完配置文件后,重启服务
说明: 没有服务脚本,直接kill杀掉进程,再重启即可。(或者kill -HUP PID)
- [root@server ~]# pkill prometheus
-
- [root@server ~]# ss -anlt
- State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
- LISTEN 0 128 [::]:22 [::]:*
- # 确认端口没有进程占用
复制代码
回到web管理界面 --》点Status --》点Targets --》可以看到多了一台监控目标
前面实现了prometheus监控本机9090, 但是还有很多metrics无法监控,比如cpu负载信息等。这个时候我们在prometheus服务器上也安装node_exporter,并监控。安装方法与前面一样。
- [root@server ~]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
- 2:global:
- 3: scrape_interval: 15s
- 4: evaluation_interval: 15s
- 8:alerting:
- 9: alertmanagers:
- 10: - static_configs:
- 11: - targets:
- 12:
- 15:rule_files:
- 19:
- 21:scrape_configs:
- 23: - job_name: "prometheus"
- 28: static_configs:
- 29: - targets: ["localhost:9090"]
- 30: - job_name: "agent"
- 31: static_configs:
- 32: - targets: ["192.168.100.10:9100"]
- 33: - job_name: "server" // 其他不变,添加这三行
- 34: static_configs:
- 35: - targets: ["192.168.100.50:9100"]
-
- 启动后查看端口
- [root@server ~]# ss -anlt
- State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
- LISTEN 0 4096 *:9090 *:*
- LISTEN 0 4096 *:9100 *:*
- LISTEN 0 128 [::]:22
-
- 启动成功
复制代码
启动prometheus,先要杀死进程
- [root@server ~]# pkill prometheus
- [root@server ~]# /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
- 启动后查看端口
- [root@server ~]# ss -anlt
- State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
- LISTEN 0 4096 *:9090 *:*
- LISTEN 0 4096 *:9100 *:*
- LISTEN 0 128 [::]:22 [::]:*
- [root@server ~]#
- 启动成功
复制代码
监控远程mysql
在被管理机agent上安装mysqld_exporter组件
下载地址: https://prometheus.io/download/ (请使用共享的软件版本,以免出现不兼容问题)
- [root@agent ~]# ls
- anaconda-ks.cfg mysqld_exporter-0.15.1.linux-amd64.tar.gz node_exporter-1.8.2.linux-amd64.tar.gz nohup.out
- [root@agent ~]#
- [root@agent ~]# tar -zxvf mysqld_exporter-0.15.1.linux-amd64.tar.gz -C /usr/local/ //解压
- mysqld_exporter-0.15.1.linux-amd64/
- mysqld_exporter-0.15.1.linux-amd64/LICENSE
- mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter
- mysqld_exporter-0.15.1.linux-amd64/NOTICE
- [root@agent ~]# mv /usr/local/mysqld_exporter-0.15.1.linux-amd64/ /usr/local/mysqld_exporter
- [root@agent ~]#
复制代码
在agent上安装mariadb并启动,用于被监控
- [root@agent ~]# yum install mariadb-server -y
- [root@agent ~]# systemctl restart mariadb
- [root@agent ~]# systemctl enable mariadb
复制代码
授权
说明: 授权ip为localhost,因为不是prometheus服务器来直接找mariadb获取数据,而是prometheus 服务器找mysqld_exporter,mysqld_exporter再找mariadb。所以这个localhost是指的mysql_exporter 的IP
- MariaDB [(none)]> grant all ON *.* to 'mysql_monitor'@'localhost' identified by 'linux';
- Query OK, 0 rows affected (0.001 sec)
-
- MariaDB [(none)]> flush privileges;
- Query OK, 0 rows affected (0.000 sec)
-
- MariaDB [(none)]>
复制代码
创建连接mariadb配置文件
- [root@agent ~]# vim /usr/local/mysqld_exporter/.my.cnf
- [root@agent ~]# cat /usr/local/mysqld_exporter/.my.cnf
- [client]
- user=mysql_monitor
- password=linux
复制代码
启动mysqld_exporter并验证9104端口
- [root@agent ~]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
- [2] 2758
- [root@agent ~]# nohup: ignoring input and appending output to 'nohup.out'
-
- [root@agent ~]# ss -anlt
- State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
- LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
- LISTEN 0 4096 *:9100 *:*
- LISTEN 0 4096 *:9104 *:*
- LISTEN 0 128 [::]:22 [::]:*
- LISTEN 0 80 *:3306 *:*
复制代码
回到prometheus服务器的配置文件里添加被监控的mariadb的配置段
- [root@server ~]# vim /usr/local/prometheus/prometheus.yml
- /// 最后一行加上这个内容
- - job_name: "agent-mariadb"
- static_configs:
- - targets: ["192.168.100.10:9104"] /被监控的主机的IP,后面接端口9104
复制代码
重启服务
- [root@server ~]# pkill prometheus
- [root@server ~]# ss -anlt | grep 9090
- [root@server ~]# /usr/local/prometheus/prometheus --
- config.file="/usr/local/prometheus/prometheus.yml" &
- [root@server ~]# ss -anlt |grep 9090
- tcp6 0 0 :::9090 :::* LISTEN 76661/prometheus
复制代码
回到web管理界面 --》点Status --》点Targets --》可以看到监控mariadb了
可以查看mariadb连接数
|