本书学习目标:
第一部分:
第1章从云计算时代的应用特点入手,分析云计算环境下监控面临的挑战,提出需要怎样的监控。接着简要介绍监控系统的基本架构及工具,最后重点介绍Prometheus的诞生及其解决方案的特点。
第2章讲解Prometheus的基本概念及部署,包括Prometheus技术架构、部署方式,最后介绍相关的基本概念。
第二部分: 对prometheus熟悉及掌握
第3章对Exporter的类型、文本数据格式进行说明,讨论如何从官方的Exporter列表获取常用的Exporter,比如用于监控基础设施中服务器性能的node_exporter、wmi_exporter,用于监控数据库的mysqld_exporter和redis_exporter等。
第4章讲解如何使用服务发现来发现主机和服务,并将它们暴露给Prometheus。介绍Prometheus发现机制中基于文件、基于Consul和基于DNS的服务发现,使其能完美地解决当下云原生态场景下的各类服务监控项目。
第5章首先对时序数据库进行介绍,然后对PromQL进行详细说明,包括PromQL数据类型、时序选择器、聚合操作、各类运算符和函数。最后通过简单的示例对PromQL查询操作进行汇总分析。
第6章对Alertmanager组件的安装、配置进行详细说明,再介绍Prometheus告警规则的定义,最后使用Email、企业微信和基于Webhook的钉钉实现告警通知的接收。
第7章介绍Grafana可视化展示,阐述其中的基本概念,结合读者自己的应用环境生成理想的漂亮图表,最终完全熟练掌握它的各个管理组件。
第8章介绍Prometheus的核心组件Pushgateway,重点介绍Pushgateway实例部署以及场景应用。
第三部分:生成环境的监控
第9章首先介绍OpenStack云管平台及其监控架构,然后介绍两种Prometheus与OpenStack整合的监控解决方案——OpenStack Exporter和OpenStack Helm监控方式。
第10章介绍Docker相关的监控方法,特别讲解了Google开源的容器度量收集工具cAdvisor的部署、使用、集成方案。最后对Docker容器的监控指标、cAdvisor架构进行深入的分析。
第11章介绍Prometheus如何监控Kubernetes,详细介绍Kubernetes部署Prometheus的两种方式,最后介绍服务配置、监控对象以及数据展现和告警。
第12章介绍Prometheus如何对微服务运行状态进行监控,重点讲解基于Spring Cloud构建微服务的技术体系,之后讲解业务运营指标以及如何构建运营指标体系,并结合Spring Boot介绍监控埋点、自定义监控指标以及具体应用。
第13章介绍如何使用日志条目来创建度量指标,然后由Prometheus来抓取数据并在Grafana中展示。
第14章介绍在生产环境中应用Prometheus应该考虑的问题以及解决的方法,包括支持高可用规模部署和自动化部署的方案。
第1章 云计算时代的监控系统
云计算的三种模式: laas paas saas
laas: 基础设施即服务:
1 服务器到ITC机房再到数据中心 数据中心对服务器的托管服务,如冷却系统到温度对服务器的影响。 服务器宽带的接入,服务器系统的统一安装 再到跳板机的设置 再到服务器做的数据中心进行监控
监控到各方面的一个系统
paas K8S实现对应用自动扩缩容的一个目标。
saas 软件设施即服务。 手机端到电脑端。再到黑盒与白盒监控
2 云计算监控目标
提供对复杂信息系统的全面监控,反映云资源池的健康状况和可用性情况,得到一个可控制、可预测的云环境,支持云业务安全、稳定、高效、持续地运行;同时,有效地控制管理成本,规范管理工作,实现运行管理的智能化和高效性,提高整体的维护水平;及时掌握各种资源现状和运行信息,为决策提供支持。
3 好的监控系统需要实现以下几大功能
1 长期趋势分析:通过对监控样本数据的持续收集和统计,对监控指标进行长期趋势分析。例如,通过对磁盘空间增长率的判断,我们可以提前预测在未来什么时间节点上需要对资源进行扩容。
2 ·对照分析:两个版本的系统运行资源使用情况的差异如何?在不同容量情况下系统的并发和负载变化如何?通过监控能够方便地对系统进行跟踪和比较。
3 ·告警:当系统出现或者即将出现故障时,监控系统需要迅速反应并通知管理员,从而能够对问题进行快速处理或者提前预防问题的发生,避免对业务产生影响。
4 故障分析与定位:当问题发生后,需要对问题进行调查和处理。通过对比分析不同监控数据与历史数据,能够找到并解决根源问题。
5 数据可视化:通过可视化仪表盘能够直接获取系统的运行状态、资源使用情况,以及服务运行状态等直观的信息.
云计算监控挑战: 1 基础设施就是服务器硬件类的一个监控 2 网络组件以及改监控软件的一个分布式组成是pull还是push 再到KVM,OPENSTACK,K8S,容器的监控 再到上层的微服务架构的组成,再到分布式大数据组成显示的一个数据。

集群规模架构控制
通过代理或者非代理收集的数据最后都发送到监控中心数据库中。一般来说这个中心数据库是分布式的,是逻辑上的中心而不是物理上的中心。数据从初始收集到中心数据库的每一步都可以进行过滤和聚合。判断过滤和聚合量的条件包括:生产数据的大小、本地节点的潜在故障和必要通信的传输粒度。因为本地节点可能发生故障且数据变得不可用,所以从本地节点获取数据并监控是重要的。将所有数据直接发送到中心数据库可能会导致网络阻塞,因此,在设计监控架构时,选择从本地节点到中心数据库之间的中间步骤以及在每一步过滤和聚合数据是重要的架构决策。
监控软件介绍:
1 zabbix监控组件
Zabbix是一个基于Web界面的提供分布式系统监视以及网络监视功能的企业级开源解决方案。
zabbix的部分原理:
Zabbix由两部分构成——Zabbix server与可选组件Zabbix agent。Zabbix server可以单独监视远程服务器的服务状态;同时也可以与Zabbix agent配合,可以轮询Zabbix agent主动接收监视数据,还可被动接收Zabbix agent发送的数据。另外,Zabbix server支持SNMP、IPMI、JMX、Telnet、SSH等多种协议,将采集到的数据存放到数据库,然后对其进行分析整理,若达到条件触发则告警。
zabbix监控系统组件
zabbix-server 服务端
zabbix-web界面(apache和nginx)
zabbix的数据库
zabbix-agent 采集远程懾的数据
2 OpenTSDB通过HBase存储进行数据的存储。 用于大数据采集的监控
OpenTSDB通过HBase存储所有的时序(无须采样)来构建一个分布式、可伸缩的时间序列数据库。它支持秒级数据采集所有指标,支持永久存储,可以做容量规划,并可很容易地接入现有的报警系统。OpenTSDB可以从大规模的集群(包括集群中的网络设备、操作系统、应用程序)中获取相应的指标,并进行存储、索引以及服务,从而使这些数据更容易被人理解,如Web化、图形化等。OpenTSDB数据流如图

1 Prometheus监控系统 是一个开源的监控系统可以支持大规模集群的监控系统 prometheus提供分层级监控比zabbix的agent端更轻量级,处理日志流量的监控更规模。 prometheus通过HTTP进行监控
2 与Borgmon一样,Prometheus主要对基于云和基于容器的动态微服务、服务和应用程序提供近实时监控。SoundCloud是这些架构模式的较早使用者。现在,Prometheus被很多公司采纳。
3.Prometheus设计理念
Prometheus能抓取或拉取应用程序导出的时间序列数据。应用程序本身经常通过客户端函数库或导出器(导出程序,作为HTTP端点)呈现出时间序列数据。导出器和客户端函数库可用于多种语言、框架和开源应用程序,例如用于Apache、Nginx等Web服务器以及MySQL等数据库,Prometheus关注的是近期发生的事情,而不是跟踪数周或数月的数据。因为大多数监视查询和警报都是从最近的(通常不到一天的)数据生成的。Prometheus假设用户试图修复的问题是最近的,因此最有用的数据是最近的数据。Prometheus监控数据默认保留15天。
4 Prometheus还有一个推送网关,可以用来接收少量数据,例如获取不能被直接抓取的监控目标的指标数据。

prometheus的架构组织


1.易管理性
2 .更契合的架构
3 .灵活的数据模型
4 良好的性能,强大的查询能力
5.可扩展性
6.健全的生态,开放、易于与第三方系统集成
7.可视化
Prometheus基本概念及部署
prometheus架构

