Day 28 of #90DaysOfDevOps || Jenkins Agents

Day 28 of #90DaysOfDevOps || Jenkins Agents

·

4 min read

Jenkins master slave architecture

The master-slave architecture in Jenkins allows for greater scalability and flexibility in managing builds across multiple machines. The master node controls the distribution of build tasks to the slave nodes, which can run on different operating systems or hardware configurations.

To set up a Jenkins master-slave configuration, you need to install the Jenkins server and then configure one or more slave nodes to connect to the master. Once configured, the master can delegate build tasks to the slave nodes, allowing for parallel processing of build jobs.

Overall, the Jenkins master-slave architecture is a powerful tool for managing large-scale builds and deployments, allowing for faster build times and improved efficiency in software development workflows.

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.


Task-01

  • Create an agent by setting up a node on Jenkins

  • Create a new 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.

  • So at start We need to create a new instance in cloud

  • Now on jenkins platform go to manage jenkins -> manage node and cloud

  • Then there is option to create a new node. A new page will open fill the name and select as permanent agent and click create for now.

  • Now a new configuration page will open fill details a description, Number of executors (that is at a time how many pipelines can be build {default: 1}), in the remote root directory set a path where projects will be executed to build over the instance, Label this node which will be used in pipeline syntax to set agent selection and now in launch method choose launch via ssh.

  • And in Host set the IP of the instance and select credentials and create new credentials, for that go to instance set ssh-keygen and copy the private key and in jenkins prompt paste it in the credentails secret key

  • And in username set the user of the instance

  • Now on instance we need to setup some configurations

  • First of we need to hit command sudo apt-get install ssh . Then sudo /etc/init.d/ssh start .

  • Also instal java prefered version 11

  • Also set public keys to authorized keys for ssh from jenkins master cd .ssh cat id_rsa.pub > authorized_keys

  • Now back to jenkins master set the credentails and set host key verification strategy to mannually set key verification strategy. And now hit save

  • A new page will appear. Before hitting launch agent. Set trusted ssh hosted key and allow them

  • Now it the laumch agent. And wait for the connection to be successfull on succesfull connection you will see

After agent connected

Now we will use this nagent to build a pipeline with mentioning it as agent label in pipeline synatx.

So, the synatx will become

pipeline {
    agent {
        label "node1"
    }

    stages {
        stage("Docker compose") {
            steps{
                sh '''
                ls
                sudo docker compose down
                sudo docker compose up -d
                sudo docker compose ps
                '''
            }
        }

    }
    post{
        always{
            echo "========always========"
        }
        success{
            echo "========pipeline executed successfully ========"
        }
        failure{
            echo "========pipeline execution failed========"
        }
    }
}

We have mentioned label as node1 to the agent attached. so now when we hit the build of this code it will execute on worker node

We can see here the worker node is the one where deployment started.

So this is how we can setup jenkins agent and use with pirpline syntax

End of Post


Reach me on:

  1. Github-> github.com/Hrmn97

  2. Twitter -> twitter.com/Harman9765

  3. LinkedIn -> linkedin.com/in/chetan-harman-56310424a

  4. Website -> devhrmn.netlify.app