Configure Jenkins with High Availability in few minutes on CentOS Servers.

With the open source Jenkins there is no out of the box and independent High Availably option.So if you are a free user of Jenkins as me. You can use the same approach I have taken in order to have a backup Jenkins server with the latest configuration on your Jenkins server.
Let’s get our hand dirty with out any further due.
Install and Configure the Jenkins server in two servers.
User Terminator if you are using linux or iTerm if you are using Mac to avoid duplicate effort on this.
SSH in to both servers on single tabs by splitting tab horizontally or vertically according to your preferences, then enable Broadcast input. You can enable in iTerms from the following menu.

Then enter the following commands on one of the panes and it will be triggered on both the servers.
# Adding the repository keyrpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key# Adding the package repository address to our repository listwget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
Now both server have the needed information to install Jenkins. Before Jenkins installation we need to install JAVA if not already installed on the Servers.
yum install java-1.8.0-openjdk Then use the following command to install Jenkins on your two servers.
yum install jenkinsStart the Jenkins Server as a service using the following command.
systemctl start jenkinsYou can check the current status of the Jenkins by using the following command.
systemctl status jenkinsYou can see a similar input as below if Jenkins server is up and running with out any issues.

To start Jenkins on server boot up use the following command enable startup for Jenkins.
systemctl enable jenkinsNow you have two Jenkins servers running on your two server. It’s time to unlock Jenkins and Configure accordingly.
Your Jenkins Server can be accessible from following links:
http://IP_MAIN_SERVER:8080/
http://IP_BACKUP_SERVER:8080/
ℹ️ You can change the default port by changing the JENKINS_PORT in /etc/sysconfig/jenkins configuration file
You will be presented with a modal like below image to enter the initial admin password.

You can find the initial admin password for each server in the following file in the both severs.
vim /var/lib/jenkins/secrets/initialAdminPasswordYou use the password in the above file and unlock Jenkins then follow the official documentation to install needed plugins and to create users.
Here comes the part we sync the two servers.
Initially log into the master/ main server and use the following commands to put the $JENKINS_HOME to a separate location and link to the the server.
You need to make sure that the Jenkins server is stopped when you are doing the below tasks.
# Stop Jenkins
sudo service jenkins stop#move Jenkins directory
sudo mv /var/lib/jenkins /opt/sync/# Adding a link to the moved directory from original jenkins dir
sudo ln -s /opt/sync/jenkins /var/lib/jenkins#Start Jenkins
sudo service jenkins start
Now log in to the Slave/Backup Server and perform the following actions to sync up with the Master/Main server.
# Stop Jenkins
sudo service jenkins stop#create a Jenkins directory
sudo mkdir /opt/sync/#Sync the current folder with the Main Server's Jenkins Directory
#Replace the x.x.x.x with your master server ip or hostname
rsync -aP --delete user@x.x.x.x:/opt/sync/ /opt/sync/#remove the jenkins folder of the current server
sudo rm -r /var/lib/jenkins# Adding a link to the moved directory from original jenkins dir
sudo ln -s /opt/sync/jenkins /var/lib/jenkins#Start Jenkins
sudo service jenkins start
Now you have a Backup Jenkins Server with all the configurations/ Jobs and related data from the original Jenkins Server.
To Maintain the sync and have the latest data available on your backup Jenkins Server. You can follow the below additional steps.
- Enable Password less ssh between two servers. You can follow the this tutorial.
- Then decide the frequency of the synchronization to be triggered.
- Create a cron job in the Backup serevr by following the below steps,
#Edit Cron Jobs
crontab -eUse the following cron jon as a example,
MAILTO=”infor@kachiex.com”
0 3,19 * * * rsync -aP — delete user@x.x.x.x:/opt/sync/ /opt/sync/ > /opt/resources-backup-taks.logReplace the x.x.x.x with your master server ip or hostname in the above sample cron job.
Above cron job will triggered an rsync from main server to back up server twice a day at 3.00am and 7.00pm.
Visit Devhints.io for get know bit more regarding rsync and what you can achieve with rsync