Deploying Spring Boot Application On Azure Kubernetes Service(AKS)

Hello everyone, Hope you are doing well. This post will teach you how to deploy the spring boot application on Azure Kubernetes Service(AKS) using Azure Portal.



A little bit of Background

Azure Kubernetes Service

Azure Kubernetes Service is a managed container orchestration service based on the open-source Kubernetes system. Azure Kubernetes Service is used to handle critical functionality such as deploying, scaling and managing Docker containers and container-based applications.
The best thing about AKS is that you don’t require deep knowledge and expertise in container orchestration to manage AKS.

Azure Container Registry

Azure Container Registry allows you to build, store, and manage container images and artifacts in a private registry for all types of container deployments. Use Azure container registries with your existing container development and deployment pipelines. Use Azure Container Registry Tasks to build container images in Azure on-demand, or automate builds triggered by source code updates, updates to a container's base image, or timers.

GitHub Actions

GitHub Actions helps you automate your software development workflows from within GitHub. You can deploy workflows in the same place where you store code and collaborate on pull requests and issues. 
In GitHub Actions, a workflow is an automated process that you set up in your GitHub repository. You can build, test, package, release, or deploy any project on GitHub with a workflow. 

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". 



Step 1: Sign in to Azure Portal and create a resource group

Sign in to Azure portal https://portal.azure.com/#home and find "Resource groups" like below.



Then, create a resource group like the one below.


Here the resource group name is "Knowledgefactory". Create a resource group by clicking the "Review + create " button.

You will be taken to a page like the below image,
Then click on the "Create" button.



Step 2: Create a container registry using Azure Portal

Sign in to the Azure portal at https://portal.azure.com
Search for "Container registries" like the below image,

Create a container registry, 
Select the "Resource group", enter "Registry name" etc...
Then click on the "Review + create" button.

You will be taken to a page like the below image,
Then click on the "Create" button.

Now, You can see "Deployment is in progress" like the below image. 

Once deployment is completed you can see the "Your deployment is complete" page like the below image.

Next, click on "Access keys"
Copy the "Login server" value, "Username" and "Password" and keep them safe for future purposes.



Step 3: Creating a simple spring boot web application.

First, open the Spring initializr https://start.spring.io/

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-helloworld. Here I selected the Maven project - language Java - Spring Boot 2.7.1 and add Spring web dependency.

Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-boot-helloworld.zip) file and downloads the project. 
Then, Extract the Zip file.



Step 4: Import the project on your favourite IDE, I am using Visual Studio Code

Final Project Structure



Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.dem</groupId>
<artifactId>spring-boot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-helloworld</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Create a Test Controller

@RestController
public class GreetingController {

@GetMapping("/hello")
public String hello() {
return "Hello World! Your application is running";
}
}


Spring Boot Main Driver

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


Dockerfile

#
# Build stage
#

FROM maven:3.6.3-jdk-11-slim AS build

WORKDIR usr/src/app

COPY . ./

RUN mvn clean package

#
# Package stage
#

FROM openjdk:11-jre-slim

ARG JAR_NAME="spring-boot-helloworld"

WORKDIR /usr/src/app

COPY --from=build /usr/src/app/target/${JAR_NAME}.jar ./app.jar

CMD ["java","-jar", "./app.jar"]


main.yml(Github workflows)

Configure Azure Container Registry endpoint, username and password which we have in our hand. Build a Docker image and push it to Azure Container Registry.
name: Build a Docker image and Push it to ACR

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: azure/docker-login@v1
with:
login-server: ${{ secrets.ACR_ENDPOINT }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- run: |
docker build . -t ${{ secrets.ACR_ENDPOINT }}/knowledgefactory:${{ github.sha }}
docker push ${{ secrets.ACR_ENDPOINT }}/knowledgefactory:${{ github.sha }}




Step 5: Create a New Repo and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository "spring-boot-helloworld".

Then, upload the source code from your local machine to the newly created Github repo.

Next, click on the "Actions" button,
Select the workflow "Build a Docker image and Push it to ACR"

Then, click on the "Run workflow" button,

You will see an image like below, "Build a Docker image and Push it to ACR" process in progress.
After successful execution of the job, you will see an image like the below,



Step 6: Check whether the image is pushed or not in Azure Container Registry

Go to ACR and click on the "Repositories"
Then click on the repository "knowledgefactory".

You will see the image tag id which has been pushed.



Step 7: Create a Kubernetes cluster on AKS

Search for "Kubernetes services" like the below image,

Create Kubernetes cluster,
Select/Enter Information like the above image. 

Then click on "Integrations".  Select the Container registry name like the below image.
Then click on the "Review + create" button.

You will be taken to a page like the below image,
Then click on the "Create" button.

Now, You can see "Deployment is in progress" like the below image. 

Once deployment is completed you can see the "Your deployment is complete" page like the below image.



Step 8: Deploy the application to the AKS cluster

Next, click on "Namespaces" and click on "Create with YAML"

Paste the following YAML,
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-helloworld-aks
spec:
replicas: 1
selector:
matchLabels:
app: springboot-helloworld-aks
template:
metadata:
labels:
app: springboot-helloworld-aks
spec:
containers:
- name: springboot-helloworld-aks
image: knowledgefactorypvtregistry.azurecr.io/knowledgefactory:tag
---
apiVersion: v1
kind: Service
metadata:
name: springboot-helloworld-aks
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: springboot-helloworld-aks
Then, click on the "Add" button.

Next, click on "Services and ingresses"

Copy the "External IP" 

Verify the API using Postman,

More related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete