WHAT IS GCP (GOOGLE CLOUD PLATFORM) AND WHAT IS SERVERLESS?
GCP (Google Cloud Platform) is one of the top 3 biggest cloud providers in the world.
GCP has different kind of products such as GCE (Compute Engine), GKE (Kubernetes Engine), Cloud Run, etc.
All of them are great products with different use cases.
Let's say our use case is a serverless application or microservice. A brief description of serverless is:
• The focus is the application. The servers are abstracted away by the cloud provider.
• Scale to zero: If the application is idle then it shouldn't use any resource.
Google Cloud Run is a great fit for the serverless use case. Google Cloud Run enables you to deploy microservices by using serverless HTTP containers. It supports fast autoscaling and scaling to zero. If the microservice is not used then you don't have to pay.
In this article it is assumed that the reader has a basic knowledge of:
• Docker and Dockerfile.
• Google Cloud Shell which is one of the main tools to manage Google Cloud products.
EXAMPLE OF MICROSERVICE DEPLOYED IN A LOCAL SERVER
The following example is a microservice that reads the command "show chassis hardware | display xml" from Juniper routers. This is a command that displays in text the hardware inventory of the router. Then the microservice parses the inventory information and draws a picture of the router chassis including hardware details such as the position and model of every linecard, routing engine and fan trays.
This microservice uses a Python framework called Flask. The microservice starts after running the command "python myweb01.py". Below we see the URL of the microservice is http://10.10.10.10:5000
Now we can use any web browser to go to http://10.10.10.10:5000. After clicking the "Browse.." button we can select a file that has the output of the "show chassis hardware | display xml" command. After selecting the file we need to click the "Upload File" button.
After clicking the "Upload File" button we are redirected to http://10.10.10.10:5000/result There we can see a JPG image with the hardware information which is the result of the parsing done by the microservice.
MIGRATION OF THE MICROSERVICE FROM A LOCAL SERVER TO A SERVERLESS CONTAINERIZED MICROSERVICE IN GCP (GOOGLE CLOUD PLATFORM)
To migrate the microservice I use the following GCP products:
• Cloud Build: used to build a container image
• Artifact Registry: used to create a repository and store the container image
• Cloud Run : used to deploy the container image as a serverless microservice
I use Google Cloud Shell to manage all products mentioned. Please notice in Google Cloud everything is done within a project. My project is called "proyecto-362707" which is seen in the prompt of Cloud Shell in the following examples.
1. ENABLING NECESSARY GCP APIS
Each product in GCP has an API. So the first task is to enable each API.
The command "gcloud services enable" is used to enable the necessary APIs.
2. UPLOADING SCRIPT FILES TO CLOUD SHELL
My microservice has several files. I can use Cloud Shell Editor to easily upload all files needed. First I create a new directory called "auben-docker"
Then in Cloud Shell Editor I right-click the directory and select "Upload Files …"
I can verify all uploaded files by using "cd auben-docker" and listing the files using "ls"
3. CREATING A DOCKER CONTAINER USING CLOUD BUILDER
Cloud Build supports several advanced and automated ways to create containers. In this example I use the simpler way by using a file called "Dockerfile". This is the same way a container is created in any local server. The content of "Dockerfile" is easy to read and it describes every step to build a container. Below I show the content of my Dockerfile.
Now I use Cloud Build to create a docker image. The full tag of the image is "gcr.io/proyecto-362707/auben-image", where "gcr.io" is one of the default repositories from Artifact Registry, "proyecto-362707" is the name of the project within Google Cloud, and "auben-image" is the name of the docker image.
Please notice that Cloud Shell's current directory 'auben-docker' must have the file called Dockerfile. Cloud Build automatically detects any Dockerfile if it is present in the current directory.
I can verify the image using the command "gcloud container images list"
4. DEPLOYING A PUBLIC WEB MICROSERVICE USING GCP CLOUD RUN
To deploy the container I use "gcloud run deploy". The name of the service is "auben-juniper-mx-to-jpg". The option "--allow-unauthenticated" permits anyone to use the microservice without authentication.
I can use "gcloud run services describe auben-juniper-mx-to-jpg" to check if the service is up. The service URL is https://auben-juniper-mx-to-jpg-l64q2dxbaa-ue.a.run.app and was automatically created by Cloud Run.
Now I can use to any browser to go https://auben-juniper-mx-to-jpg-l64q2dxbaa-ue.a.run.app
I verify that the microservice works exactly the same way as used in the local server.
Cloud Run is a great product to deploy a serverless containerized microservice. It is easy to understand and to use. And if your microservice is still not containerized then Cloud Build is another great product at your disposal to create your own container images.
What is Cloud Run
About Artifact Registry
Build and push a Docker image with Cloud Build