Deploy a Spring Boot application on Azure Kubernetes Service (AKS) using Azure DevOps CI/CD pipelines

To deploy a Spring Boot application on Azure Kubernetes Service (AKS) using Azure DevOps CI/CD pipelines, follow these steps:

Here is the general architecture diagram that we will be deploying.

Step 1: Create and Configure Azure Kubernetes Service (AKS)

1.1. Create AKS Cluster

  • Go to the Azure Portal, and under Kubernetes Services, create a new AKS cluster.
  • Select the appropriate region, resource group, and other configuration details for your cluster.
  • Ensure you have sufficient permissions to manage the cluster.

1.2. Get AKS Credentials

Once the AKS cluster is created, configure your local kubectl to access the cluster:

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

This will download the kubeconfig file and set up access to your AKS cluster.

Step 2: Create Azure Container Registry (ACR)

2.1. Create an Azure Container Registry

  1. In the Azure Portal, search for Container Registry and create a new registry under the Azure Container Registry service.
  2. Choose a unique registry name, the appropriate resource group, and the region that matches your AKS cluster.
  3. Review and create the registry.

2.2. Authenticate ACR

To push Docker images to ACR, you need to authenticate your Docker client with Azure Container Registry. Run the following command in your terminal:

az acr login --name <your_acr_name>

This will authenticate Docker to your ACR instance.

2.3. Get the ACR Login Server URL

The ACR login server URL is in the following format: <your_acr_name>.azurecr.io. You’ll need this URL to push your Docker image.

Step 3: Create Dockerfile for the Spring Boot Application

  1. In your Spring Boot project, create a Dockerfile at the root level:

    # Use the official OpenJDK base image
    FROM openjdk:17-jdk-alpine
    
    # Set the working directory inside the container
    WORKDIR /app
    
    # Copy the jar file from target folder into the container
    COPY target/*.jar app.jar
    
    # Expose the port the application will run on
    EXPOSE 8080
    
    # Command to run the app
    ENTRYPOINT ["java", "-jar", "app.jar"]
  2. The Dockerfile above uses OpenJDK 17 and assumes the application JAR file will be available in the target/ directory after the Spring Boot project is built.

Step 4: Set Up Azure DevOps CI/CD Pipeline

4.1. Create the Azure DevOps Project

  1. Navigate to Azure DevOps and create a new project if you don't have one.
  2. Create a new repository and push your Spring Boot application code there.

4.2. Set Up the CI Pipeline (Continuous Integration)

  1. In Azure DevOps, go to Pipelines > New Pipeline.
  2. Select Azure Repos Git or GitHub (depending on where your code is stored).
  3. Choose YAML pipeline configuration.
  4. Create a new azure-pipelines.yml file in the root of your repository (or use the UI to set it up).

Here’s an example azure-pipelines.yml file that builds your Spring Boot application Docker image and pushes it to Azure Container Registry:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

variables:
  imageName: 'spring-boot-app'
  acrName: '<your_acr_name>'
  registry: '$(acrName).azurecr.io'
  dockerfilePath: 'Dockerfile'

steps:
# Step 1: Checkout the code
- task: Checkout@1
  displayName: 'Checkout Code'

# Step 2: Build the Spring Boot JAR file
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'clean package'
    options: '-DskipTests=true'

# Step 3: Build the Docker Image
- task: Docker@2
  inputs:
    command: 'buildAndPush'
    containerRegistry: '<your_acr_service_connection>'
    repository: '$(registry)/$(imageName)'
    dockerfile: '$(Build.SourcesDirectory)/$(dockerfilePath)'
    tags: 'latest'

# Step 4: Deploy the Docker Image to AKS (using kubectl)
- task: AzureCLI@2
  inputs:
    azureSubscription: '<your_azure_service_connection>'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      kubectl apply -f k8s/deployment.yaml

This pipeline performs the following:

  • Step 1: Checkout your code from the repository.
  • Step 2: Build the Spring Boot application JAR using Maven.
  • Step 3: Build and push the Docker image to Azure Container Registry (ACR).
  • Step 4: Use kubectl to deploy the image to AKS by applying the Kubernetes deployment YAML file.

4.3. Set Up Azure DevOps Service Connections

To allow Azure DevOps to access Azure resources, you’ll need to create two service connections:

  1. ACR Service Connection: This allows Azure DevOps to interact with your Azure Container Registry.

    • Go to Project Settings > Service connections.
    • Create a new Docker Registry service connection, select Azure Container Registry, and authenticate it.
  2. Azure Service Connection: This enables Azure DevOps to interact with Azure resources like AKS.

    • Go to Project Settings > Service connections.
    • Create a new Azure Resource Manager service connection, authenticate it with your Azure subscription, and grant access to the AKS resource group.

Step 5: Create Kubernetes Deployment YAML

In the k8s/ directory of your project, create a deployment.yaml file to describe the Kubernetes deployment for your Spring Boot app:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-boot-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: spring-boot-app
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
        - name: spring-boot-app
          image: '<your_acr_name>.azurecr.io/spring-boot-app:latest'
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: spring-boot-service
spec:
  selector:
    app: spring-boot-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer

This deployment configuration does the following:

  • Creates a Deployment to manage the Spring Boot app with two replicas.
  • Defines a Service to expose the app and load-balance traffic to the pods.

Step 6: Trigger the CI/CD Pipeline

  1. Commit and push your changes to the main branch (or your designated branch).
  2. The pipeline will automatically trigger on each push, building the Docker image, pushing it to Azure Container Registry (ACR), and deploying it to AKS.

Step 7: Access the Application

Once the application is deployed, retrieve the external IP address for the service using the following command:

kubectl get svc spring-boot-service

This will show the external IP address, which you can use to access your Spring Boot application in the browser.

🌟 Master Microsoft Azure with Microsoft Azure in Action! ðŸŒŸ

Dive into the world of cloud computing and supercharge your skills! This practical guide is packed with step-by-step tutorials, real-world use cases, and the latest Azure features to help you build, deploy, and manage scalable cloud apps like a pro. 🚀

🔥 Exclusive Deal Alert! Unlock amazing savings of 34% today! 🎉 Don’t miss this chance to learn Azure while saving big.

👇 Click now to claim your discount and start your Azure journey! ðŸ‘‡
👉 Grab Your 34% Discount Now!

Hurry—this offer won't last forever! ⏳

Comments

Popular posts from this blog

Spring Boot OpenAI Integration: Step-by-Step Guide

Orchestration-Based Saga Architecture and Spring Boot Microservices Implementation Guide

Spring Boot 3 + Angular 15 + Material - Full Stack CRUD Application Example