Here we are learning how to set up an architecture where we can run our application on a slave node or Agent server.
Pre-requisite for this project:-
1. Create an EC2 instance for Jenkins Server
2. Create an EC2 instance for Agent Server or Slave Node
3. Install Jenkins on the Jenkins server only.
4. No need to install Jenkins on the Agent server.
Here I have created 2 instances, 1 is for Jenkins Server and the other one is for Jenkins agent server.
Now try to install Jenkins on the Jenkins Server instance, before that you have to install Java on the system.
Once Java is installed you will able to install Jenkins too.
If you are new here please try to read my previous blog where I have done step-by-step installation of Jenkins.
Copy your project from the GitHub repository to your local system.
Once it is cloned successfully make some changes in that and try to push it into your GitHub repository.
Successfully cloned node-todo-app project in my local system.
Now open your Jenkins on port 8080.
Create a New Item, specify the name for the job and through the pipeline, we implement your project.
Now try to add a description for your job and specify the GitHub URL as shown in the below image.
As shown in the above picture we are triggering your project with the help of GitHub Webhook.
Just Click on the GitHub hook trigger for GITScm polling as shown in the above image.
If you have a question that how to set up a webhook triggering then follow the below steps for the same.
Step 1:- Go to the GitHub account.
Step 2:- Go to your project
Step 3:- Go to the setting that you have seen in the above picture.
Step 4:- Go to the webhook which is listed in the Code and Automation.
Step 5:- Click on Add Webhook. Add your Jenkins address ending with github-webhook.
e.g., http://13.232.161.201:8080/github-webhook/
This is how you can add your webhook to the GitHub repository.
Let's try to create one Agent node or Slave Node in Jenkins and connect it through an SSH connection.
Step 1:- Go to the Jenkins server system and try to execute the below command in .ssh directory.
# ssh-keygen
Press 3 times enter button if you don't want to create your own private and public key.
Step 2:- Copy the public key from the Jenkins server system and paste it to the agent server authorized file which is available in .ssh folder of the agent server system.
Step 3:- Now go to the Jenkins --> Manage Jenkins --> New Node --> Node name --> Permanent Agent --> Click on Create
In the host section as shown in the above picture you have mentioned your agent server's public IP address and in the credentials section just click on add credentials.
and in the Key section just paste the Jenkins Server systems private key.
Likewise, you will able to set up an SSH connection between Jenkins Server and Agent Server or Slave Node.
PIPELINE:-
The pipeline is written in Groovy programming language. It always starts with syntax:-
pipeline {
}
In between this, you have to write your script to perform the task. Please find below the pipeline script which I have written for my project.
If you are facing any issues in Dockerfile and docker-compose file, refer to my above files and try to build Dockerfile and docker-compose file.
----------------------------------------------------------------------------------------
Output:-
See in the above picture I have successfully set up a connection and run my application on the agent server.