Jenkins on Server Push Based Approach CICD

  • What is CI/CD? ๐Ÿ› ๏ธ

    • Continuous Integration (CI) + Continuous Deployment (CD).

    • Automates software delivery process.

  • Server Push Based Approach ๐Ÿ”„

    • Code changes are automatically pushed to the server.

    • Reduces manual intervention and accelerates deployment.

  • Jenkins Overview ๐Ÿฑโ€๐Ÿ‘ค

    • Popular open-source automation server.

    • Supports building, deploying, and automating projects.

  • How It Works ๐Ÿ–ฅ๏ธ

    • Code Push: Developers push code to a version control system (e.g., Git) ๐ŸŒ.

    • Webhook Trigger: A webhook notifies Jenkins of changes ๐Ÿ””.

    • Build Process: Jenkins pulls the latest code and runs builds/tests ๐Ÿ“Š.

    • Deployment: Successful builds are automatically deployed to the server ๐Ÿšข.

  • Benefits โœ…

    • Faster Feedback: Immediate results on code changes.

    • Automated Testing: Ensures code quality through automated tests ๐Ÿงช.

    • Consistent Releases: Reduces human error in deployments.

    • Scalability: Easily integrates with other tools and systems ๐Ÿ”—.

  • Common Tools Used ๐Ÿ”ง

    • Version Control: Git

    • Notification Services: Slack, email alerts.

    • Containerization: Docker for consistent environments ๐Ÿณ.

  • Use Cases ๐Ÿ“Œ

    • Agile development teams looking for rapid iterations.

    • Organizations aiming for DevOps practices to improve collaboration.

Step by Step guide

Prerequisites

you must have your purchase domain and create hosted zone in Route 53 and replace this namespaces with your domain

Git clone

git clone https://github.com/divyasatpute/Jenkins.git

push this repo on your github

git push <your github repo URL >

Create Cluster using kubeadm script (step1)

For that you need to create 4 machine cluster

In this POC we need 1 master and 2 worker machine and Jenkins machine separately

On AWS Console

  • 4 EC2 machine

  • ubuntu 20.04 AMI

  • volume 40GB (storage)

  • c5.xlarge Machine Type

after you have to connect all on gitbash

And update all cluster using following command :

sudo apt update -y

And set hostname for each node

sudo hostname Master
sudo hostname Worker1
sudo hostname Worker2

Come on root user

sudo -i

now on master node paste the following command (EKS Script ) Controlplane script

bash <(curl -s https://raw.githubusercontent.com/isakibshaikh1/Kubeadm/main/kubeadm/master.sh)

And on another on both Worker node paste following command (worker script)

bash <(curl -s https://raw.githubusercontent.com/isakibshaikh1/Kubeadm/main/kubeadm/worker.sh)

now your master node is ready take this token from master node and paste on worker node for join worker node to master

kubeadm join 172.31.46.89:6443 --token qanxgm.tmberexbyo3n0dpr --discovery-token-ca-cert-hash sha256:c2b6ac5e46556ed69c940fa8725eb37e6ccbeaa3f39117fc71a481a1798eac3c

as you can see node will be ready but worker node yet not ready state

Note that both nodes are NotReady. This is OK because we have not yet installed networking.

for that we have use some plugins Install a Network Plugin

Doc Ref : Installing Network Plugin Addons

Install Weave Net : Weave Net

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Installation of Jenkins (step 2)

### How to deploy Jenkins with Our custom domain and SSL ###

For install Jenkins we need to follow below shell script, i am using ubuntu 20.04 this script also work for ubuntu 22.04

vi jenkins.sh

Paste this script in Jenkins.sh

#! /bin/bash

sudo apt update
sudo apt install openjdk-11-jre -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt-get update
sudo apt install -y maven
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins

Run script using this command

sh jenkins.sh

After that we need to check using Jenkins using ec2 instance id for example:- 34.203.34.186:8080 for checking purpose but our end goal its should not be run on this after configuring nginx reverse proxy will be remove port 8080 from ec2 instance security group and check , Here you can see our Jenkins up and running

Access your Jenkins Dashboard on browser now

Note: Make sure you enable 8080 port in Security Group Inbound Rules.

Get the initial administration password

$sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Now your Jenkins started : Create Admin user

๐ŸŽ‰Now , appreciate yourself you successfully deploy Jenkins on your machine ๐ŸŽ‰

it is Look like this : Jenkins up and Running but this is not yet secured we have to add SSL for secure connection


Step-by-step guide to configure SSL on Jenkins using Let's Encrypt and NGINX reverse proxy (step 3)

  1. Install NGINX on your server if it's not already installed. You can do this by running the following command:

     #update repository first 
     sudo apt-get update -y
     #install nginx by following commad
     sudo apt-get install nginx -y
     #check nginx status 
     sudo systemctl status nginx
    

  2. Create a new server block configuration file for Jenkins. You can do this by creating a new file in the /etc/nginx/sites-available/ directory. For example:

     sudo vi /etc/nginx/sites-available/jenkins
    
  3. Add the following content to the file: Note: Replace jenkins.example.com with your Jenkins domain name.

     server {
         listen 80;
         server_name jenkins.learnwithdivya.online;
    
         location / {
             proxy_pass http://localhost:8080;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        
             proxy_set_header X-Forwarded-Proto $scheme;
         }
     }
    
  4. Create a symbolic link to the server block configuration file in the /etc/nginx/sites-enabled/ directory:

sudo ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/

Note: you can see /etc/nginx/sites-available/jenkins on this path the file - represented simple file ( $sudo ls -lrt /etc/nginx/sites-available/jenkins)

after running above command its create shortcut because by default Jenkins look this file on /etc/nginx/sites-enabled location (ls -lrt /etc/nginx/sites-enabled)

  1. Test the NGINX configuration and restart the NGINX service:

     sudo nginx -t
    

#restart the nginx server
sudo systemctl restart nginx
  1. Install Certbot, the Let's Encrypt client, by running the following commands:

     sudo apt-get update -y
    
     sudo apt-get install certbot python3-certbot-nginx -y
    
  2. Obtain an SSL certificate for your Jenkins domain name using Certbot:

sudo certbot --nginx -d jenkins.learnwithdivya.online
#Note:Replace jenkins.example.com with your Jenkins domain name.
  1. After running above you can found error like this :

For resolve this issue you need to create A name record on your Route53 service

Go to route53 --> click on hosted zone --> click on your domain name ---> create on record , once it will be insync then you can try again above command

๐ŸŽ‰ Congratulations! ๐ŸŽ‰

You've put in hard work, dedication, and perseverance to reach this milestone!

Now your Jenkins runs in secure with SSL certificate in secure site

It means without using LLB we are use nginx as a reverse proxy : if you are trying to click http:// still it will be redirect to https://

Sounds interesting, right?

Now move to next

create webhook on Github(step 4)

  • Click on repo settings --->

  • click on webhook ---->

  • Add new Webhook --->

  • in Playload URL configure your Jenkins URL followed by github-webhook and content type json

  • click on ADD webhook

Our Webhooks are ready

๐ŸณDocker Install on Jenkins machine๐Ÿณ

sudo apt install docker.io -y

Give permission to socket

 sudo usermod -aG docker $USER
 sudo chown $USER:docker /var/run/docker.sock

change ownership

sudo chown jenkins:docker /var/run/docker.sock

Go to docker hub account generate token

now on your Jenkins machine click on manage Jenkins --> click on Credentials --->

system ---> global Credentials

ADD your docker token as a password

ADD (ID) which is you given in your pipeline

Plugins Installation

Manage Jenkins --> plugins ---> available plugins ---->

  1. docker

  2. docker Pipeline

  3. docker commons

  4. docker -build-steps

  5. docker slave

install it

Installation of Helm

Helm Install on Jenkins Machine and Master Node

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

configuration on master node

change directory

 cd .kube/

copy config file in /home/ubuntu

change directory

cd /home/ubuntu/

change ownership

sudo chown ubuntu:ubuntu config

break connection come into your local machine download folder

exit

Now fire this command on local (copy file from remote to local)

scp -i divya.pem ubuntu@43.204.109.240:/home/ubuntu/config .

New to go to Jenkins dashboard

  1. click on manage Jenkins

  2. click on Credencials

  3. select as a secrete file

  4. and upload config file

  • Go to Jenkins dashboard

  • click on manage Jenkins

  • click on available plugins

  1. Kubernetes Client API

  2. Kubernetes Credentials

  3. Kubernetes

  4. Kubernetes CLI

  5. Kubernetes Credentials Provider

  • install it

### How to Set Up the Jenkins + GitHub Integration ###

NOW LAST STEP TO ACHIEVE THE THINGS

create Jenkins pipeline

pipeline {
    agent any
    stages {
        stage('Build Maven') {
            steps {
                sh 'pwd'
                sh 'mvn clean install package'
            }
        }

        stage ('Copy Artifacts') {
            steps {
                sh 'pwd'
                sh 'cp -r target/*.jar docker'
            }
        }    

        stage('Unit Tests') {
            steps {
                sh 'mvn test'
            }
        }

        stage('Build Docker Image'){
            steps{
                script {
                    def customImage = docker.build("iamsakib/petclinic:${env.BUILD_NUMBER}", "./docker")
                    docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') {
                    customImage.push()    
                }
            }
        }
    }

        stage('Build on kubernetes'){
        steps {
            withKubeConfig([credentialsId: 'kubeconfig']) {
                sh 'pwd'
                sh 'cp -R helm/* .'
                sh 'ls -ltrh'
                sh 'pwd'
                sh '/usr/local/bin/helm upgrade --install petclinic-app petclinic --set image.repository=iamsakib/petclinic --set image.tag=${BUILD_NUMBER}'
        }
    }
}



}

}

#replace yours id

Deployment of Application

Go to Jenkins dashboard

click on new item

give name to your project

select pipeline

select GitHub hook trigger for GITScm polling

select pipeline script with SCM

SCM =git and configure GIT URL and branch

script path should be jenkinsfile (jenkinsfile should be available on your git repo)

save and apply

Access your application <worker_node_Pliblic_IP:32740\>

And boommmmmmmmmmmmm

๐ŸŒŸ Way to Go! ๐ŸŒŸ

You've done itโ€”what an incredible achievement! This moment is a testament to your effort, resilience, and talent. You faced the challenge head-on and came out on top, and now it's time to celebrate that success.

Remember, this is proof of what you're capable of. Keep pushing forward, and don't forget to enjoy every victory along the way. You've earned itโ€”congratulations!

โœ…Test Results โœ…

1
Subscribe to my newsletter

Read articles from Divya vasant satpute directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Divya vasant satpute
Divya vasant satpute

, I'm a seasoned DevOps engineer ๐Ÿ› ๏ธ with a knack for optimizing software development lifecycles and infrastructure operations. ๐Ÿ’ก Specializing in cutting-edge DevOps practices and proficient in tools like Docker, Kubernetes, Ansible, and more, I'm committed to driving digital transformation and empowering teams to deliver high-quality software with speed and confidence. ๐Ÿ’ป