How to Monitor a HashiCorp Consul Cluster with Prometheus and Grafana: Tracking Service Discovery, Raft Consensus, and Node Health

Monitoring tutorial - IT technology blog
Monitoring tutorial - IT technology blog

The Problem: A 2 AM Pager Alert and the Nightmare of Consul Losing Its Leader

At 2 AM, my phone wouldn’t stop buzzing with a flood of 502 Bad Gateway alerts from the API Gateway. Microservices suddenly couldn’t find each other. Application logs reported no healthy upstream even though every container was running fine. In a system using HashiCorp Consul for Service Discovery and as a Key-Value Store, the worst thing at midnight is not knowing whether the issue lies in the network, a service, or the Consul Cluster itself.

In the past, whenever an incident like this hit, I had to SSH into each server to hunt down the root cause. That night, I spent nearly 40 minutes just logging into 3 Consul Servers and over 20 Client nodes one by one.

After running consul members and consul operator raft list-peers, I had to curl /v1/health/state/critical and wade through massive blobs of JSON. Because the logs contained sensitive internal IPs and service names, I pasted them straight into the JSON Formatter & Validator on ToolCraft to make them readable. This tool runs 100% client-side in the browser and never sends data to a server, making it safe to use with production logs and configs.

Right after that night shift, I set up a centralized monitoring stack with Prometheus and Grafana. Now, opening a single dashboard reveals the entire cluster state—no more stumbling around with SSH. (This guide assumes you already have Prometheus and Grafana running in your infrastructure.)

Core Concepts: What Should You Monitor in a Consul Cluster?

Consul exports hundreds of metrics, but to diagnose issues quickly, you only need to keep a close eye on these three metric groups.

1. Raft Consensus (The Heart of Coordination)

Consul Servers synchronize configuration data using the Raft algorithm. In a cluster, there is always strictly 1 Leader, while the rest are Followers. If flaky networking or overloaded disk I/O causes Followers to miss heartbeats, the cluster will repeatedly trigger leader elections. When that happens, all write operations and new service registrations stall.

  • Leader Status: Must always equal 1 across the entire cluster.
  • Raft Last Contact: Communication latency from the Leader to Followers. By default, Consul’s heartbeat_timeout on a LAN is 1000ms, so if this metric spikes above 200ms, trouble is brewing in the cluster.
  • Raft Apply: The rate of transactions committed to the state store, helping you spot write bottlenecks early.

2. Service Discovery & Health Checks

This directly impacts user traffic. Consul continuously runs health checks (HTTP, TCP, or scripts) against every microservice. When a service transitions to warning or critical, Consul immediately pulls it from DNS and the Catalog. Monitoring this group helps you catch flapping services before customers start reporting errors.

3. Node Health & Serf Gossip

Consul uses the Serf (gossip) protocol so nodes can automatically discover each other across LAN/WAN. When a node drops UDP packets or its CPU is too overloaded to reply to gossip pings in time, it gets marked as failed or flaps continuously. Also, keep an eye on memory usage (alloc_bytes) and the goroutine count of the consul process. If goroutines jump from a few hundred to tens of thousands, the process is at high risk of an unexpected OOM Kill.

Step-by-Step Guide: Configuring Consul Telemetry, Prometheus, and Grafana

Step 1: Enable Prometheus Telemetry on Consul

Consul natively supports the Prometheus metric format, so you don’t need to install an extra exporter for the core layer. Open the configuration file on your Consul Servers (typically /etc/consul.d/consul.hcl) and add the telemetry block:

telemetry {
  prometheus_retention_time = "30s"
  disable_hostname          = true
}

The prometheus_retention_time setting keeps metrics in memory long enough for Prometheus to scrape them. You should set this slightly higher than your scrape_interval (for example, 30s when scraping every 15s). Additionally, disable_hostname = true strips the hostname prefix from metric names, preventing unnecessary cardinality explosion.

