森哥瞧世界

您现在的位置是:首页>文档内容页

文档详情

Prometheus的安装及配置教程

jeson 2020-03-24 14:24:41 应用服务22482
Prometheus 是一套开源的系统监控报警框架。它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 2012 年创建,作为社区开源项目进行开发,并于 2015 年正式发布。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,成为受欢迎度仅次于 Kubernetes 的项目。

 prometheus适用于监控所有时间序列的项目

  • Node/system metrics exporter

  • AWS CloudWatch exporter

  • Blackbox exporter

  • Collectd exporter

  • Consul exporter

  • Graphite exporter

  • HAProxy exporter

  • InfluxDB exporter

  • JMX exporter

  • Memcached exporter

  • Mesos task exporter

  • MySQL server exporter

  • SNMP exporter

  • StatsD exporter

系统环境:

CentOS Linux release 7.7.1908 (Core)

基础环境:

yum install epel-release

安装

下载解压

mkdir /root/software
cd /root/software
wget https://github.com/prometheus/prometheus/releases/download/v2.17.0-rc.4/prometheus-2.17.0-rc.4.linux-amd64.tar.gz
tar -zxvf prometheus-2.17.0-rc.4.linux-amd64.tar.gz
mv prometheus-2.17.0-rc.4.linux-amd64 /opt/prometheus
chown prometheus.prometheus /opt/prometheus -R
[root@monitor prometheus]# ./prometheus --version
prometheus, version 2.17.0-rc.4 (branch: HEAD, revision: d47bdb9d12c086ce9bb8ba1ef7a2efe1a9f0884d)
 build user:       root@c38ff2df9b3e
 build date:       20200321-19:08:16
 go version:       go1.13.9

设置系统服务启动文件及日志归档

touch /var/log/prometheus.log   //创建日志文件
vim /opt/prometheus/prometheus.sh
#!/bin/bash
/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml &>> /var/log/prometheus.log
chmod +x /opt/prometheus/prometheus.sh
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/opt/prometheus/prometheus.sh
 
[Install]
  WantedBy=multi-user.target
systemctl daemon-reload              # 重新加载daemon
systemctl enable prometheus.service  # 配置开机加载
systemctl start prometheus.service   # 启动普罗米修斯
systemctl status prometheus          # 查看是启动状态

[root@monitor system]# netstat -anp | grep prometheus
tcp        0      0 127.0.0.1:48958         127.0.0.1:9090          ESTABLISHED 7921/prometheus    
tcp6       0      0 :::9090                 :::*                    LISTEN      7921/prometheus    
tcp6       0      0 ::1:35978               ::1:9090                ESTABLISHED 7921/prometheus    
tcp6       0      0 ::1:9090                ::1:35978               ESTABLISHED 7921/prometheus    
tcp6       0      0 127.0.0.1:9090          127.0.0.1:48958         ESTABLISHED 7921/prometheus  

监控CPU及内存等

监控硬件信息,需要使用到Node_exporter插件。因此,需要先在目标服务器上安装。

mkdir /opt/exporter
cd /opt/exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
tar -zxvf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
mv node_exporter-1.0.0-rc.0.linux-amd64 node_exporter

设置系统文件服务

[root@monitor system]# cat node_exporter.service 
  [Unit]
  Description=Prometheus
  Documentation=https://prometheus.io/docs/introduction/overview/
  Wants=network-online.target
  After=network-online.target
 
  [Service]
  User=root
  Group=root
  Type=simple
  ExecStart=/opt/exporter/node_exporter/node_exporter
 
  [Install]
  WantedBy=multi-user.target
systemctl daemon-reload                 # 重新加载daemon
systemctl enable node_exporter.service  # 配置开机加载
systemctl start node_exporter.service   # 启动Node_Exporter
systemctl status node_exporter          # 查看是启动状态
[root@monitor system]# netstat -anp | grep node_exporter
tcp6       0      0 :::9100                 :::*                    LISTEN      8014/node_exporter  
unix  3      [ ]         STREAM     CONNECTED     217169   8014/node_exporter  

通过浏览器,查看服务情况

http://yourIP:9100/metrics

修改Prometheus配置

cat /opt/prometheus/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的命令,必须为全局唯一。
 - job_name: "node"
   static_configs:
   - targets:
     - '172.16.3.254:9100'
     #labels:
     #  instance: "172.16.3.254:9100"
     #  env: "pro"
     #  name: "172.16.3.254"

重启一下Prometheus。即可以Prometheus查找数据。

systemctl restart prometheus.service

配合Grafana,可以更直观的看到图形界面。

文章评论

Cancel the reply
Login Participate In Comments

Review(