【ETCD】【实操篇(十八)】ETCD监控实战:提升系统健康与集群调试效率

在现代分布式系统中,监控是确保服务高可用性和性能稳定的重要组成部分。对于 ETCD 集群而言,监控不仅可以帮助我们检测系统健康,还能在出现故障时进行快速调试。ETCD 通过多种监控端点提供了详尽的性能指标和调试信息,本篇文章将介绍如何高效地利用 ETCD 提供的监控数据来提升集群的稳定性和可靠性。

ETCD监控概述

每个 ETCD 服务器都通过其客户端端口暴露本地监控信息,数据包括了集群状态、性能指标、日志级别调试信息等,便于进行健康检查与故障排查。ETCD 提供了多个专门的调试和监控端点,支持系统健康检查、性能分析、故障诊断等多种操作。

1. 调试端点

在 ETCD 配置中,如果启用 --log-level=debug,服务器将通过 /debug 路径暴露调试信息。这些信息对于分析 ETCD 的内部工作状态和进行故障排查非常有帮助。请注意,启用调试模式会增加日志量并可能导致性能下降,因此建议在故障排查时使用。

/debug/pprof 端点

/debug/pprof 是 Go 运行时的标准性能分析端点,可以帮助我们分析 CPU、堆内存、互斥锁和 goroutine 的使用情况。例如,可以使用 go tool pprof 获取 ETCD CPU 使用的详细情况:

bash 复制代码
$ go tool pprof http://localhost:2379/debug/pprof/profile

此命令将下载 CPU 使用的性能分析数据,帮助我们识别系统性能瓶颈。以下是一个示例输出,列出了 ETCD 中 CPU 占用最多的函数:

