Day 28 Task: Jenkins Agents

Pooja BhavaniPooja Bhavani
4 min read

Jenkins Master (Server)

The Jenkins master server is the central control unit that manages the overall orchestration of workflows defined in pipelines. It handles tasks such as scheduling jobs, monitoring job status, and managing configurations. The master serves the Jenkins UI and acts as the control node, delegating job execution to agents.

Jenkins Agent

A Jenkins agent is a separate machine or container that executes the tasks defined in Jenkins jobs. When a job is triggered on the master, the actual execution occurs on the assigned agent. Each agent is identified by a unique label, allowing the master to delegate jobs to the appropriate agent.

For small teams or projects, a single Jenkins installation may suffice. However, as the number of projects grows, it becomes necessary to scale. Jenkins supports this by allowing a master to connect with multiple agents, enabling distributed job execution.

Pre-requisites

To set up an agent, you'll need a fresh Ubuntu 22.04 Linux installation. Ensure Java (the same version as on the Jenkins master server) and Docker are installed on the agent machine.

Note: While creating an agent, ensure that permissions, rights, and ownership are appropriately set for Jenkins users.

sudo usermod -aG docker $USER && sudo reboot
sudo usermod -aG docker jenkins

Task-1

  • Create two EC2 instance Jenkins-server and Jenkins-agent and connect with you SSH client

  • Update and Upgrade your system install Java and Jenkins in your Jenkins server and Jenkins-agent and install docker and docker-compose and give permission to docker and Jenkins
sudo apt-get update && sudo apt-get upgrade -y
sudo apt update
sudo apt install openjdk-17-jre
java -version
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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-get install jenkins
sudo apt-get install docker.io -y && sudo apt-get install docker-compose -y
sudo chown $USER /var/run/docker.sock
sudo usermod -aG docker jenkins
  • open your Jenkins server dashboard and in Set up a distributed build click on Set up an agent

  • Give a node name and tick on permanent agent and create

  • Give a description and Remote root directory as " /home/ubuntu " and Give the label "agent" and Usage to " use this node much as possible" and Launch method to " Launch agents via SSH " and copy Host as worker-server instance IP address like this and paste it there

  • Now come to the master-server instance terminal and go to cd .ssh/ then generate ssh key by running commands

cd .ssh/
ssh-keygen
ls

here you can see two files have been created one is jenkins which is the private key and another one is jenkins.pub which public key.

cat jenkins.pub

and copy it then paste to the Jenkins-agent terminal by going to the .ssh/ run command

Jenkins-agent:

  • now come to the Jenkins page goto credentials section click on add and select Jenkins like this

now you will see this page

Now in kind section select SSH username and private key

then set the ID, description and username shown in the image below

then select Enter directly and Add like this

  • Go to the Jenkins-server terminal and open the ssh private key like this
cat jenkins

then copy it and past it in Jenkins credentials like this and click on add

Now credential add this

  • In the Host key verification strategy select this and save

Task-2

  • Run your previous Jobs (which you built on Day 26, and Day 27) on the new agent

  • Use labels for the agent, your master server should trigger builds for the agent server.

  • In case of any issues feel free to post on any Groups, Discord or Telegram

Recreate day 26 task in it

Step 1: create a new job and in the pipeline area write this commads and save

  pipeline {
      agent any
      stages {
          stage('Build') {
              steps {
                  echo 'Hello, World!'
              }
          }
      }
  }

here you can see we have successfully runed out pipeline

Step 1: In GitHub project put the url like this

and run this command is pipeline section and save

pipeline {
    agent any

    stages{
        stage("code") {
            steps{
                echo "code cloned"
                git url: "https://github.com/pooaj-bhavani/django-todo-cicd.git", branch: "develop"
            }
        }
        stage("build") {
            steps{
                echo "building the image"
                sh "docker build . -t django-todo-app"
            }
        }
        stage("deploy") {
            steps{
                echo "deploying the image"
                sh "docker run -d -p 8000:8000 django-todo-app:latest"
            }
        }
    }
}

Step 2: Now build now then you can see its successfully running

Step 3: Go to the worker server instance and open port 8000 Copy its IP address and add ":8000" to it here you can see we have successfully deployed it through the Jenkins pipeline

0
Subscribe to my newsletter

Read articles from Pooja Bhavani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pooja Bhavani
Pooja Bhavani