Bird
Raised Fist0
Jenkinsdevops~5 mins

Master-agent architecture in Jenkins - Commands & Configuration

Choose your learning style10 modes available
Introduction
Jenkins uses a master-agent setup to split work between a main server and multiple worker machines. This helps run many tasks at once without slowing down the main server.
When you want to run multiple builds or tests in parallel to save time.
When your main Jenkins server should stay free to manage jobs and not run heavy tasks.
When you have different machines with special tools or environments for specific jobs.
When you want to add more workers easily to handle more jobs as your team grows.
When you want to isolate jobs so one failing task does not affect others.
Config File - agent-config.xml
agent-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<slave>
  <name>example-agent</name>
  <description>Agent for running builds</description>
  <remoteFS>/home/jenkins</remoteFS>
  <numExecutors>2</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.slaves.JNLPLauncher">
    <workDirSettings>
      <disabled>false</disabled>
      <internalDir>remoting</internalDir>
      <failIfWorkDirIsMissing>false</failIfWorkDirIsMissing>
    </workDirSettings>
  </launcher>
  <label>linux</label>
  <nodeProperties/>
</slave>

This XML file configures a Jenkins agent (worker).

name: The agent's name.

remoteFS: The folder on the agent machine where Jenkins stores files.

numExecutors: How many jobs the agent can run at once.

launcher: How the agent connects to the master (here using JNLP).

label: A tag to select this agent for specific jobs.

Commands
This command starts the Jenkins agent on the worker machine. It connects to the Jenkins master using the JNLP protocol with a secret key for security.
Terminal
java -jar agent.jar -jnlpUrl http://jenkins.example.com/computer/example-agent/slave-agent.jnlp -secret 1234567890abcdef -workDir "/home/jenkins/agent"
Expected OutputExpected
[Jenkins agent] Started Connecting to http://jenkins.example.com/computer/example-agent/slave-agent.jnlp Connected Waiting for tasks...
→
-jnlpUrl - URL to connect the agent to the Jenkins master
→
-secret - Security token to authenticate the agent
→
-workDir - Directory on the agent machine for Jenkins files
This command removes the agent named 'example-agent' from the Jenkins master. Useful to clean up agents no longer needed.
Terminal
curl -X POST http://jenkins.example.com/computer/example-agent/doDelete
Expected OutputExpected
No output (command runs silently)
Starts the Jenkins agent in the background so it keeps running even after closing the terminal.
Terminal
java -jar agent.jar -jnlpUrl http://jenkins.example.com/computer/example-agent/slave-agent.jnlp -secret 1234567890abcdef -workDir "/home/jenkins/agent" &
Expected OutputExpected
[Jenkins agent] Started Connecting to http://jenkins.example.com/computer/example-agent/slave-agent.jnlp Connected Waiting for tasks...
→
& - Run the process in the background
Key Concept

If you remember nothing else from this pattern, remember: Jenkins master manages jobs and agents run the jobs, allowing parallel and isolated work.

Common Mistakes
Starting the agent without the correct secret key.
The agent will fail to connect because Jenkins master rejects unauthorized agents.
Use the exact secret key provided by Jenkins for that agent when starting it.
Running multiple agents on the same machine without adjusting numExecutors.
Agents may overload the machine or Jenkins may not schedule jobs properly.
Set numExecutors to match how many jobs the machine can handle simultaneously.
Not running the agent in the background on the worker machine.
Agent stops when the terminal closes, interrupting builds.
Use '&' or a service manager to keep the agent running continuously.
Summary
Configure agents with XML files specifying connection and capacity details.
Start agents on worker machines using the java -jar agent.jar command with proper flags.
Use agents to run jobs in parallel, keeping the Jenkins master free to manage tasks.

Practice

(1/5)
1. What is the main role of the Jenkins master in the master-agent architecture?
easy
A. To run jobs on different machines
B. To control and schedule jobs
C. To store job logs only
D. To act as a backup server

