Consul provides service discovery and configuration capabilities, while Nomad offers a powerful job scheduler and orchestrator. By integrating these two tools, you can register a service in a custom Consul namespace using a Nomad job. This article will guide you through the process, assuming you have Consul and Nomad installed and configured/integrated correctly.
Prerequisites:
-
Nomad v0.10.4 or greater
- a Nomad environment with Nomad and Consul installed.
- Consul Enterprise Version would be required with a license which is having a capability to support namespace features.
Procedure:
Step 1: Create a namespace in Nomad using below CLI command -
nomad namespace apply nomad-test
Step 2: Create a namespace in Consul using below CLI command -
consul namespace create -name consul-test
Step 3: Create a Nomad job file: Open a text editor and create a new file to define your Nomad job configuration using the HCL (HashiCorp Configuration Language) or JSON syntax. Below is the sample example file for reference -
job "example" {
datacenters = ["dc1"]
## Nomad namespace where job will be registered.
namespace = "nomad-test"
group "cache" {
consul {
## Consul namespace where service will be registered.
namespace = "consul-test"
}
network {
mode = "bridge"
}
service {
name = "example-test"
port = "9001"
}
task "redis" {
driver = "docker"
config {
image = "redis:7"
}
}
}
}
Step 4: Submit the job to Nomad by saving the Nomad job file with an appropriate name and extension, such as "example.nomad" (Above sample job name is example). Then, open a terminal or command prompt and navigate to the directory containing the job file. Submit the job to Nomad using the following command:
nomad job run example.nomad
Step 5: After submitting the job, verify the same via Nomad UI and CLI.
- Nomad UI -
- Nomad CLI -
ID Type Priority Status Submit Date
example service 50 running 2023-05-24T04:33:09Z
- You can also see in the Nomad job's allocation that the service has been registered in Consul. Here is the snippet for your reference -
Step 6: Verify service registration in the custom Consul namespace:
- After submitting the job file, Nomad will schedule and deploy the service while registering it in the custom Consul namespace.
- To verify the registration, you can use the Consul API or the Consul UI.
- In Consul UI you can verify the service. Service will be present in Consul UI as below -
- For example, you can use the following cURL command to query the Consul API:
curl http://localhost:8500/v1/catalog/service/example-test?ns=test
Notes -
- In the above API command replace the name of the service and namespace accordingly as per your job file.
- Adjust the API endpoint and replace "localhost" if your Consul agent is running on a different host.
Conclusion:
By following the steps outlined in this article, you can leverage Nomad's capabilities to register a service in a custom Consul namespace. This integration allows for efficient service discovery and configuration within your distributed system.
Reference URL -
Consul Service Register - https://developer.hashicorp.com/consul/docs/services/usage/register-services-checks
Nomad Job Deployment - https://developer.hashicorp.com/nomad/docs/commands/job/deployments
Consul HTTP API - https://developer.hashicorp.com/consul/api-docs