Integrate service discovery
Nomad easily integrates with Consul's service discovery features to helps developers and operations teams automate and monitor application services at scale. Consul tracks services by registering them into a service catalog that you can access with Consul DNS names instead of hard-coded node IP addresses or hostnames.
The next step of this monolith migration is to integrate Consul and use its features to discover, secure, and monitor the HashiCups services.
In this tutorial, you deploy two versions of HashiCups with Consul service discovery integration. One version runs all of the services on a single publicly accessible node, and the other version splits the deployment between public and private nodes.
Infrastructure overview
At the beginning of the tutorial you have a Nomad and Consul cluster with three server nodes, three private client nodes, and one publicly accessible client node. Each node runs a Consul agent and a Nomad agent.
This infrastructure matches the end state of the previous tutorial.
Prerequisites
This tutorial uses the infrastructure set up in a previous tutorial in this collection, Set up the cluster. Complete that tutorial to set up the infrastructure if you have not done so.
Deploy HashiCups on a single VM
In your terminal, navigate to the directory that contains the code from the code repository.
Review the jobspec
Navigate to the jobs
directory.
Open the 02.hashicups.nomad.hcl
jobspec file and view the contents.
This version adds service
blocks to each of the tasks and sets the provider
attribute to Consul. This configuration instructs Nomad to register services in the Consul catalog. A check
block inside of the service block configures a health check for the service.
When services are registered in the Consul catalog, you can use Consul DNS instead of the client node addresses from Nomad for upstream service resolution.
Run the job
Submit the job to Nomad. Note that if the previous version of the HashiCups job is running, this command redeploys the job with the new version.
Verify deployment
Verify that the HashiCups application deployed successfully.
Use the nomad job
command to retrieve information about the hashicups
job.
Use the consul catalog
command to verify that the services are correctly registered inside Consul's catalog.
Retrieve the HashiCups URL to verify deployment.
Output from the above command.
Copy the IP address and open it in your browser to see the HashiCups application. You do not need to specify a port as nginx is running on port 80
.
Interact with the application in your browser and observe the changes in both Consul and Nomad.
Cleanup
Stop the deployment when you are ready to move on. The -purge
flag removes the job from the system and the Nomad UI.
Deploy HashiCups on multiple VMs
In this section, you run a jobspec that deploys the HashiCups application across separate client nodes.
Review the jobspec
Open the 03.hashicups.nomad.hcl
jobspec file and review the contents.
In this jobspec, each service is part of its own group instead of being part of group "hashicups"
. As a result, Nomad can schedule each service on a different node as needed. Most groups include a constraint
so that Nomad cannot schedule it on nodes with the "ingress" role. The nginx
group also has a constraint
so that Nomad always schedules it on nodes with the "ingress" role.
Run the job
Submit the job to Nomad. Note that now the output shows the status of each service group instead of just one group like previous versions.
Be aware that Nomad's scheduling algorithm may decide to place all of the non-public services on the same private client node or spread them out over several nodes. There are three nodes that match the constraint rule of meta.nodeRole != ingress
, so your output will differ from the example.
Verify deployment
Verify that the HashiCups application deployed successfully.
Use the nomad job
command to retrieve information about the hashicups
job.
Use the consul catalog
command to verify that the services are correctly registered inside Consul's catalog.
Retrieve the HashiCups URL to verify deployment.
Output from the above command.
Copy the IP address and open it in your browser to view the HashiCups application. You do not need to specify a port because nginx is running on port 80
.
Before you proceed, interact with the Nomad and Consul UIs to explore their features and understand the relationships between Nomad jobs, allocations, and nodes and Consul services, instances, and nodes.
Cleanup
Stop the deployment when you are ready to move on.
Next steps
In this tutorial, you deployed two versions of HashiCups with Consul integration. In the first deployment, all of the services ran on one client node, while in the second deployment the services ran on different nodes.
In the next tutorial, you will integrate Consul service mesh and use an API gateway for external public access.