Deploying a newer version of the application on Google Cloud with zero downtime.
By: Avanish Chauhan, Senior Java Developer
Tags: Luxoft, Software Development, Google, Google Cloud Platform, Cloud

This is Avanish Chauhan, having 8+ years of experience in Backend technologies like Java, GoLang and RubyOnRails. For the last one and half years, I have been working with Luxoft as Senior Software developer cum Scrum Master. While working with Luxoft, I got an opportunity to work on different tools and technologies, working on Google cloud is one of them.

As I promised in my last article, that I would be sharing details on how to deploy two versions of an application on Google cloud without bringing down your services or zero downtime. In this post I would be sharing steps to achieve this and the challenges that I faced and how did I fix them.

So, my first requirement was to deploy application version V1 on Google cloud and make it available to everyone. Making available to everyone means providing public access to your application so that anyone with an URL can access it. I had two options to deploy my application on Google cloud. First, I could take the source code from the GitHub or I can use the tar.gz format of my source code. Let’s go one by one:

Option 1: Taking the source code from the tar.gz file directory.

1. Go to Storage buckets on Google cloud console and create a bucket (Give any name)

2. Upload your source code to the bucket using the upload button in your bucket.

3. Click on the upload button to upload the tar.gz file(let’s name it as shopping-app.tar.gz).

4. Click on the uploaded resource and copy the bucket resource path. It would be something like this gs://<project-id>/shopping-app.tar.gz.

5. Open the Cloud shell from Google cloud console.

6. Copy the source code from the bucket to your working project.

7. gsutil cp <bucket resource path> <target directory>

gsutil cp gs://<projectID>/shopping-app.tar.gz .

8. Since this is in tar.gz format, we have to extract the resources.

tar xzvf shopping-app.tar.gz

9. Build the image from the extracted source code.

docker build –t gcr.io/<project-id>/shopping-app:V1 .

10. Push the built image to Google container registry.

docker push gcr.io/<project-id>/shopping-app:V1

11. Configure the compute zone, if not done already.

gcloud config set compute/zone us-central1-b

12. Create the Kubernetes cluster.

gcloud container clusters create shopping-cluster

13. Get credentials from the created cluster.

gcloud container clusters get-credentials shopping-cluster

14. Create a deployment for shopping-app docker image, but before that install “kubectl”, this command is used to communicate with Kubernetes components.

gcloud components install kubectl kubectl create deployment shopping-app –image=gcr.io/<Project_id>/shopping-app:V1

15. To check the running pods at any instance of time:

kubectl get pods

16. Since our application was supposed to be accessed by everyone having internet, so we had to expose our application to http.

kubectl expose deployment shopping-app –name=shopping-app-service –type=LoadBalancer –port 80 –target-port 8080

17. Get the service details.

kubectl get service

It will return in this format:

NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE

EXTERNAL-IP is used to access the application from the internet.

Option 2: To deploy the source code from BitBucket/GitHub or any other source code repository, use the following command to checkout/clone your source code into your working project:

Git clone <code repository url>

Once you have your code in your working project in Google cloud shell, remaining steps would be the same.

Challenges that I faced:

1. Issue while copying as I haven’t set the project correctly.

Solution: Set the project and zone by using gloud config commands

Export PROJECT_ID = <your project id> gcloud config set project <Your_Project_id>

Set the zone:

gcloud config set compute/zone us-central1-b

2. Proper permissions were not set on the bucket that I created.

Solution: Make sure to set required permissions on your bucket and uploaded resources, this can be done very easily by using the google cloud console.

Now I was having my application running successfully on the internet and there were many users accessing it without any issue. It was generating a good amount of traffic as there were a huge number of users using my application. After a few months, we got one new request from the client to make some changes in our current running application. We made those changes and our application was ready to deploy, but, we had a challenge; deploy the newer version of the application without bringing down the currently running application and without impacting the current users. To address this problem, Google cloud’s Kubernetes engine and traffic splitting functionality proved to be a boon for us. So, what exactly I did, please see below:

1. I build the docker image of my new source code by following the commands that I have mentioned earlier:

docker build –t gcr.io/<project-id>/shopping-app:V2 .

2. Pushed the built docker image to the Google container registry:

docker push gcr.io/<project-id>/shopping-app:V2

3. Update the existing deployment so that I would be pointing to the new application:

kubectl set image deployment/shopping-app shopping-app =gcr.io/<project-id>/shopping-app:V2

4. Get the details of the running services:

kubectl get service

If you try to access the external IP, you will see that now it is pointing to the new application. So, to conclude, Google cloud provides the functionality to deploy your application from code repositories or even from your zipped source code. Also, using Google Kubernetes Engine, it is very easy to deploy a newer version of the application with zero downtime and no maintenance window.

In my next article, I would be focussing on scaling up of the applications using replicas and virtual machine instances.

Happy Learning :)