Convert from Nomad to Consul service discovery
Nomad offers native service discovery but this may not be sufficient for some larger and more complex designs and implementations.
Integrating Consul into your application design allows you to continue to use service discovery and additional features not offered by Nomad’s service discovery, such as health checks, traffic shaping, and more.
Open the Consul UI
Consul is running alongside Nomad on the cluster you set up as part of the deployment in the previous tutorial.
Open the Consul UI by visiting the link in the Terraform output, logging in with the token from that output, and clicking on the Services page from the left navigation.
This page shows the services currently registered with Consul. Notice that the Nomad and Consul agents running on the server and client machines are the only services present.
Update the job spec to use Consul
In the previous tutorial, you set up the HashiCups application using native service discovery in Nomad.
Switching to Consul service discovery involves updating the provider
value and the query function present in any template
stanzas.
Open the HashiCups job spec file, replace each instance of provider = "nomad"
with provider = "consul"
, and save the file. There are six lines to update, one for each of the HashiCups services. This registers the services with Consul instead of Nomad.
In the same file, replace each instance of nomadService
with service
, and save the file. There are six lines to update. This instructs Nomad to query Consul for the service address instead of its own internal service catalog.
Finally, be sure to update the load balancing configurations in the nginx
task to match the Consul service example below.
Navigate back to your terminal and re-submit the HashiCups job spec to Nomad.
Navigate to the Services page in the Consul UI from earlier and note that each of the HashiCups services are now registered with Consul.
This job uses same the load balancer URL from the previous tutorial. The URL can also be found in the Consul UI by clicking on one of the nginx services and then the instance task name.
Note
Your browser may cache the HashiCups page resulting in an error when loading the products on the page. If this happens, try a hard refresh of the page or open it in a private browser window.
You may need to scroll to the right in your browser window as the service URL is printed after the task name.
Clean up
Run terraform destroy
to clean up your provisioned infrastructure. Respond yes
to the prompt to confirm the operation.
Be sure to follow the additional cleanup steps around AMIs outlined in the AWS cluster setup tutorial so you don't incur any additional charges for image storage.
Next steps
In this collection of tutorials, you learned about the native service discovery present in Nomad, how to enable services to use it, and how to transition from native service discovery to Consul service discovery.
Check out the running Nomad workloads on edge nodes tutorial for another example that uses Nomad service discovery.