Deploying with ECS

As described earlier in this chapter, ECS takes care of deploying Docker images automatically, and sets up all the services needed around the instances.

You do not need, in this case, to create EC2 instances yourself. ECS uses its own AMI, which is tweaked to run Docker containers on EC2. It is pretty similar to CoreOS, as it comes with a Docker daemon, but it is integrated with the AWS infrastructure for sharing configuration and triggering events.

An ECS cluster deployment is composed of many elements:

  • An Elastic Load Balancer (in EC2) to distribute the requests among the instance
  • A Task Definition, which is used to determine which Docker image needs to be deployed, and what ports should be bound between the host and the container
  • A Service, which uses the Task Definition to drive the creation of EC2 instances, and run the Docker container in them
  • A Cluster, which groups Services, Task Definitions, and an ELB

Deploying a cluster on ECS when you are not used to it is complex, because it requires creating elements in a specific order. For instance, the ELB needs to be set up before everything else.

Fortunately, everything can be created for you in the right ordering via the first run wizard. This wizard is displayed when you go to the ECS service on the console for the first time, and will bootstrap everything for you. Once you are on the landing page, Click on the Get Started button to launch the wizard.

You can check the Deploy a sample application onto Amazon ECS Cluster option, and get going:

This action brings up a task definition creation dialog, where you can define a name for the task and the container to be used for that task:

In the preceding example, we deployed the same Flask application that we deployed earlier on EC2, so we provide that image name on the container form for the task definition. The Docker image needs to be present on Docker Hub or AWS's own Docker images repository.

In that form, you can also set up all the port mapping between the Docker container and the host system. That option is used by ECS when the image is run. Here, we bind port 80, which is where the Flask docker image that we are using exposes the app.

The next step in the wizard is the Service configuration:

We add three tasks into that service, as we want to run three instances in our cluster with one Docker container running on each one of them. In the Application Load Balancer section, we use the container name and the port defined earlier. Lastly, we need to configure a cluster, where we set up an instance type, some instances, and the SSH key pair to use:

Once you validate that last step, the ECS wizard works for a little while to create all parts, and once it is ready, you end up with a view service button, which is enabled once all the parts are created. The Service page summarizes all the parts of the deployment, and has several tabs to check every service in detail. The deployment done by the ECS wizard can be summarized as follows:

  • A task definition was created to run the Docker container
  • A cluster of three EC2 instances was added, and in that cluster
  • A Service was added to the cluster, and Task Definition was used to deploy Docker containers in the EC2 instance
  • The deployment was load-balanced by the ELB created earlier

If you go back to the EC2 console, and visit the Load Balancing | Load Balancers menu on the left, you will find the newly created ECS-first-run-alb ELB that is used to serve your ECS cluster.

This ELB has a public DNS name, which you can use in your browser to visit your Flask app. The URL is in the form of http://<ELB name>.<region>.elb.amazonaws.com.

The next section explains how to link this ELB URL to a clean domain name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset