Hey Learners! Welcome back. In the previous challenges, we learned how to build a pipeline automatically whenever there is any change pushed in the code, how to create jobs with declarative pipelines and some basic Freestyle projects. All these things we did on One single Jenkins server(Master), moving forward in depth with Jenkins we'll learn how to connect agents nodes(slave) and execute the task/job on these agents. Let's get started...
Jenkins Master:-
The Jenkins server or master node holds all key configurations. The Jenkins master server is like a control server that orchestrates all the workflow defined in the pipeline. For example, scheduling a job, monitoring the jobs, etc.
Jenkins Agent:-
An agent is typically a machine or container that connects to a Jenkins master and this agent executes 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 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.
Pre-requisites:-
Create an agent as a new Instance of Ubuntu 22.04 on AWS. Make sure an agent machine should have Java installed in it and also Docker should be present.
Task 1-
Create an agent by setting up a node on Jenkins
As we already practised some jobs on Jenkins, I am going to use this server as Jenkins master and create one more EC2 instance as Jenkins Agent.
On the Jenkins Agent machine Docker, Java, and Docker-compose should be installed.
The connection between the Master node and the Agent should be established through SSH. ssh in both instances via Putty or directly ssh if you use Linux.
In the above screenshot LHS terminal is the Master and the RHS terminal is the Agent node. Create/Generate ssh-key on the master node using
ssh-keygen
command. Copy the public key from.ssh/<givenname.pub>
and paste on Agent nodes in.ssh/authorized_keys
Now you can access the agent node from the master node
Now we will add an agent node on Jenkins. Go to Jenkins Web UI Manage Jenkins-Nodes and Clouds-New node-Enter your node name and select Permanent Agent then create.
Add the description you want to add and other details as required.
Select Launch agent via ssh and enter the public IP of an agent node in the Host section. Click on Add to add credentials. Select SSH username and private key. Select enter directly and enter the master's private key in Passphrase. Use
cat .ssh/<keyname>
command for the private key. save.To add Credentials while setting up an agent, follow the below steps
Click on Add-> Jenkins
A popup window will appear. Select SSH Username with the private key.
Scroll down and enter the username. Select Enter directly to the enter private key. Click Add.
If you Nodes now you will see Agent1 is added and it is online. This is how we add an agent node in Jenkins.
Task 2- Run jobs on the agent node
If you check the previous challenge we have a project with the declarative pipeline. In the pipeline, we mentioned agent as any but if you want to run the job on an agent you should mention agent as <label-of-an-agent>. Ref below screenshot
Save the job and build it. Your job is running on an agent. Access your application by entering <AgentIP:8000>
This is how we can add an agent and run the jobs on it in Jenkins.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!