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

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

Sunday, 14 January 2024

Kubernetes common failures issues.

Kubernetes is a container orchestration tool ,and while working with it errors are part of it , but it would be time saving if we knew where to debug & fix the issue, as for a beginner things might get overwhelming.

Here are 5 common k8s failures and how to fix them 🛠️



𝟭) 𝗜𝗺𝗮𝗴𝗲-𝗽𝘂𝗹𝗹 𝗯𝗮𝗰𝗸 𝗼𝗳𝗳 ~ Check for 𝙄𝙢𝙖𝙜𝙚 𝙥𝙪𝙡𝙡 𝙥𝙤𝙡𝙞𝙘𝙮 , 𝙥𝙚𝙧𝙢𝙞𝙨𝙨𝙞𝙤𝙣 𝙩𝙤 𝙥𝙪𝙡𝙡 𝙛𝙧𝙤𝙢 𝙧𝙚𝙥𝙤𝙨𝙞𝙩𝙤𝙧𝙮,𝙘𝙤𝙧𝙧𝙚𝙘𝙩 𝙞𝙢𝙖𝙜𝙚 𝙣𝙖𝙢𝙚 𝙖𝙡𝙤𝙣𝙜 𝙬𝙞𝙩𝙝 𝙩𝙖𝙜.


𝙐𝙨𝙚𝙛𝙪𝙡 𝙘𝙤𝙢𝙢𝙖𝙣𝙙𝙨 🔮 

 Kubectl describe po <podname>

 Kubectl get po <podname>

 Kubectl apply -f <deployment file name>



2) 𝘾𝙧𝙖𝙨𝙝-𝙇𝙤𝙤𝙥 𝙗𝙖𝙘𝙠 𝙤𝙛𝙛~ Check for 𝘾𝙤𝙧𝙧𝙚𝙘𝙩 𝙞𝙢𝙖𝙜𝙚 𝙣𝙖𝙢𝙚 𝙖𝙡𝙤𝙣𝙜 𝙬𝙞𝙩𝙝 𝙩𝙖𝙜 , 𝙚𝙣𝙤𝙪𝙜𝙝 𝙧𝙚𝙨𝙤𝙪𝙧𝙘𝙚 𝙘𝙤𝙣𝙨𝙩𝙧𝙖𝙞𝙣𝙩𝙨,𝙢𝙞𝙨𝙘𝙤𝙣𝙛𝙞𝙜𝙪𝙧𝙖𝙩𝙞𝙤𝙣 𝙤𝙛 𝙚𝙣𝙫𝙞𝙧𝙤𝙣𝙢𝙚𝙣𝙩 𝙫𝙖𝙧𝙞𝙖𝙗𝙡𝙚𝙨 , 𝙖𝙥𝙥𝙡𝙞𝙘𝙖𝙩𝙞𝙤𝙣 𝙛𝙖𝙞𝙡𝙪𝙧𝙚 𝙗𝙚𝙘𝙖𝙪𝙨𝙚 𝙤𝙛 ( 𝙛𝙖𝙞𝙡 𝙩𝙤 𝙗𝙪𝙞𝙡𝙙 𝙟𝙖𝙧 𝙛𝙞𝙡𝙚𝙨 , 𝙞𝙨𝙨𝙪𝙚𝙨 𝙬𝙝𝙞𝙡𝙚 𝙗𝙪𝙞𝙡𝙙𝙞𝙣𝙜 𝙙𝙤𝙘𝙠𝙚𝙧 𝙞𝙢𝙖𝙜𝙚.


𝙐𝙨𝙚𝙛𝙪𝙡 𝙘𝙤𝙢𝙢𝙖𝙣𝙙𝙨 🔮 

 Kubectl describe po <podname>

 Kubectl logs <podname>

 Also to check if enough resources are allocated (memory) 



3) 𝙁𝙖𝙞𝙡𝙪𝙧𝙚 𝙬𝙞𝙩𝙝 𝙀𝙭𝙞𝙩 𝙘𝙤𝙙𝙚 1~ Check for 𝘼𝙥𝙥𝙡𝙞𝙘𝙖𝙩𝙞𝙤𝙣 𝙘𝙤𝙙𝙚 𝙘𝙧𝙖𝙨𝙝𝙚𝙨 , 𝙞𝙣𝙘𝙤𝙧𝙧𝙚𝙘𝙩 𝙚𝙣𝙫𝙞𝙧𝙤𝙣𝙢𝙚𝙣𝙩 𝙫𝙖𝙧𝙞𝙖𝙗𝙡𝙚𝙨, 𝙞𝙣𝙨𝙪𝙛𝙛𝙞𝙘𝙞𝙚𝙣𝙩 𝙛𝙞𝙡𝙚 𝙥𝙚𝙧𝙢𝙞𝙨𝙨𝙞𝙤𝙣𝙨.


𝙐𝙨𝙚𝙛𝙪𝙡 𝙘𝙤𝙢𝙢𝙖𝙣𝙙𝙨 🔮 

 Kubectl logs <podname>

 Kubectl get po <podname>

 Kubectl apply -f <deployment file name>

Lookout for any exceptions in logs /missing variables at code level as well .



4) 𝙁𝙖𝙞𝙡𝙪𝙧𝙚 𝙬𝙞𝙩𝙝 𝙀𝙭𝙞𝙩 𝙘𝙤𝙙𝙚125~ Check for 𝙞𝙣𝙘𝙤𝙧𝙧𝙚𝙘𝙩 𝙛𝙞𝙡𝙚 𝙥𝙚𝙧𝙢𝙞𝙨𝙨𝙞𝙤𝙣𝙨 , 𝙚𝙭𝙘𝙚𝙥𝙩𝙞𝙤𝙣𝙨 𝙙𝙪𝙧𝙞𝙣𝙜 𝙗𝙤𝙤𝙩𝙞𝙣𝙜 𝙪𝙥 𝙤𝙛 𝙥𝙤𝙙


𝙐𝙨𝙚𝙛𝙪𝙡 𝙘𝙤𝙢𝙢𝙖𝙣𝙙𝙨 🔮 

 Kubectl logs <podname>

 Kubectl describe po <podname>


5) 𝙋𝙤𝙙/𝙉𝙤𝙙𝙚 𝙉𝙤𝙩 𝙍𝙚𝙖𝙙𝙮 ~ Check for 𝙉𝙚𝙩𝙬𝙤𝙧𝙠 𝘾𝙤𝙣𝙣𝙚𝙘𝙩𝙞𝙫𝙞𝙩𝙮 , 𝙚𝙣𝙤𝙪𝙜𝙝 𝙧𝙚𝙨𝙤𝙪𝙧𝙘𝙚 𝙖𝙡𝙡𝙤𝙘𝙖𝙩𝙞𝙤𝙣 ,𝙪𝙣𝙝𝙚𝙖𝙡𝙩𝙝𝙮 𝙥𝙧𝙤𝙘𝙚𝙨𝙨𝙚𝙨


𝙐𝙨𝙚𝙛𝙪𝙡 𝙘𝙤𝙢𝙢𝙖𝙣𝙙𝙨 🔮 

 Kubectl logs <podname>

 Kubectl get po <podname> and check for its state 

Increase system resource usage


Connect with me 😊

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


Labels: