Skip to main content

Command Palette

Search for a command to run...

Deploying Mern Application Through Ci/

Updated
3 min read

Start By creating Launching Ec2 Machine
K8_Master (t2.medium)

jenkins (t2.medium)

node (t3.micro ) for setting the Kudeadm nodes (k8 nodes)

Login into jenkins dashboard and create job (select pipeline )

Generate Token To add In Jenkins Credentials

install docker in jenkins server and docker permission to jenkins

docker --version check version

sudo systemctl status docker to check docker installed and running

sudo usermod -aG docker jenkins

sudo systemctl restart docker

sudo systemctl restart jenkins

sudo su - jenkins

docker ps

if it list correctly docker added to Jenkins

Frontend Pipeline

pipeline {
    agent any

    stages {
        stage('Git CheckOut') {
            steps {
                git url:"https://github.com/HemanthVarupula/Banking_Frontend.git",branch:'main'
            }
        }
        stage('Build & Tag Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker') {
                        sh 'docker build -t hemanthvarupula/banking_frontend:latest .'
                    }
                }
            }
        }
        stage('Push Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker') {
                        sh 'docker push  hemanthvarupula/banking_frontend:latest'
                    }
                }
            }
        }
    }
}

git checkout where i maintain the Dockerfile and whole code

It will take the dockerfile from github and build image and push image to docker hub

Backend Pipeline

pipeline {
    agent any

    stages {
        stage('Git CheckOut') {
            steps {
                git url:"https://github.com/HemanthVarupula/Banking_Backend_Hemanth.git",branch:'main'
            }
        }
        stage('Build & Tag Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker') {
                        sh 'docker build -t hemanthvarupula/banking_backend:latest .'
                    }
                }
            }
        }
        stage('Push Docker Image') {
            steps {
                script {
                    withDockerRegistry(credentialsId: 'docker') {
                        sh 'docker push  hemanthvarupula/banking_backend:latest'
                    }
                }
            }
        }
    }
}

Go and Trigger the pipeline manually as of now we will automate that in end of the blog

Both Pipeline are success and image push to docker Hub

Install kubectl in Jenkins , to automate k8 deployment

curl -LO "https://dl.k8s.io/release/\)(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

chmod +x kubectl ======> give execute permission

sudo mv kubectl /usr/local/bin/ ====> Add to bin

kubectl version --client ====> you will get kubectl version

Deploying Frontend and Backend as pod using deployment and traffic through service (nodeport)

I have maintained a separate pipeline for frontend and backend

Frontend Cd

pipeline {
    agent any

    stages {

        stage('Git Checkout') {
            steps {
                git url: "https://github.com/HemanthVarupula/Banking_Frontend.git",
                    branch: 'main'
            }
        }

        stage('Kubernetes Test') {
            steps {
                withKubeConfig([credentialsId: 'kubeconfig']) {
                    sh 'kubectl get nodes'
                }
            }
        }
        
        stage("Deploy to k8"){
            steps{
                withKubeConfig([credentialsId: 'kubeconfig']) {
                    sh 'kubectl apply -f deployment.yml'
                    sh 'kubectl apply -f svc.yml'
                }
            }
        }
    }
}

Backend Cd

pipeline {
    agent any

    stages {
        stage('git_checkout') {
            steps {
                git url:"https://github.com/HemanthVarupula/Banking_Backend_Hemanth.git",branch:'main'
            }
        }
        
        stage("Deploy to k8"){
            steps{
                withKubeConfig([credentialsId: 'kubeconfig']) {
                    sh 'kubectl apply -f deployment.yml'
                    sh 'kubectl apply -f svc.yml'
                }
            }
        }

    }
}

Add docker hub credentials and kubeconfig to jenkins credentials

frontend http://54.234.107.39:30007 === > through nodeport

backend http://54.234.107.39:30009 ====>through Nodeport

Accessing the Frontend Application From Browser

Application is working fine

Tested Api In Postman Backend is working Fine

Automating build using GitHub WebHooks