Skip to main content

HashiCorp Consul

Service networking for any runtime, any cloud. Consul provides service discovery, health checking, distributed key-value configuration, and a zero-trust service mesh with automatic mTLS across VMs, containers, and Kubernetes.

✓ Multi-platform service discovery ✓ Health-aware load balancing ✓ Consul Connect mTLS mesh ✓ Distributed KV configuration

Docker Quickstart

# Start a Consul dev agent (in-memory, single node)
docker run -d \
  --name consul \
  -p 8500:8500 \
  -p 8600:8600/udp \
  hashicorp/consul:latest \
  consul agent -dev -client=0.0.0.0

# Access the web UI at http://localhost:8500

# Install the Consul CLI (macOS)
brew install consul

# Point CLI at local agent
export CONSUL_HTTP_ADDR=http://localhost:8500

# Check cluster members
consul members

# Verify health
consul operator autopilot get-config

Service Registration and Health Checks

# Register a service via config file (drop in /etc/consul.d/)
# /etc/consul.d/web.json
{
  "service": {
    "name": "web",
    "id": "web-1",
    "tags": ["production", "v2"],
    "address": "10.0.0.10",
    "port": 8080,
    "check": {
      "id": "web-health",
      "name": "HTTP health check",
      "http": "http://localhost:8080/health",
      "method": "GET",
      "interval": "10s",
      "timeout": "3s"
    }
  }
}

# Register via HTTP API
curl -X PUT http://localhost:8500/v1/agent/service/register \
  -H 'Content-Type: application/json' \
  -d '{
    "Name": "api",
    "Port": 3000,
    "Check": {
      "TCP": "localhost:3000",
      "Interval": "10s"
    }
  }'

# Query registered services
consul catalog services

# Check service health
consul health service web --passing

DNS Interface for Service Discovery

# Consul provides a DNS interface at port 8600
# Services are resolvable as: service-name.service.consul

# Resolve healthy instances of the 'web' service
dig @127.0.0.1 -p 8600 web.service.consul

# Resolve with specific tag (only production instances)
dig @127.0.0.1 -p 8600 production.web.service.consul

# Resolve SRV record (includes port information)
dig @127.0.0.1 -p 8600 web.service.consul SRV

# Configure system DNS to use Consul (Linux with systemd-resolved)
cat > /etc/systemd/resolved.conf.d/consul.conf <<EOF
[Resolve]
DNS=127.0.0.1:8600
Domains=~consul
EOF
systemctl restart systemd-resolved

# Now applications can just use: http://web.service.consul:8080

Key-Value Store for Configuration

# Write configuration values
consul kv put config/my-app/db_host "postgres.service.consul"
consul kv put config/my-app/max_connections "50"
consul kv put config/my-app/feature_flags '{"dark_mode": true, "beta": false}'

# Read a value
consul kv get config/my-app/db_host

# List all keys under a prefix
consul kv get --recurse config/my-app/

# Watch for changes (blocks until key changes, then prints new value)
consul watch -type=key -key=config/my-app/db_host cat

# Delete a key
consul kv delete config/my-app/old_setting

# Application integration (Go example):
# client, _ := api.NewClient(api.DefaultConfig())
# kv := client.KV()
# pair, _, _ := kv.Get("config/my-app/db_host", nil)
# fmt.Println(string(pair.Value))

Consul Connect: mTLS Service Mesh

# Enable Connect in Consul config
{
  "connect": {
    "enabled": true
  }
}

# Register a service with Connect sidecar proxy
{
  "service": {
    "name": "api",
    "port": 8080,
    "connect": {
      "sidecar_service": {
        "proxy": {
          "upstreams": [
            {
              "destination_name": "database",
              "local_bind_port": 5432
            }
          ]
        }
      }
    }
  }
}

# Create an Intention: allow api → database
consul intention create api database

# Block all traffic except explicitly allowed pairs
consul intention create -deny "*" "*"
consul intention create api database    # allow
consul intention create frontend api    # allow

