# Linux进阶 | Docker Swarm+Prometheus+Grafana实现web服务集群的监控

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/12824.html | 发布：2022-11-18 | 更新：2022-11-18 | 分类：建站教程、投稿分享

## **何为Prometheus**

 

## **Prometheus****的定义**

 

Prometheus 属于一站式监控告警平台，依赖少，功能齐全。 Prometheus 支持对云的或容器的监控，其他系统主要对主机监控。 Prometheus 数据查询语句表现力更强大，内置更强大的统计函数。 Prometheus 在数据存储扩展性以及持久性上没有 InfluxDB，OpenTSDB，Sensu 好。

 

Prometheus服务，**可以直接通过目标拉取数据，或者间接地通过中间网关拉取数据**。它在本地存储抓取的所有数据，并对这些数据运行规则，**以汇总和记录现有数据中的新时间序列，或生成警报**。 **Grafana或其他API使用者可用于可视化收集的数据。**

 

Prometheus 将其指标收集并存储为时间序列数据，即指标信息与记录的时间戳一起存储，以及称为标签的可选键值对。

 

## **Prometheus****的主要特点**

 
 
- 一个多维数据模型，具有由指标名称和键/值对标识的时间序列数据 
- PromQL，一种利用这种维度的灵活查询语言 
- 不依赖分布式存储；单个服务器节点是自治的 
- 时间序列收集通过 HTTP 上的拉模型发生 
- 通过中间网关支持推送时间序列 
- 通过服务发现或静态配置发现目标 
- 多种图形和仪表板支持模式 

 

## **Prometheus****的组成**

 

**Prometheus 生态系统由多个组件组成，其中许多是可选的：**

 
 
- 抓取和存储时间序列数据的主要Prometheus服务器 
- 用于检测应用程序代码的客户端库 
- 支持短期工作的推送网关 
- HAProxy、StatsD、Graphite 等服务的专用出口商。 
- 一个警报管理器来处理警报 
- 各种支持工具 

 

**大多数 Prometheus 组件都是用Go编写的，这使得它们易于构建和部署为静态二进制文件。**

  

## Prometheus的架构

 

![Linux进阶 | Docker Swarm+Prometheus+Grafana实现web服务集群的监控](https://www.mfisp.com/wp-content/uploads/2021/10/20221118001338GdUnAQ.png)

  

**exporter****：**可以理解为客户机的一个程序，用来专门收集客户机的数据，发送给Prometheus服务器

 

**pull metrics****：**拉取数据，Prometheus server拉取数据

 

**TSDB****：**时间序列数据库

 

**short-lived jobs****：**短时间任务

 

**Pushgateway****：**短时间运行的程序可以主动将自己的数据push到pushgateway里存放，Prometheus server会在pushgateway拉取数据。

 

**service discovery****：**

 

**PromQL：**Prometheus Query language，Prometheus时序数据据库里的查询语言。

 

**Grafana：**一个图形绘制工具

 

**pull****和****push****哪个好点？**

 

对于数据实时性，push方式更好地获取实时数据

 

服务器和网络的负载，可控性而言，pull更好

 

## **Prometheus****适用场景**

 

**Prometheus非常适合记录任何纯数字时间序列。它既适用于面向服务器等硬件指标的监控，也适合于高度动态的面向服务的体系结构的监视。** 在微服务世界中，它对多维数据收集和查询的支持是一种特别的优势。

 

Prometheus的设计旨在提高可靠性，使其成为中断期间要使用的系统，以使您能够快速诊断问题。 每个Prometheus服务器都是独立的，而不依赖于网络存储或其他远程服务。 当服务出现故障时，它可以使你快速定位和诊断问题。它的搭建过程对硬件和服务没有很强的依赖关系

 

## **Prometheus****不适用场景**

 

Prometheus，主要在于可靠性，在很恶劣的环境下，都可以随时访问它和查看系统服务各种指标的统计信息。 **如果你对统计数据需要100%的精确，它并不适用**，例如：实时计费系统

 

## Prometheus的使用

 

## 方法一：使用容器安装

 

①启动容器

 

命令：docker run -d -p 9090:9090 --name lzc-prom-1 prom/prometheus

 

[root@prometheus-server ~]# docker run -d -p 9090:9090 --name lzc-prom-1 prom/prometheus  
   
Unable to find image 'prom/prometheus:latest' locally  
   
latest: Pulling from prom/prometheus  
   
aa2a8d90b84c: Pull complete  
   
b45d31ee2d7f: Pull complete  
   
71dcff1cfd3f: Pull complete  
   
2da1e2668856: Pull complete  
   
2ba2baa9a21a: Pull complete  
   
5f5f8be9bcf4: Pull complete  
   
ec3160c71979: Pull complete  
   
b5fe1b9dfdaf: Pull complete  
   
6382c577d8fc: Pull complete  
   
4dcc5723b182: Pull complete  
   
0aff4f9499c7: Pull complete  
   
69af68c28748: Pull complete  
   
Digest: sha256:ccc801f38fdac43f0ed3e1b0220777e976828d6558f8ef3baad9028e0d1797ae  
   
Status: Downloaded newer image for prom/prometheus:latest  
   
5186089f1de41e298a1c5547472bb610b0712a4689717eeb4232548de9448ca9  
   
[root@prometheus-server ~]#

   

②查看容器启动情况

 

[root@prometheus-server ~]# docker ps  
   
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES  
   
5186089f1de4 prom/prometheus "/bin/prometheus --c…" 33 seconds ago Up 28 seconds 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp lzc-prom-1

  

③访问Prometheus

 

![Linux进阶 | Docker Swarm+Prometheus+Grafana实现web服务集群的监控](https://www.mfisp.com/wp-content/uploads/2021/10/20221118001540BNXVUB.jpeg)

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