官方提供的Prometheus体系架构及其生态圈系统组件。
流程图:
Prometheus服务器周期性地或在设定的时间段内,可以通过以下方式获取内容。
·从已经配置好的job或者exporter中拉取metric。
·接收从Pushgateway推送过来的metric。
·从其他的Prometheus服务器中拉取metric。
·Prometheus服务器获取到数据后存储在本地(也可以选择远端存储),通过一定规则对数据进行清理和整理,并且把结果存储到新的时间序列中。
·Prometheus服务器定时查询已经定义好的规则,若发现满足定义的触发条件,便将alert信息推送至已配置好的Alertmanager。
·Alertmanager收到alert信息后,根据配置文件对接收到的alert信息进行处理(聚合、去重、降噪),然后将它们转换为网页、电子邮件和其他通知方式发出告警。
·最后通过PromQL或其他API可视化地展示收集的数据,例如自带Prometheus Web UI、Grafana可视化收集查询数据等。
Prometheus在记录纯数字时间序列方面表现得非常优秀。它既适用于对服务器硬件各项指标的监控,也适用于对高度动态的面向服务架构的监控。在当下比较火的微服务生态圈中,它对多维数据收集和查询的支持具有特殊的优势。
Prometheus的基本原理是通过HTTP协议周期性获取被监控组件的状态信息,任意组件只要提供HTTP接口就可以接入监控系统内,不需要如SDK的集成过程,这就使得Prometheus可以更好地适应虚拟化,如VM或Docker容器的环境集成。当使用者监控的服务出现故障时,它可以快速定位和诊断问题。每个Prometheus服务器都是独立的,不依赖于网络存储或其他远程服务。当基础架构的其他部分损坏时,可以快速恢复,并且不需要设置大量的基础依赖架构。
prometheus的快速部署
首先我们对Prometheus进行快速部署,使其启动正常运行,然后通过Prometheus自带的一个比较简单的Web UI查看表达式搜索结果。
prometheus提供了多种部署方法,有docker容器的还提供了第三方工具Ansible自动化运行安装的文件
prometheus的官方网站
https://prometheus.io/download/ 下载系统对应的文件。

prometheus的中文文档 https://prometheus.fuckcloudnative.io/di-yi-zhang-jie-shao/overview