# Traffic flow with Connect:
# frontend:app → frontend:sidecar(mTLS) → api:sidecar(mTLS) → api:app
# All traffic encrypted, all connections authorized by intentions

Consul vs Kubernetes DNS vs Istio

Feature Consul Kubernetes DNS (kube-dns) Istio
Multi-platform Yes (VMs + containers) Kubernetes only Kubernetes only
Service mesh / mTLS Yes (Connect) No Yes (advanced)
Traffic management Basic (intentions) None Advanced (canary, retries)
KV store Yes No No
Complexity Medium Low High
Best for Multi-cloud, hybrid, HashiCorp stack Simple K8s service discovery Advanced K8s traffic management

Frequently Asked Questions

What is Consul and what are its main use cases?▼

HashiCorp Consul is a distributed service networking solution with three core capabilities: service discovery (finding where services are running), health checking (automatically routing traffic away from unhealthy instances), and key-value store (distributed configuration). With Consul Connect, it also provides a service mesh with automatic mTLS between services. Use cases include: microservices communicating across dynamic IP addresses, automatic failover when a service crashes, distributed configuration management, and zero-trust networking between services with encrypted mTLS connections.

How does Consul service discovery differ from Kubernetes native DNS?▼

Kubernetes kube-dns provides basic DNS-based service discovery within a single cluster using service names like my-service.namespace.svc.cluster.local. Consul extends this to multi-cluster and hybrid environments (VMs + containers), provides health checking at the service level (not just pod level), supports external services not running in Kubernetes, and integrates with non-Kubernetes environments like legacy VMs. For pure Kubernetes-only workloads, kube-dns is simpler. Consul shines in multi-datacenter setups, hybrid cloud, or when you need fine-grained health checks.

What is Consul Connect and how does mTLS work?▼

Consul Connect is the service mesh component of Consul. It automatically provisions TLS certificates for each service and configures sidecar proxies (Envoy by default) to enforce mutual TLS (mTLS) between services. When service A calls service B, both sides authenticate via certificates issued by Consul's built-in CA. Intentions define which services are allowed to communicate — by default, all communication is blocked and you explicitly allow pairs. This implements zero-trust networking: services cannot communicate unless explicitly permitted, and all traffic is encrypted in transit.

How do I register a service in Consul?▼

Services can be registered in three ways: (1) Config file on the Consul agent — drop a JSON or HCL file in the Consul config directory with the service definition. (2) HTTP API — POST to /v1/agent/service/register with the service definition JSON. (3) Kubernetes integration — deploy the Consul Helm chart and annotate pods with consul.hashicorp.com/connect-inject: 'true'; Consul automatically registers the pods as services. The service definition includes name, port, tags, and health checks (HTTP, TCP, script, or TTL-based).

How does the Consul KV store compare to etcd or Redis for configuration?▼

Consul's KV store is designed for distributed configuration — it supports watches (get notified when a key changes), transactions, and replication across datacenters. It is not designed for high-throughput writes or large values (unlike Redis). Compared to etcd, Consul KV is simpler to use (REST API, web UI, CLI) and integrates naturally with Consul's service discovery and health checking. etcd is optimized for Kubernetes metadata storage with stronger consistency guarantees. Use Consul KV for application configuration that changes occasionally, Vault for secrets, and Redis for caching or high-frequency data.

What is the difference between Consul, Istio, and Kubernetes native service mesh?▼

Consul Connect uses Envoy proxies but is simpler to configure and operates across multiple platforms (VMs, containers, bare metal). Istio is Kubernetes-native with more advanced traffic management (canary, circuit breaking, retries, timeouts), richer observability, and more complex configuration. Kubernetes Gateway API provides basic ingress/service networking but is not a full service mesh. Consul wins for multi-platform, hybrid, or HashiCorp stack users. Istio wins for teams needing advanced Kubernetes traffic management. For simple Kubernetes mTLS without a full service mesh, Cilium or Linkerd are lighter alternatives.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →