Skip to content

Consul

Comprehensive HashiCorp Consul commands and workflows for service discovery, configuration management, and service mesh.

Installation & Setup

CommandDescription
consul versionShow Consul version
consul agent -devStart development agent
consul agent -config-dir=/etc/consul.dStart with configuration
consul membersList cluster members
consul infoShow agent information

Agent Management

Basic Agent Operations

CommandDescription
consul agent -server -bootstrap-expect=3Start server agent
consul agent -client=0.0.0.0Start client agent
consul join 192.168.1.100Join cluster
consul leaveGracefully leave cluster
consul reloadReload configuration

Agent Configuration

CommandDescription
consul validate /etc/consul.dValidate configuration
consul configtestTest configuration

Service Discovery

Service Registration

CommandDescription
consul services register service.jsonRegister service from file
consul services deregister service-idDeregister service
consul catalog servicesList all services
consul catalog nodesList all nodes

Service Queries

CommandDescription
consul catalog service webList instances of service
consul catalog service web -tag productionFilter by tag
consul health service webHealth check status
consul health node node1Node health status

DNS Interface

CommandDescription
dig @127.0.0.1 -p 8600 web.service.consulQuery service via DNS
dig @127.0.0.1 -p 8600 web.service.dc1.consulQuery specific datacenter
dig @127.0.0.1 -p 8600 node1.node.consulQuery node via DNS

Key-Value Store

KV Operations

CommandDescription
consul kv put config/database/url "postgresql://..."Store key-value
consul kv get config/database/urlRetrieve value
consul kv get -recurse config/Get all keys under prefix
consul kv delete config/database/urlDelete key
consul kv delete -recurse config/Delete all keys under prefix

KV Advanced Operations

CommandDescription
consul kv put -cas -modify-index=123 config/app/version "2.0"Conditional update
consul kv get -detailed config/app/versionGet with metadata
consul kv export config/Export keys
consul kv import @backup.jsonImport keys

Health Checks

Health Check Management

CommandDescription
consul health checksList all health checks
consul health checks webList checks for service
consul health state criticalList critical checks
consul health state passingList passing checks

Access Control Lists (ACLs)

ACL Management

CommandDescription
consul acl bootstrapBootstrap ACL system
consul acl token create -description="Web service token"Create token
consul acl token listList tokens
consul acl token delete TOKEN_IDDelete token

ACL Policies

CommandDescription
consul acl policy create -name web-policy -rules @policy.hclCreate policy
consul acl policy listList policies
consul acl policy read web-policyRead policy
consul acl policy update -id POLICY_ID -rules @new-policy.hclUpdate policy

Connect (Service Mesh)

Connect Configuration

CommandDescription
consul connect ca get-configGet CA configuration
consul connect ca set-config -config-file ca.jsonSet CA configuration
consul connect proxy -service webStart Connect proxy

Intentions

CommandDescription
consul intention create web dbAllow web to connect to db
consul intention create -deny web cacheDeny web to cache
consul intention listList all intentions
consul intention delete web dbDelete intention

Configuration Entries

Service Configuration

CommandDescription
consul config write service-defaults.hclWrite service defaults
consul config write proxy-defaults.hclWrite proxy defaults
consul config list -kind service-defaultsList configurations
consul config read -kind service-defaults -name webRead configuration
consul config delete -kind service-defaults -name webDelete configuration

Snapshots and Backups

Snapshot Operations

CommandDescription
consul snapshot save backup.snapCreate snapshot
consul snapshot restore backup.snapRestore snapshot
consul snapshot inspect backup.snapInspect snapshot

Monitoring and Debugging

Monitoring Commands

CommandDescription
consul monitorStream logs
consul monitor -log-level=DEBUGDebug level logs
consul debugCollect debug information
consul operator raft list-peersList Raft peers

Performance

CommandDescription
consul operator autopilot get-configGet autopilot config
consul operator autopilot set-config -cleanup-dead-servers=trueSet autopilot config

Configuration Examples

Server Configuration

hcl
datacenter = "dc1"
data_dir = "/opt/consul"
log_level = "INFO"
node_name = "consul-server-1"
server = true
bootstrap_expect = 3
retry_join = ["10.0.1.10", "10.0.1.11"]

bind_addr = "10.0.1.10"
client_addr = "0.0.0.0"

ui_config {
  enabled = true
}

connect {
  enabled = true
}

acl = {
  enabled = true
  default_policy = "deny"
  enable_token_persistence = true
}

Client Configuration

hcl
datacenter = "dc1"
data_dir = "/opt/consul"
log_level = "INFO"
node_name = "consul-client-1"
retry_join = ["10.0.1.10", "10.0.1.11", "10.0.1.12"]

bind_addr = "10.0.1.20"
client_addr = "127.0.0.1"

services {
  name = "web"
  port = 80
  tags = ["production", "v1.0"]
  
  check {
    http = "http://localhost:80/health"
    interval = "10s"
  }
}

Service Definition

json
{
  "service": {
    "name": "web",
    "port": 80,
    "tags": ["production"],
    "check": {
      "http": "http://localhost:80/health",
      "interval": "10s"
    },
    "connect": {
      "sidecar_service": {}
    }
  }
}

ACL Policy

hcl
node_prefix "" {
  policy = "read"
}

service_prefix "" {
  policy = "read"
}

service "web" {
  policy = "write"
}

key_prefix "config/web/" {
  policy = "write"
}

session_prefix "" {
  policy = "read"
}

Service Mesh Configuration

Proxy Defaults

hcl
Kind = "proxy-defaults"
Name = "global"

Config {
  protocol = "http"
}

MeshGateway {
  Mode = "local"
}

Service Defaults

hcl
Kind = "service-defaults"
Name = "web"

Protocol = "http"

MeshGateway {
  Mode = "local"
}

Expose {
  Checks = true
  Paths = [
    {
      Path = "/health"
      LocalPathPort = 8080
      ListenerPort = 21500
    }
  ]
}

Multi-Datacenter Setup

WAN Federation

CommandDescription
consul join -wan 192.168.2.10Join WAN
consul members -wanList WAN members
consul catalog datacentersList datacenters

Cross-DC Queries

CommandDescription
consul catalog service web -datacenter dc2Query service in DC2
dig @127.0.0.1 -p 8600 web.service.dc2.consulDNS query to DC2

Troubleshooting

Common Issues

CommandDescription
consul operator raft list-peersCheck Raft cluster state
consul debug -duration=30sCollect debug info
consul validate /etc/consul.dValidate configuration
consul members -detailedDetailed member information

Log Analysis

CommandDescription
consul monitor -log-level=TRACETrace level logging
journalctl -u consul -fFollow systemd logs

Best Practices

Security

  1. Enable ACLs: Always use ACLs in production
  2. TLS Encryption: Enable TLS for all communication
  3. Gossip Encryption: Use gossip encryption
  4. Network Segmentation: Proper network security
  5. Token Management: Rotate tokens regularly

Performance

  1. Resource Allocation: Adequate CPU and memory
  2. Network Latency: Minimize network latency
  3. Disk I/O: Use fast storage for data directory
  4. Cluster Size: Optimal cluster sizing
  5. Monitoring: Comprehensive monitoring setup

Operations

  1. Backup Strategy: Regular snapshots
  2. Upgrade Planning: Careful upgrade procedures
  3. Health Monitoring: Monitor service health
  4. Capacity Planning: Plan for growth
  5. Documentation: Document service topology

Development

  1. Service Registration: Proper service definitions
  2. Health Checks: Comprehensive health checks
  3. Configuration Management: Use KV store effectively
  4. Service Discovery: Implement proper discovery patterns
  5. Testing: Test service mesh configurations