When using progrium/consul (now typically the official Consul) for service discovery and configuration, SERVICE_CHECK_HTTP is a highly useful environment variable that configures Consul's health checks for registered services. Through HTTP health checks, Consul periodically verifies the HTTP endpoints of services and determines their health status based on returned status codes.
Basic Setup Steps
-
Service Registration: First, register your service with Consul. This is typically done by setting environment variables at service startup or via Consul's API. For example, if running your service in a Docker container, set these environment variables in your Dockerfile or docker-compose.yml file.
-
Configure HTTP Health Checks: Use the
SERVICE_CHECK_HTTPenvironment variable to specify the HTTP health check endpoint. Its value must be the full URL of the service's HTTP endpoint.
Specific Example
Assume you have a web service running on port 8080 with a health check endpoint /health. Configure the HTTP health check using the following environment variables:
yamlversion: '3' services: web-service: image: your-web-service-image ports: - "8080:8080" environment: SERVICE_NAME: "web-service" SERVICE_TAGS: "production" SERVICE_CHECK_HTTP: "http://localhost:8080/health" SERVICE_CHECK_INTERVAL: "15s"
In this configuration, SERVICE_CHECK_HTTP is set to "http://localhost:8080/health", meaning Consul sends an HTTP GET request to this URL every 15 seconds to check the service's health.
Considerations
- Ensure your service has a
/healthendpoint that returns HTTP 200 when healthy and non-200 when unhealthy. - For Docker-based services, replace
localhostwith a specific service name or container name based on your network configuration. - Adjust
SERVICE_CHECK_INTERVALas needed, but ensure it detects issues promptly without imposing unnecessary load on the service.
Using this approach, the SERVICE_CHECK_HTTP environment variable with Consul provides a simple and effective method for service health checks. It ensures service reliability and availability while enabling timely responses to issues.