Day 28 Task: Jenkins Agents

Day 28 Task: Jenkins Agents

Jenkins Master (Server)

Jenkins’s server or master node holds all key configurations. Jenkins master server is like a control server that orchestrates all the workflow defined in the pipelines. For example, scheduling a job, monitoring the jobs, etc.

Jenkins Agent

An agent is typically a machine or container that connects to a Jenkins master and this agent that 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.

Pre-requisites

Let’s say we’re starting with a fresh Ubuntu 22.04 Linux installation. To get an agent working make sure you install Java ( same version as jenkins master server ) and Docker on it.

Note:- While creating an agent, be sure to separate rights, permissions, and ownership for jenkins users.

Please follow the Day 27 task and Day 26 task blog. In this blog, you will see how the declarative pipeline works.

Task-01

  • Create an agent by setting up a node on Jenkins

  • Create a new AWS EC2 Instance and connect it to master(Where Jenkins is installed)

  • The connection of master and agent requires SSH and the public-private key pair exchange.

  • Verify its status under "Nodes" section.

  • You can follow this article for the same

Step 1: Create two instances, master and worker.

Step 2: Connect master two Jenkins

Step 3: Click on Setup agent

Step 4: Give a node name and tick on permanent agent and create

Step 5: Give a description and Remote root directory as " /home/ubuntu " and Give the label "dev" 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

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

cd .ssh/
ssh-keygen

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

Step 7: Go to id_rsa.pub

cat jenkins-key.pub

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

Step 7: now come to the Jenkins credentials section 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

Step 8: Go to the master-server terminal and open ssh private key like this

cat id_rsa

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

Now credential add this

Step 9: In the Host key verification strategy select this and save

here you can see the both node is connected

Task-02

  • 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

here you can see we have successfully runed out pipeline

Let's recreate days 27 task in it

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/nallabellisriparthu/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

Now we have completed both task01 and task02 of day 28...


Happy Learning

Thanks For Reading! :)

-Sriparthu💝💥