Open In App

Deploy Java Microservices on Amazon ECS using AWS Fargate

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In recent, Microservices architecture has gained huge popularity. It provides an effective way to develop and deploy applications. The Architecture of Microservices works on dividing a Monolithic big application into smaller sub-applications as independent services that can be easily developed, deployed, and scalable separately. This type of approach provides more benefits of features such as flexibility, scalability, and faster development cycles. In this blog article, we will discuss on Deployment of Java microservices on Amazon Elastic Container Service (ECS).

What is Amazon Elastic Container Service?

Amazon Elastic Container Service (ECS) is an AWS Service that comes up with a fully managed container orchestration service facilitating an easier way to run, stop, and manage Docker containers on a cluster. On using Amazon ECS, you can launch and scale containerized applications easily and quickly. It comes up with integration capability with other AWS services including Amazon Elastic Load Balancing, Amazon Virtual Private Cloud, and AWS Identity and Access Management.

Key Features and Benefits of Amazon ECS

Features of Amazon ECS

  • Container Orchestration Efficiency: AWS ECS facilitates in management of containerized applications through providing easy deployment, scheduling, and scaling of containers.
  • Flexibility In Launching: AWS ECS provides flexibility over launching the deployments supporting both EC2 instances and AWS Fargate.
  • Interation with ELB: It provides seamless integration with ELB (Elastic Load balancers) for automated distribution of traffic ensuring high availability and efficient load balancing for applications.

Benefits of Amazon ECS

  • Simplicity and ease of Use: AWS ECS provides a simple setup for the configuration and Deployment process .
  • Effortless Scaling of applications: ECS optimizes the utilization of resources with efficient container usage strategies. It offers pay as you go model bringing cost-effective management of resources.
  • Security Features in Integration: AWS ECS secures the execution of containers with AWS Identity and IAM Integration.

Why deploy Java Microservices on Amazon ECS?

Deploying Java microservices on Amazon ECS provides many benefits, including:

  1. Scalability: ECS makes scaling up or down your application based on demand easy. You can add or remove containers as required to handle the traffic.
  2. Flexibility: ECS supports multiple container orchestration patterns, including EC2 and Fargate launch types. You can choose the launch type that suits your application requirements.
  3. High availability: ECS provides built-in features for load balancing, Auto-scaling, and Monitoring. This ensures that your application is highly available and can handle the traffic even during peak times.
  4. Security: ECS integrates with AWS services such as Amazon Virtual Private Cloud (VPC), AWS Identity and Access Management (IAM), and AWS CloudTrail. This provides a secure environment for your application.

Advantages and Suitability for Java Microservices

Portability and Platform Independence

Enhances strong Ecosystem

  • Java facilitates more needed libraries, frameworks, and tools for efficient deployment of the microservices. Its rich collection of resources enhances the development process with the creation of scalable maintenance of microservices.

Strong Community Support

  • Java comes up with good community support providing continuous updates and learnings benefiting the developers with shared experiences, and best practices for building and maintaining microservices.

Management of Concurrency

  • Management of concurrency is handled by Java built-in features such as multi-threading and JVM garbage collector facilitating the optimization and resource improvement of the microservice’s overall system efficiency.

Security and Reliability

  • Java focuses on security and reliability by providing extensive testing frameworks and strong typing to contribute to microservices, particularly for distributed systems of microservices architecture.

OverView of the Java Microservices Deployment Process

Deployment of java microservice application on Amazon ECS using AWS Fargate goes into the sequential process such as Containerizing the application with Docker, pushing the Image to Amazon ECR, and Defining configuration of services and tasks. Specifying the Fargate as the launching type, configuring the requirements of resources, and creating an ECS cluster. Based on the defined take launching the Fargate service and utilizing it facilitates automated infrastructure management and scalability. Monitoring the deployment using Amazon CloudWatch for performing scaling and adjustments seamlessly.

Step-by-Step Deployment of the Java Microservices on Amazon ECS

In this article, we will use AWS open-sourced project ECS_Java_Spring_PetClinic.

Step 1: Build your Java microservices container image

In this sample project, we have a Dockerfile with the below content.

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD spring-petclinic-rest-1.7.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"]

Explanation

  • The above configuration will use the alpine-Java8 Slim version as the base image
  • Creates a mount point for volume
  • Copies the application jar file from the local device volume to the image.
  • Sets the entry point, to run the jar app as a startup.

Step 2: Build and Push the Image to ECR

Build Docker Image using the below command, here you can replace my_micro_services with any name you want.

docker build -t my_micro_services .

Here, we will push our Docker image to AWS ECR a container repository, if you don’t have a repo already create one using AWS Console.

Create a ECR Repo

aws ecr get-login-password --region ap-south-1 |
docker login --username AWS --password-stdin
<AWS_ACCOUNT_ID>.dkr.ecr.ap-south-1.amazonaws.com

Tag the image with the respective image name in the ECR.

docker tag my_micro_services:latest <AWS_ACCOUNT_ID>
dkr.ecr.ap-south-1.amazonaws.com/my_micro_services:latest

Push the image to ECR

docker push <Your_Aws_Account_Id>.dkr.ecr.ap-south-1
amazonaws.com/my_micro_services:latest

Step 3: Create an Amazon ECS Cluster

Fristlt you have to create an Amazon ECS cluster. An ECS cluster is a logical grouping of EC2 instances that facilitates you for using to run your Docker containers. The following are the steps to create an ECS cluster:

  • Open the Amazon ECS console > Cluster page.
  • Choose the region where you want to create the cluster.
  • Choose “Create Cluster.”
  • Select the “EC2 Linux + Networking” cluster template.
  • Provide a name for your cluster.
  • Choose “Create.”

Create ECS Cluster

Step 4: Create an ECS Task Definition

A task definition is a blueprint for your application that describes how Docker containers should be launched and configured. The following are the steps to create an ECS task definition:

  • Open the Amazon ECS console.
  • Choose “Task Definitions” from the left-hand menu.
  • Choose “Create new Task Definition.”

Create Task Definition in ECS

  • Configure environment ( Fargate )
  • Configure storage
  • Configure monitoring, and tags.

Configure Environment, Storage, Monitoring, and Tags for Task Definition

  • Review Configured data before creating.
  • Choose “Create”

Review the Configuration and Create

Step 5: Create an ECS Service

An ECS service is a way to run and maintain a specified number of instances of a task definition. Follow these steps to create an ECS service.

  • Open the Amazon ECS console > Clusters
  • Choose the cluster in which you want to create a service.
  • Under the services tab, choose Create.
  • Provide a name for your service.
  • Choose the launch type as Fargate.
  • Select the task definition that you created in the previous step.
  • Configure the service settings, such as the number of desired tasks, load balancer settings, and auto-scaling policies.
  • Choose “Create Service”

Create a Service in the ECS Cluster

This will launch a service with desired no of tasks if everything was done properly, so you can access your services now.

Step 6: Deployment Automation

You can use the script in the given repo to do all the above things, to do that just run the below command in the CLI.

import subprocess

def create_ecr_repository(service):
# Command to create an ECR repository
mycommand = f"aws ecr create-repository --repository-name {service}"
subprocess.run( mycommand, shell=True, check=True)

def push_to_ecr(service, region):
# Command to push Docker image to ECR repository
mycommand = f"$(aws ecr get-login --no-include-email --region {region})"
subprocess.run(mycommand, shell=True, check=True)

# Build and push Docker image to ECR
docker_build_command = f"docker build -t {service} ."
docker_push_command = f"docker push {service}"

subprocess.run(docker_build_command, shell=True, check=True)
subprocess.run(docker_push_command, shell=True, check=True)

def create_load_balancer():
# Command to create a load balancer
# Customize as per your requirements
command = "aws elbv2 create-load-balancer --name my-load-balancer --subnets subnet-abc123 subnet-def456"
subprocess.run(command, shell=True, check=True)

def register_task_definition(service):
# Command to register a task definition
# Customize as per your requirements
mycommand = f"aws ecs register-task-definition --family {service} --container-definitions file://container-definition.json"
subprocess.run(mycommand, shell=True, check=True)

def create_service(service):
# Command to create an ECS service
# Customize as per your requirements
mycommand = f"aws ecs create-service --cluster my-cluster --service-name {service} --task-definition {service} --desired-count 1"
subprocess.run(mycommand, shell=True, check=True)

def main():
region = "<your region>"
services = ["service1", "service2", "service3"] # Add your list of services

for service in services:
create_ecr_repository(service)
push_to_ecr(service, region)
create_load_balancer()
register_task_definition(service)
create_service(service)

if __name__ == "__main__":
main()


Customize the region name and services such as service1 , service2 , service as needed for you. Inaddition ensure the dockerfile and container-definitions.json present in the appropriate directories.

python setup.py -m setup -r <your region>


This script will do the following things:

  • Now Create an ECR repository for each service in a list of services and pushes it to the ECR repository for each service.
  • Creates a load balancer.
  • Registers a task definition for each microservice.
  • Creates a service for each microservice.

To know more about the article and to do practical effectively go through the following Articles:

Conclusion

In conclusion, Amazon Elastic Container Service is a powerful and flexible tool for deploying Java microservices in the cloud. By following the six steps outlined in this blog post, you can easily create a scalable and reliable microservices architecture that can meet the demands of your users. With Amazon ECS, you can focus on building great applications and let Amazon handle the infrastructure management.

Java Microservices Deployment, ECS and AWS Fargate – FAQs

What are Java microservices, and why deploy them on AWS ECS or Fargate?

Java microservices are independent smaller components that are independently deployable and collectively form an application. These microservices can be scalable and manageable with infrastructure on deploying on either Amazon ECS or AWS Fargate for efficient utilization of resources.

How can I deploy Java microservices on Amazon ECS?

Through Containerizing an application and defining task definition with respective containers you can deploy a java based microservice application. On creating a service for that deployment you can facilitate efficient managing, scaling, and availability of the application.

What is the difference between AWS ECS and AWS Fargate for Java microservices deployment?

Amazon ECS is a container Orchestration Service used for the management of underlying infrastructure whereas AWS Fargate abstracts the infrastructure management letting the user completely focus on the logic of the application for deploying and running the Java Microservices.

How do I scale Java microservices on AWS ECS or Fargate?

Amazon ECS is a container Orchestration Service used for the management of underlying infrastructure whereas AWS Fargate abstracts the infrastructure management letting the user completely focus on the logic of the application for deploying and running the Java Microservices.

What considerations should be taken for high availability when deploying Java microservices on AWS ECS or Fargate?

To make the deployment of Java microservices with high availability, try deploying across multi-AZ (Availability zones ), Configuring the Auto-scaling and utilizing features such as AWS ECS for Autoscaling to ensure the resilience to failures.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads