To install Jenkins on Ubuntu and set up a master-slave configuration, you can follow these steps. Jenkins is a popular open-source automation server, and this guide will help you create a Jenkins master and connect one or more Jenkins slave nodes to distribute workloads.
Step 1: Install Jenkins on the Master Node
-
Update the package list and install Java Runtime Environment (JRE):
sudo apt update sudo apt install -y openjdk-11-jre
-
Add the Jenkins repository and its GPG key:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
-
Update the package list and install Jenkins:
sudo apt update sudo apt install -y jenkins
-
Start the Jenkins service:
sudo systemctl start jenkins
-
Enable Jenkins to start on boot:
sudo systemctl enable jenkins
-
Check the status to ensure Jenkins is running:
sudo systemctl status jenkins
-
Retrieve the Jenkins initial admin password from the following location:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
-
Open a web browser and navigate to
http://YOUR_SERVER_IP:8080
. Enter the initial admin password to unlock Jenkins. -
Follow the on-screen instructions to complete the initial Jenkins setup. Create an admin user and customize Jenkins as needed.
Step 2: Configure Jenkins Master for SSH
To set up passwordless SSH communication between the Jenkins master and slaves, follow these steps on the Jenkins master node:
-
Switch to the Jenkins user:
sudo su - jenkins
-
Generate an SSH key pair for the Jenkins user:
ssh-keygen
Press Enter for all prompts to use the default values.
-
Copy the public key to the slave server(s) you want to connect:
ssh-copy-id slave_user@SLAVE_IP
Replace
slave_user
with your actual username on the slave server andSLAVE_IP
with the IP address of the slave. Repeat this step for each slave. -
Test the SSH connection to ensure it works without requiring a password:
ssh slave_user@SLAVE_IP
You should be able to log in without entering a password.
Step 3: Set Up Jenkins Slave(s)
On the Jenkins master node, configure the slave(s):
-
Navigate to the Jenkins dashboard (
http://YOUR_SERVER_IP:8080
). -
Go to "Manage Jenkins" > "Manage Nodes and Clouds."
-
Click "New Node."
-
Enter a name for the node and select "Permanent Agent."
-
Configure the following settings:
- # of Executors: The number of concurrent builds the slave can handle.
- Remote root directory: The workspace directory on the slave.
- Labels: Add labels to identify the slave.
-
Under "Launch method," select "Launch agent via SSH."
-
Configure the SSH settings:
- Host: Slave server's IP address.
- Credentials: Click "Add" and select "SSH Username with private key." Enter the Jenkins user's credentials on the slave, and choose the private key you generated earlier.
-
Click "Save."
-
Click "Launch agent" to start the slave.
Repeat the above steps for each additional slave you want to add.
Step 4: Create and Run Jenkins Jobs
Now that you have set up Jenkins master and slave(s), you can create and run Jenkins jobs. Jenkins will automatically distribute and execute jobs on the available slaves based on labels and workload.
Here's a basic example of creating a Jenkins job:
-
Go to the Jenkins dashboard.
-
Click "New Item."
-
Enter a name for the job and select "Freestyle project."
-
In the job configuration, specify the build steps, source code management, and any other relevant settings.
-
In the "Restrict where this project can be run" section, specify the label(s) of the slave(s) you want to use for this job.
-
Click "Save" to create the job.
-
Build the job, and Jenkins will assign it to an available slave based on the specified label(s).
You now have Jenkins set up with a master-slave configuration, allowing you to distribute and manage build and deployment workloads efficiently. Customize your Jenkins setup further based on your specific requirements and workflows.