Once saved, reload the Consul configuration without disrupting the cluster (if changing prometheus_retention_time doesn’t take effect immediately on older versions, perform a rolling restart node by node):

sudo consul reload
# Quickly verify the metrics endpoint
curl -s "http://127.0.0.1:8500/v1/agent/metrics?format=prometheus" | head -n 20

If you see lines starting with # HELP consul_ and # TYPE consul_ returned, you’ve successfully enabled telemetry.

Step 2: Configure Prometheus to Scrape the Consul Cluster

Open prometheus.yml on your Prometheus server and add a new job. For 3 static Consul Servers, using static_configs is the fastest approach. If you want to automatically discover all Consul Agents, you can switch to consul_sd_configs:

scrape_configs:
  - job_name: 'consul-cluster'
    metrics_path: '/v1/agent/metrics'
    params:
      format: ['prometheus']
    scrape_interval: 15s
    static_configs:
      - targets:
          - '10.0.1.11:8500'
          - '10.0.1.12:8500'
          - '10.0.1.13:8500'

One quick note: if your cluster has ACLs enabled, you’ll need to create a token with agent:read permissions for Prometheus and add authorization (or bearer_token) to the job above. Whenever I need to convert between Consul’s JSON config and Prometheus’s YAML, I usually use the YAML ↔ JSON Converter on ToolCraft to ensure proper indentation. That saves Prometheus from syntax errors when restarting in a hurry. Next, reload Prometheus (requires Prometheus to be started with the --web.enable-lifecycle flag):

curl -X POST http://localhost:9090/-/reload

Step 3: Essential PromQL Queries and Grafana Dashboard

Now that data is flowing into Prometheus, try running these 5 core PromQL queries in the Graph tab or add them directly to a Grafana panel:

# 1. Check the number of Raft Leaders (must be equal to 1)
sum(consul_raft_state_leader)

# 2. p99 communication latency from Leader to Follower (in ms)
max(consul_raft_leader_lastContact{quantile="0.99"})

# 3. Raft transaction commit rate per second
rate(consul_raft_apply[1m])

# 4. Detect nodes experiencing Serf flapping over the last 5 minutes
rate(consul_serf_member_flap[5m]) > 0

# 5. Memory allocated to the Consul agent (MB)
consul_runtime_alloc_bytes / 1024 / 1024

Pro tip: The /v1/agent/metrics endpoint focuses on Raft, Serf, and agent runtime metrics. If you want to track detailed critical/warning states for individual services via metrics like consul_health_service_status, you should also run the official Prometheus consul_exporter.

To avoid building a dashboard from scratch, go to Dashboards → Import in Grafana and enter ID 10642 (HashiCorp Consul Monitoring) or 14518. Select the Prometheus Data Source you just configured, and you’ll immediately get charts for Raft Leader status, RPC calls, and individual node health.

Finally, be sure to set up Alert Rules for two critical thresholds: sum(consul_raft_state_leader) != 1 (lost Leader or split-brain) and max(consul_raft_leader_lastContact{quantile="0.99"}) > 200 (network or disk congestion starting to build).

When I need to cross-reference alert timestamps with Unix timestamps in Consul logs or the Prometheus API, I quickly open Timestamp Converter to convert them to local time (UTC+7) in seconds. ToolCraft supports 4 languages including Vietnamese, has no ads, and requires no sign-up, which makes midnight debugging far less stressful.

Conclusion

Combining Prometheus and Grafana gives you full visibility into your Consul Cluster—from the Raft consensus layer and each node’s Serf state to overall system health. With just a few lines in the telemetry block and 5 foundational PromQL queries, isolating an incident takes tens of seconds instead of digging through endless SSH sessions at 2 AM. If your environment has Consul ACLs or TLS enabled, your next step is adding certificates to Prometheus’s tls_config to encrypt the entire metric scraping pipeline.

Share: