github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/volume.mdx (about) 1 --- 2 layout: docs 3 page_title: volume Stanza - Job Specification 4 description: >- 5 The "volume" stanza allows the group to specify that it requires a given 6 volume from the cluster. Nomad will automatically handle ensuring that the 7 volume is available and mounted into the task. 8 --- 9 10 # `volume` Stanza 11 12 <Placement groups={['job', 'group', 'volume']} /> 13 14 The `volume` stanza allows the group to specify that it requires a 15 given volume from the cluster. 16 17 The key of the stanza is the name of the volume as it will be exposed 18 to task configuration. 19 20 ```hcl 21 job "docs" { 22 group "example" { 23 volume "certs" { 24 type = "host" 25 source = "ca-certificates" 26 read_only = true 27 } 28 } 29 } 30 ``` 31 32 ```hcl 33 job "docs" { 34 group "example" { 35 volume "data" { 36 type = "csi" 37 source = "csi-volume" 38 read_only = true 39 attachment_mode = "file-system" 40 access_mode = "single-node-writer" 41 per_alloc = true 42 43 mount_options { 44 fs_type = "ext4" 45 mount_flags = ["noatime"] 46 } 47 } 48 } 49 } 50 ``` 51 52 53 The Nomad server will ensure that the allocations are only scheduled 54 on hosts that have a set of volumes that meet the criteria specified 55 in the `volume` stanzas. These may be [host volumes][host_volume] 56 configured on the client, or [CSI volumes][csi_volume] dynamically 57 mounted by [CSI plugins][csi_plugin]. 58 59 The Nomad client will make the volumes available to tasks according to 60 the [volume_mount][volume_mount] stanza in the `task` configuration. 61 62 ## `volume` Parameters 63 64 - `type` `(string: "")` - Specifies the type of a given volume. The 65 valid volume types are `"host"` and `"csi"`. 66 67 - `source` `(string: <required>)` - The name of the volume to 68 request. When using `host_volume`'s this should match the published 69 name of the host volume. When using `csi` volumes, this should match 70 the ID of the registered volume. 71 72 - `read_only` `(bool: false)` - Specifies that the group only requires 73 read only access to a volume and is used as the default value for 74 the `volume_mount -> read_only` configuration. This value is also 75 used for validating `host_volume` ACLs and for scheduling when a 76 matching `host_volume` requires `read_only` usage. 77 78 The following fields are only valid for volumes with `type = "csi"`: 79 80 - `access_mode` `(string: <required>)` - Defines whether a volume should be 81 available concurrently. The `access_mode` and `attachment_mode` together 82 must exactly match one of the volume's `capability` blocks. Can be one of 83 `"single-node-reader-only"`, `"single-node-writer"`, 84 `"multi-node-reader-only"`, `"multi-node-single-writer"`, or 85 `"multi-node-multi-writer"`. Most CSI plugins support only single-node 86 modes. Consult the documentation of the storage provider and CSI plugin. 87 88 - `attachment_mode` `(string: <required>)` - The storage API that will be used 89 by the volume. The `access_mode` and `attachment_mode` together must exactly 90 match one of the volume's `capability` blocks. Most storage providers will 91 support `"file-system"`, to mount volumes using the CSI filesystem API. Some 92 storage providers will support `"block-device"`, which will mount the volume 93 with the CSI block device API within the container. 94 95 - `per_alloc` `(bool: false)` - Specifies that the `source` of the volume 96 should have the suffix `[n]`, where `n` is the allocation index. This allows 97 mounting a unique volume per allocation, so long as the volume's source is 98 named appropriately. For example, with the source `myvolume` and `per_alloc 99 = true`, the allocation named `myjob.mygroup.mytask[0]` will require a 100 volume ID `myvolume[0]`. 101 102 - `mount_options` - Options for mounting CSI volumes that have the 103 `file-system` [attachment mode]. These options override the `mount_options` 104 field from [volume registration]. Consult the documentation for your storage 105 provider and CSI plugin as to whether these options are required or 106 necessary. 107 108 - `fs_type`: file system type (ex. `"ext4"`) 109 - `mount_flags`: the flags passed to `mount` (ex. `["ro", "noatime"]`) 110 111 ## Volume Interpolation 112 113 Because volumes represent state, many workloads with multiple allocations will 114 want to mount specific volumes to specific tasks. The `volume` block is used 115 to schedule workloads, so `${NOMAD_ALLOC_INDEX}` can't be used directly in the 116 `volume.source` field. The following job specification demonstrates how to use 117 multiple volumes with multiple allocations, using the `per_alloc` field. This 118 job specification also shows using HCL2 -variables interpolation to expose 119 information to the task's environment. 120 121 ```hcl 122 variables { 123 path = "test" 124 } 125 126 job "example" { 127 datacenters = ["dc1"] 128 129 group "cache" { 130 131 count = 2 132 133 volume "cache-volume" { 134 type = "csi" 135 source = "test-volume" 136 attachment_mode = "file-system" 137 access_mode = "single-node-writer" 138 per_alloc = true 139 } 140 141 network { 142 port "db" { 143 to = 6379 144 } 145 } 146 147 task "redis" { 148 driver = "docker" 149 config { 150 image = "redis:7" 151 ports = ["db"] 152 } 153 resources { 154 cpu = 500 155 memory = 256 156 } 157 158 env { 159 # this will be available as the MOUNT_PATH environment 160 # variable in the task 161 MOUNT_PATH = "${NOMAD_ALLOC_DIR}/${var.path}" 162 } 163 164 volume_mount { 165 volume = "cache-volume" 166 destination = "${NOMAD_ALLOC_DIR}/${var.path}" 167 } 168 169 } 170 } 171 } 172 ``` 173 174 The job that results from this job specification has two task groups, each one 175 named for each of the two volumes. Each allocation has its own volume. 176 177 ```shell-session 178 $ nomad job status example 179 ID = example 180 ... 181 182 Allocations 183 ID Node ID Task Group Version Desired Status Created Modified 184 81d32909 352c6926 cache-1 0 run running 4s ago 3s ago 185 ce6fbfc8 352c6926 cache-0 0 run running 4s ago 3s ago 186 187 $ nomad volume status 'test-volume[0]' 188 ... 189 Allocations 190 ID Node ID Task Group Version Desired Status Created Modified 191 ce6fbfc8 352c6926 cache-0 0 run running 29s ago 18s ago 192 193 $ nomad volume status 'test-volume[1]' 194 ... 195 Allocations 196 ID Node ID Task Group Version Desired Status Created Modified 197 81d32909 352c6926 cache-0 0 run running 29s ago 18s ago 198 ``` 199 200 [volume_mount]: /docs/job-specification/volume_mount 'Nomad volume_mount Job Specification' 201 [host_volume]: /docs/configuration/client#host_volume-stanza 202 [csi_volume]: /docs/commands/volume/register 203 [csi_plugin]: /docs/job-specification/csi_plugin 204 [csi_volume]: /docs/commands/volume/register 205 [attachment mode]: /docs/commands/volume/register#attachment_mode 206 [volume registration]: /docs/commands/volume/register#mount_options