In recent years, microservices architecture has gained popularity as a way to develop and deploy applications. Microservices architecture breaks down an application into smaller, independent services that can be developed, deployed, and scaled separately. This approach allows for more flexibility, scalability, and faster development cycles. In this blog post, we will discuss deploying Java microservices on Amazon Elastic Container Service (ECS).
Prerequisites
- AWS Account
- AWS Cli Installed
- Also, make sure to have below things installed:
- Python
- Docker
- Maven
- Java
What is Amazon Elastic Container Service?
Amazon Elastic Container Service (ECS) is a fully managed container orchestration service that makes it easy to run, stop, and manage Docker containers on a cluster. With ECS, you can launch and scale containerized applications easily and quickly. It is integrated with other AWS services such as Amazon Elastic Load Balancing, Amazon Virtual Private Cloud, and AWS Identity and Access Management.
Why deploy Java Microservices on Amazon ECS?
Deploying Java microservices on Amazon ECS provides many benefits, including:
- 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.
- Flexibility: ECS supports multiple container orchestration patterns, including EC2 and Fargate launch types. You can choose the launch type that suits your application requirements.
- 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.
- 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.
Steps to Deploy Java Microservices on Amazon ECS
In this article, we will use AWS open-sourced project ECS_Java_Spring_PetClinic.
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 config 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.
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 .
We gonna push our Docker image to AWS ECR a container repository, if you don’t have a repo already create one using AWS Console.
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 <AWS_ACCOUNT_ID>.dkr.ecr.ap-south-1. amazonaws.com/my_micro_services:latest
Create an Amazon ECS Cluster
The first step is to create an Amazon ECS cluster. An ECS cluster is a logical grouping of EC2 instances that you can use to run your Docker containers. Follow these 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 an ECS Task Definition
A task definition is a blueprint for your application that describes how Docker containers should be launched and configured. Follow these 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.”
- Configure environment (Fargate)
- Configure storage
- Configure monitoring, and tags.
- Review Configured data before creating.
- Choose “Create.”
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”
This will launch a service with desired no of tasks if everything was done properly, so you can access your services now.
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.
python setup.py -m setup -r <your region>
This script will do the following things:
- Creates 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.
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.