Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Deploying Spring Boot to ECS (Part 2)

DZone's Guide to

Deploying Spring Boot to ECS (Part 2)

Want to learn more about how you can deploy your Spring Boot app with the ECS container? Check out this second installment to learn more!

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

This post is a continuation from our previous post on deploying Spring Boot to ECS.  In our second installment, we will cover how to deploy a Spring Boot application in the ECS container. We will be using a simple task planner application that is available on GitHub.

A Docker file is already available inside this project. Make sure you have Git, Docker, and AWS CLI installed wherever you are running the following commands. Check out this link for a Git clone.

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

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

Run the following command to see the image created:

docker image ls

What Is ECR?

The Amazon Elastic Container Registry (ECR) is a fully-managed Docker container 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 it a name called poc-repo.

1

After the repo is created, we need to push our images to this repo .Run using the following command:

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

Next, you need to copy and paste the output. You will see that you are now logged into the ECR.

You will then need to tag the image with a name and push the image to the ECR repo that we created above.

docker tag poc-repo:latest xxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/poce-repo:latestdocker 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 an AWS-managed service for deploying applications in containers. ECS manages instances using Fargate. Fargate is the latest technology trend in which the user does not need to bother 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 we will create an ECS cluster and deploy the Spring Boot application.

  1. Create a Cluster

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 every time 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 the Public Subnet

1

Associate to the alb security group, which will be open to HTTP and HTTPS in 80 and 443 ports respectively with the source from anywhere.

Next, create a target group where theloadbalancer will route the request. Make sure to mark the target type to IP.

1

Allow all IP addresses from the VPC subnet that we created.

1

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

  1. Creating an ECS service

1

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

Next, you will need to associate the cluster that we created above (ecs-fargate)

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

1

Associate the tasks in the private subnet in the VPC section and associate the security group to the private security group created in the previous article.

1

Also, enable the auto assign IP.

1

Associate the Loadbalancer we created and the corresponding target group

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

Create the Service.

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

1.png

The ECS cluster should have two running tasks and one service :

1

Test the loadbalancerDNS name and test the app:

1.png

That’s it – now, you have successfully deployed a Spring Boot "dockerized" application with the ECS Fargate cluster in a private subnet with an app Loadbalancerin the public subnet.

Now, let's see how we can use Apache Benchmark to increase load in this system and scale the application to a maximum of four tasks in the cluster.

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

If it is Ubuntu, you can install it 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 requests in total at 500 requests concurrently.

Finally, you can see that two more instances will scale up automatically.

Happy coding!!

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
ecs ,spring boot ,fargate ,aws ,devops ,tutorial ,java ,aws cli ,docker

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}