Skip to main content

Command Palette

Search for a command to run...

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

Published
4 min read
Day 28 : 🚀 Setting Up Jenkins Agents: A Step-by-Step Guide 🖥️
A

"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

  1. Launch an EC2 instance, named "Jenkins-master".

  2. Connect it with terminal.

  3. Install Java and Jenkins on the master instance. Refer this Installation Of Java and Jenkins.

  4. 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
    

  5. You need to setup Jenkins here, Refer to Setting up Jenkins.

  6. Launch another instance, named "Jenkins-agent".

  7. Connect it with terminal and install Java on this terminal.

  8. Now Go to Your Jenkins and click on "Manage Jenkins".

  9. Click on "Security"

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

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

  12. Click on "New Node".

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

  14. 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".

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

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

  17. 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.

  18. 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.

  19. Then run the command again, it worked.]

Creating Jobs on Jenkins with Node Configuration

Example: Deploy a Django Application

    1. Create a new Jenkins freestyle project named "Django-todo"

  1. Configure Source Code Management:

    • The GitHub URL to pull the code for your Django application.

      Github

  2. Configure the project. Under "General Settings" > Select "Restrict where this project can be run" > Select the "Node".

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

  4. 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:latest
    
  5. Before 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 reboot
    
  6. You can also give direct permission to docker demon using below command

     sudo chmod 666 /var/run/docker.sock
    
  7. Build and Verify:

    • Click on “Build Now” and monitor the output.

    • Verify that the application is running correctly on the agent server.

Thankyou for reading !!!!!

90DaysOfDevops

Part 18 of 49

So In This Series I am following 90 DaysOfDevops challenge , what i will learn in my devops journey i will share with you with my blogs ,You will get blog on tools which used by devops engineer , example :- Linux,ansible,terraform,docker,etc

Up next

Day 27 Task: Jenkins Declarative Pipeline with Docker 🚀

Day 26 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline Use your Docker Build and Run Knowledge docker build - you can use sh 'docker build . -t <tag>' in your pipelin...

More from this blog

A

Anand Raval

118 posts

Hello I am Anand Raval , i contributed my work in robotics(arduino uno) , fronted web devloper,competitive programming, now currently focusing on #90DaysOfDevops