Controller and agent architecture

Why the controller has no executors, and what a build can reach on an agent.

Beginner12 min · lesson 2 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (17 files, 7 KB): arch.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/arch.tar.gz && tar -xzf arch.tar.gz, which creates ~/lab/arch/. SHA-256: b1b4a99ab17cf7855b5bb350ee5aa2f5d20cceb513684473efac2578d94e529a

A pull request adds one line to a build script: cat /var/jenkins_home/secrets/master.key. If that build runs on the Jenkins controller, the line works, and so does anything else an attacker cares to add. Every credential Jenkins stores, every job configuration and every other team's secrets sit on that machine, readable by the account the build runs as.

That's why Jenkins splits into a controller and agents, and why the controller in this lab runs no builds at all. You'll prove it here by trying.

Two roles

Who does what
Controller
Web UI and REST API
where people and tools connect
Scheduling
decides which build goes where
JENKINS_HOME
jobs, build history, credentials, secrets
Agent
Executors
slots that run build steps
Workspace
a checkout of the code
Build tools
compilers, test runners, Cosign
The agent opens the connection to the controller, so agents can sit behind a firewall that blocks inbound traffic.

An executor is a slot for one piece of work on a node. The number of executors on a node is how many things it does at once. Set the controller's number to zero and it can't run a build step such as sh, whatever a Jenkinsfile asks for. That's the first line of the lab's configuration: numExecutors: 0.

One thing still runs on the controller in every pipeline: the pipeline's own Groovy code. The controller interprets the Jenkinsfile for the whole run, in a lightweight executor that doesn't count against numExecutors, and sends each step to an agent. So the Groovy sandbox, which a later lesson covers, is what limits that code, and heavy logic in script {} blocks costs controller CPU and memory.

A controller alone does no work

Start the lab without its agent and ask Jenkins what nodes it has:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ ./up.sh --no-agent
Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./jk.sh '/computer/api/json?tree=computer[displayName,numExecutors,offline]' | jq -r '.computer[] | "\(.displayName) executors=\(.numExecutors) offline=\(.offline)"'
Built-In Node executors=0 offline=false lab-agent executors=2 offline=true
$ ./seed.sh app app/ && ./newjob.sh where app && ./newjob.sh on-controller app Jenkinsfile.controller
pushed app: 4fa739d first commit job where reads Jenkinsfile from git://git/app.git job on-controller reads Jenkinsfile.controller from git://git/app.git

The controller appears as Built-In Node with no executors. The agent is defined but offline. Two jobs now exist: where runs on any node labelled linux, and on-controller asks for the controller by its label, built-in. Run the first one with a 15-second limit:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ BUILD_WAIT=15 ./run-job.sh where
Started by user admin waiting: ‘lab-agent’ is offline build #1 still running after 15s: stopping it

Jenkins accepted the build and started it, and then nothing happened. The pipeline's Groovy started on the controller, and it waits for a real executor when it reaches agent. The waiting: line is the queue's own explanation. Exit status 3 is how our script reports that it gave up waiting. On a real server, a build that sits like this usually means a label with no matching online agent, so the queue reason is the first thing to read.

Agents have to prove who they are

An agent connects with a name and a secret that the controller generated for that name. Try connecting with a made-up secret:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ docker compose --progress quiet run -d --name lab-bad-agent --no-deps -e JENKINS_SECRET=0000000000000000000000000000000000000000000000000000000000000000 agent >/dev/null; sleep 12; docker logs lab-bad-agent 2>&1 | grep -m1 "refused by remote"; echo "refusals in 12 s: $(docker logs lab-bad-agent 2>&1 | grep -c "refused by remote")"; docker rm -fv lab-bad-agent >/dev/null
INFO: [JNLP4-connect connection to controller/172.18.0.3:50000] Local headers refused by remote: Authorization failure refusals in 12 s: 2

The controller refused the connection with Authorization failure, and the agent kept trying again, as the count shows. The secret matters because a machine that connects as your agent receives build steps, including whatever credentials those steps use. Now connect the real agent. agent.sh fetches the correct secret through the authenticated API:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ ./agent.sh
agent lab-agent: online
$ ./run-job.sh where
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/where + hostname 3d34cbbaa256 + id -un jenkins + pwd /home/jenkins/agent/workspace/where + ls /var/jenkins_home ls: cannot access '/var/jenkins_home': No such file or directory + echo no JENKINS_HOME on this machine no JENKINS_HOME on this machine Finished: SUCCESS build #2 result: SUCCESS

