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.
More Info - https://docs.microsoft.com/en-us/azure/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".
More Info - https://spring.io/projects/spring-boot
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.
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,
Select the "Resource group", enter "Registry name" etc...
Then click on the "Review + create" button.
Then click on the "Review + create" button.
You will be taken to a page like the below image,
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 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
@RestControllerpublic class GreetingController {
@GetMapping("/hello") public String hello() { return "Hello World! Your application is running"; }}
Spring Boot Main Driver
@SpringBootApplicationpublic 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.
Then, click on the "Run workflow" button,
Step 7: Create a Kubernetes cluster on AKS
Search for "Kubernetes services" like the below image,
Select/Enter Information like the above image.
Then click on "Integrations". Select the Container registry name like the below image.
You will be taken to a page like the below image,
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"apiVersion: apps/v1kind: Deploymentmetadata: name: springboot-helloworld-aksspec: 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: v1kind: Servicemetadata: name: springboot-helloworld-aksspec: type: LoadBalancer ports: - port: 80 targetPort: 8080 selector: app: springboot-helloworld-aks
Then, click on the "Add" button.
Next, click on "Services and ingresses"
Verify the API using Postman,