前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Prometheus监控实战》第3章 安装和启动Prometheus

《Prometheus监控实战》第3章 安装和启动Prometheus

作者头像
yeedomliu
发布2019-12-19 16:34:35
1.1K0
发布2019-12-19 16:34:35
举报
文章被收录于专栏:yeedomliuyeedomliu

第3章 安装和启动Prometheus


3.1 安装Prometheus

  • 如果要将Prometheus部署到生产环境或进行扩展,则应该始终选择配置管理工具作为安装方法
  • 下载地址:https://prometheus.io/download/

3.1.4 在Mac OS X上安装Prometheus

代码语言:javascript
复制
$ brew install prometheus

3.1.5 通过监控套件安装Prometheus

  • 使用Docker Compose安装Prometheus、Node Exporter和Grafana[1]
  • 使用Docker Compose单节点安装Prometheus、Alertmanager、Node Exporter和Grafana[2]
  • [1]https://github.com/vegasbrianc/prometheus
  • [2]https://github.com/danguita/prometheus-monitoring-stack

3.1.6 通过配置管理工具安装Prometheus

  • Puppet的Prometheus模块:https://forge.puppet.com/puppet/prometheus
  • Chef的Prometheus cookbook:https://supermarket.chef.io/cookbooks/prometheus-platform
  • Ansible的Prometheus role:https://github.com/cloudalchemy/ansible-prometheus
  • SaltStack的Prometheus formula:https://github.com/arnisoph/saltstack-prometheus-formula

3.1.7 通过Kubernetes安装Prometheus

  • 使用配置文件来构建自己的部署并将Prometheus打包成服务:https://github.com/kayrus/prometheus-kubernetes
  • 使用CoreOS的Prometheus Operator:https://github.com/coreos/prometheus-operator

