Deploying a Java Spring Boot Application in Azure Kubernetes Service(AKS)

Sai Anil Kumar Deyyala
3 min readMay 9, 2023

--

Introduction:

Deploying Java Spring Boot applications on Azure Kubernetes Service (AKS) can be a challenging task, especially for those who are new to Kubernetes. In this article, we will show you how to deploy a Java Spring Boot application on AKS using a sample application that is available on GitHub. We will walk you through the entire process of building the application using Maven, creating a Dockerfile, and creating Kubernetes manifest files. By the end of this article, you will have a better understanding of how to deploy Java Spring Boot applications on AKS and be able to use this knowledge to deploy your own applications.

Steps For Deploying The Java Spring Boot Application To AKS:

  1. Building the Java Spring Boot application using Maven
  2. Creating a Dockerfile to containerize the application
  3. Pushing the Docker image to a Docker registry
  4. Creating Kubernetes manifest files for deploying the application to AKS
  5. Deploying the application to AKS and testing it.

Prerequisites:

1. Maven

2. Java Development Kit (JDK)

3. Docker Desktop or Docker Engine

4. Docker Hub account (or other Docker registry account)

5. Azure CLI

6. Azure Kubernetes Service (AKS) cluster

Procedure:

Step 1: Build the Java Spring Boot application using Maven

The first step is to download the Spring PetClinic application from GitHub using the following link: application github repository and then extract it.
Then, we will use Maven to build the application by running the following command in the application directory:

cd spring-petclinic-main
mvn clean package

This will create a JAR file of the application that we will use to create a Docker image.

Step 2: Create a Docker Image of the application

The next step is to create a Dockerfile that will define the Docker image for our application. Here’s an example Dockerfile:

FROM openjdk:latest

# Set the working directory to /app
WORKDIR /app

# Copy the JAR file to the container
COPY target/spring-petclinic-3.0.0-SNAPSHOT.jar app.jar

# Expose port 8080 for the container
EXPOSE 8080

# Set the command to run the JAR file
CMD ["java", "-jar", "app.jar"]

Step 3: Push the Docker image to a Docker Registry

And then we will create a docker image by building the Docker file.

docker build -t ImageName .

Next, we need to push the Docker image to a Docker registry. For this example, we will use Docker Hub. First, create a Docker Hub account if you haven’t already. Then, log in to Docker Hub from the command line using the following command:

docker login

After logging in, tag the Docker image with your Docker Hub username and the name of your repository using the following command:

docker tag <image_id> <docker_hub_username>/<repository_name>:<tag>

Finally, push the Docker image to Docker Hub using the following command:

docker push <docker_hub_username>/<repository_name>:<tag>

Step 4: Create Kubernetes manifest files for deploying the application to AKS

Now that we have a Docker image of our application, we can create Kubernetes manifest files that will define the deployment and service for our application on AKS. Here’s an example deployment manifest file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: pet-clinic
spec:
replicas: 1
selector:
matchLabels:
app: pet-clinic
template:
metadata:
labels:
app: pet-clinic
spec:
containers:
- name: pet-clinic
image: <docker_hub_username>/<repository_name>
ports:
- containerPort: 8080

---
apiVersion: v1
kind: Service
metadata:
name: pet-clinic
spec:
selector:
app: pet-clinic
ports:
- name: http
port: 80
targetPort: 8080
type: LoadBalancer

Step 5: Deploying The Application To AKS

Assuming that you have installed Azure CLI and created an AKS cluster.

Log in to the Azure by running the following command:

az login

After you have logged in, set the AKS context by running the following command:

az aks get-credentials --resource-group <resource-group> --name <aks-name>

Replace <resource-group> with the name of the resource group where your AKS cluster is located, and <aks-name> with the name of your AKS cluster.

Verify that you have successfully configured kubectl to use the AKS cluster by running the following command:

kubectl get nodes

Apply the Kubernetes manifest files by running the following command: (Here path to manifest file refers to the above manifest file we created)

kubectl apply -f <path-to-manifest-file>

Congratulations! You have successfully deployed a Java Spring Boot application to Azure Kubernetes Service using Docker and Kubernetes manifest files.

To access the application use the LoadBalancer ip of the service that we created.

--

--