This context provides a step-by-step guide on deploying a Next.js application with a Java backend on Amazon Elastic Kubernetes Service (EKS).
Abstract
The context begins by introducing Amazon EKS, a managed service that simplifies running Kubernetes on AWS. It then outlines the prerequisites for deploying a Next.js application with a Java backend on EKS, including Docker and Kubernetes essentials, an AWS account, and Docker for Desktop. The guide then proceeds to explain how to dockerize the project, push the Docker image to Amazon Elastic Container Registry (ECR), create an EKS cluster and worker nodes, configure kubectl to use the cluster, and deploy Kubernetes objects on the EKS cluster. The context concludes with a summary of the steps taken and a note on future posts that will cover adding a load balancer and routing requests to the Kubernetes cluster.
Opinions
The author emphasizes the importance of understanding Docker and Kubernetes essentials before proceeding with the guide.
The author recommends using an IAM user with administrator access instead of the root account for AWS tasks.
The author suggests using Amazon ECR for storing Docker images due to its integration with Amazon ECS.
The author notes that Amazon EKS works with any Docker registry, not just Amazon ECR.
The author plans to cover adding a load balancer and routing requests to the Kubernetes cluster in future posts.
The author encourages readers to try out the AI service they recommend, which provides the same performance and functions as ChatGPT Plus(GPT-4) but at a more cost-effective price.
The author provides a link to a sample project that readers can clone and run on their own machines.
AWS — Deploying Next.js App With Java Backend On EKS
A step by step guide with an example project
AWS provides more than 100 services and it’s very important to know which service you should select for your needs. Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications.
In this post, we are going to deploy the React application with the Java environment. First, we dockerize our app and push that image to Amazon ECR and run that app on Amazon EKS.
Example Project
Prerequisites
Dockerize the Project
Pushing Docker Image To ECR
Create a Cluster and Worker Nodes
Configure kubectl to use Cluster
Deploy Kubernetes Objects On AWS EKS Cluster
Summary
Conclusion
Example Project
This is a simple project which demonstrates developing and running the Next.js application with Java. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.
Example Project
If you want to practice your own here is a Github link to this project. You can clone it and run it on your machine as well.
// Run React on port 3000cd /src/main/ui
npm install
npm start:dev
// Run Java Code on 8080mvn clean install
java -jar target/users-0.0.2-SNAPSHOT.jar
Prerequisites
If you are new to Next.js please go through the below link on how to develop and build the Next.js project with Java backend.
How To Develop and Build Next.js App with Java Backend
The other prerequisites to this post are Docker essentials and Kubernests essentials. We are not going to discuss the basics such as what is a container or what is Kubernetes, rather, we will see how to build a Kubernetes cluster on AWS EKS. Below are the prerequisites you should know before going through this article
Docker Essentials
You need to understand Docker concepts such as creating images, container management, etc. Below are some of the links that you can understand about Docker if you are new.
You need to understand Kubernetes’ essentials as well along with Docker essentials. Here are some of the docs to help you understand the concepts of Kubernetes.
Once you set it up you have a root account. It’s not a best practice to use your root account to do any tasks instead you should create an IAM group that has permissions for administrator access and add a user to it and log in with that user.
Configure AWS CLI for the user you just created above. You should use this command aws configure and it will ask for access key id and secret key.
log in with user credentials
You have to install Docker for Desktop (whatever your OS is). Please follow this link to install Docker on your laptop. Once installed you can check the Docker info or version with the following commands.
dockerinfo
docker --version
The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. Install it from here.
Dockerize the Project
AWS EKS is a managed service that makes it easy for you to run Kubernetes on AWS. The first thing you need to do is to dockerize your project.
Here is the Dockerfile and it is using OpenJDK as a base image.
If you want to know the whole process of creating a Docker image for this project, please check the below link.
Dockerizing Next.js App With Java Backend
Here are the commands to build the image and run it on the Docker engine on your local machine.
// build the image
docker build -t java-nextjs .
// running on Image
docker run -d -p8080:8080--name javanextjs java-nextjs
// list the images you just builtdockerimages
//list containers
docker ps
Pushing Docker Image To ECR
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 is integrated with Amazon Elastic Container Service (ECS), simplifying your development to production workflow.
Amazon EKS works with any Docker registry such as Docker Hub, etc. But, in this post, we see how we can use Amazon ECR to store our Docker images. Once you set up the Amazon account and create an IAM user with Administrator access the first thing you need to create a Docker repository.
You can create your first repository either by AWS console or AWS CLI
AWS console
Creating a repository with AWS console is straightforward and all you need to give a name.
creating repositoryrepository
AWS CLI
The first thing you need to do is authenticate to your default registry. Here is the command to authenticate to your default registry. You need to make sure you are putting the correct regions and account id in the command.
You have created a Docker image on your local machine earlier. It’s time to tag that image with this repository URI in the above image.
docker tag java-nextjs:latest 032840272187.dkr.ecr.us-east-2.amazonaws.com/frontend/nextjs-java:v1
Once you tag the image and it’s time to push the Docker image into your repository.
// list the images
docker images
// push the image
docker push 032840272187.dkr.ecr.us-east-2.amazonaws.com/frontend/nextjs-java:v1
pushing Docker imageDocker image with tag v1
Create a Cluster and Worker Nodes
Getting started with AWS EKS is easy all you need to do the following steps
We need to create an AWS EKS cluster with AWS console, SDK, or AWS CLI.
Create a worker node group that registers with EKS Cluster
When your cluster is ready, you can configure kubectl to communicate with your cluster.
Deploy and manage your applications on the cluster
Cluster Creation
Let’s create a cluster by following this guide here. Make sure you created a role for the EKS to allow Amazon EKS and the Kubernetes control plane to manage AWS resources on your behalf. I created a role called eks_role
eks role
Go to the EKS console and configure the cluster as below. I used the kubernetes version 1.18 and you can check the Cluster service role.
Configure Cluster
It takes some time for the cluster to get created and it should be in the active state once it is created.
Cluster CreatingCluster is Active
Create Worker Nodes
It’s time to create nodes and before you do that we have to create this role called NodeInstanceRole. Follow this guide to create one.
The next thing we need to do is to install an aws-iam-authenticator. Follow this guide. We need this to authenticate the cluster and it uses the same user as AWS CLI is authenticated with.
Use the AWS CLI update-kubeconfig command to create or update your kubeconfig for your cluster. Here region-code is us-east-2 and cluster_name is frontend_clutser
// get the current context
kubectl config current-context
Connected to AWS EKS
Deploy Kubernetes Objects On AWS EKS Cluster
Now we have configured kubectl to use AWS EKS from our own machine. Let’s create deployment and service objects and use the image from the AWS ECR. Here is the manifest file which contains these objects.
If you cloned the above example project and you are at the root folder just use this command to create objects kubectl create -f manifest.yml
kubectl create -f manifest.yml
You can use the following commands to verify all the objects are in the desired state.
// list the deployment
kubectl get deploy
// list the pods
kubectl get po
// list the service
kubectl get svc
all objects are deployed
Summary
Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane.
You need to create an AWS Account as a prerequisite.
It’s not a best practice to use your root account to do any tasks instead you should create an IAM group that has permissions for administrator access and add a user to it and log in with that user.
You should use this command aws configure with access key and secret key.
Amazon EKS is a managed service that makes it easy for you to run Kubernetes on AWS.
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 ECS works with any Docker registry such as Docker Hub, etc.
You have to follow these steps to run apps on the Kubernetes cluster: we need to create an AWS EKS cluster with AWS console, SDK, or AWS CLI. Create a worker node group that registers with EKS Cluster, when your cluster is ready, you can configure kubectl to communicate with your cluster, Deploy and manage your applications on the cluster.
Conclusion
We have deployed a simple Next.js application with Java Backend on AWS EKS Cluster. In future posts, we will see how we can add a load balancer and how we can route requests to the Kubernetes Cluster.