使用 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

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Rust中的注释使用解读

《Rust中的注释使用解读》本文介绍了Rust中的行注释、块注释和文档注释的使用方法,通过示例展示了如何在实际代码中应用这些注释,以提高代码的可读性和可维护性... 目录Rust 中的注释使用指南1. 行注释示例:行注释2. 块注释示例:块注释3. 文档注释示例:文档注释4. 综合示例总结Rust 中的注释

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

Java使用Tesseract-OCR实战教程

《Java使用Tesseract-OCR实战教程》本文介绍了如何在Java中使用Tesseract-OCR进行文本提取,包括Tesseract-OCR的安装、中文训练库的配置、依赖库的引入以及具体的代... 目录Java使用Tesseract-OCRTesseract-OCR安装配置中文训练库引入依赖代码实

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台