how to persist the data volume in ecs?

Viewed 1211

i created an ecs cluster with ecs service having a task definition.The task definiton contain mount points.when i stop the task the ecs service restarts it and provide a new container id to the task definition which cause the data to be lost.How to persist the data volume when we restart the task definiton?

2 Answers

I'd advise you to use an EFS volume.

If using the Amazon ECS optimized AMIs (so Red Hat), you'd have to install on the EC2 instances the package nfs-utils to handle that file system.

You can then create a /mnt/efs directory, and in the instance /etc/fstab file an entry:

<YOUR EFS CLUSTER DOMAIN NAME>:/ /mnt/efs nfs4 nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,fsc 0 0

In your service definition, you'll be able to declare volumes such as /mnt/efs/myvolume which will be shared accross your EC2 instances.

Then run sudo mount -a to activate this new network volume.


And because you don't want to do it on each created instance, you can just add this bunch of code in the user data section of the EC2 instance creation screen (or Launch Configuration creation screen in the case of an auto-scaling group). Don't forget to edit with your own EFS cluster domain name.

Content-Type: multipart/mixed; boundary="==BOUNDARY=="
MIME-Version: 1.0

--==BOUNDARY==
MIME-Version: 1.0
Content-Type: text/text/x-shellscript; charset="us-ascii"
#!/bin/bash
yum install -y nfs-utils

mkdir "/mnt/efs"
echo "<YOUR EFS CLUSTER DOMAIN NAME>:/ /mnt/efs nfs4 
nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,fsc 0 0" >> /etc/fstab
mount -a
--==BOUNDARY==--

It will do all these steps for you at the instance creation.

If using restrictive security groups, you'd also need to add access to the NFS network port (2049 if I remind well).

Note that Amazon ECS Now Supports Docker Volumes and Volume Plugins, using REX-Ray (or other alternatives), which means that now this is the standard mechanism supported by Docker, whether you want to mount EBS or EFS.

See also:

  1. https://aws.amazon.com/blogs/compute/amazon-ecs-and-docker-volume-drivers-amazon-ebs/
  2. https://aws.amazon.com/blogs/compute/amazon-ecs-and-docker-volume-drivers-amazon-efs

Before Docker volume driver support was added to Amazon ECS, you had to manage storage volumes manually using custom tooling such as bash scripts, Lambda functions, or manual configuration of Docker volumes. Now, you can now take full advantage of the Docker plugin ecosystem by using popular plugins such as REX-Ray or Portworx.

Note of this before you use EFS:

Amazon EFS is almost 10 times more expensive than Amazon EBS pricing, but the cheaper Amazon EBS can only be accessed by one Amazon EC2 instance at a time. This can be problem in a clustering or distributed app. However, Amazon EBS can give better performance than Amazon EFS. Amazon EFS gains a point on Amazon EBS in its ability to be used as shared network storage.

Related