Friday, February 8, 2019

Jenkins Master Slave Setup | Setup Jenkins Master Slave on Ubuntu | Setup build agent and Integrate with Jenkins Master

Jenkins has powerful feature of master slave architecture which enables distributed builds. This article we will learn how to establish slave nodes on Ubuntu machines and integrate with Jenkins Master.



This lab demo is available on YouTube:
 
 Let us learn more about Jenkins master and slave:

Jenkins Master
Your main Jenkins server is the Master. The Master’s job is to handle:
  • Scheduling build jobs.
  • Dispatching builds to the slaves for the actual execution.
  • Monitor the slaves (possibly taking them online and offline as required).
  • Recording and presenting the build results.
  • A Master instance of Jenkins can also execute build jobs directly.
Jenkins Slave
A Slave is a Java executable that runs on a remote machine. Following are the characteristics of Jenkins Slaves:
  • It hears requests from the Jenkins Master instance.
  • Slaves can run on a variety of operating systems.
  • The job of a Slave is to do as they are told to, which involves executing build jobs dispatched by the Master.
  • You can configure a project to always run on a particular Slave machine, or a particular type of Slave machine, or simply let Jenkins pick the next available Slave.
Lets see how to configure both Jenkins master and slave nodes on Ubuntu EC2.

Jenkins Master uses SSH keys to communicate with slave. You need to create ssh keys in Jenkins master by executing below command.

ssh-keygen

Slave node configuration(you need new micro Ubuntu instance for this slave)
only port 22 needs to be open

Install Java

sudo apt-get update
sudo apt-get install default-jdk -y

Install Maven
sudo apt-get install maven -y

Create User as Jenkins
sudo useradd -m jenkins
sudo -u jenkins mkdir /home/jenkins/.ssh




Add SSH Keys from Master to Slave 

Execute the below command in Jenkins master Ec2.
sudo cat ~/.ssh/id_rsa.pub

Copy the output of the above command:

Now go to Slave node and execute the below command
sudo -u jenkins vi /home/jenkins/.ssh/authorized_keys

This will be empty file, now copy the public keys from master into above file.
Once you pasted the public keys in the above file in Slave, come out of the file by entering wq!


Now go into master node
ssh jenkins@slave_node_ip





this is to make sure master is able to connect slave node. once you are successfully logged into slave, type exit to come out of slave.





Now copy the SSH keys into /var/lib/jenkins/.ssh by executing below command in master(make sure you exited from slave by typing exit command:

sudo cp ~/.ssh/known_hosts  /var/lib/jenkins/.ssh

Because jenkins master will look keys from the above folder.

Register slave node in Jenkins:
Now to go Jenkins Master, manage jenkins, manage nodes.









Click on new node. give name and check permanent agent.
give name and no of executors as 1. enter /home/jenkins as remote directory.
select launch method as Launch slaves nodes via SSH.
enter Slave node ip address as Host.











click on credentials. Enter user name as jenkins. Make jenkins lowercase as it is shown.
 Kind as SSH username with private key. enter private key of master node directly by executing below command:

sudo cat ~/.ssh/id_rsa
(Make sure you copy the whole key including the below without missing anything)
-----BEGIN RSA PRIVATE KEY-----
-----
-----END RSA PRIVATE KEY-----

click Save.
select Host key verification strategy as "manually trusted key verification strategy".

Click Save.
Click on launch agent..make sure it connects to agent node.



Now you can kick start building the jobs, you will see Jenkins master runs jobs in slave nodes.

3 comments:

  1. This is great article and helpful but a small correction required.
    Command : sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh
    Need to be updated to
    sudo cp ~/.ssh/known_hosts /var/lib/jenkins/.ssh/known_hosts

    ReplyDelete
  2. very god tq u helpfull this videos and docments again tq u

    ReplyDelete
  3. SSHLauncher{host='172.31.47.145', port=22, credentialsId='jenkins', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=60, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.ManuallyTrustedKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true}
    [10/02/22 21:18:01] [SSH] Opening SSH connection to 172.31.47.145:22.
    [10/02/22 21:18:01] [SSH] SSH host key matches key seen previously for this host. Connection will be allowed.
    ERROR: Server rejected the 1 private key(s) for jenkins (credentialId:jenkins/method:publickey)
    ERROR: Failed to authenticate as jenkins with credential=jenkins
    java.io.IOException: Publickey authentication failed.
    at com.trilead.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:349)
    at com.trilead.ssh2.Connection.authenticateWithPublicKey(Connection.java:472)
    at com.cloudbees.jenkins.plugins.sshcredentials.impl.TrileadSSHPublicKeyAuthenticator.doAuthenticate(TrileadSSHPublicKeyAuthenticator.java:110)
    at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.authenticate(SSHAuthenticator.java:431)
    at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.authenticate(SSHAuthenticator.java:468)
    at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:881)
    at hudson.plugins.sshslaves.SSHLauncher.lambda$launch$0(SSHLauncher.java:434)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
    Caused by: java.io.IOException: Could not generate signature
    at com.trilead.ssh2.signature.KeyAlgorithm.generateSignature(KeyAlgorithm.java:43)
    at com.trilead.ssh2.auth.AuthenticationManager.authenticatePublicKey(AuthenticationManager.java:316)
    ... 10 more
    Caused by: java.security.SignatureException: Could not sign data
    at java.base/sun.security.rsa.RSASignature.engineSign(RSASignature.java:195)
    at java.base/java.security.Signature$Delegate.engineSign(Signature.java:1404)
    at java.base/java.security.Signature.sign(Signature.java:713)
    at com.trilead.ssh2.signature.KeyAlgorithm.generateSignature(KeyAlgorithm.java:41)
    ... 11 more
    Caused by: javax.crypto.BadPaddingException: RSA private key operation failed
    at java.base/sun.security.rsa.RSACore.crtCrypt(RSACore.java:201)
    at java.base/sun.security.rsa.RSACore.rsa(RSACore.java:122)
    at java.base/sun.security.rsa.RSASignature.engineSign(RSASignature.java:192)
    ... 14 more
    [10/02/22 21:18:01] [SSH] Authentication failed.
    Authentication failed.
    [10/02/22 21:18:01] Launch failed - cleaning up connection
    [10/02/22 21:18:01] [SSH] Connection closed.

    How do I fix this issue please???

    ReplyDelete

DevSecOps Bootcamp Dec 2024 Schedule | DevSecOps & AWS Azure Cloud Coaching by Coach AK | DevSecOps and Cloud Computing Online Classes

Lot of new topics covered like GitHub Actions, GitHub Advanced Security, Helm, Prometheus and Grafana..) The DevOps requirements in the IT m...