DevOps and Developer | Java Spring Boot Application Deployment on AWS EC2, Docker , Kubernetes | Sagar Kakkala's World


The video above here is demonstrated in Telugu,

click here - to watch on Youtube

#Note: It is recommended to watch blog and video on laptop for better visibility

#Note: All the coloured texts are links that can help you navigate to particular topic

About Project

This Project is a Java Spring Boot Project with local database used, this database is connected directly with the application,

Project is more like a login form where the entries of "Name" and "Email" are required,

since project does not have UI, we will send our inputs through POSTMAN, and get our required results Outputs in the application

You can access POSTMAN through web console

As a DevOps,We will deploy this Project in AWS EC2, Docker and Kubernetes

For example, once application is deployed, you can access it with AWS EC2 IP and Port that you have opened application for.

Our Application is called at http://{AWS_PUBLIC_IP}:{PORT_OPNED}/users

Using POSTMAN, let us give the inputs, select POST, Give the URL ,Body, raw and we will give the inputs in json format

{
    "name": "sagar",
    "email": "sagar.kakkala@outlook.com"
}


In case, if you give POST again, it will update as new input like Id2, if you want to modify existing data instead of POST, use PUT method

Also, instead of Monitoring from Application, you can use GET Method in POSTMAN to see what data is there at present in URL




once, we give inputs, our Outputs on application would be something like this


You can also access database using h2-console

http://{AWS_PUBLIC_IP}:{PORT_OPNED}/h2-console


you need to give inputs that are mentioned in the code and you can find them at this path

https://github.com/Hari0o/Crud-Demo-Project/blob/main/src/main/resources/application.properties


you can change username and password from code, if you wish to 

As of now, the inputs are as follows

JDBC URL: jdbc:h2:mem:testdb

Username: user

Password: password

The entries that we give through POSTMAN,gets updated at this database




You can say hello to our Developer at Charmila LinkedIn

Description from Our Developer for Project goes as follows:

When developing a CRUD operations API in Spring Boot using an H2 database, the process begins with using Spring Initializr to generate a Maven project. This provides a foundational structure, including dependencies for Spring Web, Spring Data JPA, and H2 Database. The CRUD operations (Create, Read, Update, Delete) are implemented using Spring Data JPA repositories, which handle data access and manipulation with minimal boilerplate code. The H2 database, an in-memory database, is ideal for development and testing due to its simplicity and easy integration with Spring Boot. Controllers expose RESTful endpoints, enabling interaction with the entities. By leveraging the Spring Boot framework, developers can focus on business logic rather than boilerplate, leading to a cleaner and more maintainable codebase. Additionally, the use of the H2 console provides a user-friendly interface to interact with the database during development.

Understanding React and Java Project



Since as we had seen in React Project, Process goes like Install NodeJs and npm, run command "npm run build" based on "package.json" file, it creates "Build" folder and it has "index.html" file which we deploy

In the same way, for Java Project , Process goes like Install Java and Maven, run command "mvn clean install"based on "pom.xml" file, it creates "target" folder and it has ".jar" file which we deploy

DevOps Part of Deployment

Now let us deploy in AWS EC2

Deployment in AWS EC2:

Since, we want to run Kubernetes and Docker as well in the same instance, take t3.large and configure storage to 30GB to avoid node memory issues




Build of Java Project


Now once instance is up, since Ubuntu already is installed with Git, let us clone our project

git clone https://github.com/Hari0o/Crud-Demo-Project.git


Now since it is a Java Project, let us install java and maven, you can run the below bash script which even configures environment variables that are required

sudo vi java_and_maven.sh

Copy the contents of the file below

#!/bin/bash

# Update package repository
echo "Updating package repository..."
sudo apt-get update -y

# Install OpenJDK 17
echo "Installing OpenJDK 17..."
sudo apt-get install openjdk-17-jdk -y

# Verify Java installation
echo "Verifying Java installation..."
java -version

# Set JAVA_HOME environment variable
JAVA_HOME=$(update-alternatives --config java | grep 'java-17-openjdk' | awk '{print $3}' | sed 's:/bin/java::')
echo "Setting JAVA_HOME to $JAVA_HOME..."

# Add JAVA_HOME to ~/.bashrc for persistence
echo "export JAVA_HOME=$JAVA_HOME" >> ~/.bashrc
echo "export PATH=\$JAVA_HOME/bin:\$PATH" >> ~/.bashrc

# Apply changes to current session
export JAVA_HOME=$JAVA_HOME
export PATH=$JAVA_HOME/bin:$PATH

