Deploying a serverless containerized microservice using Google Cloud Platform.
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:
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.?
In the following paragraphs I will describe a microservice that at first only works in a local server and later I will show the steps to deploy the same microservice in Google Cloud Run.
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 https://10.10.10.10:5000
mylocalserver:~$ python myweb01.py
?* Serving Flask app 'myweb01'
?* Running on https://10.10.10.10:5000
Press CTRL+C to quit
Now we can use any web browser to go to https://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 https://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:
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.?
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.
auben@cloudshell:~ (proyecto-362707)$ gcloud services enable cloudbuild.googleapis.com
auben@cloudshell:~ (proyecto-362707)$ gcloud services enable artifactregistry.googleapis.com
auben@cloudshell:~ (proyecto-362707)$ gcloud services enable run.googleapis.com
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”
auben@cloudshell:~ (proyecto-362707)$ mkdir 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”
auben@cloudshell:~ (proyecto-362707)$ cd auben-docker
auben@cloudshell:~/auben-docker (proyecto-362707)$ ls
Arial.ttf? Dockerfile? library_inv.py? mx960.jpg? myweb01.py? readme.MD? requirements.txt? static? templates?
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.
auben@cloudshell:~/auben-docker (proyecto-362707)$ more Dockerfile
FROM python:3.9.15
LABEL Maintainer="Edu"
RUN mkdir /home/templates && mkdir /home/static && mkdir /usr/share/fonts/truetype/msttcorefonts
WORKDIR /usr/share/fonts/truetype/msttcorefonts
COPY Arial.ttf ./
WORKDIR /home
#to COPY the remote file at working directory in container
COPY myweb01.py ./
COPY library_inv.py ./
COPY mx960.jpg ./
COPY requirements.txt ./
COPY ./templates/success.html ./templates
COPY ./templates/upload.html ./templates
RUN pip install -r requirements.txt
CMD [ "python", "./myweb01.py"]
EXPOSE 5000/tcp
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.
auben@cloudshell:~/auben-docker (proyecto-362707)$ gcloud builds submit --tag gcr.io/proyecto-362707/auben-image
Creating temporary tarball archive of 9 file(s) totalling 345.3 KiB before compression.
### output omitted for brevity
CREATE_TIME: 2023-01-12T16:36:58+00:00
DURATION: 48S
SOURCE: gs://proyecto-362707_cloudbuild/source/1673541416.689461-694e44867c554a788297a10a18ef12f1.tgz
IMAGES: gcr.io/proyecto-362707/auben-image (+1 more)
STATUS: SUCCESS
I can verify the image using the command “gcloud container images list”
auben@cloudshell:~/auben-docker (proyecto-362707)$ cd
auben@cloudshell:~ (proyecto-362707)$ gcloud container images list
NAME: gcr.io/proyecto-362707/auben-image
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.?
auben@cloudshell:~ (proyecto-362707)$ gcloud run deploy auben-juniper-mx-to-jpg --image=gcr.io/proyecto-362707/auben-image --port=5000 --memory=512M --region=us-east1 --allow-unauthenticated
Deploying container to Cloud Run service [auben-juniper-mx-to-jpg] in project [proyecto-362707] region [us-east1]
OK Deploying new service... Done.?????????????????????????????????????????????????????????????????????
??OK Creating Revision... Revision deployment finished. Checking container health.
??OK Routing traffic...
??OK Setting IAM Policy...
Done.
Service [auben-juniper-mx-to-jpg] revision [auben-juniper-mx-to-jpg-00001-yiy] has been deployed and is serving 100 percent of traffic.
Service URL: https://auben-juniper-mx-to-jpg-l64q2dxbaa-ue.a.run.app
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.
auben@cloudshell:~ (proyecto-362707)$ gcloud run services describe auben-juniper-mx-to-jpg --region=us-east1
? Service auben-juniper-mx-to-jpg in region us-east1
URL: ? ? https://auben-juniper-mx-to-jpg-l64q2dxbaa-ue.a.run.app
Ingress: all
Traffic:
??100% LATEST (currently auben-juniper-mx-to-jpg-00001-yiy)
Last updated on 2023-01-12T18:00:18.764466Z by auben@:
??Revision auben-juniper-mx-to-jpg-00001-yiy
??Image: ? ? ? ? ? gcr.io/proyecto-362707/auben-image
??Port:? ? ? ? ? ? 5000
??Memory:? ? ? ? ? 512M
??CPU: ? ? ? ? ? ? 1000m
??Service account: [email protected]
??Concurrency: ? ? 80
??Max Instances: ? 100
??Timeout: ? ? ? ? 300s
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.
CONCLUSION
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.
HELPFUL LINKS:
What is Cloud Run
About Artifact Registry
Build and push a Docker image with Cloud Build
Author: Eduardo Aliaga
Telecommunication engineer. Latin-American expert in networking technologies for Service Provider, Enterprise and Security markets. Committed to constant training regarding networking specifications, focusing mainly on high-scale networks design, support, deployment and consultancy.
Network Solution Architect | MSE in Cloud Computing
1 年Tks Eduardo Aliaga!