Introduction
When running uptime tests in a dynamic environment where service endpoints change frequently, manually updating the uptime test check list becomes impractical. Tyk supports configuring uptime tests to use service discovery, allowing the Gateway to dynamically discover which endpoints to monitor. This is particularly useful when:- Your services are managed by container orchestrators (Kubernetes, Docker Swarm)
- You use service registries (Consul, etcd, Eureka, Zookeeper)
- Service endpoints change frequently due to scaling or failover
- You want to automatically monitor all healthy instances of a service
Prerequisites
Before configuring service discovery for uptime tests, ensure you have:- Uptime tests enabled in your Gateway configuration (
tyk.conf). See Uptime Tests for initial setup. - A service discovery endpoint that returns your service endpoints in a supported JSON format.
- Tyk Self-Managed installation (uptime tests are not available on Tyk Cloud).
How It Works
When service discovery is enabled for uptime tests:- Tyk queries your service discovery endpoint at the configured interval
- The response is parsed according to your
data_pathconfiguration to extract endpoint URLs - Tyk generates uptime test entries for each discovered endpoint
- The uptime tests run against all discovered endpoints
- When endpoints are added or removed from the service registry, Tyk automatically updates the test list
Query Endpoint JSON Structure
The service discovery endpoint must return JSON that Tyk can parse to extract endpoint URLs. The structure depends on your service discovery system, but Tyk expects to find endpoint information at a path you specify.Simple JSON Structure
For a simple response where the endpoint URL is directly accessible:Nested JSON Structure
When the endpoint information is stored as a JSON string within the response:Target List Structure
When you have multiple endpoints to monitor (common with load-balanced services):API Definition Configuration
To configure service discovery for uptime tests in your API definition, add theservice_discovery configuration within the uptime_tests section:
Configuration Options
| Option | Type | Required | Description |
|---|---|---|---|
use_discovery_service | boolean | Yes | Set to true to enable service discovery for uptime tests |
query_endpoint | string | Yes | The URL of your service discovery endpoint (Consul, etcd, Eureka, etc.) |
data_path | string | Yes | The JSON path to the hostname/address field in the response |
parent_data_path | string | No | Used with use_nested_query to specify the path to a JSON-encoded string containing the actual data |
port_data_path | string | No | The JSON path to the port field if separate from hostname |
use_nested_query | boolean | No | Set to true if the endpoint data is within a JSON-encoded string |
use_target_list | boolean | No | Set to true if the response contains multiple endpoints |
cache_timeout | integer | No | How long (in seconds) to cache the discovered endpoints. Default: 60 |
target_path | string | No | A path to append to discovered endpoints (e.g., /health for health check endpoints) |
Service Discovery Examples
Consul Example
Consul’s health endpoint returns healthy service instances. Configure Tyk to query Consul and extract service addresses: Query Endpoint:etcd Example
For etcd v2 API returning service endpoints: Query Endpoint:etcd with Nested JSON Example
When etcd stores structured JSON as a string value: Sample Response:Eureka Example
For Netflix Eureka service registry: Query Endpoint:Eureka returns XML by default. To receive JSON, either configure your Eureka endpoint or use Tyk to add an
Accept: application/json header via an API definition that proxies to Eureka.Dashboard Configuration
To configure service discovery for uptime tests via the Tyk Dashboard: Step 1: Navigate to the API From the Dashboard, select APIs from the menu and choose the API you want to configure. Step 2: Open the Uptime Tests tab Click on the Uptime Tests tab in the API Designer. Step 3: Enable Service Discovery In the uptime tests configuration section, enable service discovery and fill in the required fields:- Query Endpoint: Enter your service discovery URL
- Data Path: Specify the JSON path to the endpoint value
- Target Path: Optionally add a path to append (e.g.,
/health) - Cache Timeout: Set how often to refresh the endpoint list
- Use Target List: Enable if multiple endpoints are returned
- Use Nested Query: Enable if data is JSON-encoded within the response
Combining Static and Dynamic Tests
You can combine static uptime test entries with service discovery. Any entries in thecheck_list array will be tested alongside dynamically discovered endpoints:
Integration with Load Balancing
When using uptime tests with service discovery, you can enable automatic failover by settingcheck_host_against_uptime_tests in your proxy configuration. This ensures that hosts detected as down are skipped during load balancing:
The fully qualified host, including the port, must be exactly the same between the uptime test configuration and the load balancer target list for Tyk to link them together. For example:
10.0.0.1:8080.Troubleshooting
No endpoints discovered
If uptime tests are not running against your service discovery endpoints:- Verify the query endpoint is accessible from the Tyk Gateway
- Check the JSON response matches your
data_pathconfiguration - Enable debug logging in Tyk to see service discovery queries
- Verify
use_discovery_serviceis set totrue
Incorrect endpoints discovered
If wrong or malformed endpoints are being tested:- Check the
data_pathmatches the actual JSON structure - Verify
use_nested_queryis set correctly for your response format - Ensure
port_data_pathis correct if ports are separate from hostnames - Test your query endpoint directly and verify the response structure
Cache not refreshing
If endpoint changes are not reflected:- Reduce
cache_timeoutto refresh more frequently - Check that your service registry is returning updated information
- Restart the Gateway to force a cache refresh if needed
Related Documentation
- Uptime Tests - Core uptime test configuration
- Service Discovery - Service discovery for API proxy configuration
- Load Balancing - Configuring load balancing in Tyk
- Circuit Breakers - Automatic circuit breaking for failed endpoints