# Day 33 Jenkins Agents

# Day 33 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.

/*Jenkin master server requires both Jenkin & Jave to be installed. But in Agent servers, only Java needs to be installed on the job which is sent by the Jenkin server.*/

How do we connect Agent servers?

Connection to the agent will be done through SSH [Secure Shell].In Jenkin server will generate ssh-keygen and will share the public key to the agent servers and the private key to the master server.

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.

  • Create 2 EC2 instances in AWS master and agent.

  • Generate SSH key on the Jenkins-master using ssh-keygen command.

  • Then on the Jenkins-agent server, paste the public key from the Jenkins-master in the authorized_keys file.

  • Make sure to install Java on the agent server.

    sudo apt-get update sudo apt-get install OpenJDK-11-jre

  • Now from the Jenkins master, we can connect the Jenkins agent server

  • Now go to the Jenkins Dashboard and click on Manage Jenkins.

  • Two types of build:

  • 1)Simple build: Build,deploy,test jobs on Jenkin servers

  • 2) Distributed build: master server and multiple agent servers. from master sending the task to agents.

  • Now click on Nodes, and click on "+ New Node"

  • Enter the node name and select type and click on create.

  • Add details of the agent node.

  • Now click "OK" and Node will be connected and online.

  • Install docker and docker-compose for future requirements.

    sudo apt-get install docker.io docker-compose sudo usermod -aG docker $USER sudo restart

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.

  • Now create a new item with the pipeline.

  • Give it a description and put the GitHub repo URL.

  • Now we have to pass the pipeline script in the pipeline to test the script.

    COPY

         pipeline {
             agent { label "dev" }
             stages{
                 stage("Clone Code"){
                     steps{
                         git url: "https://github.com/shreeegupta/node-todo-cicd.git", branch: "master"
                     }
                 }
                 stage("Build and Test"){
                     steps{
                         sh "docker build . -t node-app-new"
                     }
                 }
                 stage("Push to Docker Hub"){
                     steps{
                         echo "Push the image to docker hub"
                     }
                 }
                 stage("Deploy"){
                     steps{
                         sh "docker-compose down && docker-compose up -d"
                     }
                 }
             }
         }
    
  • Now go to Manage Jenkins > Manage Plugin and install the "Environment Injector" and Download and install after restart Jenkins.

  • Now go to Manage Jenkins > Credentials > System > Global Credentials > globals > + Add Credentials

  • Add details and click on create.

  • Now configure the pipeline for pushing the image into the docker hub.

    COPY

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

  • Open 8000 port on jenkins-agent, now the app can be accessed from the browser.

  • Our application image is also pushed to DockerHub.

Set Up the Node app using Pipeline Script from SCM

  • We can build the application using the Pipeline script from SCM by passing the GitHub URL where "Jenkinsfile" is present in the repository.

  • Now in the pipeline script, use "Pipeline script from SCM"

  • Now the application is up & running and there is a new step added by Jenkins, Declarative Checkout SCM.

Thank you for reading!!
~Mecrin D Luvis

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #jenkins #jenkinsagents #jenkinsprojects