Introduction
When you decide to deploy microservices on Amazon ECS, you will have to use AWS Fargate. The advantage of using AWS Fargate is that it offers scalability along with flexibility for your containerized applications. In order to deploy the Java microservices on ECS using Fargate, we will be taking help of the following two approaches.
- Using Docker images from Docker hub
- Using Elastic Container Registry
Approach 1: Docker Hub Deployment
Prerequisites
- There should be existing java microservices application on Docker hub
- A public Docker repository
- A running AWS account
- Know how about Amazon ECS and AWS Fargate
- Having access to Docker, Spring Boot framework and Java
- For the optional services you can use Amazon RDS and VPC
Architecture
Source Technology Stack
- The source technology stack includes deployment of Java microservices implemented in Spring Boot framework.
Source Architecture
- The client sends request to On-premises Docker
- The requests include GET, PUT, POST and DELETE
- Docker sends the response in JSON format back to the client
Target Technology Stack
- Amazon ECS cluster is responsible for hosting the microservices with Fargate
- VPC must be used for ECS cluster along with all the associated security groups
Target Architecture
- AWS Fargate service within Amazon ECS contains all the running tasks
- Protected by Subnet, the request sent by the client is handled accordingly
- If the sent request is successfully accepted, then the response is sent back in the form of JSON
Tools
- Amazon ECS removes the need of manual scaling and container orchestration
- AWS Fargate will run the containers without the management of servers
- Docker is used for the containerization i.e. building, testing and deploying applications in a containerized form
Dockerfile
FROM openjdk:11penjdk:11
ADD target/Spring-docker.jar Spring-docker.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","Spring-docker.jar"]
Epics
Creating New Task Definitions
- Create a task definition in the ECS console
- You should possess the skills of an AWS system administrator along with app developer
- Steps
- Open ECS console
- Go to the Task Definitions
- Create a New Task Definition
- Choose the launch type as Fargate
- Configure the things like task name, CPU and memory
- Defining the container will include details about Docker image, port mappings and memory limits
- At last, create the task
Configure the Cluster
- Create and configure an ECS cluster
- You should possess the skills of an AWS system administrator along with app developer
- Steps
- Select cluster type as Networking Only
- Configure the name of the cluster according to your choice
- Create the cluster
- Along with this, create and update the security groups
- Configure public IP settings, subnets and VPC
Run the Task
- Now it is time to run the configured task in the cluster
- You should possess the skills of an AWS system administrator along with app developer
- Steps
- Select Run New Task option present inside the cluster
- Select launch type as Fargate
- Select the following: revision, task definition, cluster and platform version
- Now specify the optional task group and number of tasks
- Configure public IP settings, security groups, VPC and subnets
- Confirm the settings
- Run the task
- Cut Over
- It is important to test the working of the application after its deployment
- You should possess the skills of an AWS system administrator along with app developer
- Steps
- The running task will have the application’s URL, copy it
- Go to your browser and enter the public IP in order to test the application
- Related Resources
Approach 2: Amazon ECR and Fargate Deployment
Prerequisites
- There should be Java microservices application running on Docker (On-Premises)
- A running AWS account
- Know how about Amazon ECS, AWS CLI and AWS Fargate
- Having access to Docker and Java
- Using AWS CLI 1.7 version or later
Architecture
Source Technology Stack
- You should have Java microservices developed using Spring Boot framework
- These microservices should be deployed On-Premises with Docker
Source Architecture
- The client sends request to On-premises Docker
- The requests include GET, PUT, POST and DELETE
- Docker sends the response in JSON format back to the client
Target Technology Stack
- For managing Docker containers, use Amazon ECR service
- Amazon ECS cluster should be compatible with Fargate
Target Architecture
Tools
- Amazon ECR will be used for registry management like managing, storing and deploying container images from Docker
- Amazon ECS will be used to provide a scalable container orchestration service that support Docker containers
- AWS Fargate will serve the role of a compute engine for ECS. It will run the containers without actually managing the servers
- Docker will be used for testing, building and delivering containerized applications
Dockerfile
FROM openjdk:8
ADD target/Spring-docker.jar Spring-docker.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","Spring-docker.jar"]
Epics
Creating an Amazon ECR Repository
- Create a private repository in Amazon ECR
- You should possess the skills of a developer along with a System Admin
- Steps
- Sign in to your AWS account using AWS Management Console
- Go and open Amazon ECR console
- Create a private repository from there
- Start uploading the project using AWS CLI version 1.7 or newer
- Copy the URL of your repository for future needs
Create and Spin Up the Container
- Create a task definition and run the task in the ECS cluster
- You should possess the skills of a developer along with a System Admin
- Steps
- Firstly, open ECS console
- Create a new task definition
- For launch type choose Fargate
- Configure the memory, task name, CPU along with this define the container by using ECR repository URL
- Now finally create the task
Create an ECS Cluster and Configure a Service
- Creating an ECS cluster, a service, and configure its settings accordingly
- You should possess the skills of a developer along with a System Admin
- Steps
- Create a cluster
- Based on your requirements, choose the cluster type
- Select Fargate as the Launch Type and create a service within the cluster
- Configure the following: platform version, revision and task definition
- Provide the service name, set deployment settings and specify the number of tasks
- Configure load balancing, cluster VPC, subnets, public IP, automatic scaling and security groups
- Confirm the settings
- Create the service
- Cut Over
- Test the application after its deployment
- You should possess the skills of a developer along with a System Admin
- Steps
- Start testing the application with the help of public DNS that you received during the task deployment
- If load balancer has been used in your application then it is important to test the application before giving it a final yes for cut over
- Related Resources
Conclusion
All in all, if you want a robust solution for containerized applications then you have to deploy the Java microservices on Amazon ECS using AWS Fargate. It doesn’t matter which approach you use (pulling an image from Docker hub or using Amazon ECR), both of them will offer you ease in deployment, scalability and manageability. It is up to you which approach you use depending upon your requirements. Here is the detailed AWS Re-invent Talk on Deployment of microservices using AWS Fargate