Day 28 Jenkins Agents: Your Guide to Scaling Your CI/CD Pipeline Like a Pro!

Dhruv MoradiyaDhruv Moradiya
4 min read

Hey there, awesome readers! ๐Ÿ‘‹ Today we're going to talk about something super cool in the Jenkins world - Agents! Don't worry if you're new to this; I'll break it down in a way that's easy to understand. Let's dive in!

๐ŸŽฏ What's Jenkins Master? Think of it as Your Project's Command Center!

Imagine you're running a busy restaurant kitchen:

  • The Head Chef (Jenkins Master) plans all the meals ๐Ÿ“‹

  • They organize the menu (workflows)

  • They keep an eye on all the cooking (monitoring)

  • They make sure everything runs smoothly (management)

๐Ÿ‘ฅ What's a Jenkins Agent? Your Helpful Kitchen Staff!

Just like a restaurant can't run with just one chef, Jenkins needs help too! That's where Agents come in:

  • They're like your skilled cooks ๐Ÿ‘จโ€๐Ÿณ

  • Each one can handle different tasks

  • They work under the Head Chef's direction

  • They can specialize in different types of "dishes" (tasks)

๐ŸŒฑ Why Do You Need Agents?

Think of it this way:

  • Starting small? One kitchen (Jenkins server) might be enough

  • Business growing? ๐Ÿ“ˆ You'll need more hands on deck!

  • More projects = More helpers needed

  • Agents help share the workload, just like having multiple cooks in different stations

๐Ÿ› ๏ธ Setting Things Up: The Basics

Before you get started, you'll need:

  • A fresh computer (Ubuntu 22.04) for your new agent

  • Some basic tools installed (Java and Docker)

  • Proper permissions (like giving your staff the right keys to the kitchen! ๐Ÿ”‘)

๐Ÿ“ Task 1: Creating Your First Jenkins Agent

Step 1: Launch an EC2 Instance ๐Ÿš€

  1. Go to AWS Console > EC2 > Launch Instance

     Name: jenkins-agent-1
     OS: Ubuntu 22.04 LTS
     Instance type: t2.micro (free tier)
     Key pair: Create new key pair if you don't have one
     Security Group Settings:
     - Allow SSH (Port 22)
     - Allow Custom TCP (Port 8080)
    
  2. Click "Launch Instance" and wait for it to start

Step 2: Set Up Your Agent Machine ๐Ÿ–ฅ๏ธ

Connect to your EC2 instance:

chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@your-ec2-ip

Install required tools:

# Update system
sudo apt update
sudo apt upgrade -y

# Install Java (same version as Jenkins master)
sudo apt install openjdk-17-jdk -y

# Install Docker
sudo apt install docker.io -y
sudo usermod -aG docker ubuntu
sudo systemctl enable docker
sudo systemctl start docker

# Create Jenkins user
sudo useradd -m -s /bin/bash jenkins
sudo mkdir -p /home/jenkins/.ssh

Step 3: Set Up SSH Keys ๐Ÿ”‘

On your Jenkins master server:

# Generate SSH key pair
ssh-keygen -t rsa -C "jenkins-master"

# View public key
cat ~/.ssh/id_rsa.pub

On your agent machine:

# Add master's public key to authorized_keys
sudo vim /home/jenkins/.ssh/authorized_keys
# Paste the public key here

# Set correct permissions
sudo chown -R jenkins:jenkins /home/jenkins/.ssh
sudo chmod 700 /home/jenkins/.ssh
sudo chmod 600 /home/jenkins/.ssh/authorized_keys

Step 4: Configure Agent in Jenkins UI ๐ŸŽฏ

  1. Open Jenkins dashboard

  2. Go to Manage Jenkins > Manage Nodes and Clouds

  3. Click New Node

  4. Fill in the details:

     name: Agent-1
     Type: Permanent Agent
    
     # Configuration
     Remote root directory: /home/jenkins
     Labels: ubuntu-agent docker-agent
     Launch method: Launch agents via SSH
     Host: Your-EC2-IP
     Credentials: Add > SSH Username with private key
     Host Key Verification Strategy: Non verifying
    
  5. Click Save

Step 5: Verify Connection โœ…

  1. Check agent status in Manage Jenkins > Manage Nodes and Clouds

  2. Your agent should show as "Connected"

  3. Check logs if you see any issues


๐Ÿ“ Task 2: Running Jobs on Your New Agent

Step 1: Modify Your Previous Pipeline ๐Ÿ”„

Open your previous pipeline from Day 26 / Day27 and add agent specification:

pipeline {
    agent {
        label 'ubuntu-agent'  // This will run on our new agent
    }
    stages {
        stage('Build') {
            steps {
                // Your existing build steps
                sh 'docker build -t my-app .'
            }
        }
        // Other stages...
    }
}

Step 2: Configure Agent Labels ๐Ÿท๏ธ

  1. Go to your job configuration

  2. Under "Restrict where this project can be run"

  3. Enter ubuntu-agent or docker-agent

  4. Save configuration

Step 3: Test Your Setup ๐Ÿงช

  1. Run your pipeline

  2. Check Console Output to verify it's running on the agent

  3. Monitor agent's system resources

๐Ÿ” Troubleshooting Tips

Having issues? Check these common problems:

Connection Issues ๐Ÿ”Œ

# On agent:
sudo service ssh status
sudo tail -f /var/log/auth.log

# On master:
ssh -i private_key jenkins@agent-ip

Permission Issues ๐Ÿ“

# On agent:
ls -la /home/jenkins
sudo chown -R jenkins:jenkins /home/jenkins

Docker Issues ๐Ÿณ

# On agent:
sudo usermod -aG docker jenkins
sudo service docker status
docker ps  # Test docker access

Remember: The key to success is practice! Don't worry if it doesn't work perfectly the first time - troubleshooting is part of the learning process! ๐Ÿš€

Drop a comment if you get stuck or have questions! Happy automating! โœจ

0
Subscribe to my newsletter

Read articles from Dhruv Moradiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhruv Moradiya
Dhruv Moradiya