Post Syndicated from tiffany jernigan (@tiffanyfayj) original https://aws.amazon.com/blogs/compute/amazon-ecs-and-docker-volume-drivers-amazon-efs/
Post by: Tiffany Jernigan and Jeremy Cowan
This is the second post in a series showing how to use Docker volumes with Amazon ECS. If you are unfamiliar with Docker volumes or REX-Ray, or want to know how to use a volume plugin with ECS and Amazon Elastic Block Store (Amazon EBS), see Part 1.
In this post, you use the REX-Ray EFS plugin with Amazon Elastic File System (Amazon EFS) to persist and share data among multiple ECS tasks. To help you get started, we have created an AWS CloudFormation template that builds a two-instance ECS cluster across two Availability Zones.
The template bootstraps the REX-Ray EFS plugin onto each node. Each instance has the REX-Ray EFS plugin installed, is assigned an IAM role with an inline policy with permissions for REX-Ray to issue the necessary AWS API calls, and a security group to open port 2049 for EFS. The template also creates a Network Load Balancer that is used to expose an ECS service to the internet.
Set up the environment
First, create a folder in which you create all files and enter it. Next, set the full path for your EC2 key pair that you need later to connect to your instance using SSH.
Step 1: Instantiate the CloudFormation template
Next, create a CloudFormation stack with the following S3 template:
The ECS container instances are bootstrapped with a user data script that installs the
rexray/efs Docker plugin using:
Step 2: Export output parameters as environment variables
This shell script exports the output parameters from the CloudFormation template. With the following command, import them as OS environment variables. Later, you use these variables to create task and service definitions.
Now run the script:
Step 3: Create a task definition
In this step, you create a task definition for an Apache web service, Space, which is an example website using Apache2 on Ubuntu. The scheduler and the REX-Ray EFS plugin ensure that each copy of the task establishes a connection with EFS.
autoprovision is set to true, the Docker volume driver,
rexray/efs, creates a new file system for you. And because
shared, the file system can be used across multiple tasks.
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 the rexray-efs task definition. 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 web service is fronted by a Network Load Balancer that is configured for forward traffic on port 80 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 run only 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 Apache service:
Wait for service to be up with the last status as RUNNING for the tasks using either the CLI or the console:
Next, look at your file system and see two mount points—one for each Availability Zone:
Step 5: View the webpage
Now, open a browser and paste
NLBDNSName as the URL.
If you refresh the page, you can see that the task ID and EC2 instance ID change as the traffic is being load balanced.
Get the DNS info for an instance so that you can connect to it using SSH and modify index.shtml:
Now, get one of the Docker container IDs and use
docker exec to change the image being displayed:
To see the update, refresh the load balancer webpage.
Step 6: Clean up
To clean up the resources that you created in this post, take the following steps.
Delete the mount targets and file system.
Delete the service.
Delete the CloudFormation template. This removes the rest of the environment that was pre-created for this exercise.
Congratulations on getting your service up and running with Docker volume plugins and EFS!
You have created a CloudFormation stack including two instances, running the REX-Ray EFS plugin, across two subnets, a Network Load Balancer, as well as an ECS cluster. You also created a task definition and service which used the plugin to create an elastic filesystem.
We look forward to hearing about how you use Docker Volume Plugins with ECS.
– Tiffany and Jeremy