Post Syndicated from tiffany jernigan (@tiffanyfayj) original https://aws.amazon.com/blogs/compute/amazon-ecs-and-docker-volume-drivers-amazon-ebs/
Containers are emerging as the default compute primitive for building cloud-native applications. They facilitate the adoption of continuous delivery, and help increase infrastructure use.
However, deploying stateful application as containers has been challenging because containers have short life-spans, get re-deployed frequently, are scaled up and down dynamically, and often share the same host with other containers. All of these factors make it challenging for you to appropriately align the lifecycles of storage volumes and containers.
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.
ECS support for Docker volumes means that you can now deploy stateful and storage-intensive use cases. These include:
- Machine learning and data processing workloads
- Applications such as GitLab or Jenkins that share a filesystem across multiple tasks
- Databases such as Cassandra or RocksDB
- Streaming tools such as Kafka
- Additional scratch space added to containers that process large workloads and are storage-intensive
To support this broad array of use cases, ECS offers you the flexibility to configure the lifecycle of the Docker volume. For example, you can specify whether it is a scratch space volume specific to a single instantiation of a task, or a persistent volume that persists beyond the lifecycle of a unique instantiation of the task. You can also choose to use a Docker volume that you’ve created before launching your task.
In addition to managing the Docker volume configuration and lifecycle, the ECS scheduler is now plugin-aware. ECS takes the availability of the requested driver into account in its placement decisions, so that tasks that require a certain driver are only placed on container instances that have the driver installed.
Docker and Docker volumes
Docker volumes are a way to persist data outside of the lifecycle of a container. Containers themselves are made up of multiple immutable layers of storage with an ephemeral layer, which is read/write. If your application writes files to the ephemeral layer, these changes are lost when the container stops.
Volumes are managed outside of the container lifecycle—stopping or removing the container does not remove the volume. Docker also supports volume drivers that allow you to use volumes as an abstraction between containers and persistent storage such as Amazon EBS or Amazon EFS. By default, Docker provides a driver called ‘local’ that provides local storage volumes to containers. With Docker plugins, you can now add volume drivers to provision and manage EBS and EFS storage, such as REX-Ray, Portworx, and NetShare.
To deploy a stateful application such as Cassandra, MongoDB, Zookeeper, or Kafka, you likely need high-performance persistent storage like EBS. Docker volumes allow you to present an EBS volume to your application as a Docker volume.
There are other applications such as Jenkins and GitLab, where multiple copies of the application need access to the same data. With volume drivers and EFS, you can present EFS as a shared volume to multiple instances of your container so that you can scale your application yet still retain and persist shared data on EFS.
Another overlooked use case involves applications that need scratch space. When you define a task in ECS and your application writes to the filesystem inside of the container (not on a Docker volume), the task consumes space on the underlying EC2 instance that is shared by all other running tasks. This can lead to issues of ‘noisy neighbors’ if a task were to write a bunch of data to
/tmp on its local filesystem.
Now with Docker volume support in ECS, you can map an EBS volume to
/tmp (or whatever your scratch space directory you prefer). You can ensure good performance while limiting the size of the underlying EBS volume using arguments in your ECS task to the volume driver.
What is REX-Ray?
REX-Ray is just one example of a Docker volume driver plugin that provides an abstraction between Docker volumes and the underlying storage. Built on top of the libStorage framework, REX-Ray’s simplified architecture consists of a single binary. It runs as a stateless service on every host, using a configuration file to orchestrate multiple storage platforms. REX-Ray supports multiple storage backends. For this post, we focus on EBS as a storage backend. Part two of this series focuses on EFS.
Using a plugin such as REX-Ray, your Docker container is able to persist data outside of the lifespan of a running container. You don’t have to worry about the underlying storage. Instead, you simply reference a Docker volume in your task definition and let REX-Ray provide the abstraction. While this post is specific to REX-Ray, ECS is designed to be open and pass through the volume driver arguments from your task definition to Docker. You can use any volume driver (such as Portworx) that is supported by Docker.
Putting it all together
Before you can get started using Docker volumes with ECS, there are a few things you need to do.
First, you need a suitable volume driver plugin, such as REX-Ray, to provide an abstraction between the Docker volume and the underlying storage, for example, EBS or EFS. Docker designed volumes and the associated driver mechanism to be pluggable to support a variety of storage backends. Although we’ve chosen to highlight REX-Ray for this post, there are several others to choose from, including Portworx and NetShare.
Because the volume plugin interacts with the AWS storage services on your behalf, an IAM role has to be assigned to the ECS container instances. This allows REX-Ray to issue the appropriate AWS API calls and perform actions such as attaching and detaching EBS volumes, and so on.
Using REX-Ray with Amazon EBS
To help you get started, we’ve created an AWS CloudFormation template that builds a two-node ECS cluster. The template bootstraps the
rexray/ebs volume driver onto each node and assigns them an IAM role with an inline policy that allows them to call the API actions that REX-Ray needs. The template also creates a Network Load Balancer, which is used to expose an ECS service to the internet.
Finally, you create a task definition for a stateful service—MySQL—that uses the the
rexray/ebs driver. Observe how the volume where MySQL stores its data is moved when the MySQL task is scheduled on another instance in the cluster.
Set up the environment
Here’s how to set up the environment for this walkthrough.
Step 1: Instantiate the AWS CloudFormation template
The ECS container instances are bootstrapped using the following script, which is given as user data in rexyray-demo.json.
Step 2: Export output parameters as environment variables
This shell script exports the output parameters from the CloudFormation template and imports them as OS environment variables. You use these variables later to create task and service definitions.
Export the output parameters. The region parameter is only needed if your Region configuration is not us-west-2, as defined in the CloudFormation template.
Step 3: Create the task definition
In this step, you create a task definition for MySQL. MySQL is considered stateful service because the data stored in the database has to persist beyond the life of the task.
When the MySQL task is restarted on another instance in the cluster, the scheduler and the
rexray/ebs plugin ensure that the task is launched on an instance that can re-establish a connection to the EBS volume where the database is stored.
The placement constraint in the task definition informs the ECS service scheduler to launch the task in a specific Availability Zone; the available zone where the EBS volume was originally created. Such a constraint is necessary because instances cannot connect to volumes in a different Availability Zone.
Docker volumes support adds several new the parameters to the ECS task definition. These include the volume type, scope, drivers, and Docker options and labels. A volume can either be scoped to a single, specific task or it can be shared among multiple tasks.
When a volume is scoped to a task, it is not meant to be shared across different running tasks. In contrast, a shared volume is for use cases where the volume lifecycle is independent of the ECS task. The volume can be used by different tasks concurrently or at different times. It is primarily intended for use cases such as single-task applications where the volume persists after the task dies and is re-used when the task starts again. Another use case is when multiple tasks on the same EC2 container instance access the volume concurrently.
autoprovision parameter is used to specify whether ECS manages the lifecycle of the volume. When this is set to
true, ECS automatically provisions the volume for you, which is what you are doing in the above example. When it’s set to
false, ECS assumes that the volume already exists. For this example, you could instead set
false and run the following command to create a volume:
The driver options are used to configure the type of EBS storage use, for example, gp2, standard, io1, and so on, the size of the volume to provision, IOPS, and encryption. The specific options vary depending on the volume plugin that you are using.
Register the task definition and extract the task definition ARN from the result:
Step 4: Create a service definition
In this step, you create a service definition for MySQL. An ECS service is a long running task that is monitored by the service scheduler. If the task dies or becomes unhealthy, the scheduler automatically attempts to restart the task.
The MySQL service is fronted by a Network Load Balancer that is configured for forward traffic on port 3306 to the tasks registered with a specific target group. The desired count is the desired number of task copies to run. The minimum and maximum healthy percent parameters inform the scheduler to only run exactly the number of desired copies of this task at a time. Unless a task has been stopped, it does not try starting a new one.
Create the MySQL service:
Step 5: Connect to the MySQL service
After the service is running, configure a MySQL client, such as MySQL Workbench, to connect to the service:
- For Connection Name, type “rexray-demo”.
- For Hostname, copy and paste the DNS name of the Network Load Balancer.
- For Password, type the default password found in the mysql-taskdef.json file.
- Choose Test Connection, Close.
- Under MySQL Connections, open the rexray-demo connection.
In the Query window, paste the following:
CREATE DATABASE rexraydb; USE rexraydb; CREATE TABLE pets (name VARCHAR(20), breed VARCHAR(20)); SHOW TABLES; DESCRIBE pets; INSERT INTO pets VALUES ('Fluffy', 'Poodle'); SELECT * FROM pets;
You can execute each line separately by placing the cursor on a line and clicking the execute statement button.
Step 6: Drain the instance
Now that you have a running MySQL database server running under a container and persisting its data, make sure that it will survive a container replacement.
Docker containers by their nature are designed to be ephemeral. If you upgrade the underlying host operating system, you must drain the tasks off of the instance and let them be re-scheduled onto another ECS host. Below, I show the behavior of persisting the MySQL instance’s data to an EBS volume and allowing the task to be re-scheduled.
The following script identifies the instance that is currently running the task and puts it in a draining state. This forces the task to be rescheduled onto the other EC2 container instance in the cluster.
In the ECS console, if you click on the cluster and then the tab for the cluster’s tasks, you see the container instance ID for the MySQL task:
Clicking the link of the container instance ID takes you to another page that shows the EC2 instance ID of the instance where the MySQL task is running:
Now run the script:
When you run the script, the tasks on the draining instance are stopped. Because you have an ECS service definition for MySQL, ECS launches new tasks on other ECS instances in the cluster that meet the placement constraints. In this example, you placed a constraint on the Availability Zone of the EBS volume as it’s not possible to detach and re-attach volumes across Availability Zones. Because the volume already exists, REX-Ray attaches the existing volume to the new task. When MySQL starts, it sees this as its data volume and you have access to the recently stored data.
Step 7: Re-connect to the MySQL service
After you see that a new task has been provisioned on the ECS cluster, you can return to MySQL Workbench and attempt to run the following query:
This environment is meant as a demonstration on how to use Docker volume plugins with ECS for supporting persistent workloads. For an actual production implementation, I recommend scoping the VPC and security groups to only allow network access from trusted resources. This post creates a MySQL server that is accessible from the internet. In addition, you should implement your own strong MySQL root password, among other things.
To clean up this demo, take the following steps.
Delete the service.
Delete the volume.
Even though you deleted the task and the service, you still need to clean up the EBS volume that you created. You created this volume and referenced it in the ECS task definition. ECS passed this information along to Docker running on the host, which in turn handed it to REX-Ray (your volume driver), which knew how to attach the EBS volume and map it to the container.
The easiest way to delete this volume is from the EC2 console. In the list of volumes, you should see a volume named
rexray-vol that is unattached (state=available). Delete this volume as it is no longer needed.
Otherwise, you can run the following command, which grabs the volume ID and deletes it:
Delete the CloudFormation template.
Lastly, delete the CloudFormation template. This removes the rest of the environment that was pre-created for this exercise.
While it was possible to use Docker volume plugins with ECS previously, doing so required you to create volumes out of band, that is, outside of ECS, and create placement constraints to restrict where tasks could be run. With native support for Docker volumes, volumes can now be provisioned simply by adding a handful of parameters to an ECS task definition.
Moreover, the ECS scheduler is now volume plugin aware. Instances that have a volume driver installed on them automatically get annotated with attributes that inform the scheduler where to place tasks that use a particular driver. Together, these features help you to run stateful, storage intensive applications such as databases, machine learning, and data processing applications, streaming applications like Kafka, as well as applications that need additional scratch space. We look forward to hearing about the use cases that this new feature enables.