Day 28 Task: Jenkins Agents
On the 28th day of our Jenkins mastery journey, we delve into the concept of Jenkins Agents and explore their significance in the overall Jenkins ecosystem. Agents, also known as slaves or nodes, act as the workhorses of Jenkins, executing tasks and jobs assigned to them by the Jenkins master.
What is Jenkins Master (Server)?
Jenkins’s server or master node holds all key configurations. Jenkins master server is like a control server that orchestrates all the workflow defined in the pipelines. For example, scheduling a job, monitoring the jobs, etc.
What is Jenkins Agent ?
An agent is typically a machine or container that connects to a Jenkins master and this agent that actually execute all the steps mentioned in a Job. When you create a Jenkins job, you have to assign an agent to it. Every agent has a label as a unique identifier.
When you trigger a Jenkins job from the master, the actual execution happens on the agent node that is configured in the job.
A single, monolithic Jenkins installation can work great for a small team with a relatively small number of projects. As your needs grow, however, it often becomes necessary to scale up. Jenkins provides a way to do this called “master to agent connection.” Instead of serving the Jenkins UI and running build jobs all on a single system, you can provide Jenkins with agents to handle the execution of jobs while the master serves the Jenkins UI and acts as a control node.
Why Jenkin Master Slave Architecture is imporant?
Jenkins is an open-source automation server that is widely used in the software development industry for continuous integration and continuous delivery (CI/CD) processes. The concept of Jenkins master and slave (or Jenkins controller and agent) is essential for scaling and distributing workloads in Jenkins.
Here’s why Jenkins master and slave are important:
- Scalability: Jenkins master and slave architecture allows you to scale your Jenkins environment by distributing the workload across multiple machines. The master is responsible for managing the configuration, scheduling jobs, and monitoring the overall system, while the slaves (or agents) perform the actual build and deployment tasks. This distributed architecture enables you to handle large and complex projects efficiently.
- Parallel Execution: With Jenkins master and slave configuration, you can execute multiple builds or tests concurrently on different slave machines. This parallel execution capability helps in reducing build times and increasing overall productivity. Slaves can be set up on different operating systems or with different hardware configurations, allowing you to test your software on multiple environments simultaneously.
- High Availability: By using multiple Jenkins slaves, you can achieve high availability and fault tolerance for your CI/CD pipelines. If one slave machine goes offline or experiences issues, the master can distribute the workload to the remaining available slaves, ensuring uninterrupted build and deployment processes. This setup reduces the risk of downtime and increases system reliability.
- Resource Isolation: Jenkins slaves provide a means to isolate resources for specific jobs or projects. Each slave machine can have its own set of dependencies, libraries, and configurations, enabling you to run different types of builds or tests with specific requirements without affecting the entire Jenkins environment. This isolation helps in managing complex build environments and ensures consistency across different projects.
- Security and Access Control: Jenkins master and slave architecture allows you to enforce security measures and access control. The master can be configured to restrict access to sensitive information or specific jobs, while slaves can be set up with limited permissions based on their roles. This segregation helps protect your build infrastructure and ensures that only authorized individuals have access to critical resources.
Pre-requisites
Let’s say we’re starting with a fresh Ubuntu 22.04 Linux installation. To get an agent working make sure you install Java ( same version as jenkins master server ) and Docker on it.
Note:-
While creating an agent, be sure to separate rights, permissions, and ownership for Jenkins users.
#Task-01
Create an agent by setting up a node on Jenkins. Create a new AWS EC2 Instance and connect it to master(Where Jenkins is installed). The connection of master and agent requires SSH and the public-private key pair exchange. Verify its status under “Nodes” section.
Step A) Create EC2 instances as:
“Jenkins Server”, “Jenkins-Agent1”, “Jenkins-Agent2”
Step B) Generate SSH keys on “Jenkins-server” by running “ssh-keygen”
Now goto “.ssh” folder and there will be public and private key in Jenkins-server.
Step C) Add public key from “Jenkins-server” to “Jenkins-agent-1” under location “.ssh/authorized_keys”
Step D) Now, go to the Jenkins dashboard, and click on “Manage Jenkins”.
Step E) Now go to Jenkins > Dashboard > Manage Jenkins > Manage Nodes and Clouds > Click on “+ New Node”
Name: JenkinsAgent
Description: This agent deploys a Node JS application.
Number of executors: 1
Remote root directory: /home/ubuntu
label: agent_node1
Usage: Only build jobs with label expressions matching this node
Launch method: Launch agents via SSH
Host: Public IP of Agent Node
Credentials: Select from the dropdown (More about adding credentials in the next session)
Host Verification Strategy: Non verifying Verification Strategy
The Credentials are added!