Jenkins Agents

Jenkins Agents

·

4 min read

Jenkins Master

The Jenkins master server is the core component that manages configurations, scheduling jobs, and monitoring tasks.

It acts as a control server that coordinates workflows defined in pipelines. The master node handles interactions with agents, scheduling jobs to be executed on them, and collecting results afterwards.

Jenkins Agent

A Jenkins agent is a machine or container that connects to the Jenkins master server. Agents are responsible for executing the steps outlined in a Jenkins job. When creating a Jenkins job, it's necessary to assign an agent to it. Each agent is labeled to provide a unique identifier.

When a Jenkins job is triggered from the master, the execution takes place on the agent node specified in the job configuration. This distribution of work ensures that the master node doesn't get overwhelmed and can focus on controlling the overall process.

For teams with growing needs, Jenkins offers the ability to scale using a "master-to-agent connection." This approach allows agents to handle job execution, while the master concentrates on serving the Jenkins UI and managing the workflow.

Task 01: Create a Jenkins Agent Node

  1. Launched two instances - One as a Jenkins Master Node and another as an agent.

  2. Install Jenkins on the Master EC2 instance.

  3. Create private and public keys via ssh-keygen to connect Jenkins Master and Agent server via SSH.

  4. Copy the Public key from the Master Server to the Agent Server in the below file.

    cd .ssh/authorized_keys

  5. Log in to your Jenkins dashboard.

  6. Navigate to "Set up an agent".

  7. Click on "New Node" or "New Agent."

  8. Assign a name to the agent, such as "Todo-App-Dev" or "Docker-Agent".

  9. Opt for the "Permanent Agent" option.

  10. Choose "Launch agents via SSH" under "Launch method."

  11. Copy the Master Private key.

  12. Define the agent's root directory for storing files.

  13. Save the agent configuration. Here is the successful console output.

  14. Install Jenkins on the master.

     sudo apt update
     sudo apt install openjdk-11-jre
     #Install Jenkins
     curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
       /usr/share/keyrings/jenkins-keyring.asc > /dev/null
     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
     #Start jenkins
     sudo systemctl enable jenkins
     sudo systemctl start jenkins
     sudo systemctl status jenkins
    
  15. Install Docker, docker-compose and Java on Jenkins-Slave.

     sudo apt install docker.io
     sudo apt install docker-compose -y
     sudo apt install openjdk-11-jre
     sudo usermod -aG docker $USER
     sudo reboot
    

Task 02: Run a Jenkins Job on Agent Node

  1. From the Jenkins dashboard, click on "New Item" to create a new project.

  2. Enter a project name and select "Pipeline" Click "OK" to create the project.

  3. Under the Configuration, Select the GitHub Project checkbox -> Enter the URL and Enabled the GitHub hook trigger for GITScm polling.

GitHub WebHooks Setup:

  1. Accessed my repository on GitHub and go to Settings -> Webhooks

  2. Added a new webhook with my Jenkins job's URL Selected events to trigger (e.g., push, pull request).

  3. Saved the webhook.

Create Environment variable credential for docker hub:

  1. Click on "manage jenkins" > Credentials.

  2. Click on "Systum" > "Global Credentials(unrestricted)" > "Add Credentials".

  3. Add the docker hub credentials and click on create

Running the Application:

  1. Install docker and docker-compose on the server. Add the current user to the docker group:

      sudo usermod -aG docker $USER 
      sudo reboot
    
  2. Add the Jenkins user to the docker group:

       sudo usermod -aG docker jenkins
       sudo systemctl restart jenkins
    
  3. Inside the Dashboard "Node-app" job > Within the Jenkins job's Pipeline build step.

       pipeline {
           agent {label 'todo-dev'}
    
           stages{
               stage('Clone Code'){
                   steps {
                       echo "Cloning the code"
                       git url:"https://github.com/ajaygite/node-todo-app.git", branch:"master"
                   }
               }
               stage('Build'){
                   steps {
                       echo "Building the Image"
                       sh "docker build -t todo-app ."
                   }
               }
               stage('push to dockerhub'){
                   steps {
                       echo "Pushing the code to dockerhub"
                       withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                       sh "docker tag todo-app ${env.dockerHubUser}/node-app:latest"
                       sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                       sh "docker push ${env.dockerHubUser}/node-app:latest"
                       }
                   }
               }
               stage('Deploy'){
                   steps {
                       echo "Deploying the application"
                       sh "docker-compose down"
                       sh "docker-compose up -d --build"
    
                   }
               }
           }
       }
    

  4. Save and click on Build Now.

  5. Once done hit the URL ipaddress:8000 to check if it is running.