# Verify JAVA_HOME
echo "Verifying JAVA_HOME..."
echo $JAVA_HOME

# Install Maven manually
echo "Downloading and installing Maven..."
wget https://downloads.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz

# Extract Maven
tar -xvzf apache-maven-3.9.4-bin.tar.gz

# Move Maven to /opt directory
sudo mv apache-maven-3.9.4 /opt/maven

# Set M2_HOME and update PATH for Maven
echo "Setting up Maven environment variables..."
echo "export M2_HOME=/opt/maven" >> ~/.bashrc
echo "export PATH=\$M2_HOME/bin:\$PATH" >> ~/.bashrc

# Apply Maven changes to current session
export M2_HOME=/opt/maven
export PATH=$M2_HOME/bin:$PATH

# Verify Maven installation
echo "Verifying Maven installation..."
mvn -version

echo "Installation of OpenJDK 17 and Maven completed successfully!"

Give the execute permissions

sudo chmod +x java_and_maven.sh

Run the command
sudo ./java_and_maven.sh

The above script sometimes does not install maven, so i suggest check maven version once script is run

mvn --version

In case, you see maven is not installed, i suggest run the command

sudo apt install maven

Now, we are ready to Build the file, let us go inside the repository that we cloned, point to be noted that our application is running on Port 8080

cd Crud-Demo-Project/



From the above screenshot , you will be able to see that there is no target folder, means code is not packaged, let us do our build before we deploy

mvn clean install



once build is done, you will be able to see target folder formed, now let us get into target folder

cd target



You will be able to find .jar file which is the main file for us to Deploy in AWS EC2, Docker and Kubernetes

Deployment of Java Project

Deployment in AWS EC2,

To see, the project deployed in AWS EC2, navigate to path where you have .jar file and just run

java -jar crudDemo-0.0.1-SNAPSHOT.jar



Now to access, our Application which is running on port 8080, make sure you have allowed port in Inbound rules, since it is a demo purpose, i have allowed all traffic from all ports in inbound rules



Now you can access our application from AWS Public IP: 8080

You should see something like this


Now access POSTMAN through web console, and you need to give the same URL that you accessed
in GET method, these methods are explained clearly in About Project session

enter the data in JSON format as shown in screenshot

{
    "name": "sagar",
    "email": "sagar.kakkala@outlook.com"
}



Now , if you refresh, the application, it looks like this


This shows we successfully deployed it in AWS EC2

Deployment in Docker

Install Docker in same AWS EC2, follow the Blog to understand docker deployment in detail, get the docker installation commands from Blog - Docker Blog

Once docker is installed, there is a DockerFile, in the repo, navigate to path inside server where Dockerfile is placed and run following command

sudo docker build -t crud-demo .

once command is run check docker images

sudo docker images

To run in docker, use the command

sudo docker run --name crud-docker-container -p 8080:8080 -d crud-demo

Since, the process is same from here like we did in AWS Deployment, to check the application, and to give inputs in POSTMAN

you will be able to access your application from http://{AWS_PUBLIC_IP}:8080/users

Deployment in Kubernetes

Push our newly created image to docker hub, and also install Kubernetes by following initial steps from Kubernetes Blog

once you have done the installation, use the alias command

alias kubectl="microk8s kubectl"

And now create namespace , crud-demo

kubectl create ns crud-demo

let us create deployment.yaml file

vi deployment.yaml

copy the contents below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: crud-deployment
  namespace: crud-demo
  labels:
    app: crud-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: crud-demo
  template:
    metadata:
      labels:
        app: crud-demo
    spec:
      containers:
      - name: crud-demo
        image: sagarkakkala385/crud-demo ##change image name to that of your docker hub
        ports:
        - containerPort: 8080


and now 

kubectl apply -f deployment.yaml

and now service file,we will be using nodePort here

vi service.yaml

copy the contents as below

apiVersion: v1
kind: Service
metadata:
  name: crud-service
  namespace: crud-demo
spec:
  type: NodePort
  selector:
    app: crud-demo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30000
i have used nodePort as 30000, you can use any value between 30000 to 32676, you will be able to access your application with nodePort now

http://{AWS_PUBLIC_IP}:30000/users

Hence this concludes our blog here

This Concludes Our Blog here

##I Post most of my content in Telugu related to contrafactums(changing lyrics to original songs),fun vlogs, Travel stories and much more to explore, You can use this link as single point of link to access - Sagar Kakkala One Stop

🖊feedback,queries and suggestions about blog are welcome in the comments.

Comments