Nomad Service Connect Native

I have deployed 2 services with connect native=true, and both are running fine, and appear in consul service catalog with connect enabled. If one service is the backend DB and the second service is the frontend UI for example, how do I reference in the Nomad Job file ENV stanza the Consul Service Natively without a sidecar proxy? The only way I have been able to do it dynamically is with the following template:

template {
data = <<EOF
{{ range service “<db_service_name” }}
DB_HOST = “{{ .Address }}:{{ .Port }}”
{{ end }}
destination = “local/env”
env = true

Is there a cleaner way?

Hi @1Marcelo :wave:

Using templates is the definitely the easiest way to go, and how service discovery is usually done in Nomad. The only change I would add is to use the connect function instead of service to limit the query to only Connect-enabled services.

Another option would be to use Consul’s DNS interface but that will require additional setup in your nodes to properly expose the Consul API to the Nomad tasks. They steps to do it will heavily depend on your environment though, so it’s a bit hard to provide specific guidance. Here’s an example of one way to do it: Question - DNS Resolution from Consul inside of Mesh Network · Issue #8343 · hashicorp/nomad · GitHub

This will also require your application to handle SRV records to use the correct ports, so it may not be a cleaner way if it doesn’t.

Awesome, Thanks for the info… I have managed ti get the DNS Rsolution with Consul working too, with some node system config tweaks for DNS…

Thank you

1 Like