title: Prometheus部署 #标题tags: Prometheus #标签
date: 2020-08-26
categories: 监控 # 分类

系统化学习Prometheus第一天,跟我一起部署它!
参考官方文档

下载及调整目录

  1. $ wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
  2. $ tar zxf prometheus-2.20.1.linux-amd64.tar.gz -C /opt/
  3. $ ln -sf /opt/prometheus-2.20.1.linux-amd64 /opt/prometheus
  4. $ ln -sf /opt/prometheus-2.20.1.linux-amd64/prometheus /usr/local/bin/

分析配置文件

  1. $ cd /opt/prometheus
  2. $ cat prometheus.yml # 查看默认配置文件,# 号开头表示注释行
  3. # 配置文件分为三个模块,global、rule_file及scrape_configs。
  4. # global一般不用改动
  5. # my global config
  6. global:
  7. # 设置每15s更新一次监控数据
  8. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  9. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  10. # scrape_timeout is set to the global default (10s).
  11. # Alertmanager configuration
  12. # rule_files块指定了我们希望Prometheus服务器加载的任何报警规则的位置。
  13. alerting:
  14. alertmanagers:
  15. - static_configs:
  16. - targets:
  17. # - alertmanager:9093
  18. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  19. rule_files:
  20. # - "first_rules.yml"
  21. # - "second_rules.yml"
  22. # A scrape configuration containing exactly one endpoint to scrape:
  23. # Here it's Prometheus itself.
  24. # scrape_configs控制Prometheus监视哪些资源。
  25. scrape_configs:
  26. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  27. - job_name: 'prometheus'
  28. # metrics_path defaults to '/metrics'
  29. # scheme defaults to 'http'.
  30. static_configs:
  31. - targets: ['192.168.20.10:9090'] # 这里需要将localhost改为本机地址,否则有些小bug

配置文件支持很多东西,建议阅读下官方配置文档

Prometheus的启停方式

  1. # 启动Prometheus
  2. $ prometheus --config.file="/opt/prometheus/prometheus.yml" --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention.time=30d & # 启动Prometheus
  3. # --web.enable-lifecycle 表示允许通过web接口重载配置文件
  4. # --storage.tsdb.path=/opt/prometheus/data 指定数据存储目录,默认是当前目录的data目录
  5. # --storage.tsdb.retention.time 指定数据保留时间,默认为15d。
  6. $ pkill prometheus # 停止Prometheus
  7. # 重载Prometheus
  8. # 命令行中给发送hup信号至Prometheus进程
  9. $ kill -HUP $(ps -ef | grep prometheus | grep -v grep | awk '{print $2}')
  10. # 通过web接口重载,启动时需增加选项 --web.enable-lifecycle
  11. $ curl -XPOST http://192.168.20.10:9090/-/reload
  12. # 如果配置文件有误,重启时将会看到类似以下的错误信息
  13. ERRO[0161] Error reloading config: couldn't load configuration (-config.file=prometheus.yml): unknown fields in scrape_config: job_nae source=main.go:146

输出如下,表示启动成功:

Prometheus部署 - 图1

  1. $ ss -lnput | grep 9090 # 确定端口在监听
  2. tcp LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=16429,fd=11))

访问web界面

访问Prometheus主机的 9090 端口,点击如下,即可看到监控主机的相关信息(Prometheus默认监控本机):
Prometheus部署 - 图2

管理API

健康检查

  1. $ curl http://192.168.20.10:9090/-/healthy

准备检查

  1. $ curl http://192.168.20.10:9090/-/ready
  2. Prometheus is Ready.

重载配置文件

  1. $ curl -XPOST http://192.168.20.10:9090/-/reload
  2. # 需启动时增加 --web.enable-lifecycle 选项。

停止Prometheus

  1. $ curl -XPUT http://192.168.20.10:9090/-/quit
  2. # 或者
  3. $ curl -XPOST http://192.168.20.10:9090/-/quit
  4. # 同样需启动时增加 --web.enable-lifecycle 选项。