Friday, May 19, 2023

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

We are going to learn how to automate build and deployment of Springboot Microservices Docker Container into Elastic Kubernetes Cluster(EKS) using Helm and Jenkins pipeline.

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.

To learn more about Helm, please click here.

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 Elastic container registry(ECR)
- Automate Springboot docker container deployments into Elastic Kubernetes Cluster using Helm charts



Pre-requisites:
1. EKS cluster needs to be up running. Click here to learn how to create Amazon EKS cluster.
2. Jenkins instance is up and running
3. Install AWS CLI on Jenkins instance
4. Helm installed on Jenkins instance
5. Install Kubectl on Jenkins instance
6. Install eksctl on Jenkins instance
7. Install Docker in Jenkins and Jenkins have proper permission to perform Docker builds
8. Make sure to Install Docker, Docker pipeline 


10. Dockerfile created along with the application source code for springboot App.
11. Namespace created in EKS cluster

The Code for this video is here:

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.


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 for connecting to EKS cluster using Kubeconfig
Go to Jenkins UI, click on Credentials -->


Click on Global credentials
Click on Add Credentials

use secret file from drop down.

you should see the nodes running in EKS cluster.

kubectl get nodes

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




Open your text editor or notepad, copy and paste the entire content and save in a file.
We will upload this file.

Enter ID as K8S and choose File and upload the file and save.


Step # 3 - Create a namespace in EKS
kubectl create ns helm-deployment

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

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

pipeline {
   tools {
        maven 'Maven3'
    }
    agent any
    environment {
        registry = "account_id.dkr.ecr.us-east-1.amazonaws.com/my-docker-repo"
    }
   
    stages {
        stage('Cloning Git') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: 'https://github.com/akannan1087/docker-spring-boot']]])     
            }
        }
      stage ('Build') {
          steps {
            sh 'mvn clean install'           
            }
      }
    // Building Docker images
    stage('Building image') {
      steps{
        script {
          dockerImage = docker.build registry 
          dockerImage.tag("$BUILD_NUMBER")
        }
      }
    }
   
    // Uploading Docker images into AWS ECR
    stage('Pushing to ECR') {
     steps{  
         script {
                sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin account_id.dkr.ecr.us-east-1.amazonaws.com'
                sh 'docker push account_id.dkr.ecr.us-east-1.amazonaws.com/my-docker-repo:$BUILD_NUMBER'
         }
        }
      }
        stage ('Helm Deploy') {
          steps {
            script {
                sh "helm upgrade first --install mychart --namespace helm-deployment --set image.tag=$BUILD_NUMBER"
                }
            }
        }
    }
}

Step # 6 - Build the pipeline


Step # 7 - Verify deployments in EKS
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 # 8 - Access Springboot App Deployed in EKS cluster
Once deployment is successful, go to browser and enter above load balancer URL mentioned above

You should see page like below:

Cleanup EKS Cluster using eksctl
To avoid charges from AWS, you should clean up resources. 

eksctl delete cluster --name demo-eks --region us-east-1


Watch steps in Youtube channel:

Sunday, May 14, 2023

CICD Process Flow Diagram | Implement CICD using Azure DevOps and Deploy Applications into Azure Cloud

 CICD Process Flow Diagram - How to Implement CICD using Azure DevOps?



Azure DevOps (previously known as VSTS) is Microsoft's cloud based offering for any technology stack, any platform to turn idea into a product. You can migrate any applications into Azure by building pipelines in Azure Devops. Lets us quickly see what are the services provided by Azure Devops.


What is Continuous Integration?

Continuous integration is a DevOps software development practice where developers regularly merge their code changes into a central repository, after which automated builds and tests are run.

The key goals of continuous integration are to find and address bugs quicker, improve software quality, and reduce the time it takes to validate and release new software updates.

Azure DevOps is a software as a service (SaaS) platform that provides DevOps practices and tools for the end-to-end software life cycle.

How does Continuous Integration Work?

Developers frequently commit to a shared repository using a version control system such as Git. Prior to each commit, developers may choose to run local unit tests on their code as an extra verification layer before integrating. A continuous integration service automatically builds and runs unit tests on the new code changes to immediately surface any errors.

Benefits of Continuous Integration
  • Improve Developers productivity 
  • Find bugs early in the software development stage
  • Deliver products into market place sooner
  • Improve the feedback loop
What is Continuous Delivery?

Continuous delivery is a software development practice where code changes are automatically prepared for a release to production. Continuous delivery is the next extension of continuous integration. The delivery phase is responsible for packaging an artifact together to be delivered to end-users. This phase runs automated building tools to generate this artifact.

Benefits of Continuous Delivery
  • Automate the Software Release Process
  • Improve Developer Productivity
  • Find bugs early in the software development stage
  • Deliver updates faster

Tuesday, May 9, 2023

Tuesday, May 2, 2023

How to Deploy Springboot Microservices App into AKS cluster using Jenkins Pipeline and Helm | Deploy Microservices into AKS cluster using Helm 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


Watch Steps in YouTube Channel:

Pre-requisites:
1. AKS cluster needs to be up running. You can create AKS cluster, ACR Repo using shell script provided in my website.
2. Jenkins instance is up and running
3. Make sure to Install Docker, Docker pipeline 


4.  Install Docker in Jenkins and Jenkins have proper permission to perform Docker builds
5. Install Kubectl on Jenkins instance
6. ACR is also setup in Azure cloud. 
8. Dockerfile created along with the application source code for springboot App.
9. Install Azure CLI on your local machine. (We will be creating the AKS cluster from our local machine)
10. Helm installed on Jenkins instance

The Code for this video is here:

Step # 1 - 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 #2 - Create Credentials for connecting to AKS cluster using Kubeconfig

Go to Jenkins UI, click on Credentials -->


Click on Global credentials
Click on Add Credentials

use secret file from drop down.

you should see the nodes running in EKS cluster.

kubectl get nodes

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




Open your text editor or notepad, copy and paste the entire content and save in a file.
We will upload this file.

Enter ID as K8S and choose File and upload the file 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 = 'myacrrepo531.azurecr.io'
    }
    
    stages {
        stage('checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'check_out_from_your_repo_after_forking_my_repo']]])
            }
        }
        
        stage ('Build') {
        steps {
            sh 'mvn clean install'           
        }
     }
     
    stage ('Build Docker image') {
        steps {
                script {
                    dockerImage = docker.build registryName
                }
            }
        }
        
    // Uploading Docker images into ACR
        stage('Upload Image to ACR') {
         steps{   
             script {
                docker.withRegistry( "http://${registryUrl}", registryCredential ) {
                dockerImage.push("$BUILD_NUMBER")
                }
            }
          }
        }
        
        stage ('Helm Deploy') {
          steps {
            script {
                sh "helm upgrade ver-$BUILD_NUMBER --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 myResourceGroup --yes --no-wait

Tuesday, April 25, 2023

Shell Script for creating AKS Cluster and Azure Container Registry | Setup ACR and AKS Cluster in Azure Cloud using Azure CLI and Script

#!/bin/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 \


# 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

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

We are going to learn how to automate build and deployment of Springboot Microservices Docker Container into Elastic Kubernetes Cluster(EKS)...