Is there a way 2 jobs can communicate on nomad

I have 2 jobs:-

  1. an api with websocket
  2. a service that sends message to the websocket of service 1

Is there a way service 2 can send message to service 1’s endpoint?

Note:-
Service 1 is a public api, but i dont want to expose the websocket to the internet.

Hi @saujanya01,

It is definitely possible for applications in multiple jobs to talk to each other. This is an example job, that talks to your WebSocket application using the Nomad Service Discovery option.

job "ws-client" {
  group "ws-client" {
    task "ws-client" {
      driver = "docker"

      config {
        image   = "curlimages/curl:7.87.0"
        command = "/bin/ash"
        args    = ["local/script.sh"]
      }

      template {
        data        = <<EOF
#!/usr/bin/env ash

while true; do
{{range nomadService "ws-test"}}
  curl -L -v http://{{.Address}}:{{.Port}}/
{{end}}
  sleep 3
done
EOF
        destination = "local/script.sh"
      }
    }
  }
}

I hope this helps!

Hi @saujanya01,

Adding to the answer given from @Ranjandas, this is absolutely possible. The service discovery networking page is a good starting point for understanding what is possible and how to get started.

Thanks,
jrasell and the Nomad team

That’s a good example for the Nomad provider, thanks!
Do you have an example on how the same could be done in the same manner with the Consul provider? I’m struggling to understand the Consul template format.

Something along these lines:

  {{- $dns_forward := "192.168.0.1:53" }}{{- /* init with router IP */}}
  {{- range service "adguard-dns" }}{{- /* iterates over [0..1] instances of the adguard-dns service  */}}
    {{- $dns_forward = print .Address ":" .Port }}{{- /* overwrite with AdGuard ip:port if service is present */}}
  {{- end}} 
  forward . {{ $dns_forward }}{{- /* generate forward entry */}}

This is the part which reconfigures my CoreDNS to forward DNS requests to the AdGuard Home service, or the router if the AGH service is not available.

In addition, Consul has quite a few features to link services together:

  • Consul DNS, which let’s you point your FE-service to <backend_service>.service.consul:<port>
  • Consul Connect, to transparently link your BE-service to localhost:<port> of the FE-task group.