AWS ECS (Elastic Container Service) & Docker Containers Using CLI

AWS ECS

Introduction:

ECS is container management micro service provided by AWS. It is highly scalable and fast way to manage / start / stops Docker containers within AWS cloud.

It allows easily to manage applications running under docker containers and deployed on ECS cluster of EC2 instances.

When you deployed docker container in ECS that means you placed them on cluster which is logical grouping of EC2 instances.

ECS allows us to access some core important features like security group, EBS volume and Elastic Load Balancer (ELB) same like EC2 Instance.

ECS is an abstraction of EC2 service so instead of using EC2 virtual machines we can use most compact portable docker containers.

AWS provides APIs to automate and integrate with AWS ECS services.

Challenges with traditional software development life cycle:

Infrastructure:

  • Difficulties to scale up apps
  • Long build / test / release cycle
  • Operation was nightmare
Application:

  • Architecture as hard to maintain AND evolve
  • New release takes months
  • Long time to add new features
Business:

  • Lack of agility
  • Lack of innovations
  • Frustrated customers

Why move to containers?

  • Portable 
  • Flexible
  • Fast
  • Efficient
  • Super Lightweight


Following are the core concept of ECS service:

Docker File: Is s simple text file, defines app environment and dependencies libraries.
Example: Server (Linux/Ubuntu etc.) & dependencies libraries (Apache, MySql and PHP etc.)

Docker File Best Practice:

  • Reduce build image size and number of layers. For example don't add unnecessary things in image and keep it optimized.
  • Use specific tag naming conventions for version of image so that we can rollout and rollback image. For example: Version 1.0.0.1 & Version 1.0.0.2 etc.
  • In case of multi region pull same image and push to all region.

Docker Image:
Is a packaged application code like source code, dependencies libraries, environment variables, config files etc. It is reproducible, portable and immutable that means you can run this image on any platform.

Elastic Container Registry (ECR):
It is a docker container registry which is fully managed that means you no need to manage infrastructure & hosting these docker registry.
It is highly available as it internally using S3 service.
It is secure as it uses IAM resource based policies so you can provide push permission to some account and pull permission to other account user.

Cluster:
It is logical grouping of all resources that you have.
Infrastructure isolations boundary
IAM permission boundary

Container Instances:
They are EC2 instances with ECS agent installed on ECS agent. ECS agent is a open source software that looks state of instances and respond back to service.

Task Definitions:
It is a way to define your applications
Defines application containers which includes image URL , CPU , and memory requirements. etc. It is a blueprint of task.

Task:
It is a running instantiation of task definition

Service:
Run and maintain the desired number of copies of tasks.
Maintain n running copies
Integrated with ELB
Unhealthy task automatically replaced with healthy tasks.
It very similar to EC2 auto-scaling concept.

Advantages of ECS:

  • You can group multiple containers in task definition
  • You can attach / mount volume to task definition
  • You can link two containers and established a network between them to talk each other. 
  • For Example: There are 2 applications one in JAVA and other in .NET and both are in different containers, then using APIS they can interact with each other.
  • Good for log-running applications
  • Load balance traffic across containers
  • Automatically recover unhealthy containers
  • Discover services


Container Use Case:
You can use containers to create distributed applications by breaking your application into independent tasks or processes like AWS microservices.

Example 1: Distributed Applications and Microservices
You can have separate containers for your webserver, application server, message queue and backend workers. Containers are ideal for running single tasks, so you can use containers as the base unit for a task when scaling up and scaling down. Each component of your application can be made from different container images. Docker containers provide process isolation allowing you to run and scale different components side by side regardless of the programming language or libraries running in each container.

Example 2: A Multiplayer Gaming Platform
Ubisoft gaming company using AWS ECS. They creates, publishes, and distributes popular interactive video games for players throughout the world.
Read more here about Ubisoft 

ECS WorkFlow


How To Install Wordpress and PhpMyAdmin with Docker Compose on Amazon Linux instance using CLI:

1. Update the installed packages and package cache on your instance.

>> sudo yum update -y

2. Install the most recent Docker Community Edition package.

>> sudo yum install -y docker

3. Start the Docker service.

>> sudo service docker start

4. Add the ec2-user to the docker group so you can execute Docker commands without using sudo.

>> sudo usermod -a -G docker ec2-user

5. Pull centos docker public image

>> sudo docker pull centos

6. Run centos container to test

>>sudo docker run -it centos
>> exit

7. We will use Docker compose to run all containers with a command (wordpress , mysql , phpmyadmin containers) define all these containers in one yaml file.

8. First install python pin (if not already done)

>>sudo yum install python-pip

9. Install docker compose

>> sudo pip install docker-compose

10. Now create a directory called wordpress in project root directory in my case var/www/html

>> cd /var/www/html
>> sudo mkdir wordpress
>> cd wordpress

11. create docker-compose.yaml file &

>> sudo touch docker-compose.yaml
>> sudo chmod -R 666 docker-compose.yaml

paste below definitions in above file

wordpress:
  image: wordpress
  links:
    - wordpress_db:mysql
  ports:
    - 8080:80
wordpress_db:
  image: mariadb
  environment:
    MYSQL_ROOT_PASSWORD: pgtest
phpmyadmin:
  image: corbinu/docker-phpmyadmin
  links:
    - wordpress_db:mysql
  ports:
    - 8181:80
  environment:
    MYSQL_USERNAME: root
    MYSQL_ROOT_PASSWORD: pgtest


12. Below command will create 3 containers and you are done with setup.

>> sudo docker-compose up -d

Lets verify:

http://your-public-ip-address:8080 wordpress admin
http://your-public-ip-address:8181 wordpress phpmyadmin


How to un-install docker on Centos image and docker containers:

First stop your current running docker-compose session

>> docker-compose stop

Remove the existing container

>> docker-compose rm wordpress




Happy Learning AWS Services!!!! :) Still Doubts? lets put your questions in below comment box! Thanks!

Watch Video: 


References:
https://aws.amazon.com/ecs/
https://docs.aws.amazon.com/cli/latest/userguide/awscli-install-linux.html
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html
https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-and-phpmyadmin-with-docker-compose-on-ubuntu-14-04
https://docs.docker.com/machine/examples/aws/
https://docs.docker.com/compose/reference/rm/

AWS ECS (Elastic Container Service) & Docker Containers Using CLI AWS ECS (Elastic Container Service) & Docker Containers Using CLI Reviewed by Web Technology Funda on 4:24:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!