I welcome you back to my DevOps blog series!
In my previous blog, we explored the power of Jenkins Declarative Pipelines integrated with Docker, simplifying the process of building and deploying applications.
In this blog, we delve into the essential topic of Jenkins Agents, examining how they enhance the efficiency and scalability of your continuous integration and continuous deployment (CI/CD) workflows.
You can check my previous blog here: "Jenkins Declarative Pipeline with Docker"
In Jenkins' distributed setup, Jenkins Agents, often referred to as Jenkins Slaves, hold a crucial position. These Agents, distinct from the Jenkins Master, act as worker nodes responsible for executing build jobs and tasks. This distributed system enables parallel execution of jobs, leading to substantial enhancements in the speed and efficiency of your pipelines.
Jenkins Master (Server)
The Jenkins server, also known as the master node, stores important configurations. Think of it as a control tower that manages the entire workflow described in your pipelines. It handles tasks like scheduling jobs and monitoring their progress.
Jenkins Agent
Jenkins Agents are individual nodes that can perform tasks on their own. They can be installed on various machines or environments, offering flexibility and scalability to your CI/CD setup.
In Jenkins, an agent, whether it's a machine or a container, connects to the Jenkins master and carries out the steps outlined in a job. When you create a Jenkins job, you specify an agent for it, identified by a label.
When you initiate a job from the Jenkins master, the actual tasks are performed on the configured agent node.
Initially, a single Jenkins installation is suitable for small teams with a limited number of projects. However, as your requirements expand, scaling up becomes essential. Jenkins offers a solution called "master to agent connection." Instead of running UI and jobs on a single system, agents handle job execution while the master manages the UI and acts as a control node.
Prerequisites for Setting Up Jenkins Agents
Java Runtime Environment (JRE): Make sure Java is installed on the machine where the Jenkins Agent will be set up.
Jenkins Master Configuration: Configure the Jenkins Master to enable connections from Agents.
Network Connectivity: Ensure smooth communication by verifying network connectivity between the Master and Agents.
Agent Installation: Install Jenkins Agent software on the specified machines, considering tools like Docker for streamlined setup.
Task 01
Set up a master server on an EC2 instance named "devops." Ensure to install Jenkins and all necessary dependencies on this node.
Set up an agent server. We've created an instance named "agent-1."
Ensure that Java and the necessary dependencies are installed on this node.
sudo apt-get update
sudo apt-get install fontconfig openjdk-11-jre
sudo apt install
docker.io
sudo apt install docker-compose
sudo usermod -aG docker $USER
newgrp docker
Establishing a connection between the master and agent involves SSH and the exchange of public-private key pairs.
On the "devops" master instance, running ssh-keygen
will generate both public and private keys. These keys can be located in the .ssh folder, which is hidden.
Retrieve the public key from the master node, located in the file named "id_rsa.pub".
Navigate to the .ssh folder on the agent node and insert the public key from the master node into the "authorized_keys" file.
On the Jenkins Server. Configure the agent node by accessing "Manage Jenkins" > "Manage Nodes and Clouds" > "New Node."
To know about setting up a Jenkins server, refer to my previous blog on "Getting Started with Jenkins"
Fill in the details required.
Enter the public IP address of the agent node in the "host" option. For credentials, click on "Add" and then select "Jenkins."
When adding credentials, ensure the following:
In this step, we need to copy the private key from the master node and paste it into the designated key option.
Choose the options below and then click on "Save".
After the creation, you will observe that the agent is now online.
Task 02
Run the jobs that were built in the previous blog posts on the recently set up agent to ensure their seamless execution and validate the functionality of the new agent.
Refer to my previous blog here: "Jenkins Declarative Pipeline" and "Jenkins Declarative Pipeline with Docker"
Utilize labels to specify the agent, enabling your master server to initiate builds on the designated agent server.
Since we labeled our agent node as "agent1," let's modify the code and run the previous pipelines accordingly.
Therefore, we have efficiently established an agent node and executed pipelines on it.
Conclusion
Integrating Jenkins Agents into your CI/CD pipelines brings added flexibility, speed, and efficiency. By harnessing the potential of distributed computing, you can notably cut down on build times and enhance overall development productivity. It's crucial to grasp and utilize Jenkins Agents effectively for a strong and efficient CI/CD setup.
I hope you found this blog insightful! If you have any questions or want to discuss Jenkins, DevOps, or any related topics, feel free to connect with me on LinkedIn. Let's continue the conversation and explore the exciting world of DevOps together.
Happy Learning :)