hashicorp / nomad

Nomad is an easy-to-use, flexible, and performant workload orchestrator that can deploy a mix of microservice, batch, containerized, and non-containerized applications. Nomad is easy to operate and scale and has native Consul and Vault integrations.
https://www.nomadproject.io/
Other
15k stars 1.96k forks source link

Add a new volume type for ephemeral use between tasks #19530

Closed hajali-amine closed 11 months ago

hajali-amine commented 11 months ago

Proposal

I feel like there should be type of volume that is ephemeral, the moment the tasks are stopped, it gets removed and wiped. Something similar to K8S' emptyDir. For example;

job "job_a" {
  datacenters = ["dc1"]
  type        = "service"

  group "group_a" {

   volume "eph" {
      type      = "ephemeral"
    }

    task "task_a_prestart" {
      driver = "docker"

      config {
        image        = "busybox:1.28"
        command      = "sh"
        args         = ["-c", "echo 'hello world' >  path/to/somewhere/text.txt"]
        network_mode = "host"
      }

      resources {
        cpu    = 200
        memory = 128
      }

      volume_mount {
        volume      = "eph"
        destination = "path/to/somewhere"
      }

      lifecycle {
        hook    = "prestart"
        sidecar = false
      }
    }

    task "task_a" {
      driver = "docker"

      config {
        image   = "busybox"
        command = "sh"
        args    = ["-c", "cat path/in/task/text.txt"]
      }

      volume_mount {
        volume      = "eph"
        destination = "path/in/task"
      }

      resources {
        cpu    = 200
        memory = 128
      }
    }
  }
}

This allows two tasks to share a volume and fast without having to create a host volume or having something persistent.

Use-cases

Inter-task communication, especially when it's between prestart, poststart and a main task. It facilitates the choice on where to mount the ephemeral volume instead of having alloc and managing the movement of directories.

Attempted Solutions

None

tgross commented 11 months ago

Hi @hajali-amine! The allocation directory is intended for this use, and can be referenced inside each task at the path in the $NOMAD_ALLOC_DIR env var. See https://developer.hashicorp.com/nomad/docs/concepts/filesystem for more details on this.

We've also got an open feature request for Dynamic Host Volumes https://github.com/hashicorp/nomad/issues/15489 which will likely be a slightly more powerful version of the same concept.