Skip to content

How to spin up a Google Cloud instance and run a web application in it by using Ansible

docker logo google cloud platform jenkins_8

About me

My name is Farid Amirli and I recently joined the Devoteam family as a DevOps trainee. I joined the company right after graduating from the University of Groningen with an economics degree. Although I have studied economics, I have always been interested in following a career in the IT sector. I have been constantly amazed by how fast the IT sector is growing and the new technologies that emerge. But that is not the only reason why I wanted a career in IT. One of the biggest reasons for me to follow a career in IT was that the IT industry is a perfect path for those looking for constant challenges in their work.

After deciding to follow a career in IT, I needed to choose a company that would align with my interests. After some profound research, I decided that Devoteam was the company to work at. The biggest reason for me to follow a career at Devoteam was that it really offered super learning possibilities and great work & life balance. I should honestly admit that so far, I have enjoyed being the part of this family.

Introduction

Since the first day of work at Devoteam, I have learned new technologies and skills. I have also been constantly challenged with assignments which I enjoyed. One of the challenges I have recently finished was about provisioning: How to test and deploy an application using AWS, Ansible and Docker? And how to automate the whole process in a Jenkins pipeline? As every trainee was given an option to choose the cloud platform to finish the assignment, I asked myself the following question? “Why don’t I try a different cloud platform than everyone before me, who have chosen AWS?” In the end, I decided to go for Google Cloud Platform (GCP).

There are couple of reasons why I chose GCP to finish the assignment. First, Google recognized Devoteam as the 2018 Google Cloud EMEA Partner of the Year for helping Google Cloud customers achieve success across various services. Second, in my opinion GCP offers more consistent compute performance, cheaper machines, pre-emptible machines and better committed usage stories, to name a few comparisons to AWS. Third, I do like to expose myself to challenges and explore unknowns. Although these reasons led me to choose GCP to start with, during the assignment, I just learned that AWS and GCP are more similar than different. Please take into account that I approach the topic from the given assignment perspective. This means that I don’t deny the possible big differences between two platforms. Hence, I can only compare AWS and GCP in a given traineeship assignment scope.

As the assignment required the use of Ansible to spin up a cloud instance and Docker to start the application in the provisioned instance, I will discuss Ansible modules for managing Google Compute Engine (GCE) resources and Docker components to have a web application running in GCE. As a final step, I will also discuss how we can automate the whole process of application testing and deploying in GCE by using a Jenkins pipeline.

Server/Configuration management: Ansible

In this part of the assignment, I was required to spin up a cloud instance by using Ansible. Ansible is an open source software that automates software provisioning, configuration management, and application deployment. It’s in fact one of the simpler tools to get started with, and you can use it on just about any Linux machine.

The nice thing about Ansible is that we can use our entire Ansible code base in both the AWS and GCP environments without having to change any of our actual code. The only things that we need to change are generally our dynamic inventory scripts, which are just Python scripts that Ansible executes to find the machines in your environment. Ansible allows you to use multiple of these dynamic inventory scripts simultaneously, allowing us to run it across both clouds at once.

In general, Ansible code is written in playbooks and playbooks are written in “. yml” format. They are like a to-do list for Ansible that contains a list of tasks. Playbooks contain the steps which the user wants to execute on a particular machine. The following diagram sketches how the Ansible playbooks can be used to spin up a Cloud instance:

provisions-cloud-instance

 

 

 

 

 

 

For the assignment, I referred to the official website of Ansible for writing an Ansible playbook. After executing the Ansible playbook, I was able to provision a GCE instance.

Containers: Docker

The next part of the assignment was to use a container to have a certain application running in a provisioned cloud instance (later, to automate by use of Ansible). Containers are a solution to the problem of how to get software to run reliably when moved from one computing environment to another. This could be from a developer’s laptop to a test environment, from a staging environment into production, and perhaps from a physical machine in a data center to a virtual machine in a private or public cloud.

docker-container-technology

In fact, the most successful container technology is Docker. It has become synonymous with container technology. Docker makes it easier to create and deploy applications in an isolated environment. It also allows us to build a single shippable artifact for a service that we can run on any Linux system.

Before discussing how I fulfilled the requirements of the given assignment by using Docker, I want to explain how Docker works. The main component of Docker is a Dockerfile. A Dockerfile is a script that contains collections of commands and instructions that will be automatically executed in a sequence in the Docker environment for building a new Docker image. Later, the Docker image is used to run the Docker container.

To complete this part of the assignment and to better understand Docker, I downloaded the famous Game of Life web application and dockerized it. Once the Docker container for the Game of Life web application was running inside the GCE instance, I was able to access the application by specifying the external IP address of the provisioned GCE instance and the port:

game-of-life-web-applicationHaving Game of Life application running in Google Cloud instance, the next question was how I could automate this process by using Ansible? There were at least two ways available for me. The first one was to directly create an image and run the container in separate tasks in Ansible playbook. This process is in fact very similar in AWS. The second way is that we first build a Docker image from a Dockerfile in our local computer and then push the created image to the Docker hub. After this step, we can pull the created image from the Docker hub to our Google Cloud instance and run a container from the image in Google Cloud instance. We can do so by specifying metadata in our playbook. To complete this part of the assignment, I followed the second step. In addition to running the Docker container, I also specified a static IP address within the GCE instance from which the web application can be accessed. The overall procedure can be described in a following diagram:

google-cloud-platform

 

Continuous integration: Jenkins

The last part of the assignment was to test the application and then deploy it to production by using the Jenkins Pipeline. Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository. This is the foundation of “Pipeline-as-code”; treating the continuous delivery pipeline as part of the application to be versioned and reviewed like any other code. A Jenkinsfile can be written using two types of syntax – Declarative and Scripted. To finish the assignment, I first wrote the declarative Jenkinsfile. Next, I pushed the file to source repository (Bitbucket). Finally, I built a pipeline by specifying the source repository in Jenkins. By using the Jenkinsfile, Jenkins started the Game of Life application in a provisioned Google Cloud instance, tested it and then deployed it to production. The process is described in the diagram below:

jenkinsfile-diagram

 

Conclusion

The main takeaway from the assignment for me was that there is hardly any difference between GCP and AWS. The only difference was that I needed to write the Ansible playbook somewhat differently. However, this difference was pretty subtle. The main components of the Ansible playbook to spin up a GCE instance and AWS instance were still similar. However, it is also worth to note that I have only compared AWS and GCP in the scope of the given assignment.

References:

https://docs.ansible.com/ansible/latest/modules/list_of_cloud_modules.html#google

https://docs.docker.com/engine/reference/commandline/docker/

https://cloud.google.com/docs/

Other relevant DevOps content