一、服务端配置
官方下载地址:https://prometheus.io/download/
前提:注意服务器之间已经同步时间,否则会出现dashborad获取不到数据。
#ref:https://github.com/prometheus
#1、下载promethues软件
mkdir /usr/local/monitor -p && cd /usr/local/monitor
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*
#2、Configuring Prometheus to monitor itself
配置配置文件监控自己
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
#3、通过systemctl管理Prometheus
#增加系统管理软件文件
vim/etc/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/monitor/prometheus-2.27.1.linux-amd64/prometheus --config.file=/usr/local/monitor/prometheus-2.27.1.linux-amd64/prometheus.yml
Rastart=on-failure
[Install]
WantedBy=multi-user.target
#保存退出后,重新加载配置,启动服务即可
chmod a+x prometheus.service
systemctl daemon-reload
systemctl restart prometheus.service
systemctl enable prometheus.service
#查看服务是否启动
systemctl status prometheus.service
#当然还可以通过下面的方式启动服务
nohup ./prometheus --config.file=prometheus.yml &
#4、访问web页面
http://localhost:9090
#查看监控规则
localhost:9090/metrics
二、客户端配置
官方软件下载地址:https://prometheus.io/download/
node_exporter主要监控主机的CPU,内存等硬件资源信息。
#1、下载node_exporter
mkdir /usr/local/monitor -p && cd /usr/local/monitor
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
#2、解压软件
tar -xzvf node_exporter-*.*.tar.gz
cd node_exporter-*.*
#3、通过systemctl管理node_exporter
进入/etc/systemd/system目录,新建文件node_exporter.service,添加下面内容
[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/monitor/node_exporter-1.1.2.linux-amd64/node_exporter
Rastart=on-failure
[Install]
WantedBy=multi-user.target
#保存退出后授予node_exporter.service文件执行权限
chmod a+x node_exporter.service
#启动node_exporter
systemctl start node_exporter.service
systemctl enable node_exporter.service
#查看启动状态
systemctl status node_exporter.service
默认node_exporter以9100端口启动,当然也可以指定端口启动,如下
# Start 1 example targets in separate terminals:
nohup ./node_exporter --web.listen-address 127.0.0.1:8080 &
#4、Configure Prometheus to monitor the targets
编辑服务器端配置文件prometheus.yml
scrape_configs:
- job_name: 'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9100', 'localhost:8081']
labels:
group: 'production'
# 5、至此,查看服务端上两个服务是否启动
$ netstat -lnupt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1103/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 904/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1103/master
tcp6 0 0 :::9090 :::* LISTEN 59528/./prometheus
tcp6 0 0 :::9100 :::* LISTEN 59510/node_exporter
tcp6 0 0 :::22 :::* LISTEN 904/sshd
说明:
本文只是说明了安装客户端和服务端软件,至于数据的采集和特定规则指标采集和报警下面文章将会涉及到。
exporter是prometheus监控中重要的组成部分,负责数据指标的采集。本系列文章将用到alertmanager,blackbox_exporter,node_exporter三个exporter.