Consul Service Discovery Questions

I just want to know answers to the following questions which I didn’t find in documentation

  1. Can Consul report change in service ip-address and health status dynamically? For example, if there is a change in service health status, can that be notified to all other services immediately?
  2. How can Consul know about cluster ips to join using DNS? This way ips can be configured dynamically.

Yes, Consul’s API has a feature called blocking queries which allows an API client to be notified when a resource changes state on the servers. This state change can be a change in IP, health status, or any other value associated with the resource.

Many of the API SDK’s that are available for Consul already have support for blocking queries.

If you want to leverage blocking queries without using an SDK, you can define watches within the Consul agent configuration that execute a CLI command or HTTP request when a resource changes. See Consul’s docs on Watches for more info.

Watches can also be set up outside of the agent’s configuration by using the consul watch CLI command running as a daemon. consul watch only supports executing script handlers. It does not support the HTTP handler type.

I do want to note that if you have configured your operating system to forward DNS to Consul, and your applications are using service lookups to find upstream services, then you do not need to use blocking queries to be notified when there are changes to a service’s IP and/or health status. Consul’s DNS server will always return healthy instance IPs in the DNS response, assuming health check are configured for the upstream service. As soon as a service becomes unhealthy, it will be removed from the DNS response list.

I assume you’re referring to the join/retry_join addresses that tell an agent how to join the cluster. If so, Consul does support specifying a DNS address so that you do not have to hard code an IP address in the agent configuration.

$ consul agent -retry-join "consul.domain.internal"
1 Like