This is#90DaysofDevopschallenge under the guidance ofShubham Londhesir.
Welcome to Day 28 of our journey! Today, we're diving into the world of Jenkins agents. If you've been wondering how to scale up your Jenkins setup or streamline your workflow, you're in the right place. Let's roll up our sleeves and explore the ins and outs of Jenkins agents together.
Understanding Jenkins Agents
At the heart of Jenkins lies the master server, orchestrating the execution of pipelines and managing job workflows. However, as projects grow in complexity and scale, a single master may become overwhelmed. Enter Jenkins agents โ the distributed workforce that executes tasks delegated by the master.
Jenkins Master (Server): The Jenkins master serves as the central control hub, housing configurations and coordinating pipeline workflows, job scheduling, and monitoring.
Jenkins Agent: An agent, whether a physical machine or container, connects to the Jenkins master to execute tasks defined in jobs. Each agent is assigned a unique label, allowing for targeted job execution.
Preparing for Deployment
Before diving into the agent setup, ensure you have the following prerequisites in place:
A fresh Ubuntu 22.04 Linux installation for the Jenkins agent.
Java, matching the version on the Jenkins master server, and Docker installed on the agent.
Access to the Jenkins master server for SSH key pair generation and Jenkins installation.
Task-01: Setting Up a Jenkins Agent
Before we begin, let's establish that we already have a Jenkins master server where we've practiced older blogs on Day 26 and Day 27. Today, our focus will be on creating a Jenkins agent server. However, keep in mind that you need to create two instances โ one for the Jenkins master server (existing) and one for the Jenkins agent server.
Now, let's proceed with setting up the Jenkins agent server:
Create a New AWS EC2 Instance for Jenkins Agent:
Go to the AWS Console and start a new EC2 instance named
jenkins-server
.Ensure that the instance meets the requirements for Java and Jenkins installation.
Install Java and Jenkins on Jenkins Agent:
Log in to the Jenkins agent server.
Install Java and Jenkins following the official installation instructions for Ubuntu.
sudo apt update sudo apt install fontconfig openjdk-17-jre
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \ https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins
Start Jenkins
sudo systemctl enable jenkins sudo systemctl start jenkins sudo systemctl status jenkins
Generate SSH Key Pair on Jenkins Master:
Open your terminal on the Jenkins master server.
Use the command:
ssh-keygen -t rsa
Follow the prompts to specify the file location and passphrase (optional).
Copy Public Key from Jenkins Master to Jenkins Agent:
Copy the public key (
id_rsa.pub
) generated in Step 2 from the Jenkins master server.Open the
id_rsa.pub
file using a text editor:cat ~/.ssh/id_rsa.pub
Copy the entire content of the
id_rsa.pub
file.
Paste the public key into the
authorized_keys
file on the Jenkins agent server.Connect to your Jenkins agent server.
Navigate to the
.ssh
directory (create it if it doesn't exist):mkdir -p ~/.ssh
Open the
authorized_keys
file using a text editor:nano ~/.ssh/authorized_keys
Paste the content of the
id_rsa.pub
file into thisauthorized_keys
file and save the changes.
Establish SSH Connection between Jenkins Master and Agent:
Use the private key (id_rsa
) generated in Step 3 on the Jenkins master server to establish an SSH connection with the Jenkins agent server.
ssh -i /path/to/private_key.pem user@agent_ip_address
Verify Node Status on Jenkins Master:
Log in to the Jenkins master server.
Navigate to "Manage Jenkins" > "Manage Nodes and Clouds" to ensure that the agent node appears under the "Nodes" section.
Now, our agent is connected
Task-02: Configure Jenkins Jobs to Run on the New Agent:
Navigate to the configurations of previous jobs built on Day 26 and Day 27. Configure the jobs to run on the newly created agent using labels. Save the job configurations.
Trigger Job Builds to Run on the Agent:
Initiate job builds to trigger execution on the agent. Monitor the job status to ensure successful execution on the agent.
Conclusion
Congratulations! You've mastered the art of Jenkins agents, unlocking the power of scalability and efficiency in your automation workflows. As you continue your journey, remember that automation is the cornerstone of modern DevOps practices, and Jenkins agents are your trusted allies in achieving seamless deployment and delivery.