复制代码
C:\Users\admin>go tool pprof http://localhost:2379/debug/pprof/profile
Fetching profile over HTTP from http://localhost:2379/debug/pprof/profile
Saved profile in C:\Users\admin\pprof\pprof.etcd.exe.samples.cpu.001.pb.gz
File: etcd.exe
Build ID: D:\data\etcd.exe2024-12-19 11:11:08.2450555 +0800 CST
Type: cpu
Time: Dec 26, 2024 at 10:05am (CST)
Duration: 30.01s, Total samples = 250ms ( 0.83%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 160ms, 64.00% of 250ms total
Showing top 10 nodes out of 92
      flat  flat%   sum%        cum   cum%
      40ms 16.00% 16.00%       40ms 16.00%  runtime.stdcall1
      30ms 12.00% 28.00%       30ms 12.00%  runtime.cgocall
      20ms  8.00% 36.00%       50ms 20.00%  runtime.selectgo
      10ms  4.00% 40.00%       10ms  4.00%  bufio.(*Reader).Read
      10ms  4.00% 44.00%       10ms  4.00%  runtime.checkTimers
      10ms  4.00% 48.00%       10ms  4.00%  runtime.execute
      10ms  4.00% 52.00%       70ms 28.00%  runtime.findRunnable
      10ms  4.00% 56.00%       10ms  4.00%  runtime.gopark
      10ms  4.00% 60.00%       10ms  4.00%  runtime.growWork_fast64
      10ms  4.00% 64.00%       10ms  4.00%  runtime.mapdelete
    ...
/debug/requests 端点

此端点提供 gRPC 调用的跟踪信息和性能统计,通过浏览器可以查看每个请求的详细执行情况。例如,以下是一个对键 abcRange 请求的日志:

复制代码
/debug/requests
grpc.Recv.etcdserverpb.Auth	[0 active]	[≥0s]	[≥0.05s]	[≥0.1s]	[≥0.2s]	[≥0.5s]	[≥1s]	[≥10s]	[≥100s]	[errors]	[minute]	[hour]	[total]
grpc.Recv.etcdserverpb.Cluster	[0 active]	[≥0s]	[≥0.05s]	[≥0.1s]	[≥0.2s]	[≥0.5s]	[≥1s]	[≥10s]	[≥100s]	[errors]	[minute]	[hour]	[total]
grpc.Recv.etcdserverpb.KV	[0 active]	[≥0s]	[≥0.05s]	[≥0.1s]	[≥0.2s]	[≥0.5s]	[≥1s]	[≥10s]	[≥100s]	[errors]	[minute]	[hour]	[total]
grpc.Recv.etcdserverpb.Maintenance	[0 active]	[≥0s]	[≥0.05s]	[≥0.1s]	[≥0.2s]	[≥0.5s]	[≥1s]	[≥10s]	[≥100s]	[errors]	[minute]	[hour]	[total]
Family: grpc.Recv.etcdserverpb.KV
[Normal/Summary] [Normal/Expanded] [Traced/Summary] [Traced/Expanded]
Completed Requests
When	Elapsed (s)
2024/12/26 09:38:27.694690	0.010501	/etcdserverpb.KV/Compact
09:38:27.694690	 .      	... RPC: from 127.0.0.1:57267 deadline:4.998501s
...

2. 性能指标端点

ETCD 还通过 /metrics 端点暴露了丰富的指标,帮助我们监控集群的各项性能数据。这些指标包括磁盘延迟、请求延迟、流量等,可以通过 curl 命令获取:

bash 复制代码
http://localhost:2379/metrics

这会返回 ETCD 的相关性能数据,例如:

复制代码
# HELP etcd_cluster_version Which version is running. 1 for 'cluster_version' label with current cluster version
# TYPE etcd_cluster_version gauge
etcd_cluster_version{cluster_version="3.5"} 1
# HELP etcd_debugging_auth_revision The current revision of auth store.
# TYPE etcd_debugging_auth_revision gauge
etcd_debugging_auth_revision 9
# HELP etcd_debugging_disk_backend_commit_rebalance_duration_seconds The latency distributions of commit.rebalance called by bboltdb backend.
# TYPE etcd_debugging_disk_backend_commit_rebalance_duration_seconds histogram
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.001"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.002"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.004"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.008"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.016"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.032"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.064"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.128"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.256"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="0.512"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="1.024"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="2.048"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="4.096"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="8.192"} 25
etcd_debugging_disk_backend_commit_rebalance_duration_seconds_bucket{le="+Inf"} 25
...

3. 健康检查端点

从 ETCD v3.3.0 开始,ETCD 增加了 /health 端点,能够提供更加详细的健康检查信息。如果集群配置了互斥的客户端 TLS 认证,或者使用负载均衡器进行监控时,可以通过该端点进行健康状态的快速访问。

/livez 和 /readyz 端点

在 ETCD v3.5.12 版本中,新增了 /livez/readyz 两个健康检查端点:

  • /livez:检查 ETCD 进程是否存活。
  • /readyz:检查 ETCD 是否准备好处理流量。

例如,执行以下命令可以获取详细的健康检查结果:

bash 复制代码
curl -k http://localhost:2379/readyz?verbose

返回结果可能如下所示,表示 ETCD 的各项健康检查均正常:

复制代码
[+]data_corruption ok
[+]serializable_read ok
[+]linearizable_read ok
ok

如果需要排除某些检查,可以通过 exclude 参数来过滤,例如:

bash 复制代码
curl -k http://localhost:2379/readyz?exclude=data_corruption

4. Prometheus 集成

Prometheus 是一个流行的开源监控工具,可以轻松地收集和存储 ETCD 的监控数据。通过 Prometheus,我们可以在一个统一的平台上进行 ETCD 性能指标的可视化,帮助我们更好地理解集群的状态。

安装 Prometheus

首先,下载并安装 Prometheus,下载及安装教程可以参考
【prometheus】【实战篇(一)】使用二进制方式安装prometheus以及使用systemd管理,文末附一键安装脚本

配置 Prometheus

创建 Prometheus 配置文件,指定 ETCD 集群的监控端点:

bash 复制代码
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: node
    static_configs:
      - targets: ['localhost:9100']
  - job_name: etcd
    static_configs:
      - targets: ['192.168.188.108:2379','192.168.188.108:2384','192.168.188.108:2383']
重启Prometheus

如果是通过文章所说的方式安装的prometheus,那么则可以通过如下方式来进行重启:

sh 复制代码
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart prometheus
[root@localhost ~]# systemctl status prometheus
● prometheus.service - Prometheus
   Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2024-12-26 03:28:36 UTC; 8s ago
通过grafana的来查看监控指标

从网址https://grafana.com/grafana/dashboards/22236-etcd/下载对应的模块,然后使用导入

点击load

点击import,

最后查看对应的操作面板

总结

通过本文介绍的 ETCD 调试端点、性能指标和健康检查功能,管理员可以实时了解 ETCD 集群的运行状况,提前发现潜在的性能瓶颈或故障。在生产环境中,结合 Prometheus 进行数据抓取与监控,将大大提升 ETCD 集群的可观察性和稳定性。

相关推荐
IvorySQL7 小时前
PostgreSQL 技术日报 (3月9日)|EXPLAIN ANALYZE 计时优化与复制语法讨论
数据库·postgresql·开源
stark张宇10 小时前
MySQL 核心内幕:从索引原理、字段选型到日志机制与外键约束,一篇打通数据库任督二脉
数据库·mysql·架构
倔强的石头_10 小时前
融合数据库架构实践:关系型、JSON与全文检索的“一库多能”深度解析
数据库
星辰员12 小时前
KingbaseES数据库:ksql 命令行用户与权限全攻略,从创建到删除
数据库
谢小飞13 小时前
如何让AI用一个下午开发上架Chrome插件助我摸鱼
前端·chrome
华仔啊1 天前
千万别给数据库字段加默认值 null!真的会出问题
java·数据库·后端
小溪彼岸1 天前
谷歌发布Gemini in Chrome,Chrome正式成为AI浏览器
chrome·google
小溪彼岸1 天前
谷歌上线Gemini in Chrome,想免费使用还需打怪升级
chrome·google
随风飘的云2 天前
MySQL的慢查询优化解决思路
数据库
IvorySQL2 天前
PostgreSQL 技术日报 (3月7日)|生态更新与内核性能讨论
数据库·postgresql·开源