Build and deploy to Azure kubernetes service(AKS): A Comprehensive Guide

INTRODUCTION

In today’s dynamic and cloud-centric world, containerization and orchestration technologies like Docker and Kubernetes have become integral parts of modern application development and deployment. In this blog post, we’ll explore the process of building a spring boot application and deploying it to Azure Kubernetes Service (AKS), Microsoft’s managed Kubernetes offering. using GitHub registry

Prerequisites

Before we delve into the deployment process, make sure you have the following prerequisites:

  • Azure Account: You need an active Azure account. If you don’t have one, you can sign up for a free account on the Azure Portal.
  • Azure CLI: Install the Azure Command-Line Interface (CLI) on your local machine. You can find installation instructions here

STEP 1: Develop and containerize your application.

Create a spring boot application add functionality to it based on your requirement once it is done. The next step is to containerize your application using Docker. Ensure that your application runs inside a Docker container. Create a Docker file to define the container environment and dependencies.

Step 2: Create an Azure Kubernetes Service (AKS) Cluster

Use the Azure CLI to create an AKS cluster. Specify the resource group, cluster name, and other configurations. Adjust the --node-count parameter based on your application’s requirements.

az aks create --resource-group <ResourceGroupName> --name <ClusterName> --node-count 3 --enable-addons monitoring --generate-ssh-keys

Step 3: Create a service principal.

Open the azure cli and run the command given below to generate the service principal as it will be need later as we are using GitHub registry here when we run the below command it will give us a Json reason in which we will get the details which we need to add in out GitHub registry, so it gets connected with it.

az ad sp create-for-rbac --name <service_principal_name> --role contributor --scopes /subscriptions/<subscription_id>/resourceGroups/<resource_group_name> --sdk-auth

Step 4: Deploy Your Application to AKS

Create Kubernetes deployment and service YAML files for your application. Define the number of replicas, ports, and other settings. here we will two files for defining all details deployment.yml and service.yml

Deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-app
spec:
replicas: 2
selector:
matchLabels:
app: springboot-app
template:
metadata:
labels:
app: springboot-app
spec:
containers:
- name: springboot-app
image: ghcr.io/dev-swapnildixit4161/my-app:${{ github.sha }}
ports:
- containerPort: 8080
Service.yml
apiVersion: v1
kind: Service
metadata:
name: springboot-app-service
spec:
type: LoadBalancer
selector:
app: springboot-app
ports:
- protocol: TCP
port: 80
targetPort: 8080

Step:5 Create a GitHub Actions workflow file:

  • In your repository, navigate to the .github/workflows/ directory (create it if it doesn’t exist) and create a new YAML file, e.g., deploy.yml.
  • Add the following content to the file:
name: Build and Deploy to AKS

on:
push:
branches:
- main
workflow_dispatch:

env:
IMAGE_NAME: ghcr.io/{github-username}/myapp/myimage
AKS_CLUSTER: demo
AKS_NAMESPACE: default

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install Docker
run: |
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

- name: Log in to GitHub Artifact Registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login "ghcr.io/{github-username}/my-app" -u {github-username} --password-stdin

- name: Build and push Docker image
run: |
docker build -t "${{ env.IMAGE_NAME }}:${{ github.sha }}" .
docker push "${{ env.IMAGE_NAME }}:${{ github.sha }}"

- name: Configure Kubernetes environment
uses: azure/aks-set-context@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
cluster-name: demo
resource-group: azure-demo

- name: List Manifests Directory
run: ls manifests

- name: Apply Kubernetes manifest
run: |
sed -i "s|image:.*|image: ${{ env.IMAGE_NAME }}:${{ github.sha }}|g" manifests/deployment.yml
kubectl apply -f manifests/deployment.yml --namespace ${{ env.AKS_NAMESPACE }}
kubectl apply -f manifests/service.yml --namespace ${{ env.AKS_NAMESPACE }}

The provided GitHub Actions workflow is designed to automate the build and deployment of a Dockerized application to an Azure Kubernetes Service (AKS) cluster.

Conclusion

By following these steps, you’ve established a robust CI/CD pipeline for your Spring Boot application, leveraging GitHub Container Registry and Azure Kubernetes Service. This automated workflow ensures that your application is built, tested, and deployed consistently, allowing you to focus on delivering value to your users.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top