Sub topic 15
Sharing a volume with Multi-Attach
This chapter assumes you can create and attach a volume, know that a volume lives in one Availability Zone, and understand why a file system must be mounted by only one instance at a time.
What Multi-Attach does
Normally an EBS volume attaches to one instance at a time. Multi-Attach lets a single Provisioned IOPS SSD volume (io1 or io2) attach to up to 16 instances at once. Every instance can read and write the same blocks.
Multi-Attach gives you shared block storage. It does not give you a shared file system. EBS does not coordinate writes between instances.
Requirements and limits
- The volume type must be io1 or io2. gp3, st1 and sc1 do not support Multi-Attach.
- All instances must be in the same Availability Zone as the volume.
- The instances must be Nitro-based.
- The volume cannot be a root volume.
- The volume's provisioned IOPS and throughput are shared by all attached instances. Adding instances does not add performance.
Create and attach a shared volume
Enable Multi-Attach when you create the volume:
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type io2 \
--size 100 \
--iops 5000 \
--multi-attach-enabledThen attach the same volume to each instance. Repeat the command once per instance:
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0123456789abcdef0 \
--device /dev/sdf
aws ec2 attach-volume \
--volume-id vol-0123456789abcdef0 \
--instance-id i-0fedcba9876543210 \
--device /dev/sdfOn Nitro instances the device appears under an NVMe name such as /dev/nvme1n1. Check with lsblk on each instance.
The main mistake: mounting a normal file system twice
XFS and ext4 assume that only one host changes the file system. Each instance caches metadata and data in its own memory. If two instances mount the same XFS volume read-write, they overwrite each other's changes and the file system becomes corrupted.
Coordinating writers
You have two common options:
- Cluster-aware file system. A file system such as GFS2 uses a distributed lock manager so that the instances agree on who may change which blocks. You also need a cluster manager to handle membership.
- Application-level coordination. A clustered database or a custom application divides the volume into regions and assigns each writer its own region. It can also use write fencing.
io2 volumes on Nitro instances support NVMe reservations. These let cooperating instances take persistent reservations on the device. A failed node can then be fenced off so it can no longer write. Your cluster software drives this feature, so check that your cluster stack supports it.
Operational notes
- Detach the volume from one instance without affecting the others. Make sure that instance has unmounted it first.
- Snapshots work as usual, but they capture only data that has reached the volume. An instance's cached writes are not included, so quiesce your application first.
- If you set DeleteOnTermination on the attachment, the volume is deleted only when the last attached instance terminates.
- Multi-Attach does not span Availability Zones. For cross-zone sharing, use EFS.
Exercise
Two instances in the same Availability Zone both attach a Multi-Attach io2 volume. A teammate formats it with XFS and mounts it read-write on both instances. What goes wrong, and what are two ways to fix the design?
Show answer
XFS assumes a single host. Each instance caches metadata and writes without knowing about the other, so the file system becomes corrupted. Fix it by using a cluster-aware file system such as GFS2 or OCFS2, or by using an application that coordinates its own writes, for example by giving each writer its own region of the volume. Another option is to drop Multi-Attach and use EFS for shared file access.