Tuesday, 16 January 2024

Setup EKS Cluster using eksctl and Deploy Springboot Microservices into EKS using Jenkins Pipeline


Deploy Springboot Microservices App into Amazon EKS Cluster using Jenkins Pipeline and Kubectl CLI Plug-in | Containerize Springboot App and Deploy into EKS Cluster using Jenkins Pipeline


We will learn how to automate springboot microservices builds using Jenkins pipeline and Deploy into AWS EKS Cluster with help of Kubernetes CLI 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 AWS ECR
- Automating Docker Containers Deployments to Kubernetes Cluster


Source  Code for this project is here:

Pre-requisites:
1. Amazon EKS Cluster is setup and running. Click here to learn how to create Amazon EKS cluster.
5. Docker, Docker pipeline and Kubernetes CLI plug-ins are installed in Jenkins




6. Install kubectl on your instance

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 Kubernetes Cluster using kubeconfig
Click on Add Credentials, use Kubernetes configuration from drop down.

use secret file from drop down.


execute the below command to login as jenkins user.
sudo su - jenkins

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 /var/lib/jenkins/.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.


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 as per your settings:
Your docker user id should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied

pipeline {
    agent any

    tools {
        maven "Maven3"
    }
    
    environment {
     repository = "456137991393.dkr.ecr.ap-south-1.amazonaws.com/harry"   
     
        
        
    }

    stages {
        stage('Git Checkout') {
            steps {
                script {
                    // Check out code from the specified Git repository
                    git branch: 'main', url: 'https://github.com/imHarry404/springboot-app.git'
                }
            }
        }
        stage('Build Jar') {
            steps {
                script {
                    
                    sh "mvn clean install"
                }
            }
        }    
        stage('Build Docker Image') {
            steps {
                script {
                    docker.build repository
                }
            }
        }
        stage('Push into ECR') {
            steps {
                script {
                    sh "aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 456137991393.dkr.ecr.ap-south-1.amazonaws.com"
                    sh "docker push 456137991393.dkr.ecr.ap-south-1.amazonaws.com/harry:latest"
                }
            }
        }
        
        stage('Deploy to k8s') {
            steps {
                script {
                    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                        sh "kubectl apply -f eks-deploy-from-ecr.yaml"
                    }
                }
            }
        }
    }
}

Step # 5 - Build the pipeline
Once you create the pipeline and changes values per your configuration, click on Build now:


Step # 6 - Verify deployments to K8S

kubectl get pods



kubectl get deployments

kubectl get services


If you see any errors after deploying the pods, you can check the pod logs.
kubectl logs <pod_name>

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://loadbalancer_ip_address

You should see page like below:



Note:

Make sure you fork my repo my-git-repo
and make changes in eks-deploy-k8s.yaml to pull Docker image from your AWS ECR repo.



Connect with me 😊



Labels: , , ,

Monday, 15 January 2024

Connect Jenkins with Microsoft teams

 Connect Jenkins with Microsoft teams.


Create a team from scratch (public/ private  depends on you)

 



Give appropriate team name & description and create


 


Add member in your team using name/email.


 


Now add a channel for your team


 


Give name, description and channel type 


 


if you don't find add Connectors option, go to ... (view more apps) in teams and search for jenkins


 


{Select Add  (for first time it will ask to install & configure)}

Select Add then Add to a team 


 


you will find your team name there, select and click on set up a connector


 


Give name for your Jenkins connection,  it will generate a web-hook url and click on done.

Copy the web-hook url, it will be used later in pipeline setup.



 

Then you can see this message of setup in teams

 





Now create an EC2 instance and install setup jenkins on it and configure jenkins as well.

We need one plugin to be installed

Dashboard >> Manage Jenkins >> Plugins

Install this Office 365 plugin

 



Now create a pipeline.

 



You will see  office 365 connector

 



click on Add Webhook



Paste the url that you copied which configuration.

 


in pipeline select Hello world -> Apply -> Save

 



Build the first job

 



Log of the triggered build

 



in MS-Teams you can see a new message of success.

 



what if the build fails.

go to configure -> in pipeline script make some mistake and save.

now trigger the build again

 





you can see new message of failed, and you can check the build as well.

 



you can customize the notifications in office 365 connector section 

 





Thanks.

Connect with me 😊

https://www.linkedin.com/in/imharry404/


Labels: , ,

Spring Boot Shopping Cart Web App: Project with git-repo, all files and steps

 Building a Spring Boot Shopping Cart Web App: A Practical Guide



1. Creating VM Machines on AWS