Solution

  1. Step 1: Understand the master role

    The Jenkins master is responsible for controlling and scheduling jobs, meaning it decides when and where jobs run.
  2. Step 2: Differentiate from agent role

    Agents actually run the jobs on separate machines, not the master.
  3. Final Answer:

    To control and schedule jobs -> Option B
  4. Quick Check:

    Master controls jobs = B [OK]
Hint: Master schedules jobs; agents run them [OK]
Common Mistakes:
  • Confusing master with agent role
  • Thinking master runs jobs directly
  • Assuming master only stores logs
2. Which of the following is the correct way to define an agent in a Jenkins pipeline script?
easy
A. run agent
B. master agent
C. agent any
D. node master

Solution

  1. Step 1: Recall Jenkins pipeline syntax

    In Jenkins declarative pipeline, agent any means the job can run on any available agent.
  2. Step 2: Check other options

    Options like master agent, run agent, and node master are not valid syntax for defining agents.
  3. Final Answer:

    agent any -> Option C
  4. Quick Check:

    Correct agent syntax = A [OK]
Hint: Use 'agent any' to run on any available agent [OK]
Common Mistakes:
  • Using invalid keywords like 'master agent'
  • Confusing 'node' with 'agent'
  • Writing 'run agent' which is not valid
3. Given this Jenkins pipeline snippet, what will happen?
pipeline {
  agent { label 'linux' }
  stages {
    stage('Build') {
      steps {
        echo 'Building on Linux agent'
      }
    }
  }
}
medium
A. The job runs on any available agent
B. The job runs on the master node
C. The job fails due to syntax error
D. The job runs on an agent labeled 'linux'

Solution

  1. Step 1: Analyze the agent label

    The pipeline specifies agent { label 'linux' }, so it will run on an agent with the label 'linux'.
  2. Step 2: Understand job execution

    Since the label is specific, the job will not run on any agent or the master unless they have that label.
  3. Final Answer:

    The job runs on an agent labeled 'linux' -> Option D
  4. Quick Check:

    Agent label 'linux' = C [OK]
Hint: Agent label selects specific machine for job [OK]
Common Mistakes:
  • Assuming 'agent any' runs here
  • Thinking job runs on master by default
  • Believing syntax causes failure
4. You configured an agent but Jenkins shows it as offline. What is the most likely cause?
medium
A. Agent machine is turned off or unreachable
B. Master node is overloaded
C. Pipeline syntax error in Jenkinsfile
D. Agent label does not match any job

Solution

  1. Step 1: Understand agent offline status

    An agent shows offline if Jenkins cannot connect to it, often because the machine is off or network unreachable.
  2. Step 2: Eliminate other causes

    Master overload or pipeline syntax errors do not cause agent offline status. Label mismatch prevents job assignment but does not make agent offline.
  3. Final Answer:

    Agent machine is turned off or unreachable -> Option A
  4. Quick Check:

    Offline agent = unreachable machine [OK]
Hint: Offline agent means machine unreachable or off [OK]
Common Mistakes:
  • Blaming master overload for offline agent
  • Confusing syntax errors with connectivity
  • Thinking label mismatch causes offline
5. How can you improve Jenkins performance when running many jobs simultaneously using master-agent architecture?
hard
A. Add more agents to distribute the workload
B. Run all jobs on the master node to avoid delays
C. Disable agents to reduce network traffic
D. Use only one agent with high CPU

Solution

  1. Step 1: Understand workload distribution

    Adding more agents allows Jenkins to run jobs in parallel on different machines, improving performance.
  2. Step 2: Evaluate other options

    Running all jobs on master overloads it, disabling agents reduces capacity, and one agent limits parallelism.
  3. Final Answer:

    Add more agents to distribute the workload -> Option A
  4. Quick Check:

    More agents = better parallel job handling [OK]
Hint: More agents mean faster job execution [OK]
Common Mistakes:
  • Overloading master with all jobs
  • Disabling agents thinking it helps
  • Relying on single powerful agent only
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard