Jenkins Agents

Jenkins Agents

#90 Days of DevOps Challenge - Day 28

Jenkins Master (Server)

Your main Jenkins server is the Master. The Master’s job is to handle:

  • Scheduling build jobs.

  • Dispatching builds to the slaves for the actual execution.

  • Monitor the slaves (possibly taking them online and offline as required).

  • Recording and presenting the build results.

  • A Master instance of Jenkins can also execute build jobs directly.

Jenkins Agent

An agent is typically a machine or container that connects to a Jenkins master and this agent actually execute all the steps mentioned in a Job. When you create a Jenkins job, you have to assign an agent to it. Every agent has a label as a unique identifier.

When you trigger a Jenkins job from the master, the actual execution happens on the agent node that is configured in the job.

A single, monolithic Jenkins installation can work great for a small team with a relatively small number of projects. As your needs grow, however, it often becomes necessary to scale up. Jenkins provides a way to do this called “master to agent connection.” Instead of serving the Jenkins UI and running build jobs all on a single system, you can provide Jenkins with agents to handle the execution of jobs while the master serves the Jenkins UI and acts as a control node.

Project: Deploy a web app through Jenkins master to Jenkins worker node

Step 1:- Firstly we have launch 2 EC2 machines

Step 2:- Now need to go to Jenkins master Ubuntu machine need to type "ssh-keygen -t ed25519" and enter 3 times

Step 3:- Then we need to go to cd .ssh and we can see the RSA public and private key

Step 4:- Copy the id_ed25519.pub which is the public key. Then need to go jenkins-agent machine and paste the public key into vim authorized_keys

Step 5:- Now we need to install Java in the agent server by running below commands

sudo apt-get update
sudo apt-get install openjdk-11-jre
java --version

Step 6:- Go to the Jenkins dashboard -> manage Jenkins -> Manage Nodes and Clouds

Step 7:- While creating a new node below details we need to fill

Node name:-agent-node

✅ Permanent node

Name:- agent

Desc:- This is agent which will run react_django app

Number of execution:-1 (cpu)

Remote root directory:-/home/ubuntu/jenkins-agent

Labels:- dev-server

Usage:- Only build jobs with label expression matching this node

Launch method:- launch agents via SSH

Host:-<Public-IPv4-addr-agent>

Credentials:-

Kind:-SSH username with private key

ID:-jenkins-ssh-key

Description:- jenkins-ssh-key

Username:- ubuntu

Private Key:-<Private-key-master-node>

Host Key Verification Strategies:- Non verifying verification strategy (SSH check with keys)

Now we need to click on New Node

give the Node-name -> and select Permanent Agent -> create

after clicking on create need to give your name, Description, Remote root directory, Labels

After clicking on Save Node will be connected and online.

Set Up React Django App

Step 1:- First we need to create a new item and need to give the project name "django-cicd-project-Declarative Pipeline" and choose Pipeline and click on ok

Step 2:- After clicking ok, Need to give the Description

Step 3:- Now need to go to Pipeline and give the below code

 pipeline {
     agent { label "dev-server" }
     stages{
         stage("Clone Code"){
             steps{
                 git url: 'https://github.com/Saikat55/react_django_demo_app.git' , branch: 'main'
             }
         }
         stage("Build and Test"){
             steps{
                 sh "docker build . -t rect-django-app"
             }
         }
         stage("Push to Docker Hub"){
             steps{
                 echo "Docker Hub will receive the image"
             }
         }
         stage("Deploy"){
             steps{
                 sh "docker-compose down && docker-compose up -d"
             }
         }
     }
 }

Step 4:- Now go to Manage Jenkins -> Manage Plugin and install the "Environment Injector" and Download It will store all the environment variables where we can pass the credentials.

Step 5:- Now go to Manage Jenkins -> Credentials -> System -\> Global Credentials -> globals -> + Add Credentials

Kind:- username with password

username:-<DockerHub_Username>

password:-<DockerHub_Password>

ID:- dockerHub

Desc:- This is my Docker Hub Credentials

Step 6:- Now configure the pipeline for pushing the image into the docker hub.

 pipeline {
     agent { label "dev-server" }
     stages{
         stage("Clone Code"){
             steps{
                 git url: "https://github.com/Saikat55/react_django_demo_app.git" , branch: "main"
             }
         }
         stage("Build and Test"){
             steps{
                 sh "docker build . -t react_django_app-test-new"
             }
         }
         stage("Push to Docker Hub"){
             steps{
                 withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                 sh "docker tag react_django_app-test-new ${env.dockerHubUser}/react_django_app-test-new:latest"
                 sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                 sh "docker push ${env.dockerHubUser}/react_django_app-test-new"
                 }
             }
         }
         stage("Deploy"){
             steps{
                 sh "docker-compose down && docker-compose up -d"
             }
         }
     }
 }

Step 7:- After running the Build Now we can see Stage View. your project will be tied to Jenkins-agent.

Step 8:- Once the build is successful, you can go to the console output and check the output of the build.

Step 9:- Now we need to verify whether we can browse the application or not by using Public IPv4 address:prot_number

Step 10:- Application also pushed to docker hub

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!