In the initial phase of our journey, we set up a robust development environment on Amazon Web Services (AWS). This involved creating virtual machines to serve as the foundation for our project. The detailed steps and snapshots of required vm machines on AWS can be found below.



 2. Kubernetes (K8s) Setup


With our VMs in place, the next step was to configure Kubernetes for efficient container orchestration. The steps for setting up K8s and joining nodes to the cluster are outlined.

https://github.com/imHarry404/Ekart/blob/main/k8s-setup.txt


 3. Configuring Security Groups with Ports


Ensuring secure communication within our environment is paramount. We established security groups to control incoming and outgoing traffic, implementing port restrictions for enhanced safety.



4. Building Jenkins Pipelines for Success

Our Jenkins setup included defining pipelines to automate the build, test, and deployment phases. A successful pipeline is outlined in detail.

🛠️ 𝗣𝗶𝗽𝗲𝗹𝗶𝗻𝗲 𝗦𝘁𝗮𝗴𝗲𝘀:

𝗗𝗲𝗳𝗶𝗻𝗲 𝗧𝗼𝗼𝗹𝘀:

JDK, Maven, SonarQube - essential tools defined within our pipelines.

𝗖𝗼𝗺𝗽𝗶𝗹𝗲 𝘁𝗵𝗲 𝗖𝗼𝗱𝗲:

Transforming source code into executable binaries with precision.

𝗥𝘂𝗻 𝗨𝗻𝗶𝘁 𝗧𝗲𝘀𝘁𝘀:

Ensuring the integrity of the codebase with thorough unit testing.

𝗦𝘁𝗮𝘁𝗶𝗰 𝗖𝗼𝗱𝗲 𝗔𝗻𝗮𝗹𝘆𝘀𝗶𝘀:

Leveraging SonarQube for in-depth code quality assessments. 🔍

𝗩𝘂𝗹𝗻𝗲𝗿𝗮𝗯𝗶𝗹𝗶𝘁𝘆 𝗦𝗰𝗮𝗻:

A critical step! Scanning dependencies for potential vulnerabilities to fortify our code.

𝗕𝘂𝗶𝗹𝗱/𝗣𝗮𝗰𝗸𝗮𝗴𝗲 𝘁𝗵𝗲 𝗔𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻:

Bringing our application to life with a robust build process.

𝗣𝘂𝗯𝗹𝗶𝘀𝗵 𝗔𝗿𝘁𝗶𝗳𝗮𝗰𝘁𝘀:

Storing our precious artifacts securely in Nexus for future use.

𝗕𝘂𝗶𝗹𝗱 & 𝗧𝗮𝗴 𝗗𝗼𝗰𝗸𝗲𝗿 𝗜𝗺𝗮𝗴𝗲:

Crafting Docker images and assigning meaningful tags.

𝗦𝗰𝗮𝗻 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿 𝗜𝗺𝗮𝗴𝗲𝘀:

Ensuring Docker images are free from vulnerabilities for a secure deployment.

𝗣𝘂𝘀𝗵 𝘁𝗼 𝗜𝗺𝗮𝗴𝗲 𝗥𝗲𝗽𝗼𝘀𝗶𝘁𝗼𝗿𝘆:

Safely storing our Docker images in a repository for accessibility.

𝗬𝗔𝗠𝗟 𝗠𝗮𝗻𝗶𝗳𝗲𝘀𝘁𝘀:

Crafting YAML manifest files for Kubernetes deployment - the heart of our scalable architecture.

𝗗𝗲𝗽𝗹𝗼𝘆 𝘁𝗼 𝗞𝘂𝗯𝗲𝗿𝗻𝗲𝘁𝗲𝘀:

The grand finale! Deploying our application to Kubernetes for efficient orchestration.







5. Exploring the Working Project Homepage

The heart of our endeavor is the fully functional Spring Boot Shopping Cart Web App. This section provides insights into the features, user experience, and the seamless integration of technologies. Explore the working project homepage

access it using node-ip:port (given in pipeline log) 

then login using admin & admin



6. Nexus Repository Snapshots for Dependency Management


Efficient dependency management is crucial for a stable and reproducible build environment. We utilized Nexus Repository to store snapshots of project dependencies. Learn more about our Nexus Repository setup.





7. All Files Present on Repository

For your convenience, all the files associated with this project, including configurations, scripts, and code, are available in the project repository. You can access the repository and its contents 

https://github.com/imHarry404/Ekart/tree/main


8. Image present on DockerHub 



 Conclusion

This blog post serves as a comprehensive guide through the process of building a Spring Boot Shopping Cart Web App. Feel free to explore the provided links and snapshots to gain a deeper understanding of each step in this exciting development journey. Happy coding!


Connect with me 😊

https://www.linkedin.com/in/imharry404/

Labels: , ,