DEV Community

Bartłomiej Danek
Bartłomiej Danek

Posted on • Originally published at bard.sh

Consul health checks

consul health checks

Consul supports five types of health checks - pick the one that fits your service.

  • http - GET request, expects 2xx
  • tcp - opens a TCP connection, expects it to succeed
  • grpc - sends a gRPC health check request
  • script - runs a command, checks exit code
  • ttl - service reports its own health by calling the API

http check

service {
  name = "api"
  port = "http"

  check {
    type     = "http"
    path     = "/health"
    interval = "10s"
    timeout  = "2s"
  }
}
Enter fullscreen mode Exit fullscreen mode

tcp check

check {
  type     = "tcp"
  interval = "10s"
  timeout  = "2s"
}
Enter fullscreen mode Exit fullscreen mode

grpc check

check {
  type                            = "grpc"
  interval                        = "10s"
  timeout                         = "2s"
  grpc_use_tls                    = false
}
Enter fullscreen mode Exit fullscreen mode

Optionally target a specific service within the gRPC server:

check {
  type     = "grpc"
  interval = "10s"
  timeout  = "2s"
  # grpc_service = "my.ServiceName"
}
Enter fullscreen mode Exit fullscreen mode

script check

check {
  type     = "script"
  command  = "/usr/local/bin/check-db.sh"
  args     = ["-timeout", "5s"]
  interval = "30s"
  timeout  = "10s"
}
Enter fullscreen mode Exit fullscreen mode

Exit codes: 0 = passing, 1 = warning, anything else = critical.

ttl check

Useful when the service knows its own state better than an external probe.

check {
  type                            = "ttl"
  ttl                             = "30s"
  deregister_critical_service_after = "1m"
}
Enter fullscreen mode Exit fullscreen mode

Update the status from inside the service:

curl -X PUT -d '{"Status": "passing"}' \
  http://localhost:8500/v1/agent/check/update/<check_id>
Enter fullscreen mode Exit fullscreen mode

Status values: passing, warning, critical.

multiple checks

service {
  name = "worker"
  port = "http"

  check {
    name     = "http"
    type     = "http"
    path     = "/health"
    interval = "10s"
    timeout  = "2s"
  }

  check {
    name     = "db connection"
    type     = "http"
    path     = "/health/db"
    interval = "30s"
    timeout  = "5s"
    deregister_critical_service_after = "2m"
  }
}
Enter fullscreen mode Exit fullscreen mode

Originally published at https://bard.sh/posts/consul_health_checks/

Top comments (0)