Jenkins Agents | Master & Agent (Slave) Nodes
I have deployed a Node JS application on the agent node by triggering the build job on the Master node.
Table of contents
In my previous blog, I have written about the Jenkins Master-Slave architecture. Let's understand in depth about the Master Node, agent nodes, and how to configure agents on the master.
Jenkins Master Server
The Jenkins Master is responsible for managing the Jenkins job pipeline, which is a series of steps that are used to build, test, and deploy software. The master also provides a central repository for storing code, artifacts, and other project data.
The master also provides a web interface that allows users to interact with the Jenkins server and manage the job pipeline.
Activities handled by the Master
Master Server does the following things:
Scheduling jobs
Build automation
Testing
Deployment
Managing the job pipeline
Providing a web interface
Storing code, artifacts, and other project data
Why do we need an agent(s) when the master can do all the jobs?
The master server can do all the jobs, but it is not always the best way to do them. Agents can help to improve the efficiency and scalability of the Jenkins environment.
The reasons why we need agents when the master can do all the jobs are:
Scalability: The master server can only handle a limited number of jobs at a time. Agents can help to increase the number of jobs that can be run simultaneously.
Efficiency: Agents can help to improve the efficiency of the Jenkins environment by offloading some of the work from the master server.
Availability: Agents can help to improve the availability of the Jenkins environment by providing a failover mechanism. If the master server fails, the agents can continue to run jobs.
Security: Agents can help to improve the security of the Jenkins environment by providing a layer of isolation between the master server and the agents.
Jenkins Agents
Jenkins agents are servers that are used to run jobs that are submitted to the master server. Agents can be physical or virtual machines, and they can be running any operating system that supports Java.
The Jenkins master server assigns a job to an agent. The agent then executes the job, which may include building, testing, or deploying software. The agent reports the status of the job back to the master server.
Configuration of Agents
Jenkins agents can be configured in a variety of ways.
They can be configured to run specific jobs.
They can be configured to run any job that is submitted to the master server.
Agents can also be configured to run jobs in parallel, which can improve the performance of the Jenkins environment.
Note: While creating an agent, be sure to separate rights, permissions, and ownership for jenkins users.
Let us configure the Agent Node on Jenkins.
Tasks
Task 1: Configure an agent by setting up a node on Jenkins and verify its status under the "Nodes" section.
1) The main criteria to get both Agent(Slave) and Master Node working, are the versions of installed Java and Docker must be the same.
I have installed 'openjdk-11-jre' on both agents:
sudo apt-get install openjdk-11-jre
The same can be verified from the below screenshots:
2) Now in the Master server, generate keys:
ssh-keygen
And then get into the .ssh directory.
3) Copy the Public keys (id_rsa.pub) into the Agent server.
The location to copy the keys is the .ssh directory of Agent under authorized_keys.
sudo vim authorized_keys
Copy the keys, save, and exit.
4) Now go to Jenkins > Dashboard > Manage Jenkins > Manage Nodes and Clouds > Click on "+ New Node"
Follow the following instructions:
Node Name: JenkinsAgent
Type: Permanent Node
Click Create
Name: JenkinsAgent
Description: This agent deploys a Node JS application.
Number of executors: 1
Remote root directory: /home/ubuntu/jenkins-agent
label: agent_node1
Usage: Only build jobs with label expressions matching this node
Launch method: Launch agents via SSH
Host: Public IP of Agent Node
Credentials: Select from the dropdown (More about adding credentials in the next session)
Host Verification Strategy: Non verifying Verification Strategy
Why Non-verifying Verification Strategy? over Known Hosts Verification Strategy?
The non-verifying verification strategy can be used if you are connecting to a remote host that you trust.
The known host's verification strategy checks the fingerprint of the remote host's SSH key, which can help to prevent an attacker from impersonating the remote host.
5) Click on Save and the agent will be connected and online.
Adding Credentials to Jenkins
Go to Manage Jenkins > Credentials > Global Credentials > Select 'SSH Username with private key' > Scope 'Global' > ID (you can give your choice)
\> Username 'ubuntu' > Private Key > Select 'Enter Directly' > Copy the Private Key of the agent node into the key section > Click Add.
The Credentials are added!
Task 2: Deploy the Day 27 application on the new agent. Use labels for the agent, your master server should trigger builds for the agent server.
Repo link: github.com/thesnehasuresh/node-todo-cicd.git
1) Create a pipeline with the help of my previous project.
2) In the configure > Pipeline Script > Write the following steps:
pipeline {
agent {label 'agent_node1'}
stages {
stage('Clone the Repo:') {
steps {
echo 'Cloning the repository:'
git 'https://github.com/thesnehasuresh/node-todo-cicd.git'
}
}
stage('Build') {
agent {
docker {
image 'snehaks/sample_nodejs:latest'
reuseNode true
}
}
steps {
echo "Building app using Docker Image"
}
}
stage('Deploy the Application') {
steps {
sh "docker-compose down"
sh "docker-compose up -d"
}
}
}
}
You can observe that in the agent block, we have specified the agent name.
agent {label 'node_agent1'}
3) Click on Save and build the Job.
We can observe that the Job is running on the Agent node:
4) For the error, let's fix the permission issue and run the job again.
sudo usermod -aG docker $USER
sudo reboot
The build is successful now:
5) The same can be verified from the Console Output too.
In this blog, I have discussed on how to configure an agent to master node in Jenkins. Also, I have deployed an application on the agent by triggering the job on master. If you have any questions or would like to share your experiences, feel free to leave a comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.
To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.
#Day28 #90daysofdevops