Jenkins Master
The Jenkins master server is the core component that manages configurations, scheduling jobs, and monitoring tasks.
It acts as a control server that coordinates workflows defined in pipelines. The master node handles interactions with agents, scheduling jobs to be executed on them, and collecting results afterwards.
Jenkins Agent
A Jenkins agent is a machine or container that connects to the Jenkins master server. Agents are responsible for executing the steps outlined in a Jenkins job. When creating a Jenkins job, it's necessary to assign an agent to it. Each agent is labeled to provide a unique identifier.
When a Jenkins job is triggered from the master, the execution takes place on the agent node specified in the job configuration. This distribution of work ensures that the master node doesn't get overwhelmed and can focus on controlling the overall process.
For teams with growing needs, Jenkins offers the ability to scale using a "master-to-agent connection." This approach allows agents to handle job execution, while the master concentrates on serving the Jenkins UI and managing the workflow.
Task 01: Create a Jenkins Agent Node
Launched two instances - One as a Jenkins Master Node and another as an agent.
Install Jenkins on the Master EC2 instance.
Create private and public keys via
ssh-keygen
to connect Jenkins Master and Agent server via SSH.Copy the Public key from the Master Server to the Agent Server in the below file.
cd .ssh/authorized_keys
Log in to your Jenkins dashboard.
Navigate to "Set up an agent".
Click on "New Node" or "New Agent."
Assign a name to the agent, such as "Todo-App-Dev" or "Docker-Agent".
Opt for the "Permanent Agent" option.
Choose "Launch agents via SSH" under "Launch method."
Copy the Master Private key.
Define the agent's root directory for storing files.
Save the agent configuration. Here is the successful console output.
Install Jenkins on the master.
sudo apt update sudo apt install openjdk-11-jre #Install Jenkins curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \ /usr/share/keyrings/jenkins-keyring.asc > /dev/null echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ https://pkg.jenkins.io/debian-stable binary/ | sudo tee \ /etc/apt/sources.list.d/jenkins.list > /dev/null sudo apt-get update sudo apt-get install jenkins #Start jenkins sudo systemctl enable jenkins sudo systemctl start jenkins sudo systemctl status jenkins
Install Docker, docker-compose and Java on Jenkins-Slave.
sudo apt install docker.io sudo apt install docker-compose -y sudo apt install openjdk-11-jre sudo usermod -aG docker $USER sudo reboot
Task 02: Run a Jenkins Job on Agent Node
From the Jenkins dashboard, click on "New Item" to create a new project.
Enter a project name and select "Pipeline" Click "OK" to create the project.
Under the Configuration, Select the GitHub Project checkbox -> Enter the URL and Enabled the GitHub hook trigger for GITScm polling.
GitHub WebHooks Setup:
Accessed my repository on GitHub and go to Settings -> Webhooks
Added a new webhook with my Jenkins job's URL Selected events to trigger (e.g., push, pull request).
Saved the webhook.
Create Environment variable credential for docker hub:
Click on "manage jenkins" > Credentials.
Click on "Systum" > "Global Credentials(unrestricted)" > "Add Credentials".
Add the docker hub credentials and click on create
Running the Application:
Install docker and docker-compose on the server. Add the current user to the
docker
group:sudo usermod -aG docker $USER sudo reboot
Add the Jenkins user to the
docker
group:sudo usermod -aG docker jenkins sudo systemctl restart jenkins
Inside the Dashboard "Node-app" job > Within the Jenkins job's Pipeline build step.
pipeline { agent {label 'todo-dev'} stages{ stage('Clone Code'){ steps { echo "Cloning the code" git url:"https://github.com/ajaygite/node-todo-app.git", branch:"master" } } stage('Build'){ steps { echo "Building the Image" sh "docker build -t todo-app ." } } stage('push to dockerhub'){ steps { echo "Pushing the code to dockerhub" withCredentials([usernamePassword(credentialsId:"dockerhub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){ sh "docker tag todo-app ${env.dockerHubUser}/node-app:latest" sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}" sh "docker push ${env.dockerHubUser}/node-app:latest" } } } stage('Deploy'){ steps { echo "Deploying the application" sh "docker-compose down" sh "docker-compose up -d --build" } } } }
Save and click on Build Now.
Once done hit the URL ipaddress:8000 to check if it is running.