Install Jenkins on Ubuntu and Set up a Master-Slave Configuration

Part 1: Install Jenkins on Ubuntu

Step 1: Update System Packages

sudo apt update && sudo apt upgrade -y

Step 2: Install Java

Jenkins requires Java to run. Install OpenJDK:

sudo apt install -y openjdk-11-jdk

Verify Java installation:

java -version

Step 3: Add Jenkins Repository

Add the Jenkins key and repository:

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

Update the package list:

sudo apt update

Step 4: Install Jenkins

sudo apt install -y jenkins

Start and enable Jenkins:

sudo systemctl start jenkins
sudo systemctl enable jenkins

Step 5: Access Jenkins Web Interface

  1. Open your browser and go to:

    http://<server-ip>:8080
    
  2. Retrieve the initial admin password:

    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
    
  3. Use this password to log in and complete the setup wizard by installing recommended plugins.


Part 2: Setup Jenkins Master-Slave Configuration

Step 1: Add a Slave Node

  1. Log in to the Jenkins dashboard.

  2. Navigate to Manage JenkinsManage Nodes and Clouds.

  3. Click New Node.

  4. Enter a name for the node (e.g., slave-node-1) and select Permanent Agent.

  5. Configure the node:

    • Remote root directory: Directory on the slave where Jenkins files will be stored (e.g., /home/jenkins).

    • Labels: Add labels to identify this node for specific jobs.

    • Save the configuration.


Step 2: Prepare the Slave Node

  1. Install Java on the slave machine:

    sudo apt update && sudo apt install -y openjdk-11-jdk
    java -version
    
  2. Create a directory for Jenkins:

    mkdir /home/jenkins
    

Step 3: Configure SSH Access

  1. Generate SSH keys on the master server:

    ssh-keygen -t rsa -b 4096
    
  2. Copy the public key to the slave machine:

    ssh-copy-id user@slave-ip
    
  3. Test SSH access:

    ssh user@slave-ip
    

Step 4: Connect the Slave Node

  1. On the Jenkins master dashboard, edit the node configuration:

    • Select Launch agents via SSH.

    • Enter the slave's IP address, credentials (username/password or private key).

    • Test the connection to ensure successful setup.

  2. Save the configuration.


Step 5: Verify Node Connection

  1. Go to Manage JenkinsManage Nodes and Clouds.

  2. Check the status of the slave node. It should show as online.


Step 6: Assign Jobs to the Slave Node

  1. Open a Jenkins job configuration.

  2. Scroll to the "Restrict where this project can be run" section.

  3. Enter the label of the slave node.

  4. Save the configuration.


Troubleshooting

  1. SSH Errors: Ensure the slave node allows SSH connections and that the user has the correct permissions.

  2. Agent Not Starting: Check logs on the slave node or /var/log/jenkins/jenkins.log on the master.

  3. Firewall Rules: Ensure port 22 (SSH) and 8080 (Jenkins) are open on the respective machines.

Updated on