In this guide, we’ll cover how to leverage Consul Prepared Queries for metadata-driven traffic routing. You’ll learn to create, inspect, and update prepared queries to shift traffic seamlessly between service versions.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Consul Cluster Overview
Start by verifying your cluster members on a server node:| Node | Address | Status | Type | Protocol | Datacenter |
|---|---|---|---|---|---|
| consul-node-a | 10.0.101.110:8301 | alive | server | 2 | us-east-1 |
| consul-node-b | 10.0.101.248:8301 | alive | server | 2 | us-east-1 |
| web-server-01 | 10.0.101.177:8301 | alive | client | 2 | us-east-1 |
| web-server-02 | 10.0.101.114:8301 | alive | client | 2 | us-east-1 |
front-end-eCommerce service with version tags.
Service Registration in the Consul UI
In the Consul web interface, you’ll see two instances offront-end-eCommerce. One is tagged v7.05 and the other v8, both in the production environment.

| Server | IP Address | Version Tag |
|---|---|---|
| web-server-02 | 10.0.101.114 | v7.05 |
| web-server-01 | 10.0.101.177 | v8 |
Creating a Prepared Query
-
Save this JSON as
prepared-query.json: -
Register the query via Consul’s HTTP API:
Response:
-
Inspect the full query definition:
Set
"OnlyPassing": true in your query definition to ensure only healthy service instances are returned.Querying via DNS
Consul exposes prepared queries under the*.query.consul DNS domain. Run:
v7.05 instance:
Updating the Prepared Query to v8
When it’s time to shift traffic to versionv8, update prepared-query.json:
PUT request (replace the ID):
Always replace the query ID in your API URL when inspecting or updating prepared queries.