How to Attach an AWS EBS Storage Volume to Your Docker Container
How to Attach an AWS EBS Storage Volume to Your Docker Container
In this article, see how to attach an AWS EBS storage volume to your Docker container.
Join the DZone community and get the full member experience.Join For Free
In an ideal world, Docker containers should be ephemeral without any reliance on external storage. In the microservice world, this is achievable when services are connecting to external databases, queues, and other services.
Sometimes though, we need persistent storage, when we're running services such as Jenkins, Prometheus, or Postgres. Fortunately, there's a straightforward way to set this up now for our ECS Clusters using Docker volume drivers. In this article, you'll learn how to attach EBS volumes to your ECS Tasks, which detach and reattach automatically when your ECS Task gets restarted.
Overview of Volumes in ECS
By default, when you run an ECS Task it's going to have an area of storage on the host that's running it. This host is known as the ECS Container Instance, and is in actual fact an EC2 instance. This is fine for temporary data, but as soon as our ECS Task restarts we lose the data. What we need is a way to connect to external storage, such as AWS EBS or AWS EFS.
With Docker volume plugins (also known as volume drivers), such as REX-Ray, we can now achieve this. The REX-Ray plugin can configure AWS services, such as creating volumes and attaching volumes to EC2 instances.
As you can see in the diagram below, if we have an ECS Task running on an EC2 Instance, then the volume (e.g. EBS) needs to be attached to that instance:
REX-Ray takes care of all of this for us, and also specifically can manage:
- creating the volume if it doesn't already exist, including configuring volume type and size
- making sure our Docker container/ECS Task is mounted with the volume
- detaching re-attaching the volume when the ECS Task moves from one EC2 instance to another
ECS launch types
ECS has the EC2 and Fargate launch types. With EC2 you are responsible for provisioning the underlying EC2 instances on which your ECS Tasks will be deployed. With Fargate, you just have to specify the CPU and memory requirements, then AWS provisions everything needed to run your ECS Task.
It's worth noting that you can only use persistent storage with the EC2 launch type, not with Fargate. That's why in this article we will only be considering the EC2 launch type.
Setting up a Persistent Docker Volume: A Working Example
You can follow along with this example, where we'll:
- create an ECS Cluster built on top of 2 EC2 instances. The REX-Ray docker plugin will be installed on both of the instances.
- create an ECS Task definition for the Postgres database. The task definition will include the Docker volume configuration required to use the REX-Ray volume driver to attach a new EBS volume.
- launch the ECS Service for our ECS Task, which will deploy to one of our EC2 instances
- connect to our Postgres container, and create some data in a new database
- move the ECS Task from one EC2 instance to the other, which will restart the task
- connect to Postgres again, and see that data has persisted
You'll need access to the AWS Console and AWS CLI to complete this example.
Provisioning an ECS Cluster
First up, we're going to create an ECS Cluster built on two ECS Container Instances (EC2 instances), provisioned by an AutoScalingGroup. The CloudFormation template below contains everything you need.
Since it's a rather large template, in particular, pay attention to the following parts which are specific to the fact that we're using volumes:
- When each of our ECS Container Instances is launched,
docker plugin install rexray/ebsis run to install the required REX-Ray plugin (see UserData in ContainerInstances).
- The IAM Role attached to our EC2 instances has permissions which include
ec2:DetachVolume. This allows the REX-Ray volume driver to manage the EBS volumes (see EC2Role).
Save the CloudFormation into a file ecs-cluster.yml, then run the following AWS CLI command:
Make sure to add the parameters values specific to your setup:
- VPCID: you can use the default VPC
- SubnetId: you need to select a public subnet, so you can always use one of the default subnets
- KeyName: this needs to be an existing keypair. Required so you can SSH into the ECS Container Instances later.
In the AWS Console go to Services > CloudFormation After some time you'll see your stack reach the UPDATE_COMPLETE status. This may take up to 10 minutes.
Head over to Services > ECS, and you'll see you've got a new ECS Cluster called docker-volume-demo. Click on the name and you'll see you don't have any services or tasks yet, but go to ECS Instances and you'll see details of your two EC2 instances:
Provisioning an ECS Task and Service
Now that our ECS Cluster is setup, we just need to deploy an ECS Task and ECS Service. Remember that the ECS Task can be thought of as a Docker container, whereas the ECS Service manages the ECS tasks, including ensuring enough replicas are running and setting up networking.
You can add the following template to the end of your ecs-cluster.yml file. Specifically, it's worth noting the following sections, specific to volumes:
/var/lib/postgresql/datais where Postgres stores it's data, and in this case, it will be mounted into the
- In the
Volumessection, we have a volume named
rexray-vol. Here we're saying we want an AWS EBS volume to be auto-provisioned of type gp2 with size 5Gb. The type and size are specific to the REX-Ray driver we're using and are passed to the underlying
docker volume createcommand.
Let's run the AWS CLI update-stack command to update our existing CloudFormation stack. You can use all the same parameters as you used in the create-stack command:
Once your CloudFormation stack update has completed, check out your cluster again in the AWS Console:
We now have an active service, with one running Postgres ECS Task. ✅
Connecting to the Postgres Container
Since our Postgres container doesn't have a public IP and isn't connected to a load balancer, we'll have to connect via an SSH tunnel. This way we can have a Postgres client on our local machine, with a connection to our Postgres container routed via the ECS Container Instance on which it's deployed:
To set this up you need the private IP address of the ECS Task, which you can find on the task details page of the AWS Console under Network:
We'll also need the public IP of one of the ECS Container Instances, which you can grab by clicking on the container instance id on the same the task details page.
This will setup the tunnel and continue running in the foreground. Note that if you already have Postgres installed on your local machine, you may have to choose a port other than 5432.
Create Database Data
To create some data on the EBS volume, we're going to create a Postgres database and add some test data. To do that, you can either use the psql command line tool or follow along with steps below which use pgAdmin, which is free to download.
pgAdmin data setup
Once you've installed pgAdmin, starting it will open up a page in your browser. Right click on Servers and select Create > Server. Enter a server name:
Click on the Connection tab, enter localhost as the Host name, then click Save:
If prompted, the default password is Postgres.
Right click on the new dockervolume server, and select Create > Database. Enter a database name, then click Save:
Click on the new database, then select Tools > Query Tool, and we can start running some SQL.
Execute the following SQL (shortcut to execute is F5) which will create a table with some healthy test data:
Drain the Instance
We're going to change the container instance state to DRAINING, which will force ECS to deploy our task onto the other container instance. If we can still access the database data once the ECS Task moves over, then that proves it's successfully persisted in the EBS volume.
To make sure we're draining the correct container instance, in ECS grab the container instance id that the task is currently running in:
You'll need the full ARN of the container instance, which you can get with this AWS CLI command and picking the matching result:
Now we have the ARN, it's time to run the following update-container-instances-state command to change the state to DRAINING:
Once that's happened, head over to ECS Instances in the AWS Console and you'll see the instance is in the DRAINING state:
Head on over to Tasks and eventually, you'll see a new task coming up on the remaining ACTIVE container instance.
Wait for it's status to reach RUNNING.
Verify that the Database Has Come Back
Now that our ECS Task has moved over to the other container instance, we can validate that the data has persisted by running an SQL SELECT query.
First though, your old SSH tunnel will now have a connection error. You'll need to grab the new private IP address from the ECS Task details page, then run the ssh command again:
Back in pgAdmin, disconnect and reconnect your dockervolume Server. Then run the following SELECT query on the dockervolume database:
You'll see we still have the same data. Awesome!
You can remove the CloudFormation stack with the following command:
Note that this won't delete the EBS volume, which was created automatically by REX-Ray outside of CloudFormation. Find the correct volume id with the following command:
aws ec2 delete-volume --volume-id <volume-id>
You should now understand that with the correct configuration, ECS Tasks can easily be setup to connect to AWS EBS volumes. The REX-Ray Docker volume driver does the hard work for us, and AWS ECS easily integrates with it to make sure that volumes are always attached to the correct EC2 host.
Please remember that this CloudFormation stack was designed as a simple example, and should not be used in production. For example, the Postgres instance should ideally not be exposed over the internet, and the ECS Container Instances should be deployed in a private subnet.
REX-Ray can also be configured to use AWS Elastic File System (EFS) too. If you have a requirement to access a volume from multiple ECS Tasks at the same time, you'll want to check out this option.
This article was originally published on https://appfleet.com/
Published at DZone with permission of Sudip Sengupta . See the original article here.
Opinions expressed by DZone contributors are their own.