github.com/hernad/nomad@v1.6.112/e2e/volumes/input/volumes.nomad (about)

     1  # Copyright (c) HashiCorp, Inc.
     2  # SPDX-License-Identifier: MPL-2.0
     3  
     4  job "volumes" {
     5    datacenters = ["dc1", "dc2"]
     6  
     7    constraint {
     8      attribute = "${attr.kernel.name}"
     9      value     = "linux"
    10    }
    11  
    12    group "group" {
    13  
    14      volume "data" {
    15        type   = "host"
    16        source = "shared_data"
    17      }
    18  
    19      task "docker_task" {
    20  
    21        driver = "docker"
    22  
    23        config {
    24          image   = "busybox:1"
    25          command = "/bin/sh"
    26          args    = ["/usr/local/bin/myapplication.sh"]
    27  
    28          mounts = [
    29            # this mount binds the task's own NOMAD_TASK_DIR directory as the
    30            # source, letting us map it to a more convenient location; this is a
    31            # frequently-used way to get templates into an arbitrary location in
    32            # the task for Docker
    33            {
    34              type     = "bind"
    35              source   = "local"
    36              target   = "/usr/local/bin"
    37              readonly = true
    38            }
    39          ]
    40        }
    41  
    42        # this is the host volume mount, which we'll write into in our task to
    43        # ensure we have persistent data
    44        volume_mount {
    45          volume      = "data"
    46          destination = "/tmp/foo"
    47        }
    48  
    49        template {
    50          data = <<EOT
    51  #!/bin/sh
    52  echo ${NOMAD_ALLOC_ID} > /tmp/foo/${NOMAD_ALLOC_ID}
    53  sleep 3600
    54  EOT
    55  
    56          # this path is relative to the allocation's task directory:
    57          # /var/nomad/alloc/:alloc_id/:task_name
    58          # but Docker tasks can't see this folder except for the bind-mounted
    59          # directories inside it (./local ./secrets ./tmp)
    60          # so the only reason this works to write our script to execute from
    61          # /usr/local/bin is because of the 'mounts' section above.
    62          destination = "local/myapplication.sh"
    63        }
    64  
    65        resources {
    66          cpu    = 256
    67          memory = 128
    68        }
    69      }
    70  
    71      task "exec_task" {
    72  
    73        driver = "exec"
    74  
    75        config {
    76          command = "/bin/sh"
    77          args    = ["/usr/local/bin/myapplication.sh"]
    78        }
    79  
    80        # host volumes for exec tasks are more limited, so we're only going to read
    81        # data that the other task places there
    82        #
    83        # - we can't write unless the nobody user has permissions to write there
    84        # - we can't template into this location because the host_volume mounts
    85        #   over the template (see https://github.com/hernad/nomad/issues/7796)
    86        volume_mount {
    87          volume      = "data"
    88          destination = "/tmp/foo"
    89          read_only   = true
    90        }
    91  
    92        template {
    93          data = <<EOT
    94  #!/bin/sh
    95  sleep 3600
    96  EOT
    97  
    98          # this path is relative to the allocation's task directory:
    99          # /var/nomad/alloc/:alloc_id/:task_name
   100          # which is the same as the root directory for exec tasks.
   101          # we just need to make sure this doesn't collide with the
   102          # chroot: https://www.nomadproject.io/docs/drivers/exec#chroot
   103          destination = "usr/local/bin/myapplication.sh"
   104        }
   105  
   106        resources {
   107          cpu    = 256
   108          memory = 128
   109        }
   110      }
   111    }
   112  }