Our script stopped build 1 when it gave up waiting, so this is build 2, and this one ran. hostname is the agent container's ID, the account is jenkins, and the workspace is under /home/jenkins/agent. Most importantly, /var/jenkins_home doesn't exist there. A build step on this agent can't read the controller's files, because they're on a different machine.

What the separation protects

Here's what a build would have reached on the controller:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ docker compose exec -T controller ls /var/jenkins_home/secrets | head -4
hudson.console.ConsoleNote.MAC hudson.model.Job.serverCookie hudson.model.User.DIRNAMES hudson.util.Secret

hudson.util.Secret is one of the keys that decrypts stored credentials. Now try to run the second job, which asks for the controller directly:

ubuntu@secopslog-cicd:~/lab/arch · Jenkins 2.580.2
$ BUILD_WAIT=15 ./run-job.sh on-controller
Started by user admin waiting: Waiting for next available executor on ‘Built-In Node’ build #1 still running after 15s: stopping it

It waits, and goes on waiting, even with a healthy agent online, because the only node with the label built-in has no executors. Keeping numExecutors: 0 turns "don't build on the controller" from a rule people must remember into something Jenkins enforces. Clean up with ./down.sh.

Watch out
Agents aren't a complete security boundary. A build on an agent can read that agent's workspaces and any secret passed to another build there, and it can tamper with tools for the next build. Use separate agents for separate trust levels, and prefer disposable ones. The last lesson comes back to this. Also note that in the lesson labs the controller and agent share one Docker host; in production they'd be separate machines.

Hands-on

In a fresh lab (./up.sh, ./seed.sh app app/, then ./newjob.sh where app), edit casc.d/00-base.yaml so the agent has labelString: linux fast, then run docker compose --progress quiet up -d --force-recreate --wait controller (this recreates Jenkins with the new configuration; the next lesson explains it) and ./agent.sh. Change app/Jenkinsfile to request label 'gpu', push it with ./commit.sh, and run it with BUILD_WAIT=15 ./run-job.sh where. Read the waiting: line, then change the label to fast and run it again. Finish with ./down.sh.

What you should see: with gpu the build starts and then waits, and the queue explains why: There are no nodes with the label ‘gpu’. run-job.sh stops it after 15 seconds and exits 3. After the change to fast, the next build runs on lab-agent and finishes SUCCESS, because the agent now carries both labels.

Remember

The on-controller job could wait forever and never read secrets/, because the controller has no executors to give it. When a build sits still with no error, read the queue reason first.

Quick check
01A build's console shows Started by user admin and then nothing for ten minutes. CPU on the controller is idle. What do you check first?
Incorrect — A stalled checkout is possible, but the queue states directly when a build is waiting for a node, so look there first.
Correct — A pipeline can start and then wait for an executor, and /queue says why, usually a label with no online agent.
Incorrect — An idle controller and an empty console do not point at memory. The queue is a cheaper first check.
Incorrect — A syntax error fails the build quickly with a compile error. It does not leave it waiting.
02A team sets numExecutors: 0 on the controller. A Jenkinsfile then uses agent { label 'built-in' }. What happens?
Incorrect — A label selects among nodes with free executors. It cannot create an executor.
Incorrect — The file is valid and the label exists. The problem appears only when scheduling.
Incorrect — Jenkins does not fall back to a different label. That would break the point of labels.
Correct — This is the behaviour you want: nothing a Jenkinsfile says can run a step on the controller.
03Why is it a problem if someone obtains an agent's connection secret?
Correct — The controller sends work to whatever authenticates as that agent, including steps using bound credentials.
Incorrect — The agent secret authenticates only that agent connection. It is not a user password.
Incorrect — The agent secret is derived on the controller, and it does not decrypt stored credentials.
Incorrect — The outbound connection is exactly what the secret authenticates. Knowing it is what allows impersonation.

Related