Working with Amazon Elastic Container Service

Amazon Elastic Container Service (Amazon ECS) is a highly scalable, fast, container management service that makes it easy to run, stop, and manage Docker containers on a cluster of Amazon EC2 instances.

This tutorial leads you through the steps to:

  • Create a Task Definition, a description of an application that contains one or more Docker containers.
  • Create a Cluster, a logical grouping of Amazon EC2 instances on which you can place tasks.
  • Create a Service, that allows you to run and maintain a specified number of instances of a task definition simultaneously.

You needs to have Auto Scaling group of EC2 instances that you will use to populate the Cluster and an Elastic Load Balancer for use with your ECS Service.

By the end of this tutorial, you will be able to:

  • Create an ECS Task Definition
  • Populate an ECS Cluster
  • Deploy an application to an ECS Service
  • Deploy an application update by changing the Task Definition
  • Scale up the application

What is Amazon Elastic Container Service?

Amazon Elastic Container Service (Amazon ECS) is a highly scalable, fast, container management service that makes it easy to run, stop, and manage Docker containers on a cluster of Amazon EC2 instances. Amazon ECS lets you launch and stop container-enabled applications with simple API calls, allows you to get the state of your cluster from a centralized service, and gives you access to many familiar Amazon EC2 features.

You can use Amazon ECS to schedule the placement of containers across your cluster based on your resource needs, isolation policies, and availability requirements. Amazon ECS eliminates the need for you to operate your own cluster management and configuration management systems or worry about scaling your management infrastructure.

COMPONENTS OF AMAZON ECS

Amazon ECS contains the following components:
  • Container Instance: An Amazon EC2 instance that is running the Amazon ECS agent and has been registered into a cluster.
  • Cluster: A logical grouping of container instances on which you can place tasks.
  • Task Definition: A description of an application that contains one or more container definitions. 
  • Task: A single instantiation of a Task Definition.
  • Scheduler: The method used for placing tasks on container instances. 
  • Amazon ECS service: Allows you to run and maintain a specified number of instances of a task definition simultaneously.
SCHEDULING AMAZON ECS TASKS

Amazon Elastic Container Service (Amazon ECS) is a shared state, optimistic concurrency system that provides flexible scheduling capabilities for your tasks and containers. The Amazon ECS schedulers leverage cluster state information provided by the Amazon ECS API to make an appropriate placement decision. Amazon ECS provides the service scheduler for long running stateless services and applications.

The service scheduler ensures that the specified number of tasks are constantly running and reschedules tasks when a task fails (for example, if the underlying container instance fails for some reason). The service scheduler optionally ensures that tasks are registered against a load balancer. You can update your services that are maintained by the service scheduler, such as deploying a new task definition, or changing the running number of desired tasks.

SERVICE DEFINITION PARAMETERS

A service definition defines which task definition to use with your service, how many instantiations of that task to run, and which load balancers (if any) to associate with your tasks.

SERVICE LOAD BALANCING

Your Amazon ECS service can optionally be configured to use Elastic Load Balancing to manage traffic.

SERVICE LOAD BALANCING


Register A Task Definition With The amazon-ecs-sample Image
  1. In the AWS Management Console, on the Services menu, click ECS.
  2. In the left navigation pane, click Task Definitions.
  3. Click Create new Task Definition
  4. On the Select launch type compatibility page,
    1. Click EC2
    2. Click Next step
  5. Scroll to the bottom of the screen, then click Configure via JSON (This option allows you to load a Task Definition from a configuration file rather than manually creating the definition.)
  6. Delete the code in the JSON panel. (The following task definition JSON file specifies two containers that are pulled from the Docker Hub repository.)
  7. Copy and paste the following task definition into the JSON panel (https://gist.github.com/truongnhatbui/e8bcab67dc419680013015d14548972d)
  8. Click Save (You will notice that the configuration screen has been populated based upon the template.)
  9. Scroll to the botton of the screen, then click Create (This will register the Task Definition in the yourApp-demo family.)

    Task Definitions
Create a Service

In this tutorial you will create a service definition.
  1. In the left navigation pane, Click Clusters. (Make sure you have already an ECS Cluster).


  2. Click ECSCluster >.
  3. On the Services tab, click Create then configure:
    1. Launch type:  EC2
    2. Service name: myServiceNUMBER
    3. Replace NUMBER with a random number
    4. Number of tasks: 1 (This is the number of tasks that will be launched and maintained on your cluster.)
  4. Scroll to the bottom of the screen, then click Next step
  5. In the Load balancing section, configure:
    1. Load balancer type: Network Load Balancer
    2. Service IAM role: ecsServiceRole. (This role allows Amazon ECS to register and deregister container instances on the load balancer as tasks are placed on them.)
  6. Click Add to load balancer then configure:
    1. Production listener port: 80:TCP
    2. Target group name: myTargetGroup
  7. Scroll to the bottom of the screen, then click Next step
  8. For Set Auto Scaling (optional), click Next step
  9. Review your information, then click Create Service
  10. Click View Service (Details of the service you just created will be displayed.)
  11. Click refresh every 30 seconds until the status for each task displays as RUNNING.
  12. Click the Events tab. (This shows the progress of the various tasks associated with your service creation request.)
  13. Click the Refresh  button till you get an event indicating that your service has reached a steady state. (This may take anywhere from 3-5 minutes.)
  14. Click Details tab.
  15. In the Details tab, click the Target Group Name.
    1.  myTargetGroup should be selected.
  16. Click the Targets tab. In the Targets tab, you can see all of the registered targets.

    all of the registered targets

    Instance Summary


  17. Under Basic configuration, click on MyLoadBalancer
  18. Under the Description, copy the DNS name of your load balancer to your clipboard.

    Load Balancer

  19. Open a new browser tab, then:
    1. Paste the load balancer DNS name
    2. Press Enter (If everything has been configured correctly you will see the sample app displayed as shown here:) 
Sample Apps


Deploy a New Application Version to the Service

Whenever you update the Docker image of an application, you can create a new task definition with that image and deploy it to your service, one task at a time. The service scheduler creates a task with the new task definition (provided there is an available container instance to place it on), and after it reaches the RUNNING state, a task that is using the old task definition is drained and stopped. This process continues until all of the desired tasks in your service are using the new task definition.
Keep the sample application browser tab open – you will use it again later.
  1. In the AWS Management Console, on the Services menu, click ECS.
  2. In the left navigation pane, click Task Definitions.
  3. Select  yourApp-demo.
  4. Click Create new revision
  5. Scroll to the bottom of the screen, then click Configure via JSON
  6. Delete the code in the JSON pane.
  7. Copy and paste the code updated task definition below into the JSON pane (https://gist.github.com/truongnhatbui/e8bcab67dc419680013015d14548972d)
  8. Click Save
  9. Click Create (This will change the message displayed from Congratulations! to Thank you!. In a production environment, you would likely change other aspects of your task definition.)


  10. In the left navigation pane, click Clusters.
  11. Click ECSCluster >.
  12. On the Services tab, select  myService.
  13. Click Update then configure:
    1. Revision: Select the latest revision
    2. Click Next Step
  14. On Step 2 (visible on the left side), click Next step
  15. On Step 3, click Next step
  16. Click Update Service
  17. Click View Service (This will deploy a new version of the application.)
  18. Click the Events tab.
  19. Monitor the process of draining connections and stopping tasks.
    1. You can refresh the events by clicking refresh .
    2.  You might see the message: service myService was unable to place a task because no container instance met all of its requirements.
    3. Because there are only four instances available this is the expected behavior. The system needs to stop the task on each instance before it can start a new one on each instance.
  20. Continue to refresh  occasionally until you see service myService has reached a steady state at the top of the Event list.
  21. Click the Deployments tab. (The Running Count should be the same as the Desired count.)
  22. Return to your web browser and refresh the Load Balancer’s URL (DNS name).
  23. You should see the new version of the app that says Thank You!
Update a Running Service

You can update a running service to change the number of tasks that are maintained by the service or which task definition is used by the tasks. If you have an application that needs more capacity, you can scale-up the service to use more of your container instances (if they are available). If you have unused capacity that you would like to scale-down, you can reduce the number of desired tasks in your service and free up resources.
  1. In the left navigation pane, click Clusters.
  2. Click ECSCluster >.
  3. On the Services tab, select  myService
  4. Click Update then configure:
    1. Number of Tasks 
    2. Click Next step
  5. On Step 2, click Next step
  6. On Step 3, click Next step
  7. Review the changes and then click Update Service
  8. Click View Service (The service will add an additional task.)
  9. Click refresh  every 30 seconds until the Running count matches the Desired count.
  10. Click the Events tab. (You will see an event that an additional instance was registered to myService.)
  11. Click the Tasks tab. (You should now see three tasks lists.)
ECS Clusters


Congratulations, you have deployed your first ECS Container, deployed a new version, and scaled it up!

Comments

Let's connect. A great way to get my attention is to comment on one of my posts.

Get In Touch

Send