Day 28 : 🚀 Setting Up Jenkins Agents: A Step-by-Step Guide 🖥️

"I'm a 3rd-year Computer Engineering student at Marwadi University with skills in C++, web development (MERN stack), and DevOps tools like Kubernetes. I contribute to open-source projects and share tech knowledge on GitHub and LinkedIn. I'm learning cloud technologies and app deployment. As an Internshala Student Partner, I help others find jobs and courses." now currently focusing on #90DaysOfDevops
Jenkins Master (Server)
The Jenkins master server is the central control unit that manages the overall orchestration of workflows defined in pipelines. It handles tasks such as scheduling jobs, monitoring job status, and managing configurations. The master serves the Jenkins UI and acts as the control node, delegating job execution to agents.
Jenkins Agent
A Jenkins agent is a separate machine or container that executes the tasks defined in Jenkins jobs. When a job is triggered on the master, the actual execution occurs on the assigned agent. Each agent is identified by a unique label, allowing the master to delegate jobs to the appropriate agent.
For small teams or projects, a single Jenkins installation may suffice. However, as the number of projects grows, it becomes necessary to scale. Jenkins supports this by allowing a master to connect with multiple agents, enabling distributed job execution.
Pre-requisites
To set up an agent, you'll need a fresh Ubuntu 22.04 Linux installation. Ensure Java (the same version as on the Jenkins master server) and Docker are installed on the agent machine.
Note: While creating an agent, ensure that permissions, rights, and ownership are appropriately set for Jenkins users.
Let's start With Jenkins Master and Agent Instances
Launch an EC2 instance, named "Jenkins-master".
Connect it with terminal.
Install Java and Jenkins on the master instance. Refer this Installation Of Java and Jenkins.
Now login to Jenkins using <Your_Public_IP:8080> via browser. Add 8080 to security group under inbound rules in your instance using the AWS console.
You will see the below page.
#You can find the Admin password on the location given in the above snapshot sudo cat /var/lib/jenkins/secrets/initialAdminPassword
You need to setup Jenkins here, Refer to Setting up Jenkins.
Launch another instance, named "Jenkins-agent".
Connect it with terminal and install Java on this terminal.
Now Go to Your Jenkins and click on "Manage Jenkins".

Click on "Security"

Under Security, Go to "Agents" and in TCP port, Select "Random" and save the configuration.

Now, In "Manage Jenkins", Go to "Nodes".

Click on "New Node".

Now give the name of your node and select "Permanent Agent".

Now, configure your node. Give the Remote root directory where your node will get launch and will do the tasks and leave all the settings as default and click on "Save".

You can see, we have our node and it is offline as of now.

Click on your agent, you will see the below page.

On Jenkins-agent instance just paste the commands given in the above screenshot a per you choice. You can choose any of the above according to your OS.



I have got the above error, while connecting as the port was not specified on the master instance. So, I have added the port in the security group.

Then run the command again, it worked.]


Creating Jobs on Jenkins with Node Configuration
Example: Deploy a Django Application
Create a new Jenkins freestyle project named "Django-todo"

Configure Source Code Management:
The GitHub URL to pull the code for your Django application.
Configure the project. Under "General Settings" > Select "Restrict where this project can be run" > Select the "Node".

In the Source Code Management section > Select Git > Enter the GitHub Repository Link and the branch name.

In the Build Steps section > Select Add Build Steps > Select Execute Shell > And type the commands required for your activity. Click on Save.


docker build -t app . docker run -d --name todoapp -p 8000:8000 app:latestBefore building the project, see if Docker is installed on your Jenkins-agent or not. If not then install it and gave permission to it using below command.
sudo apt install docker sudo usermod -aG docker $user sudo rebootYou can also give direct permission to docker demon using below command
sudo chmod 666 /var/run/docker.sockBuild and Verify:
Click on “Build Now” and monitor the output.
Verify that the application is running correctly on the agent server.

Thankyou for reading !!!!!





