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: , ,

Sunday, 14 January 2024

📘 Top 12 Git Commands Cheatsheet

git init - Initialize a new Git repository.

git clone - Clone a remote repository to your local machine.

git status - Check the current state of your working directory.

git add - Stage changes for the next commit.

git commit - Record staged changes and create a snapshot.

git push - Upload local changes to a remote repository.

git pull - Fetch and merge changes from a remote repository.

git branch - List, create, or delete branches.

git checkout / git switch - Switch between branches or commits.

git merge - Integrate changes from one branch into another.

git diff - View differences between working directory and staging area.

git log - Display a chronological list of commits.




### Basic Commands:


1. **Initialize a Repository:**

   ```bash

   git init

   ```


2. **Clone a Repository:**

   ```bash

   git clone <repository_url>

   ```


3. **Add Changes:**

   ```bash

   git add <file(s)>

   ```


4. **Commit Changes:**

   ```bash

   git commit -m "Commit message"

   ```


### Branching:


5. **Create a New Branch:**

   ```bash

   git branch <branch_name>

   ```


6. **Switch to a Branch:**

   ```bash

   git checkout <branch_name>

   ```


   *(or use `git switch <branch_name>` in Git 2.23 and later)*


7. **Create and Switch to a New Branch:**

   ```bash

   git checkout -b <new_branch_name>

   ```


   *(or use `git switch -c <new_branch_name>` in Git 2.23 and later)*


8. **List Branches:**

   ```bash

   git branch

   ```


### Merging:


9. **Merge Branch into Current Branch:**

   ```bash

   git merge <branch_name>

   ```


### Remote Repositories:


10. **Add a Remote Repository:**

    ```bash

    git remote add <remote_name> <repository_url>

    ```


11. **Fetch Changes from a Remote Repository:**

    ```bash

    git fetch <remote_name>

    ```


12. **Pull Changes from a Remote Repository:**

    ```bash

    git pull <remote_name> <branch_name>

    ```


13. **Push Changes to a Remote Repository:**

    ```bash

    git push <remote_name> <branch_name>

    ```


### Undoing Changes:


14. **Discard Changes in Working Directory:**

    ```bash

    git checkout -- <file(s)>

    ```


15. **Undo Last Commit (Keep Changes in Working Directory):**

    ```bash

    git reset HEAD^

    ```


16. **Undo Last Commit (Discard Changes):**

    ```bash

    git reset --hard HEAD^

    ```


### Logging and Status:


17. **View Changes:**

    ```bash

    git status

    ```


18. **View Commit History:**

    ```bash

    git log

    ```


### Miscellaneous:


19. **Configure Git:**

    ```bash

    git config --global user.name "Your Name"

    git config --global user.email "your.email@example.com"

    ```


20. **Ignore Files:**

    Create a `.gitignore` file and list files/directories to be ignored.


Remember to replace placeholders like `<repository_url>`, `<branch_name>`, `<remote_name>`, etc., with your actual values. This cheat sheet covers basic Git commands, and Git offers more advanced features that you may explore as needed.



Connect with me 😊

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




 

Labels: