Publishing Microservices into Google Kubernetes Engine (GKE)

Publishing Microservices into Google Kubernetes Engine (GKE)

I’ll cover here the Google Cloud configuration that I made to run my project as cheap as possible.

Originally published at renanfranca.github.io

I will show you how to publish the Mamazinha Baby Care Web App - Open Source Project on Google Kubernetes Engine (GKE)

Before we start

I used JHipster to build the web app, everything you need to know about how to generate the Kubernetes (k8s) yml files to publish on GKE is detailed step by step in this tutorial (I strongly recommend watching the video first before getting your hand dirty 😁) created by @mraible

Publishing on GKE

I open source the yaml files that i used to publish on GKE: renanfranca/mamazinha-k8s

I’ll cover here the Google Cloud configuration that I made to run my project as cheap as possible. My goal is to keep the cost below or near the Always Free tiers:

The GKE free tier provides $74.40 in monthly credits per billing account that are applied to zonal and Autopilot clusters. If you only use a single Zonal or Autopilot cluster, this credit will at least cover the complete cost of that cluster each month. Unused free tier credits are not rolled over, and cannot be applied to any other SKUs (for example, they cannot be applied to compute charges, or the cluster fee for Regional clusters).

Initial setup

So I made this pricing simulation (I choose São Paulo as my Region because I am from Brazil):

Pricing simulation

cloud.google.com/products/calculator/#id=d2..

After logging in at Google Cloud Console, Let’s create the Cluster Zone and the first Node Pool

image

Open GKE Standar Option

image

Choose those options. Only edit the cluster name and the cluster zone (choose the nearest zone from your location or your client location)

image

You will end up with something like this

Now you have to connect to your cluster. Install google cloud SDK and install Google kubectl (I'm using windows) gcloud components install kubectl

On git bash:

  1. confirm that the installation was successful wich kubectl and the response should be something like
    /c/Users/AccountName/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/kubectl
    
  2. execute the following command to connect to your cluster, change mamazinha to your cluster name and change southamerica-east1-a to your zone gcloud container clusters get-credentials mamazinha -zone=southamerica-east1-a

You can now publish the yml files to GKE:

  1. clone the repository git clone git@github.com:renanfranca/mamazinha-k8s.git
  2. Switch to google-cloud branch git checkout google-cloud
  3. On git bash go to the root of the cloned project folder and run the command ./kubectl-apply.sh -f

Install the amazing k9s k9scli.io/topics/install and type the command to connect to your cluster k9s -n mamazinha

image

k9s home screen

As you can see only 3 nodes weren't enough for my project, after digging around I discovered the preemptible VM option 😆! The registry/baby microservice both are stateless, so I don't care if after 24h the VM will be destroyed and they will be initialized at another VM.

Let’s create a new node pool with a preemptible VM which costs less than the regular machine type.

image

Go to Cluster Kubernetes Engine > Clusters > Clique on your cluster > clique on ADD NODE POOL

image

Only one node number

image

You must mark the node as preemptible! Then click on CREATE! 🤩

image

Now we can see 4 nodes and only one preemptible

image

That’s what happened after 24h! Shutdown the preemptible VM 😜

The cluster is ready! But how to choose the place to deploy each component of my project? The Postgres database can’t be deployed at the preemptible node, because the data will be lost every 24hrs!

The answer is NodeAffinity, let’s take the gateway as an example. I want to always deploy the gateway on the node gke-mamazinha-pool-small-cost-03fa0890-zqn0

  1. Create a label for the node kubectl label nodes gke-mamazinha-pool-small-cost-03fa0890-zqn0 app=gateway
  2. Add this nodeAffinity property to my yml file

image

gateway-deployment.yml
  1. Change toreplicas: 0 and apply the deployment yml to terminate every gateway running instances kubectl apply -f gateway-k8s/gateway-deployment.yml -n mamazinha

  2. Change back to replicas: 1 and apply again kubectl apply -f gateway-k8s/gateway-deployment.yml -n mamazinha

  3. Repeat this for the other components if you want to deploy at a specific node.

Here is the real cost of this configuration. I am from Brazil, so the costs are in my country's currency. You can do the conversion by knowing that $1 Dollar was R$5,40 Reais when I capture the print screen from the following billing report.

image

Mamazinha Google Cloud Project billing report

Final setup

The preemptible VM cost 80% less than the regular machine, so my goal was to use only preemptible. The only way I figured it out to do this is to create persistent volume on my kubernetes cluster for each Postgres database because after 24h even the Postgres could be shut down without losing data.

First I port-forward the Postgres pods to backup the data using pgAdmin then I defined the persistence volume in the yml file:

image

gateway-postgresql.yml (partial)

The next step was to delete the regular machine-type node pool then I resize the preemptible node pool to have 4 nodes.

After that, I apply both yml files ( kubectl apply -f baby-k8s/baby-postgresql.yml -n mamazinha and kubectl apply -f gateway-k8s/gateway-postgresql.yml -n mamazinha) and restore the backup using pgAdmin that will be stored at the persistent volume.

With this approach, my web app is going to have downtime every 24hrs. To mitigate that I decided to deploy everything simultaneously so after 24h everything will be shut down and going up in 2 minutes.

image

All pods running on preemptible nodes at the same time

The final cost

According to Google Pricing, the cluster manager fee will be free.

After using only preemptible nodes the cost was reduced a lot.

image

Mamazinha billing report after using only preemptible nodes

The invoice from January 2022 was R$844,09 (~$162.14), I don’t think it is too expensive. But I can’t afford this value by myself.

image

The cost from January 2022

So I couldn't keep the web app online on Google Cloud.

image

The cost until I shut down the web app

How was my experience with Google Cloud?

Google cloud platform is easy to use and I set things up quickly. I could learn while I was using it, I could undo, destroyed and build easily 😆

I think Google Cloud should have a real free plan for small microservices applications, so the user will pay when the product gain traction and started to be used ☺

Now it's your turn! How was your experience with Google Cloud?

I will enjoy to read your comments 🤗

If you have any question feel free to ask me anything here or Direct Message (DM) me on Twitter @renan_afranca (follow me and I will follow you back to unlock the DM option).

Did you find this article valuable?

Support Renan Franca by becoming a sponsor. Any amount is appreciated!