Hi! I don’t think there is a native
way to get fqdn of a task in Nomad, but you can use the service discovery from Nomad to get IP and port of a task as a service. Although if you are injecting the service through a template stanza, by default containers will reboot on dependency service change, which is quite annoying. You need to configure change_signal and change_mode in template (although I haven’t tried this myself)
Some people use Traefik as an internal DNS service, so you can use fqdn in this case to connect your tasks, although I haven’t tried myself.
Nomad service discovery.
A job in which you declare a service:
job "grafana-agent" {
datacenters = ["dc1"]
group "grafana-agent" {
network {
port "grafana-agent-port" {
to = 9411
}
}
[...]
task "grafana-agent" {
service {
name = "grafana-agent"
port = "grafana-agent-port"
provider = "nomad"
}
[...]
You can check this service is correct by using the nomad CLI nomad service list
Then you have to use this service in another job.
job "sample" {
datacenters = ["dc1"]
group "sample" {
[...]
task "sample" {
[...]
template {
destination = "local/env"
data = <<EOH
GRAFANA_AGENT={{ with nomadService "grafana-agent" }}{{ with index . 0 }}{{.Address}}:{{.Port}}{{ end }}{{ end }}
EOH
env = true
}
[…]
This will create an ENV variable called GRAFANA_AGENT
in the sample task with the IP and address of the grafana-agent service we declared before. It will be dynamically update.
You have to be careful with the circle dependencies, tho. And I would suggest to check change_signal and change_mode in template, so the sample container will not reboot if the grafana-agent service change its IP or port, or if the grafana-agent reboots.
For more advanced features I would suggest you to try the traefik solution. It seems a more fancy way, so you could balance your traffic with more than one instance dynamically.
I hope this helps! Have a nice day.