Master-Slave Node Setup Explained

Jenkins is based on Master-Slave Architecture

Jenkins_Master Node(VM)

  • Install jdk,jenkins,git.

  • Used to Create the CI/CD Pipeline Projects and schedule to run in slave nodes.

Jenkins_Slave Node(VM)

  • To Execute the Application Build.

  • Perform Application Build - Compile, Unit Test, Create Artifacts

Creation of Master-Slave Architecture

  • In Master Node - install git, jdk and jenkins

  • In slave node - Install jdk,maven and git

Configure Slave Node1 for Java Maven App. :

  • Launch an Ubuntu Machine : v22.04

  • sudo -i

  • apt update -y

  • Install Java :

    • sudo apt install openjdk-17-jre -y

    • java -version

  • Install GIT :

    • sudo apt install git -y

    • git --version

  • Install Maven - Build Tool : https://maven.apache.org/install.html

    • sudo apt install maven -y

    • mvn --version

  • Create User in Jenkins Slave Machine & Create SSH Keys

    • sudo apt install docker.io -y

    • Add User :

      • useradd devopsadmin -s /bin/bash -m -d /home/devopsadmin

      • su - devopsadmin

    • ssh-keygen

      • for Ubuntu :

        • ssh-keygen -t rsa -b 2048 -m PEM #ubuntu 20.04

        • ssh-keygen -t ecdsa -b 521 #ubuntu 22.04 or higher version of ubuntu

    • ls ~/.ssh

      • You should see following two files:

        • id_ecdsa - private keyi

        • id_ecdsa.pub - public

    • cat id_ecdsa.pub > authorized_keys

    • chmod 600 /home/devopsadmin/.ssh/*

  • Add devopsadmin user to docker group. Run this command as a root user

    • usermod -aG docker devopsadmin
  • Login to Jenkins

    • Goto to Manage Jenkins

    • Select Nodes

    • On Nodes Dashboard, Click on New Node

    • Give Node Name, and choose permanent agent.

    • Provide a description, the number of executors, the remote root directory, labels, usage, and the launch method.

    • In the Launch method, select "Launch agents via SSH" and enter the private IP address as the host. Then, add a new credential in the credentials section.

      • provide slave node username and copy paste private key content

    • Select “Manually trusted Key Verification Strategy” in Host key verification strategy

    • Click on Save and slave node is created.

Let’s update pipeline

    • SCM_Checkout stage
pipeline {
    agent { label 'slave1' }

    stages {
        stage('SCM_Checkout') {
            steps {
               echo "Perform SCM Checkout"
               git 'https://github.com/SA-Team-DevOps-03-Dec-24/java-mvn-springbootapp.git'               
            }
        }
      }
    }
  • we can use snippet generator to create scripts

  • Click on "Build Now" and wait for the status to show success, then view the output console.

  • Application Build stage

    •   pipeline {
            agent { label 'slave1' }
      
            stages {
                stage('SCM_Checkout') {
                    steps {
                       echo "Perform SCM Checkout"
                       git 'https://github.com/SA-Team-DevOps-03-Dec-24/java-mvn-springbootapp.git'               
                    }
                }
                stage('Application Build') {
                    steps {
                        echo "Perform Application Build"
                        sh 'mvn clean package'
                    }
                }
              }
            }
      
  • Execute the build and view the output console