</>

Technology

Jenkins

Difficulty

Intermediate

Interview Question

What are Jenkins agents and how does Master-Agent (Master-Slave) architecture work?

Answer

Jenkins Master-Agent Architecture

Architecture Overview

CODE
┌─────────────────────────────────────────────────────────┐
│                    Jenkins Master                        │
│                                                         │
│  - Web UI & REST API           - Job scheduling         │
│  - Plugin management           - Build queue            │
│  - User authentication         - Monitors agents        │
│  - Configuration storage       - Distributes work       │
│                                                         │
│  Communication: SSH / JNLP (Java Web Start)            │
│         ┌────────────┬────────────┬────────────┐        │
│         ↓            ↓            ↓            ↓        │
│    Agent-Linux   Agent-Win   Agent-Docker  Agent-Mac    │
│    Selenium      IE tests    Build image   iOS tests    │
│    Tests         Tests                                  │
└─────────────────────────────────────────────────────────┘

Jenkins Master Responsibilities

  • Hosts the Jenkins web interface
  • Stores all job configuration
  • Manages plugins and credentials
  • Schedules jobs and assigns them to available agents
  • Should NOT run builds — delegates to agents

Jenkins Agent Responsibilities

  • Executes the actual pipeline steps
  • Has the required tools (JDK, Maven, Node.js, Chrome, etc.)
  • Reports results back to master
  • Can be on different OS (Linux, Windows, macOS)

Defining Agents in Jenkinsfile

GROOVY
// Run on any available agent
pipeline {
    agent any
    ...
}

// Run on a specific label (e.g., agent with "selenium" label)
pipeline {
    agent { label 'selenium-agent' }
    ...
}

// Run in a Docker container (agent-less infrastructure)
pipeline {
    agent {
        docker {
            image 'maven:3.9-openjdk-17'
            args  '-v /root/.m2:/root/.m2'  // cache Maven deps
        }
    }
    ...
}

// Run on no agent at top level — define per stage
pipeline {
    agent none

    stages {
        stage('Build on Linux') {
            agent { label 'linux' }
            steps { sh 'mvn clean package' }
        }
        stage('Test on Windows') {
            agent { label 'windows' }
            steps { bat 'mvn test' }
        }
    }
}

Adding an Agent to Jenkins

SSH Agent Setup

  1. Jenkins → Manage Jenkins → Manage Nodes → New Node
  2. Name: selenium-agent-01
  3. Remote root directory: /home/jenkins
  4. Labels: selenium chrome linux
  5. Launch method: Launch agents via SSH
  6. Host: 192.168.1.50
  7. Credentials: SSH key pair

JNLP Agent (Agent connects to master)

Bash
# On agent machine:
java -jar agent.jar \
  -jnlpUrl http://jenkins-master:8080/computer/agent-01/slave-agent.jnlp \
  -secret <secret-key> \
  -workDir /home/jenkins

Parallel Stages Across Agents

GROOVY
pipeline {
    agent none
    stages {
        stage('Parallel Tests') {
            parallel {
                stage('Chrome Tests') {
                    agent { label 'agent-chrome' }
                    steps { sh 'mvn test -Dbrowser=chrome' }
                }
                stage('Firefox Tests') {
                    agent { label 'agent-firefox' }
                    steps { sh 'mvn test -Dbrowser=firefox' }
                }
                stage('API Tests') {
                    agent { label 'agent-api' }
                    steps { sh 'mvn test -Dsuite=api' }
                }
            }
        }
    }
}

Follow AutomateQA

Related Topics