Skip to main content
In this tutorial, we’ll walk through enhancing a Consul service definition by adding a TCP health check. You will learn how to:
  • Register a basic Consul service
  • Add a TCP health check on port 80
  • Observe and interpret the service’s health status in the Consul UI
Prerequisites
You need a running Consul agent and an Apache HTTP server on your node.

Registering a Basic Consul Service

First, create a minimal service definition and register it with Consul.
Simulate a service restart to see how Consul handles temporary outages:
When you’re done, deregister the service:
The basic service definition offers no health checks, so Consul assumes it is always healthy.

Adding a TCP Health Check

To ensure only healthy instances are returned by service discovery, update the definition to include a TCP health check on port 80. Save the following as service-with-health.json:
Register the enhanced service:
Until the first TCP check passes, Consul marks the service as failing and will not return it in discovery queries.

Observing Health Check Status

  1. Open the Consul UI.
  2. Look for the front-end-eCommerce entry under Services.
  3. If the check is still pending or failing, you’ll see a red “X” icon.
After approximately 10 seconds, Consul performs the TCP check against localhost:80. A successful connection flips the status to passing, indicated by a green checkmark.

Simulating Failure and Recovery

To test Consul’s failure detection:
Within the next interval, the Consul UI shows the failure details: Restart Apache to restore health:
After the next 10-second interval, the TCP check passes and Consul marks the service as healthy again.

Service Definition Comparison


Watch Video

Practice Lab