github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/volume.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: volume Stanza - Job Specification
     4  sidebar_title: volume
     5  description: >-
     6    The "volume" stanza allows the group to specify that it requires a given
     7    volume from the cluster. Nomad will automatically handle ensuring that the
     8    volume is available and mounted into the task.
     9  ---
    10  
    11  # `volume` Stanza
    12  
    13  <Placement groups={['job', 'group', 'volume']} />
    14  
    15  The `volume` stanza allows the group to specify that it requires a
    16  given volume from the cluster.
    17  
    18  The key of the stanza is the name of the volume as it will be exposed
    19  to task configuration.
    20  
    21  ```hcl
    22  job "docs" {
    23    group "example" {
    24      volume "certs" {
    25        type      = "host"
    26        source    = "ca-certificates"
    27        read_only = true
    28      }
    29    }
    30  }
    31  ```
    32  
    33  The Nomad server will ensure that the allocations are only scheduled
    34  on hosts that have a set of volumes that meet the criteria specified
    35  in the `volume` stanzas. These may be [host volumes][host_volume]
    36  configured on the client, or [CSI volumes][csi_volume] dynamically
    37  mounted by [CSI plugins][csi_plugin].
    38  
    39  The Nomad client will make the volumes available to tasks according to
    40  the [volume_mount][volume_mount] stanza in the `task` configuration.
    41  
    42  ## `volume` Parameters
    43  
    44  - `type` `(string: "")` - Specifies the type of a given volume. The
    45    valid volume types are `"host"` and `"csi"`.
    46  
    47  - `source` `(string: <required>)` - The name of the volume to
    48    request. When using `host_volume`'s this should match the published
    49    name of the host volume. When using `csi` volumes, this should match
    50    the ID of the registered volume.
    51  
    52  - `read_only` `(bool: false)` - Specifies that the group only requires
    53    read only access to a volume and is used as the default value for
    54    the `volume_mount -> read_only` configuration. This value is also
    55    used for validating `host_volume` ACLs and for scheduling when a
    56    matching `host_volume` requires `read_only` usage.
    57  
    58  - `mount_options` - Options for mounting CSI volumes that have the
    59    `file-system` [attachment mode]. These options override the `mount_options`
    60    field from [volume registration]. Consult the documentation for your storage
    61    provider and CSI plugin as to whether these options are required or
    62    necessary.
    63  
    64    - `fs_type`: file system type (ex. `"ext4"`)
    65    - `mount_flags`: the flags passed to `mount` (ex. `"ro,noatime"`)
    66  
    67  ## Volume Interpolation
    68  
    69  Because volumes represent state, many workloads with multiple allocations will
    70  want to mount specific volumes to specific tasks. You can use the [HCL2]
    71  syntax in Nomad 1.0 for fine-grained control over how volumes are used.
    72  
    73  There are two limitations to using HCL2 interpolation for `volume` blocks:
    74  
    75  - The `volume` block is used to schedule workloads, so any interpolation needs
    76    to be done before placement. This means that variables like
    77    `NOMAD_ALLOC_INDEX` can't be used for interpolation.
    78  - Nomad does not yet support dynamic volume creation (see [GH-8212]), so volumes
    79    must be created and registered before being used as a `volume.source`.
    80  
    81  The following job specification demonstrates how to use multiple volumes with
    82  multiple allocations. It uses a `dynamic` block to create a new task group for
    83  each of the two volumes. This job specification also shows using HCL2
    84  variables interpolation to expose information to the task's environment.
    85  
    86  ```hcl
    87  variables {
    88    volume_index = ["0", "1"]
    89    path         = "test"
    90  }
    91  
    92  job "example" {
    93    datacenters = ["dc1"]
    94  
    95    dynamic "group" {
    96      for_each = var.volume_index
    97      labels   = ["cache-${group.value}"]
    98  
    99      content {
   100  
   101        volume "cache-volume" {
   102          type   = "csi"
   103          source = "test-volume${group.value}"
   104        }
   105  
   106        network {
   107          port "db" {
   108            to = 6379
   109          }
   110        }
   111  
   112        task "redis" {
   113          driver = "docker"
   114          config {
   115            image = "redis:3.2"
   116            ports = ["db"]
   117          }
   118          resources {
   119            cpu    = 500
   120            memory = 256
   121          }
   122  
   123          env {
   124            # this will be available as the MOUNT_PATH environment
   125            # variable in the task
   126            MOUNT_PATH = "${NOMAD_ALLOC_DIR}/${var.path}"
   127          }
   128  
   129          volume_mount {
   130            volume      = "cache-volume"
   131            destination = "${NOMAD_ALLOC_DIR}/${var.path}"
   132          }
   133  
   134        }
   135      }
   136    }
   137  }
   138  ```
   139  
   140  The job that results from this job specification has two task groups, each one
   141  named for each of the two volumes. Each allocation has its own volume.
   142  
   143  ```shell-session
   144  $ nomad job status example
   145  ID            = example
   146  ...
   147  
   148  Allocations
   149  ID        Node ID   Task Group  Version  Desired  Status   Created  Modified
   150  81d32909  352c6926  cache-1     0        run      running  4s ago   3s ago
   151  ce6fbfc8  352c6926  cache-0     0        run      running  4s ago   3s ago
   152  
   153  $ nomad volume status test-volume0
   154  ...
   155  Allocations
   156  ID        Node ID   Task Group  Version  Desired  Status   Created  Modified
   157  ce6fbfc8  352c6926  cache-0     0        run      running  29s ago  18s ago
   158  
   159  $ nomad volume status test-volume1
   160  ...
   161  Allocations
   162  ID        Node ID   Task Group  Version  Desired  Status   Created  Modified
   163  81d32909  352c6926  cache-0     0        run      running  29s ago  18s ago
   164  ```
   165  
   166  [volume_mount]: /docs/job-specification/volume_mount 'Nomad volume_mount Job Specification'
   167  [host_volume]: /docs/configuration/client#host_volume-stanza
   168  [csi_volume]: /docs/commands/volume/register
   169  [csi_plugin]: /docs/job-specification/csi_plugin
   170  [csi_volume]: /docs/commands/volume/register
   171  [attachment mode]: /docs/commands/volume/register#attachment_mode
   172  [volume registration]: /docs/commands/volume/register#mount_options
   173  [hcl2]: /docs/job-specification/hcl2
   174  [gh-8212]: https://github.com/hashicorp/nomad/issues/8212