介绍

Prometheus 是一款基于时序数据库的开源监控告警系统,Prometheus 的基本原理是通过 HTTP 协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控,输出被监控组件信息的 HTTP 接口被叫做 Exporter 。目前常用的组件大部分都有 Exporter 可以直接使用,比如 Nginx 、 MySQL 、 ES 、 Redis 、 Linux 系统 、 Windiws 系统等。


1 下载安装prometheus

$ wget https://github.com/prometheus/prometheus/releases/download/v2.41.0/prometheus-2.41.0.linux-amd64.tar.gz
$ tar xf  prometheus-2.41.0.linux-amd64.tar.gz -C /data
$ mv /data/prometheus-2.41.0.linux-amd64 /data/prometheus

2 配置prometheus

$ cd /data/prometheus
$ 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'
    scrape_interval: 60s
    static_configs:
    - targets:
      - 'localhost:29090'
      labels:
        hostname: Prometheus-Server

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:29090"]

3 创建prometheus服务

$ vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --web.listen-address=:29090 --web.max-connections=512 --storage.tsdb.path="/data/prometheus/data/" --storage.tsdb.retention=30d --web.enable-lifecycle

[Install]
WantedBy=multi-user.target
  • 指定配置文件 --config.file="prometheus.yml"
  • 默认指定监听地址端口,可修改端口 --web.listen-address="0.0.0.0:9090"
  • 最大连接数 --web.max-connections=512
  • tsdb数据存储的目录,默认当前data/ --storage.tsdb.path="data/"
  • premetheus 存储数据的时间,默认保存15天 --storage.tsdb.retention=15d
  • 通过命令热加载无需重启 curl -XPOST 127.0.0.1:9090/-/reload --web.enable-lifecycle

启动选项了解:./prometheus --help

4 启动、开机自启

$ systemctl start prometheus
$ systemctl enable prometheus

5 访问测试

浏览器访问:http://[IP]:29090

文章作者: hzbb
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 运维小记
自动化 Prometheus prometheus
喜欢就支持一下吧
打赏
微信 微信
支付宝 支付宝