How to configue multitask job in one file

How to configure multi task job like configure nginx php mysql in one job each other talk with internal.

Hi @asd99557,

You can have multiple group in a job, and multiple tasks inside a group.

The key thing to keep in mind when designing your jobspec is that a group is the scheduling unit, which means that all the tasks in a group will always run in the same client so they are able to share resources (for example, they can run in the same network namespace).

This also implies that different groups are not guaranteed to be running in the same client, so you can’t expect them to be able to communicate over a shared network interface (like localhost).

There are multiple ways for groups to communicate though. One common approach is to use Consul for service discovery or service mesh.

In service discovery, you use the service block to ask Nomad to register your service in Consul. Nomad will automatically update its IP and port and you can then query Consul to find these values and configure your application. Here’s a Wordpress example.

Notice how the database group registers a service called my-website-db:

    service {
      name = "my-website-db"
      port = "db"

And then the wordpress task uses a template to query Consul for the my-website-db service, retrieve its address and port, and populate the config file:

    task "wordpress" {
      driver = "docker"

      template {
        data = <<EOH
{{- if service "my-website-db" -}}
{{- with index (service "my-website-db") 0 -}}
WORDPRESS_DB_HOST={{ .Address }}:{{ .Port }}
{{- end -}}
{{- end }}

        destination = "local/envvars.txt"
        env = true

With service mesh, you use Consul Connect to deploy a proxy that runs alongside your main task, and that proxy is responsible to automatically route requests to the right service. The benefit of this approach is that you can get mTLS encryption by default, and your tasks only need to talk to localhost, since the sidecar proxy will be deployed in the same network namespace.

1 Like