前言
环境:centos7.9
下载Prometheus 官方软件
Prometheus 官方提供了预编译的二进制文件和Docker 映像,这里我们下载预编译的二进制包即可,预编译的二进制包下载解压就可以直接使用,不需要编译安装。
Prometheus 官方网站:https://prometheus.io/
下载prometheus的包:https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
环境准备
准备3台服务器,都能上外网,IP与主机名如下所示:
主机IP | 主机名 |
---|---|
192.168.118.128 | server |
192.168.118.129 | agent1 |
192.168.118.130 | grafana |
[root@server ~]# systemctl restart ntpd #同步时间,3台都做同样的操作
[root@server ~]# systemctl enable ntpd
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
安装prometheus
下面来安装prometheus服务端,将下载好的软件上传到服务器,开始安装:
[root@nginx /]# cd prometheus_soft/
[root@nginx prometheus_soft]# ll
total 35296
-rw-r--r-- 1 root root 36142430 Oct 13 02:36 prometheus-2.5.0.linux-amd64.tar.gz
[root@nginx prometheus_soft]# tar -zxvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local/ #解压软件包
prometheus-2.5.0.linux-amd64/
prometheus-2.5.0.linux-amd64/NOTICE
prometheus-2.5.0.linux-amd64/prometheus
prometheus-2.5.0.linux-amd64/console_libraries/
prometheus-2.5.0.linux-amd64/console_libraries/menu.lib
prometheus-2.5.0.linux-amd64/console_libraries/prom.lib
prometheus-2.5.0.linux-amd64/LICENSE
prometheus-2.5.0.linux-amd64/promtool
prometheus-2.5.0.linux-amd64/consoles/
prometheus-2.5.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.5.0.linux-amd64/consoles/prometheus.html
prometheus-2.5.0.linux-amd64/consoles/node-cpu.html
prometheus-2.5.0.linux-amd64/consoles/node-disk.html
prometheus-2.5.0.linux-amd64/consoles/index.html.example
prometheus-2.5.0.linux-amd64/consoles/node.html
prometheus-2.5.0.linux-amd64/consoles/node-overview.html
prometheus-2.5.0.linux-amd64/prometheus.yml
[root@nginx local]# cd /usr/local/prometheus-2.5.0.linux-amd64/
[root@nginx prometheus-2.5.0.linux-amd64]# ll #安装完成,由于prometheus提供的是二进制包,所以解压即可,都不需要编译安装
total 94936
drwxr-xr-x 2 3434 3434 38 Nov 6 2018 console_libraries
drwxr-xr-x 2 3434 3434 173 Nov 6 2018 consoles
-rw-r--r-- 1 3434 3434 11357 Nov 6 2018 LICENSE
-rw-r--r-- 1 3434 3434 2769 Nov 6 2018 NOTICE
-rwxr-xr-x 1 3434 3434 58074162 Nov 6 2018 prometheus
-rw-r--r-- 1 3434 3434 926 Nov 6 2018 prometheus.yml
-rwxr-xr-x 1 3434 3434 39113694 Nov 6 2018 promtool
[root@nginx prometheus-2.5.0.linux-amd64]#
启动prometheus
启动prometheus 很简单,直接命令启动即可,默认加载配置文件就是当前目录的prometheus.yml配置文件,默认开启端口是9090,,如下所示:
[root@server ~]#cd /usr/local/prometheus-2.5.0.linux-amd64 #切换目录
[root@server prometheus-2.5.0.linux-amd64]# ./prometheus --help #查看帮助文档
[root@server prometheus-2.5.0.linux-amd64]# ./prometheus & #启动普罗米修斯,后台运行
[root@server prometheus-2.5.0.linux-amd64]# lsof -i:9090 #查看端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
prometheu 6000 root 6u IPv6 44690 0t0 TCP *:websm (LISTEN)
prometheu 6000 root 7u IPv4 44692 0t0 TCP www.iflytek.com:58698->server:websm (ESTABLISHED)
prometheu 6000 root 8u IPv6 46053 0t0 TCP www.iflytek.com:websm->server:58698 (ESTABLISHED)
[root@server prometheus-2.5.0.linux-amd64]#
web网页访问普罗米修斯
以上,我们发现,需要出入查询语句(普罗米修斯有自己的查询语句),然后点击执行就能得到图形或控制台 输出结果。
client安装node_exporter
Prometheus的监控不同的指标需要不同的exporter包,如监控Linux的CPU负载等性能指标,就需要安装官网的node_exporter-0.16.0.linux-amd64.tar.gz包,监控mysql服务器就需要安装mysqld_exporter包。
下面开始安装node_exporter-0.16.0.linux-amd64.tar.gz包,来演示如何在客户端安装exporter,并让普罗米修斯服务端来监控客户端:
#自行从官网下载node_exporter包并上传到被监控的客户端上
[root@client1 ~]# tar xf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/ #在客户端解压node_exporter包
[root@client1 ~]# cd /usr/local/node_exporter-0.16.0.linux-amd64 #切换目录
[root@node3 node_exporter-0.16.0.linux-amd64]# nohup ./node_exporter & #启动node_exporter,后台运行
[root@node3 node_exporter-0.16.0.linux-amd64]# lsof -i:9100 #默认启动的端口是9100
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node_expo 15046 root 3u IPv6 450117 0t0 TCP *:jetdirect (LISTEN)
[root@node3 node_exporter-0.16.0.linux-amd64]#
Prometheus服务端监控客户端
客户端的node_exporter已经安装完毕,下面来配置prometheus服务端,,如下所示:
[root@server ~]# cd /usr/local/prometheus-2.5.0.linux-amd64 #切换目录
[root@nginx prometheus-2.5.0.linux-amd64]# 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']
- job_name: 'client1' #复制一个job_name的标签,client1是job_name,可自定义
static_configs:
- targets: ['192.168.118.135:9100'] #填写客户端的IP和node_export的端口
#注意:普罗米修斯的配置文件时yml格式的,yml语法标签对齐,空格等都要非常严格的语法
[root@nginx prometheus-2.5.0.linux-amd64]# kill -9 34937 #关闭prometheus服务
[root@nginx prometheus-2.5.0.linux-amd64]# ./prometheus & #重启prometheus服务
查看普罗米修斯能否正常监控客户端:
查看客户端的1分钟负载: