Jenkins Agents

Jenkins Agents

Jenkins Master (Server):

Jenkins’s server or master node holds all key configurations. The 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 executes 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.

Configuring Agent on Jenkins:

Jenkins Agent is configured in multiple ways. Jenkins agents can be launched in physical machines, virtual machines, Kubernetes clusters, and Docker images.

Basic Requirements of Become a Jenkins Agent:

a) Java Installation ( Same Version on Agent or Master)

b) Network Connection to Jenkins Installation

c) Docker (Same Version)

Task-01

Create an agent by setting up a node on Jenkins. Create a new AWS EC2 Instance and connect it to the master(Where Jenkins is installed). The connection of the master and agent requires SSH and the public-private key pair exchange. Verify its status under the "Nodes" section.

Step 1: Create two EC2 instances, One use as "Jenkins Master" and Another use as "Jenkins Server".

Step 2: Now generate SSH-Key pair on Jenkins-Master

Step 3: Copy the Public key id_rsa.pub from Jenkins-Master and paste on Jenkins-Agent inside this file ".ssh/authorized_keys ".

Before Proceeding Please make sure Jenkins and Docker are successfully installed on both machines.

for Installing and Jenkins, you can go through these Articles:

Jenkins-installation: devunnatig.hashnode.dev/jenkins-basics

Docker-Installation: devunnatig.hashnode.dev/docker

Step 4: Create a directory in a particular location for storing all the data on the Jenkins-Agent EC2 instance and give all the required permissions.

Step 5: Now Go and Access Jenkins-Master, Jenkins GUI.

Step 6: Click on Manage Jenkins --> Manage Credentials --> Add new Credentials for connecting Jenkins Master to Jenkins Agent

To create credentials you have to copy the private key and give it directly.

Follow this Article:

devunnatig.hashnode.dev/jenkins-declarative..

Step 6: Now Go to Manage Jenkins ---> Nodes for Creating Agent.

Step 7: Click on "New Node" to create a New agent.

Step 8: Give the Name to your New Agent and click on "Create".

Step 9: Use the Same Path in the Root directory, which was created in the previous step on the Jenkins-Agent Machine. Give Jenkins-Agent Machine Public-IP and use the same credentials that you created in the previous step."

Step 10: Congratulations! Now it's Successfully Created.

Task-02

Create a new pipeline and give an agent to run that. Use labels for the agent, your master server should trigger builds for the agent server.

Step 1: Create a new job

Step 2: Write a Pipeline for running that job using the newly created Agent.

pipeline {
    agent {
        label 'jenkins-agent-1'
    }

    environment {
        DOCKERHUB_CREDENTIALS = credentials('dockerhub')
    }

    stages {
        stage('Clone the repo') {
            steps {
                echo 'Cloning the repository:'
                git 'https://github.com/DevUnnati/node-todo-cicd.git'
            }
        }

        stage('Build') {
            steps {
                echo 'Building the ToDo application on Docker'
                sh 'docker build . -t jenkins-docker:latest'
            }
        }

        stage('Login to DockerHub') {
            steps {
                echo 'Login to DockerHub'
                sh "echo \$DOCKERHUB_CREDENTIALS_PSW | docker login -u \$DOCKERHUB_CREDENTIALS_USR --password-stdin"
            }
        }
    }
}

Step 3: Click on "Build Job" and Check your Console Output.

Congratulations, Your Pipeline is running Successfully.

In the Next Article, will go through the Interview Question of "Jenkins".

Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta.