Developing End-to-End NLP text generator application(part 4) — Deploying app on Kubernetes in Google Cloud

Kevin MacIver
7 min readApr 30, 2020

This is the part 4 of a series of stories to show the steps to develop an end-to-end product to help write news articles by suggesting the next words of the text.

On part 1 we focused on generating a Bidirectional LSTM model, check out this link if you haven’t seen it yet:

On part 2 we focused on creating a full-stack application with FLASK, check out this link if you haven’t seen it yet:

On part 3 we focused on containerizing our application using Docker and deploying it locally. Check out this link if you haven’t seen it yet:

Now that we have our application in containers, we want to deploy it in a server for everyone to have access to. In this project we’ll use google cloud kubernetes to achieve that.

Google cloud has an extensive number of services to use (will touch some of them on part 5). In this story we will work with the following:

Kubernetes Engine

Container Registry

Before we start, if you want to follow up you can register an account on Google Cloud and receive some free credits to start learning and doing some cool stuff.

Once you got your account set, you need to create a new project to start with. And copy your files to the google cloud shell.

Note: you could also use your own machine instead of the google cloud shell, just need to download the google SDK and set the proper authentications.

Note 2: Although this project uses Google Cloud, the same steps could be done in other providers such as AWS, AZURE and IBM. The difference will rely on the names used for the services since they usually change for different providers.

Kubernetes

What is Kubernetes?

From the digital ocean site:

Kubernetes, at its basic level, is a system for running and coordinating containerized applications across a cluster of machines. It is a platform designed to completely manage the life cycle of containerized applications and services using methods that provide predictability, scalability, and high availability.”

Pods and Nodes

Two of the most fundamental components of the kubernetes’ architecture are pods and nodes.

Pods are the atomic unit in Kubernetes, and is a concept that represents a group of one or more application containers. This can be a little confusing and sometimes leads to the question:

“Shouldn't containers be the atomic unit of Kubernetes?”

Well, one answer to that question is that pods are called the atomic unit because during the deployment of kubernetes, that Deployment creates Pods with containers inside them (as opposed to creating containers directly). One of the reasons is that containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.

This brings up to the Node. Pods run inside Nodes.

A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster.

Deploying Kubernetes

Now that we’ve touch a bit of the architecture, we need to understand the concepts of deployments and services.

A deployment is responsible for keeping a set of pods running. A service is responsible for enabling network access to a set of pods.

In order to deploy our project on kubernetes we’ll need to make some few modifications to our files and create a .yaml file to specify how to deploy pods and what service to create.

Creating a Cluster

We’ll start by creating our cluster on google Kubernetes Engine.

We’ll click on create cluster.

Here we can choose our cluster name and location. We can also set up the number of nodes and type of virtual machine that will be running on the nodes.

After creating our cluster, it will take a while to set up.

Note: Creating a cluster has a cost that will be billed to your account or use your credits

Now that our cluster is created, we need to create the docker images that the cluster will use.

Docker Images

As mentioned before, some modifications must be made in order to serve our containers to kubernetes.

Dockerfile modifications:

client/Dockerfile

We modify the Dockerfile to add the EXPOSE command, instead of leaving it on the docker-compose.yml file.

Nginx.conf modification:

nginx/nginx.conf

We add upstream server with a localhost ip and the port exposed in our client container.

client/application/routes.py modification:

client/application/routes.py

Changed the url from “http://api/read_text” to “http://127.0.0.1:8080/read_text”

Now that the modifications are in place, we can run the docker-compose build command to create our container images.

It will take a while to create the images, which then we can list with out with the docker images command.

docker images

We can see the images related to nginx, client and api. We now need to push these images to Google Cloud Container Registry.

But before we do that, in order to have a proper version control for future modifications we will rename the images with the following command:

Rename Docker images

By following this command and running for the 3 containers we end up with:

Images renamed with version:vtest

Now we can push the gcr.* images to Container Registry with the following command:

Push images to Container Registry

Once that is done, we should be able to see our images in the google cloud container registry page

Images in Container Registry

YAML file

Now that we’ve got our images in the container registry, we can deploy our kubernetes using the .yaml file.

As mentioned before, the yaml file is responsible to tell kubernetes how to deploy the pods and what services to execute.

For this project each pod will consist of our three containers, since they will communicate with each other locally.

The yaml file is composed of two parts:

  • The first part describes the service to be deploy and defines it as a load balancer;
  • The second part describes the deployment called app with 1 replica of a pod containing the three images in the Container Registry

With the yaml file ready, we need to connect to our cluster be clicking in the connect button at the google cloud kubernetes engine page:

This will generate a command to allow access to our cluster.

Now we can deploy our app using the following command:

  • kubectl create -f kubemanifest.yaml

It will take a while to generate the pods and services but after that an IP address is created and anyone will have access to your application.

Hurray!!! 👏👏👏👏

Conclusion of Part 4

We got to deploy successfully our app using kubernetes. On part 5 we will see how to create an architecture of a series of services to allow a CI/CD pipeline for our application.

Thanks for reading!

--

--

Kevin MacIver

Driven for innovation, waiting for the robots uprising..