Codementor Events

Deploying Spring Boot in ECS Cluster- Part 2

Published Feb 04, 2019

In continuation to part 1 of the article ..

In part 2 of this article , we will cover how to deploy Spring boot application in ECS container . For this article we will using a simple task planner application available in  github https://github.com/jokumar/task-planner

Already a docker file is available inside this project . Make sure you have git , docker and aws cli  installed where ever you are running the below commands .

git clone https://github.com/jokumar/task-planner.git

Navigate inside task-planner folder and run the below command to create an image :

docker build -t {dockerId}/taskplanner-0.1.0 -f Dockerfile .

Run the below command to see the image created :

docker image ls

What is ECR

Amazon Elastic Container Registry (ECR) is a fully-managed Dockercontainer registry that makes it easy for developers to store, manage, and deploy Docker container images. Amazon ECR eliminates the need to operate your own container repositories. ECR is well integrated with ECS and we will see that shortly .

Create a repository from the ECS console in AWS and give  a name called poc-repo . 1

After the repo got created , now we need to push our images to this repo .Run the below command :

aws ecr get-login – – region us-east-1

copy paste the output and you will see you got logged in to the ecr .

now tag the image with a name and push the image to the ecr repo we created above .

docker tag poc-repo:latest xxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/poce-repo:latest__docker push xxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/poce-repo:latest

You should be able to see the image in the ECR console.

What is ECS ?

ECS is a AWS managed service for deploying applications in containers . ECS manages with instances and also using Fargate . Fargate is the latest technology trend in which user does not need to bother about managing the instances . Fargate enables you to focus on building and running applications, not the underlying infrastructure.

Amazon ECS lets you easily build all types of containerized applications, from long-running applications and microservices to batch jobs and machine learning applications. Amazon ECS launches your containers in your own Amazon VPC, allowing you to use your VPC security groups and network ACLs. We will see below how how we will create an ECS cluster and deploy the spring boot application .

1

1

  1. Create a new Task definition :

Select the Fargate option and start creating a new task .

Task Definition Name : fargate-new-A

Task Role : ecsTaskexecutionRole

Network Mode: awsvpc

Requires compatibilities: FARGATE

Task execution role: ecsTaskexecutionRole

Task memory:4GB

Task CPU:2vCPU

Add Container:

Container name: task-planner

Image: xxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/poce-repo:latest

Memory Limits: Hard limit : 1024 , Soft limit :512(Hard and soft limits correspond to the memory and memoryReservation parameters, respectively, in task definitions. )

Port Mapping : 9000 (as my container port is 9000)

Volume Mounting: Volume mounting is important else everytime all the m2 jars will be downloaded and there will be no persistence across the cluster . Create a storage volume named as dockervolume and map the container path like below:

1

  1. Creating an Application Load balancer in public subnet.

1

Associate to alb security group which will be open to http and https in 80 and 443 ports respectively with source from anywhere.

Next create a target group where the loadbalancer will route the request to . Make sure to mark the target type to ip .

1

Allow all ip addresses from the vpc subnet we created .

1

A target group also gets created “ecs-target”, which has no target registered yet .

1

Task definition : fargate-new-A which we created above with latest revision.

Associate the cluster we created above (ecs-fargate)

Give a service name and the required number of task to be running .

1

Associate the tasks in private subnet in vpc section and associate the security group to private security group created in previos article .

1

Also enable the auto assign IP .

1

Associate the Load balancer we created and the corresponding target group

1

Attach the auto scaling policy where the required number of task would be 2 and it will scale up to 4 tasks when the number of ALB request per target goes above 50 . We will test this in future section with Apache benchmark .

Create the Service .

1

Now come back to the load balancer target group and you can see the below all the availability zone has healthy status

1.png

ECS cluster should have 2 running task and 1 service :

1

Test the loadbalancer DNS name and test the app :

1.png

That’s it now you have successfully deployed a spring boot dockerized application in ECS fargate cluster in private subnet with an Application Loadbalancer in public subnet .

Now lets see how we can use Apache Benchmark to increase load in this system and scale the application to maximum of 4 tasks in the cluster .

Install Apache Benchmark in your machine and run the following command :

If it is Ubuntu , you can install by running : apt install apache2-utils

ab -n 50000 -c 500 http://ecs-fargate-lb-734855749.us-east-1.elb.amazonaws.com/login

It tells to give 50000 request in total at 500 request concurrently

Finally you can see the 2 more instances will scale up automatically .

Digiprove sealCopyright secured by Digiprove © 2019 Geeks 18

Discover and read more posts from Joydip Kumar
get started
post commentsBe the first to share your opinion
Sagar Churi
4 years ago

Thanks for the detailed steps. Two issues I faced

  1. aws ecr get-login - This command seems incomplete. I followed the instructions from ECR console on the push command.
  2. Platform version for the Service is ‘1.4.0’. LATEST gives an error. This may have changed from the time when the blog was written.
Show more replies