快速部署Prometheus:
1 官网下载Prometheus完成后,获取软件包的哈希值:
[root@localhost ~]# sha256sum prometheus-2.31.0.linux-amd64.tar.gz
2
2)解压缩二进制软件包到指定的安装目录,运行Prometheus,操作如下:
[root@localhost ~]# tar -zxvf prometheus-2.31.0.linux-amd64.tar.gz -C /data
# 这里安装在/data目录下
[root@localhost data]# cd /data/ ; ls 查看data目录下解压的软件包
prometheus-2.31.0.linux-amd64
chown -R root:root prometheus-2.31.0.linux-amd64 给prometheus目录设置属主;属组
[root@localhost prometheus-2.31.0.linux-amd64]# ./prometheus 启动prometheus 在安装目录下启动
[root@localhost prometheus-2.31.0.linux-amd64]# netstat -lnpt | grep 9090 查看prometheus监听的9090端口
tcp6 0 0 :::9090 :::* LISTEN 2120/./prometheus
查看prometheus的监控信息
[root@localhost prometheus-2.31.0.linux-amd64]# ./prometheus -h
usage: prometheus [<flags>]
The Prometheus monitoring server
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--version Show application version.
--config.file="prometheus.yml"
Prometheus configuration file path.
--web.listen-address="0.0.0.0:9090"
Address to listen on for UI, API, and telemetry.
--web.config.file="" [EXPERIMENTAL] Path to configuration file that can enable TLS or authentication.
--web.read-timeout=5m Maximum duration before timing out read of the request, and closing idle connections.
--web.max-connections=512 Maximum number of simultaneous connections.
--web.external-url=<URL> The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating
relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served
by Prometheus. If omitted, relevant URL components will be derived automatically.
--web.route-prefix=<path> Prefix for the internal routes of web endpoints. Defaults to path of --web.external-url.
--web.user-assets=<path> Path to static asset directory, available at /user.
--web.enable-lifecycle Enable shutdown and reload via HTTP request.
--web.enable-admin-api Enable API endpoints for admin control actions.
--web.console.templates="consoles"
Path to the console template directory, available at /consoles.
--web.console.libraries="console_libraries"
Path to the console library directory.
--web.page-title="Prometheus Time Series Collection and Processing Server"
Document title of Prometheus instance.
--web.cors.origin=".*" Regex for CORS origin. It is fully anchored. Example: 'https?://(domain1|domain2)\.com'
--storage.tsdb.path="data/"
Base path for metrics storage.
--storage.tsdb.retention=STORAGE.TSDB.RETENTION
[DEPRECATED] How long to retain samples in storage. This flag has been deprecated, use "storage.tsdb.retention.time" instead.
--storage.tsdb.retention.time=STORAGE.TSDB.RETENTION.TIME
How long to retain samples in storage. When this flag is set it overrides "storage.tsdb.retention". If neither this flag nor
"storage.tsdb.retention" nor "storage.tsdb.retention.size" is set, the retention time defaults to 15d. Units Supported: y, w, d, h, m, s,
ms.
--storage.tsdb.retention.size=STORAGE.TSDB.RETENTION.SIZE
Maximum number of bytes that can be stored for blocks. A unit is required, supported units: B, KB, MB, GB, TB, PB, EB. Ex: "512MB".
--storage.tsdb.no-lockfile
Do not create lockfile in data directory.
--storage.tsdb.allow-overlapping-blocks
Allow overlapping blocks, which in turn enables vertical compaction and vertical query merge.
--storage.remote.flush-deadline=<duration>
How long to wait flushing sample on shutdown or config reload.
--storage.remote.read-sample-limit=5e7
Maximum overall number of samples to return via the remote read interface, in a single query. 0 means no limit. This limit is ignored for
streamed response types.
--storage.remote.read-concurrent-limit=10
Maximum number of concurrent remote read calls. 0 means no limit.
--storage.remote.read-max-bytes-in-frame=1048576
Maximum number of bytes in a single frame for streaming remote read response types before marshalling. Note that client might have limit
on frame size as well. 1MB as recommended by protobuf by default.
--rules.alert.for-outage-tolerance=1h
Max time to tolerate prometheus outage for restoring "for" state of alert.
--rules.alert.for-grace-period=10m
Minimum duration between alert and restored "for" state. This is maintained only for alerts with configured "for" time greater than grace
period.
--rules.alert.resend-delay=1m
Minimum amount of time to wait before resending an alert to Alertmanager.
--alertmanager.notification-queue-capacity=10000
The capacity of the queue for pending Alertmanager notifications.
--query.lookback-delta=5m The maximum lookback duration for retrieving metrics during expression evaluations and federation.
--query.timeout=2m Maximum time a query may take before being aborted.
--query.max-concurrency=20
Maximum number of queries executed concurrently.
--query.max-samples=50000000
Maximum number of samples a single query can load into memory. Note that queries will fail if they try to load more samples than this into
memory, so this also limits the number of samples a query can return.
--enable-feature= ... Comma separated feature names to enable. Valid options: exemplar-storage, expand-external-labels, memory-snapshot-on-shutdown,
promql-at-modifier, promql-negative-offset, remote-write-receiver, extra-scrape-metrics, new-service-discovery-manager. See
https://prometheus.io/docs/prometheus/latest/feature_flags/ for more details.
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
prometheus启动后会在当前界面卡住 如果ctrl+c 终止的话会关闭掉prometheus
直接执行命令:nohup./prometheus&使其后台进行运行即可 nohup 守护进程放到后台运行
使用systemctl 管理prometheus
[root@localhost prometheus-2.31.0.linux-amd64]# ln -s /data/prometheus-2.31.0.linux-amd64 /usr/local/prometheus 创建软链接到/usr/local 下
[root@localhost prometheus-2.31.0.linux-amd64]# cp prometheus.yml /etc/
[root@localhost prometheus-2.31.0.linux-amd64]# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Node Exporter
After=network.target
[Service]
ExecStart=/usr/local/prometheus/prometheus --config.file=/etc/prometheus.yml --web.read-timeout=5m --web.max-connections=10 --storage.tsdb.retention=15d --storage.tsdb.path=/prometheus/data --query.max-concurrency=20 --query.timeout=2m
User=root
[Install]
WantedBy=multi-user.target
[root@localhost prometheus-2.31.0.linux-amd64]# systemctl daemon-reload 重新加载daemon-reload
[root@localhost prometheus-2.31.0.linux-amd64]# systemctl start prometheus 启动prometheus
使用docker安装prometheus
下载prometheus的镜像
浙公网安备 33010602011771号