Saturday, January 8, 2022

Deploy Springboot Microservices App into Amazon AKS Cluster using Jenkins Pipeline | Containerize Springboot App and Deploy into AKS Cluster using Jenkins Pipeline

We will learn how to automate Docker builds using Jenkins pipelines and Deploy into Azure Kubernetes Cluster(AKS) with help of Kubernetes Continuous Deploy plug-in. 

We will use Springboot Microservices based Java application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:


- Automating builds using Jenkins
- Automating Docker image creation
- Automating Docker image upload into Azure Container Registry
- Automating Deployments to Kubernetes Cluster


Pre-requisites:
1. AKS Cluster is setup and running. Click here to learn how to create AKS cluster.
2. Jenkins Master is up and running. 
3. Setup Jenkins slave to run Docker builds
4. Docker, Docker pipeline and Kubernetes Deploy plug-ins are installed in Jenkins



5. Docker hub account setup in https://cloud.docker.com


Step #1 - Create Credentials for Docker Hub
Go to Jenkins UI, click on Credentials -->


Click on Global credentials
Click on Add Credentials


Now Create an entry for your Docker Hub account. Make sure you enter the ID as dockerhub

Step #2 - Create Credentials for Kubernetes Cluster
Click on Add Credentials, use Kubernetes configuration from drop down.


execute the below command to get kubeconfig info, copy the entire content of the file:
sudo cat ~/.kube/config


Enter ID as K8S and choose enter directly and paste the above file content 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 red highlighted values below:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied

pipeline {
    agent {
        label "slave"
    }

    tools {
        maven 'Maven3'
    }
    environment {
        registry = "akdevopscoaching/springbootapp"
        registryCredential = "dockerhub"
        def image = ''
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://bitbucket.org/ananthkannan/myawesomeangularapprepo']]])    
            }
        }
        
    stage ('Build') {
        steps {
            sh 'mvn -f MyAwesomeApp/pom.xml clean install'           
        }
    }
        
    stage ('Docker Build') {
        steps {
            script 
            {
         // Build and push image with Jenkins' docker-plugin
            withDockerRegistry([credentialsId: "dockerhub", url: "https://index.docker.io/v1/"]) {
            image = docker.build("akdevopscoaching/springbootapp", "MyAwesomeApp")
            image.push()    
            }
            }
        }
    }
    
      stage ('K8S Deploy') {
       steps {
                kubernetesDeploy(
                    configs: 'MyAwesomeApp/springboot-docker-hub.yaml',
                    kubeconfigId: 'K8S',
                    enableConfigSubstitution: true
                    )               
        }
      }
  }
}


Step # 5 - Build the pipeline
Once you create the pipeline and changes values per your Docker user id and credentials ID, click on 


Step # 6 - Verify deployments to K8S

kubectl get pods


kubectl get deployments

kubectl get services


Steps # 7 - Access SpringBoot App in K8S cluster
Once build is successful, go to browser and enter master or worker node public ip address along with port number mentioned above
http://master_or_worker_node_public_ipaddress:port_no_from_above

You should see page like below:


Please watch the above steps in YouTube channel: 


No comments:

Post a Comment

GitHub Actions CICD Pipeline to Deploy Java WebApp into Azure App Service | Integration GitHub Actions with Azure App Service

Pre-requisites: Make sure Java web app is setup in GitHub Azure subscription to create web app What are we going to do in this lab? 1. Creat...