Jenkins Master-Agent Architecture for CI/CD Pipeline Automation ( With Project)
Jenkins follows a master-agent architecture, where the master server manages and coordinates the tasks, and the agents perform the actual work.
Jenkins Master Server
The Jenkins master server is the primary instance that manages the entire CI/CD pipeline. It acts as the central point of control, where the jobs are created, managed, and monitored. The master server is responsible for scheduling and distributing the work to the agents, collecting and analyzing the results, and triggering the notifications.
The master server is also responsible for managing the security, plugins, and configurations of the entire Jenkins environment. It provides a web-based interface that allows users to interact with Jenkins, configure jobs, and view the build status.
The Jenkins master server can be installed on any machine that meets the hardware and software requirements. The installation process is straightforward, and it only requires downloading and running the Jenkins installation package.
Jenkins Agent Server
The Jenkins agent server, also known as a slave or node, is a worker machine that performs the actual work of building, testing, and deploying the software projects. Agents connect to the master server over the network and receive instructions on what to do.
Agents can be installed on any machine that meets the hardware and software requirements. They can run on different platforms and can be located in different geographical locations. This makes it possible to distribute the workload across multiple agents, which improves the overall performance and scalability of the Jenkins environment.
Each agent is configured with a set of labels that define the capabilities of the machine, such as the operating system, programming languages, and tools. Jobs can be configured to run on specific agents based on their labels, which ensures that the job runs on a machine that has the required capabilities.
Agents communicate with the master server using a protocol called the Jenkins Remoting protocol. This protocol allows the master server to send commands to the agent, receive status updates, and collect the results.
We will practically see all these Process.
Create Two servers Jenkins Master & Another Jenkins-Agent
On The Master you have to Install Jenkins And Java. And On The Agent Server install Only Java. make sure that the Java version must be same on master and agent server .
Next making connection between them. The Agents are connected to the master server over a network using the SSH protocol. This allows the master server to securely communicate with the agent and send commands to perform tasks such as building, testing, and deploying the software projects.
You will need to generate an SSH key pair on the Master server machine and add the public key to the Jenkins Agent server's authorized keys list. This will allow the master server to authenticate the agent when it connects over SSH.
cd .ssh
shh-keygen
#press enter 3 times
ls
#now you can see the genrated keys
ssh ubuntu@AgentPublicIPaddress
As you can check it successfully connected.
Configure Jenkins: After starting Jenkins, you can configure it by accessing the Jenkins web interface using a web browser. By default, Jenkins runs on port 8080, so you can access the web interface by opening a web browser and entering the following URL: your-server-ip-address:8080. And then setup the jenkins.
Add the agent to the Jenkins master server.
Click on the "New Node" button to create a new node. Enter a name for the node and select "Permanent Agent". Click "OK" to continue.
Configure the node by entering the necessary information, such as the node's name, the number of executors, and the remote root directory.
Configure the node's launch method by selecting "Launch agent via SSH" and entering the necessary information, such as the remote host IP address, the SSH credentials, and the path to the agent's Java executable.
Here We Have To Give The Private Key of Master to the Jenkins server.
Save the node configuration and launch the agent by clicking on the "Launch Agent" button.
Now Your Agent Is Live
Now Create Job
Select Pipeline and click Ok.
Fill details.
For Login And Pushing you have to
Click on the "Manage Jenkins" link on the left-hand side of the screen.
Select "Manage Plugins" from the options that appear.
Click on the "Available" tab to view the available plugins.
Search for Environment variable plugin and install and restart Jenkins
And the Add the credenntials.
pipeline {
agent {label 'dev-agent'}
stages {
stage('Code'){
steps {
git url: 'https://github.com/Sushrutnet/Restorent-lts.git', branch: 'master'
}
}
stage('Building'){
steps {
sh 'docker build . -t sushrutnet/Restorent-lts.git:latest'
}
}
stage('Login & Pushing'){
steps {
echo "Login & Pushing..."
withCredentials([usernamePassword(credentialsId:'dockerHub',passwordVariable:'dockerHubPassword',usernameVariable:'dockerHubUser')]) {
sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
sh "docker push Sushrutnet//Restorent-lts.git:latest"
}
}
} stage(‘Deloy’){
steps {
sh 'docker-compose down && docker up -d’
echo “Deploying…”
}
}
}
}
All your work will be stored in these location
workspace /var/lib/jenkins/workspace/repo name
/So in these project we selected "Build Triggers" section, select the "Poll SCM" checkbox. Git SCM polling is a feature in Jenkins that enables continuous integration by automatically triggering builds when changes are detected in a Git repository. With Git SCM polling, Jenkins periodically checks the Git repository for new commits, and if it detects any changes, it triggers a new build of the associated Jenkins job.
To set up a GitHub webhook follow these
Go to your repository on GitHub and click on "Settings".
Click on "Webhooks" in the left-hand sidebar, then click the "Add webhook" button.
In the "Payload URL" field, enter the URL of the server that will receive the webhook payload.
In the "Secret" field, enter a secret key that will be used to validate the authenticity of the webhook payload.
In the "Which events would you like to trigger this webhook?" section, select the events that you want to trigger the webhook.
Click on the "Add webhook" button to save the webhook.
Once the webhook is set up, GitHub will send a payload to the specified URL whenever the selected events occur in the repository