3.2 配置Prometheus

  • YAML配置非常复杂,可能让人很痛苦,建议你通过YAML Lint(http://www.yamllint.com/)在线方式或者在命令行使用yamllint(https://github.com/adrienverge/yamllint)来验证YAML配置文件
  • 我们的默认配置文件中定义了4个YAML块
    1. global
    2. alerting
    3. rule_files
    4. scrape_configs

3.2.1 global

3.2.2 alerting

3.2.3 rule_files

3.2.4 scrape_configs


3.3 启动Prometheus

  • 配置文件prometheus.yml
代码语言:javascript
复制
global:
  scrape_interval:     15s
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  • 启动Prometheus
代码语言:javascript
复制
$ prometheus --config.file ./prometheus.yml
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:293 msg="no time or size retention was set so using the default time retention" duration=15d
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:329 msg="Starting Prometheus" version="(version=2.11.1, branch=non-git, revision=non-git)"
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:330 build_context="(go=go1.12.7, user=brew@Mojave-2.local, date=20190711-02:47:26)"
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:331 host_details=(darwin)
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:332 fd_limits="(soft=4864, hard=unlimited)"
level=info ts=2019-11-07T14:45:47.188Z caller=main.go:333 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2019-11-07T14:45:47.189Z caller=main.go:652 msg="Starting TSDB ..."
level=info ts=2019-11-07T14:45:47.189Z caller=web.go:448 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-11-07T14:45:47.195Z caller=main.go:667 fs_type=19
level=info ts=2019-11-07T14:45:47.195Z caller=main.go:668 msg="TSDB started"
level=info ts=2019-11-07T14:45:47.195Z caller=main.go:738 msg="Loading configuration file" filename=./prometheus.yml
level=info ts=2019-11-07T14:45:47.707Z caller=main.go:766 msg="Completed loading of configuration file" filename=./prometheus.yml
level=info ts=2019-11-07T14:45:47.707Z caller=main.go:621 msg="Server is ready to receive web requests."
  • 如果发生异常,则可以使用promtool来验证配置文件,这是Prometheus附带的一个代码校验工具
代码语言:javascript
复制
$ promtool check config prometheus.yml
Checking prometheus.yml
  SUCCESS: 0 rule files found
  • 通过Docker运行Prometheus
代码语言:javascript
复制
$ docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:296 msg="no time or size retention was set so using the default time retention" duration=15d
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:332 msg="Starting Prometheus" version="(version=2.13.1, branch=HEAD, revision=6f92ce56053866194ae5937012c1bec40f1dd1d9)"
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:333 build_context="(go=go1.13.1, user=root@88e419aa1676, date=20191017-13:15:01)"
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:334 host_details="(Linux 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 765454899e9c (none))"
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:335 fd_limits="(soft=1048576, hard=1048576)"
level=info ts=2019-11-07T14:51:10.156Z caller=main.go:336 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2019-11-07T14:51:10.158Z caller=main.go:657 msg="Starting TSDB ..."
level=info ts=2019-11-07T14:51:10.159Z caller=web.go:450 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-11-07T14:51:10.161Z caller=head.go:514 component=tsdb msg="replaying WAL, this may take awhile"
level=info ts=2019-11-07T14:51:10.161Z caller=head.go:562 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
level=info ts=2019-11-07T14:51:10.162Z caller=main.go:672 fs_type=EXT4_SUPER_MAGIC
level=info ts=2019-11-07T14:51:10.162Z caller=main.go:673 msg="TSDB started"
level=info ts=2019-11-07T14:51:10.162Z caller=main.go:743 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2019-11-07T14:51:10.165Z caller=main.go:771 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml
level=info ts=2019-11-07T14:51:10.165Z caller=main.go:626 msg="Server is ready to receive web requests."

3.4 第一个指标

  • 查看http://localhost:9090/metrics内容
代码语言:javascript
复制
$ curl http://localhost:9090/metrics
# HELP go_gc_duration_seconds A summary of the GC invocation durations.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 1.9736e-05
go_gc_duration_seconds{quantile="0.25"} 5.9597e-05
go_gc_duration_seconds{quantile="0.5"} 0.000154742
go_gc_duration_seconds{quantile="0.75"} 0.000280268
go_gc_duration_seconds{quantile="1"} 0.00042205
go_gc_duration_seconds_sum 0.001300487
go_gc_duration_seconds_count 7
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 45
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.13.1"} 1
# HELP go_memstats_alloc_bytes Number of bytes allocated and still in use.
# TYPE go_memstats_alloc_bytes gauge
代码语言:javascript
复制
go_gc_duration_seconds{quantile="0.5"} 0.000154742
  • 这个指标的名称是go_gc_duration_seconds,里面有一个标签quantile="0.5",表示这衡量的是第50百分位数,后面的数字是这个指标的值

3.5 表达式浏览器

  • 浏览http://localhost:9090/graph来获得
  • 看看另一个指标prometheus_build_info,它包含有关Prometheus服务器的构建信息
  • 可以看到这个指标使用了大量的标签,并且指标的值为1。这是通过指标将信息传递到Prometheus服务器的常见模式,它使用恒定值为1的指标,并且添加了你可能希望通过标签附加的相关信息

3.6 聚合时间序列

  • 还可以进行指标的复杂聚合。我们选择promhttp_metric_handler_requests_total指标来看
  • 使用sum()运算符累加,但没有按作业分类,子句by它允许按特定维度聚合
  • rate()函数用来计算一定范围内时间序列的每秒平均增长率,只能与计数器一起使用,最适合用于增长较慢的计数器或用于警报的场景
  • 计算5分钟范围向量的速率。范围向量(range vector)是第二个PromQL数据类型,它包含一组时间序列,其中每个时间序列都包含一系列数据点。范围向量允许我们显示该时间段的时间序列,持续时间被包含在中括号[ ]中,内容是一个整数值后跟一个单位缩写,其中单位缩写
    • s:秒
    • m:分钟
    • h:小时
    • d:天
    • w:周
  • 如果在构建PromQL查询方面需要帮助,则可以使用查询编辑器Promeditor,它可以在Prometheus本地运行(https://github.com//davkal/promeditor)

3.7 容量规划

  • Prometheus的性能很难估计,因为它在很大程度上取决于你的配置、所收集的时间序列的数量以及服务器上规则的复杂性。一般容量规划关注两个问题:内存和磁盘

3.7.1 内存

  • Prometheus在内存中做了很多工作。每个收集的时间序列、查询和记录规则都会消耗进程内存。但一个有用的、粗略的经验法则是将每秒采集的样本数乘以样本的大小。我们可以使用以下查询语句来查看样本收集率
代码语言:javascript
复制
rate(prometheus_tsdb_head_samples_appended_total[1m])
  • 这将显示你在最后一分钟添加到数据库的每秒样本率。如果想知道收集的指标数量
代码语言:javascript
复制
sum(count by (__name__)({__name__=~".+"}))
  • 这里使用sum聚合来计算所有匹配的指标的计数和,使用=~运算符和.+的正则表达式来匹配所有指标
  • 每个样本的大小通常为1到2个字节,让我们谨慎一点,按照2个字节计算。假设在12小时内每秒收集100 000个样本,那我们可以像下面这样计算内存使用情况:结果大概是8.64GB的内存
代码语言:javascript
复制
100,000 * 2 bytes * 43200 seconds
  • 你还需要考虑在查询和记录规则方面的内存使用情况。这个不太好计算,并且依赖于许多其他变量,建议根据内存使用情况灵活调整。你可以通过检查process_resident_memory_bytes指标来查看Prometheus进程的内存使用情况

3.7.2 磁盘

  • 磁盘使用量受存储的时间序列数量和这些时间序列的保留时间限制。默认情况下,指标会在本地时间序列数据库中存储15天。数据库的位置和保留时间由命令行选项控制
  • --storage.tsdb.path选项:它的默认数据目录位于运行Prometheus的目录中,用于控制时间序列数据库位置
  • --storage.tsdb.retention选项:控制时间序列的保留期。默认值为15d,代表15天
  • 建议采用SSD作为时间序列数据库的磁盘。对于每秒10万个样本的示例,我们知道按时间序列收集的每个样本在磁盘上占用大约1到2个字节。假设每个样本有2个字节,那么保留15天的时间序列意味着需要大约259 GB的磁盘
  • 有关Prometheus磁盘使用情况的更多信息,请参见存储文档(https://prometheus.io/docs/prometheus/latest/storage/)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-12-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 yeedomliu 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第3章 安装和启动Prometheus
    • 3.1 安装Prometheus
      • 3.1.4 在Mac OS X上安装Prometheus
      • 3.1.5 通过监控套件安装Prometheus
      • 3.1.6 通过配置管理工具安装Prometheus
      • 3.1.7 通过Kubernetes安装Prometheus
    • 3.2 配置Prometheus
      • 3.2.1 global
      • 3.2.2 alerting
      • 3.2.3 rule_files
      • 3.2.4 scrape_configs
    • 3.3 启动Prometheus
      • 3.4 第一个指标
        • 3.5 表达式浏览器
          • 3.6 聚合时间序列
            • 3.7 容量规划
              • 3.7.1 内存
              • 3.7.2 磁盘
          相关产品与服务
          数据库
          云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档