Day 28 Jenkins Agents: Your Guide to Scaling Your CI/CD Pipeline Like a Pro!
Table of contents
- ๐ฏ What's Jenkins Master? Think of it as Your Project's Command Center!
- ๐ฅ What's a Jenkins Agent? Your Helpful Kitchen Staff!
- ๐ฑ Why Do You Need Agents?
- ๐ ๏ธ Setting Things Up: The Basics
- ๐ Task 1: Creating Your First Jenkins Agent
- ๐ Task 2: Running Jobs on Your New Agent
- ๐ Troubleshooting Tips
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 ๐
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)
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 ๐ฏ
Open Jenkins dashboard
Go to
Manage Jenkins
>Manage Nodes and Clouds
Click
New Node
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
Click
Save
Step 5: Verify Connection โ
Check agent status in
Manage Jenkins
>Manage Nodes and Clouds
Your agent should show as "Connected"
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 ๐ท๏ธ
Go to your job configuration
Under "Restrict where this project can be run"
Enter
ubuntu-agent
ordocker-agent
Save configuration
Step 3: Test Your Setup ๐งช
Run your pipeline
Check Console Output to verify it's running on the agent
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! โจ
Subscribe to my newsletter
Read articles from Dhruv Moradiya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by