Deploy springboot app to Azure AKS+ACR+Helm+Azure DevOps pipeline
- Anand Nerurkar
- Sep 25, 2024
- 4 min read
Pre-requisites:
Azure CLI is installed on your local machine.
Helm Installed
kubectl installed
Azure subscription
AKS cluster
ACR
Azure DevOps project dashboard.
Dockerfile
Make sure AKS has pull access from ACR
Note:
====
What is Helm?
Helm is a package manager for Kubernetes. Helm is the K8s equivalent of yum or apt. It accomplishes the same goals as Linux system package managers like APT or YUM: managing the installation of applications and dependencies behind the scenes and hiding the complexity from the user.
Helm Charts
Helm uses a packaging format called Charts. A Helm Chart is a collection of files that describe a set of Kubernetes resources. Helm Charts helps you define, install, and upgrade even the most complex Kubernetes application. Charts are easy to create, version, share, and publish.
Create Azure Kubernetes Cluster
Execute below script Azure CLI
=========AKS-Cluster.sh==================
# This is the shell script for creating AKS cluster, ACR Repo and a namespace
#Create Resource Group
AKS_RESOURCE_GROUP=aks-rg
AKS_REGION=centralus
# Set Cluster Name
AKS_CLUSTER=aks-cluster
# set ACR name
ACR_NAME=myacrrepo531
echo $AKS_RESOURCE_GROUP, $AKS_REGION, $AKS_CLUSTER, $ACR_NAME
# Create Resource Group
az group create --location ${AKS_REGION} --name ${AKS_RESOURCE_GROUP}
# Create AKS cluster with two worker nodes
az aks create --resource-group ${AKS_RESOURCE_GROUP} --name ${AKS_CLUSTER} --node-count 2 --generate-ssh-keys
# Create Azure Container Registry
az acr create --resource-group ${AKS_RESOURCE_GROUP} \
--name ${ACR_NAME} \
--sku Standard \
--location ${AKS_REGION}
#Providing required permission for downloading Docker image from ACR into AKS Cluster
az aks update -n ${AKS_CLUSTER} -g ${AKS_RESOURCE_GROUP} --attach-acr ${ACR_NAME}
# Configure Kube Credentials
az aks get-credentials --name ${AKS_CLUSTER} --resource-group ${AKS_RESOURCE_GROUP}
# Create a namespace in AKS cluster for Helm deployment
kubectl create namespace helm-deployment
=======================================
Set up Azure DevOps pipeline
====
set up build pipeline
1. Login into your Azure DevOps dashboard
2. Click on Pipelines.
3. Click on New Pipeline
4. Click on use the classic editor
Enter your repo name and branch name where you have stored your source code along with Dockerfile:
Click on Continue. Now choose the template by typing Helm, Select Azure Kubernetes service and click Apply.
Now pipeline is created with six tasks already. We need to start customizing the pipeline:
Select Ubuntu as build agent from Agent specification drop down, avoid Windows server as build agent.
Let's also add Maven build task for building the JAR file.
Click on + icon and type Maven. this should be the first task.
And then enter maven goal as package
Let's modify Build an image task.
Select Push an image task
Leave Install Helm Task as it is, we need that task to install Helm on build agent
Remove helm init task by selecting remove selected task
Customize helm package task, select Chart Path by clicking ... dots
Choose the folder where you have helm chart files, select OK
Leave Publish artifact task as it is.
Now click Save + Queue and run to start Building the pipeline
Check build output..
Once the build is completed, you should be able to see the Docker image in Azure Portal under Resource Group, ACR repo name --> Repositories
How to Create Release pipeline for deploying Springboot Microservices containers into AKS Cluster.
Go to Pipelines --> Click on Releases --> New Release pipeline
Click on Stage 1 and choose a template by typing helm
and choose Deploy an application to K8S cluster using helm chart
Change the stage name to Deploy to AKS
Now click on Add an artifact
Select the Build pipeline and click on the latest version
Now click on Deploy to AKS stage
Click on Deploy to AKS
Enter right value for Azure subscription, Resource group and AKS Cluster by selecting from down down.
Now click on the Agent Job, and select Azure pipelines and choose Ubutu as Build agent, avoid windows agents.Leave install Helm 2.9.1 task
make sure check for latest version of Helm. this will install latest version of Helm which is 3.x
Remove helm init task by selecting remove selected task
Let's start customizing helm upgrade task. Enter helm-deployment as namespace, chart type as File path and click on three dots.
choose the package mychart-0.1.0.tgz and click ok.
Enter first as release name
enter below values for for set values:
image.tag=$(Build.BuildId)
Now click on Save.
Optional step - Enable Continuous Deploy Trigger
This will deploy microservices into AKS cluster for every new build in build pipeline.
Click on Create a release
and then click Create
Click on Release number to see the output
Click on Stage to see the logs
Click on Logs, you will see the following tasks are in green to confirm Deployment was successful.
Let's check if deployment created any pods in helm-deployment namespace.
How to access Springboot Application using port forward locally?
kubectl get deployments -n helm-deployment
kubectl get pods -n helm-deployment
Get the pod name and use port forward to access locally
kubectl port-forward first-springboot-pod_name 8080 -n helm-deployment
If you see any errors after deploying the pods, you can check the pod logs.
kubectl describe pod <pod_name> -n helm-deployment
Go to the browser enter http://localhost:8080
You should see below web page.
Comments