Jenkins Distributed Architecture

Akshay Bhadange
5 min readJan 15, 2022

Jenkins is the most famous Continuous Integration tool and is an integral part of DevOps.

In this blog, we will look at how to configure Jenkins distributed architecture using Jenkins master-slave configuration.

Jenkins Single Node Architecture

You can refer to the below diagram to understand Jenkins's single-node architecture.

Jenkins Single Node Architecture

This single node is not enough to meet certain requirements

  • Job has to wait if all executors are busy processing builds.
  • You will most likely run out of resources when the number or the load of your projects increases.

To come back up and running with your Jenkins infrastructure you will need to enhance your server by increasing memory, Number of CPUs, etc. This is not scalable to maintain and upgrade the server as per the requirements.

Your build environment will be down, the jobs will be stopped and the whole Jenkins infrastructure will be unusable.

This Single node architecture would introduce many idle periods where all provisioned resources are assigned to your Jenkins environment are useless.

Single Node architecture introduces security issues, as Jenkins user would have full permissions on all resources and workspaces.

For all these reasons Jenkins support distributed architecture, Where workloads of building projects are delegated to multiple Jenkins agents.

Jenkins Distributed Architecture

A Jenkins agent is a Java executable that runs on a remote server and offloads projects from Master Node.

Characteristics of Jenkins Master:

Master Node is responsible for:

  1. Schedule build jobs.
  2. Dispatch Builds to the agents for actual execution.
  3. Monitor Slaves, taking them offline as required.
  4. Execute jobs if all Jenkins agents are occupied.

Characteristics of Jenkins agent:

Characteristics of Jenkins agent:

  1. The Jenkins agent receives requests or projects from the master node. The job of a Jenkins agent is to do as they are told to.
  2. The project can be configured to always run on the particular agent
  3. The master node will use its resources to handle HTTP requests and manage the build environment. The actual Execution of builds will be delegated to the agents.

With this configuration, it is possible to horizontally scale Jenkins Architecture, Where Jenkins will be installed on a single Node.

You can refer to the below diagram to understand Jenkins’s multi-node or Distributed architecture.

Jenkins Distributed Architecture

Now we will set up Jenkins Agent on AWS EC2 and integrate it with Jenkins Master.

Step 1:

Jenkins Master should be running on one server

Step 2:

  • Create New EC2 Instance for Jenkins Agent
  • In order to the agent node, the Jenkins agent runs a program to establish bi-directional communication with Master.

There are different methods to establish a connection between Master and Agent:

  1. SSH Connector
  2. Inbound Connector
  3. Custom Script
  • We will use SSH connector, because Jenkins has a built-in SSH client implementation, using which Jenkins master can easily establish a connection with Agents using authorized keys.
  • Create SSH Keys using ssh-keygen command on new EC2 instance.

Step 3:

  • As mentioned above Jenkins Agent is Java Executable, so we need to install JDK (Java Development Kit) on the Jenkins agent.

Step 4:

  • SSH into EC2 instance where your Jenkins Master is deployed.
  • Add SSH keys from Master to Jenkins Agent sudo cat ~/.ssh/id_rsa.pub .
  • Copy the output of the above command.

Step 5:

  • SSH into EC2 instance where your Jenkins Agent is deployed.
  • Create an empty file under the .ssh folder to copy Master Node’s SSH key sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys

Step 6:

  • SSH into EC2 instance where your Jenkins Master is deployed.
  • Test Connection with Agent Node using ssh jenkins@Jenkins_Agent_IP_ADDRESS .

Note : Public IP of Jenkins Agent must be whitelisted in Master’s Security Group.

  • Now copy the SSH keys into /var/lib/jenkins/.ssh by executing the command sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh
  • Because Jenkins master will look for keys from the above folder.
  • If you are running Jenkins Master on Docker then use the command sudo cp ~/.ssh/known_hosts <JENKINS HOME DIRCTORY>/.ssh

Step 7:

  • Create Credentials for Jenkins Agent Connection
  • Go to Jenkins Master → Manage Jenkins → Manage Credentials → Global Credentials → Add Credentials
  • The username is jenkins because we have created user jenkins on Agent Node in Step 3.
  • The private key can be copied from the master Node using command sudo cat ~/.ssh/id_rsa
  • Make Sure you copy the whole key
-----BEGIN RSA PRIVATE KEY-----
-----
-----END RSA PRIVATE KEY-----

Step 8:

  • Now register Agent Node in Jenkins Master
  • Go to Jenkins Master → Manage Jenkins → Manage Nodes
Manage Node
  • Click on New Node and enter Node Name and select Node as Permanent Agent.
  • Configure your Agent with the following values and save
  • You can select key which is created in Step 7

Step 9:

  • Launch your agent and check logs.
Agent Connection Logs

Step 10:

  • Congratulations your Jenkins Distributed System is configured. Now you can kick start building jobs.
  • You will see Jenkins master runs jobs in slave nodes.

--

--

No responses yet