Google Cloud - Jenkins deployments for autoscaler servers

How do you deploy seamlessly to a bunch of web servers behind a load balancer and part of an autoscaler? The deployment process should pull the list of servers in the instance group to make sure that newer instances that might have come into existence due to scaling also have the same version of code running. Below are steps that explain setting up a small Jenkins server and configuring for deployments. This tutorial assumes deploying to tomcat.

Launch Jenkins Server

Logon to the console and launch a small instance for Jenkins. Make sure that API Access is enabled with compute - ReadOnly. This allows applications running on this instance can interact with API and pull information about instance groups, list IPs etc.

Install Jenkins

apt-get update  
    wget -q -O - | apt-key add -
    echo deb binary/ > /etc/apt/sources.list.d/jenkins.list
    apt-get update
    apt-get install jenkins

sometimes you may see an exception like jenkins E: Sub-process /usr/bin/dpkg returned an error code (1) and this can be fixed using sudo apt-get -f install. For the rest of jenkins setup on Ubuntu, you can follow this guide.

Create the first job in Jenkins, Go for freestyle projects and give a name for reference and click on OK.

Select Execute Shell option from 'Add Build' step.

Pass the below script and click save option. The script below uses google cloud command line utility gcloud. Check comments for details

#Get the list of the server names behind the instanstce group using gcloud compute and grep the initial words of the instance group and output to a file.

gcloud compute instance-groups managed list-instances prod-app-utoo-grp --zone asia-east1-a | grep -o "prod-app-utoo[^ ]*" > servername

#Get the IP addresses for the above server names and output to a file. Clean up the IP list file which may already be there from previous deployments

cat /dev/null > iplist  
for app in `cat servername`  
    gcloud compute instances describe $app --zone asia-east1-a | grep 'natIP' | cut -f2 -d : >> iplist   

#Once you get the IP addresses, stop the tomcat and remove the worker folder
for ip in `cat iplist`  
    ssh -o "StrictHostKeyChecking no" jenkins@$ip "sudo service tomcat7 stop && sudo rm -rf /var/lib/tomcat7/work/*"

#Rsync the folders between the FTP folder and the destination server
for ip in `cat iplist`  
    rsync --delete -auvz --rsync-path="sudo rsync" /var/lib/jenkins/webapps/ -e 'ssh ' jenkins@$ip:/var/lib/tomcat7/webapps/
        ssh jenkins@$ip "sudo chown -R tomcat7:tomcat7 /var/lib/tomcat7/webapps/"

#once it's deployed, restart the tomcat service
for ip in `cat iplist`  
  ssh jenkins@$ip "sudo service tomcat7 restart"

Click on Save and build is ready to deploy!


Add ssh key for Jenkins to login into the app server instances, that can be done from google cloud console using Metadata under the compute engine section.

Jenkins servers should have api access enabled as mentioned at the beginning of this post.

FTP location of the server should contain all the WAR files and existing WAR as well while deploying so that it should no conflict with the running one.

Hope this was useful. Happy deploying! :)

Raju Banerjee

Raju is a Cloud Solution Architect - His strengths are AWS and Google Cloud, and all things DevOps. He likes automating boring stuff using Python.

comments powered by Disqus