一、服务端配置

官方下载地址:https://prometheus.io/download/
前提:注意服务器之间已经同步时间,否则会出现dashborad获取不到数据。
#ref:https://github.com/prometheus

#1、下载promethues软件

  1. mkdir /usr/local/monitor -p && cd /usr/local/monitor
  2. wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
  3. tar xvfz prometheus-*.tar.gz
  4. cd prometheus-*

#2、Configuring Prometheus to monitor itself

配置配置文件监控自己

  1. global:
  2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
  3. # Attach these labels to any time series or alerts when communicating with
  4. # external systems (federation, remote storage, Alertmanager).
  5. external_labels:
  6. monitor: 'codelab-monitor'
  7. # A scrape configuration containing exactly one endpoint to scrape:
  8. # Here it's Prometheus itself.
  9. scrape_configs:
  10. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  11. - job_name: 'prometheus'
  12. # Override the global default and scrape targets from this job every 5 seconds.
  13. scrape_interval: 5s
  14. static_configs:
  15. - targets: ['localhost:9090']

#3、通过systemctl管理Prometheus

  1. #增加系统管理软件文件
  2. vim/etc/systemd/system/prometheus.service
  3. [Unit]
  4. Description=prometheus
  5. After=network.target
  6. [Service]
  7. Type=simple
  8. User=root
  9. ExecStart=/usr/local/monitor/prometheus-2.27.1.linux-amd64/prometheus --config.file=/usr/local/monitor/prometheus-2.27.1.linux-amd64/prometheus.yml
  10. Rastart=on-failure
  11. [Install]
  12. WantedBy=multi-user.target
  13. #保存退出后,重新加载配置,启动服务即可
  14. chmod a+x prometheus.service
  15. systemctl daemon-reload
  16. systemctl restart prometheus.service
  17. systemctl enable prometheus.service
  18. #查看服务是否启动
  19. systemctl status prometheus.service
  20. #当然还可以通过下面的方式启动服务
  21. nohup ./prometheus --config.file=prometheus.yml &

#4、访问web页面

  1. http://localhost:9090
  2. #查看监控规则
  3. localhost:9090/metrics

二、客户端配置

官方软件下载地址:https://prometheus.io/download/
node_exporter主要监控主机的CPU,内存等硬件资源信息。

#1、下载node_exporter

  1. mkdir /usr/local/monitor -p && cd /usr/local/monitor
  2. wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz

#2、解压软件

  1. tar -xzvf node_exporter-*.*.tar.gz
  2. cd node_exporter-*.*

#3、通过systemctl管理node_exporter

  1. 进入/etc/systemd/system目录,新建文件node_exporter.service,添加下面内容
  2. [Unit]
  3. Description=node_exporter
  4. After=network.target
  5. [Service]
  6. Type=simple
  7. User=root
  8. ExecStart=/usr/local/monitor/node_exporter-1.1.2.linux-amd64/node_exporter
  9. Rastart=on-failure
  10. [Install]
  11. WantedBy=multi-user.target
  12. #保存退出后授予node_exporter.service文件执行权限
  13. chmod a+x node_exporter.service
  14. #启动node_exporter
  15. systemctl start node_exporter.service
  16. systemctl enable node_exporter.service
  17. #查看启动状态
  18. systemctl status node_exporter.service
  19. 默认node_exporter9100端口启动,当然也可以指定端口启动,如下
  20. # Start 1 example targets in separate terminals:
  21. nohup ./node_exporter --web.listen-address 127.0.0.1:8080 &

#4、Configure Prometheus to monitor the targets

  1. 编辑服务器端配置文件prometheus.yml
  2. scrape_configs:
  3. - job_name: 'node'
  4. # Override the global default and scrape targets from this job every 5 seconds.
  5. scrape_interval: 5s
  6. static_configs:
  7. - targets: ['localhost:9100', 'localhost:8081']
  8. labels:
  9. group: 'production'

# 5、至此,查看服务端上两个服务是否启动

  1. $ netstat -lnupt
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1103/master
  5. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 904/sshd
  6. tcp6 0 0 ::1:25 :::* LISTEN 1103/master
  7. tcp6 0 0 :::9090 :::* LISTEN 59528/./prometheus
  8. tcp6 0 0 :::9100 :::* LISTEN 59510/node_exporter
  9. tcp6 0 0 :::22 :::* LISTEN 904/sshd

说明:
本文只是说明了安装客户端和服务端软件,至于数据的采集和特定规则指标采集和报警下面文章将会涉及到。

exporter是prometheus监控中重要的组成部分,负责数据指标的采集。本系列文章将用到alertmanager,blackbox_exporter,node_exporter三个exporter.