使用 Prometheus 和 Grafana 监控 Consul

2024-09-05 09:48

本文主要是介绍使用 Prometheus 和 Grafana 监控 Consul,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用 Prometheus 和 Grafana 监控 Consul

使用 Prometheus 和 Grafana 监控 Consul ,便于了解 Consul当前的状态,使用 Docker分别启动多个容器

启动 Consul

  • 创建配置文件
mkdir -p ~/docker/consul/config/config
vi ~/docker/consul/config/config.json

添加以下内容,目的是为了启用 Consul 的 Prometheus,否则会在调用相关端口时提示 415 Unsupport Media TypePrometheus is not enabled since its retention time is not positive

{"telemetry": {"prometheus_retention_time": "480h","disable_hostname": true}
}
  • 启动容器

将当前目录挂载到容器的config目录下

docker run \-d \--name consul \-v ~/docker/consul/config:/consul/config \-p 8500:8500 \consul
  • 检查是否正常

如果正常的话会返回相应的 Prometheus 数据

curl http://127.0.0.1:8500/v1/agent/metrics\?format\=prometheus

启动 Prometheus

  • 创建配置文件
mkdir -p ~/docker/prometheus/config
vi ~/docker/prometheus/config/prometheus.yml

添加以下内容:

global:scrape_interval: 15sscrape_timeout: 10sevaluation_interval: 15s
alerting:alertmanagers:- static_configs:- targets: []scheme: httptimeout: 10sapi_version: v1
scrape_configs:
- job_name: prometheushonor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: /metricsscheme: httpstatic_configs:- targets:- localhost:9090
- job_name: consulhonor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: '/v1/agent/metrics'scheme: httpstatic_configs:- targets:- host.docker.internal:8500

其中targets的值为 host.docker.internal:8500,目的是为了从容器中通过宿主机访问另一个容器

docker run \-d \--name prometheus \-p 9090:9090 \-v ~/docker/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml \prom/prometheus    

待启动后访问 http://localhost:9090/graph,可以看到 Prometheus的页面,选择 Status => Targets,可以看到 Consul 和 Prometheus 的状态都是UP

consul-prometheus-targets.png

启动 Grafana

  • 启动容器
docker run \-d \--name=grafana \-p 3000:3000 \grafana/grafana
  • 访问Grafana

访问 http://localhost:3000/,用户名和密码都是 admin,输入后重新设置密码

  • 创建数据源

Dashboard 点击 Create a datasource,选择 Prometheus,URL 输入 http://host.docker.internal:9090,点击 Save & Test,看到访问成功

consul-grafana-datasource.png

  • 添加监控看板

点击左侧+按钮,选择 import,输入Dashboard ID为 10642,选择Prometheus为刚才添加的数据源,点击Import后即可看到监控面板

consul-grafana-dashboard.png

consul-grafana-dashboard-data.png


遇到的问题

415 Unsupported Media Type

Consul 启动之后Prometheus的Target提示 415 Unsupported Media Type

  • 415 Unsupported Media Type
  • Prometheus is not enabled since its retention time is not positive* Closing connection 0

consul-prometheus-target-error.png

通过CURL访问数据接口,发现提示同样的问题:

curl -v localhost:8500/v1/agent/metrics\?format=prometheus
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8500 (#0)
> GET /v1/agent/metrics?format=prometheus HTTP/1.1
> Host: localhost:8500
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 415 Unsupported Media Type
< Vary: Accept-Encoding
< Date: Thu, 07 May 2020 00:09:23 GMT
< Content-Length: 66
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
Prometheus is not enabled since its retention time is not positive* Closing connection 0

解决方法是修改Consul的配置,将自定义的配置挂载到容器 /consul/config目录下

config.json

{"telemetry": {"prometheus_retention_time": "480h","disable_hostname": true}
}

“INVALID” is not a valid start token

这个问题是因为数据格式不正确导致的,通过指定拉取的数据格式即可解决,在 Prometheus 的配置文件添加中param 参数,这样会在请求时将参数附加在链接中

- job_name: consulhonor_timestamps: truescrape_interval: 15sscrape_timeout: 10smetrics_path: '/v1/agent/metrics'scheme: httpparam: format: ["prometheus"]static_configs:- targets:- host.docker.internal:8500

这篇关于使用 Prometheus 和 Grafana 监控 Consul的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138617

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四