Thursday, June 15, 2023

How to Deploy Springboot Microservices App into AKS cluster using Jenkins Pipeline and Helm | Deploy Microservices into AKS cluster using Helm Charts and Jenkins Pipeline

We are going to learn how to Automate build and deploy of Springboot Microservices App into Azure Kubernetes Cluster(AKS) using Helm and Jenkins pipeline.

Sample springboot App Code:

I have created a sample Springboot App setup in GitHub. Click here to access code base in GitHub. 

Jenkins pipeline will:

- Automate maven build(jar) using Jenkins
- Automate Docker image creation
- Automate Docker image upload into Azure container registry
- Automate Springboot docker container Deployments to Azure Kubernetes Cluster using Helm charts

2.  Install Docker in Jenkins and Jenkins have proper permission to perform Docker builds
3. Install Azure CLI on your Jenkins machine. (We will be creating AKS cluster from Jenkins machine running in Azure Cloud)
4. Helm installed on Jenkins instance
5. Install Kubectl on Jenkins instance
6. AKS cluster needs to be up running. You can create AKS cluster, ACR Repo using shell script provided in my website.
7. Make sure to Install Docker, Docker pipeline 

8. ACR is also setup in Azure cloud. 
10. Dockerfile created along with the application source code for springboot App.

The Code for this video is here:

Implementation steps:

  1. Create a resource group, AKS cluster and Azure container registry 
  2. Provide pull access for AKS to pull image from ACR 
  3. Create a namespace for helm deployment
  4. Create a helm chart for spring boot app
  5. Create a Jenkins pipeline with below stages:
    • checkout
    • Build Jar
    • Build docker image
    • Upload image to ACR
    • Deploy to AKS using helm
  6. Run the pipeline to deploy springboot app into AKS
  7. Verify deployments in the namespace in AKS
  8. Access the app in the browser
Create AKS cluster from Jenkins Virtual machine
Login to Jenkins virtual machine. switch as jenkins user
sudo su - jenkins

Authenticate to Azure Cloud by typing:
az login

Now to the browser, type
enter the code as received from previous step:

Enter your microsoft credentials, click continue per below screen:

You will be shown below screen:

Create a shell script based on the script provided here

copy and paste entire script provided in this link.

Execute shell script to create the following in Azure clid:
  • Resource group
  • AKS cluster
  • ACR repo
  • provide pull access to AKS for pulling docker image from ACR
  • namespace in AKS cluster for deploying our springboot app
This will take 2 to 5 mins to create the resources in Azure cloud.

Make sure cluster is up and running with worker nodes
kubectl get nodes

Create Helm chart using helm command
Go to your root of repo where you have source code for your springboot application. Create helm chart by executing below command:

helm create mychart
tree mychart
Execute the above command to see the files created.

Add Docker image details to download from ECR before deploying to EKS cluster
open mychart/values.yaml. change per below values:

tag: ""

Enter service type as LoadBalancer
And also
open mychart/templates/deployment.yaml and change containerPort to 8080

Save the files, commit and push into repo.
Step # 1 - Create Maven3 variable under Global tool configuration in Jenkins
Make sure you create Maven3 variable under Global tool configuration. 

Step #2 - Create Credentials to connect to ACR from Jenkins

Go to Azure Portal console, go to container registry
Settings--> Access keys
Get the username and password 
Go to Jenkins-> Manage Jenkins. Create credentials.

Enter ID as ACR and enter some text for description and Save.

Step # 3 - Create a pipeline in Jenkins
Create a new pipeline job.

Step # 4 - Copy the pipeline code from below
Make sure you change values as per your settings highlighted in yellow below:

pipeline {
  tools {
        maven 'Maven3'
    agent any
        environment {
        //once you create ACR in Azure cloud, use that here
        registryName = "myacrrepo531"
        //- update your credentials ID after creating credentials for connecting to ACR
        registryCredential = 'ACR'
        dockerImage = ''
        registryUrl = ''
    stages {
        stage('checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'check_out_from_your_repo_after_forking_my_repo']]])
        stage ('Build Jar') {
        steps {
            sh 'mvn clean install'           
    stage ('Build Docker image') {
        steps {
                script {
                    dockerImage = registryName
    // Uploading Docker images into ACR
        stage('Upload Image to ACR') {
             script {
                docker.withRegistry( "http://${registryUrl}", registryCredential ) {
        stage ('Helm Deploy') {
          steps {
            script {
                sh "helm upgrade first --install mychart --namespace helm-deployment --set image.tag=$BUILD_NUMBER"

Step # 5 - Build the pipeline

Step # 6 - Verify deployments in AKS
Execute the below command to list the helm deployments:
helm ls -n helm-deployment

kubectl get pods -n helm-deployment

kubectl get services -n helm-deployment

Steps # 7 - Access Springboot App Deployed in AKS cluster
Once deployment is successful, go to browser and enter above load balancer URL mentioned above

You should see page like below:

Clean up the Cluster:

To avoid charges from Azure, you should clean up unneeded resources. When the cluster is no longer needed, use the az group delete command to remove the resource group, container service, and all related resources. 

az group delete --name resource-group-name --yes --no-wait

Watch steps in YouTube Channel:

No comments:

Post a Comment