Deploying a containerized web application to Google Kubernetes Engine (GKE)

This is a tutorial Guide by Google Cloud, just that I modify the sample web application for the demonstration purpose.

Reference: https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app

  1. Enable your billing account
  2. Create a new project
  3. Associate the new project with the billing account
  4. Enable Google Kubernetes Engine API and register it to the project

  1. Enable Artifact Registry and create credentials (You should see the below upon completion)

  1. download and install Google Cloud SDK

run below in PowerShell
(New-Object Net.WebClient).DownloadFile(“https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", “$env:TempGoogleCloudSDKInstaller.exe&quot😉

it will prompt for the Google Cloud SDK installation and install with the default selection

gcloud Init

login with google cloud from the terminal

Go to Browser to give permission of login

You are now authenticated

back to your command prompt terminal, choose the project you previously created to use.

All other configuration go with default.

  1. use gcloud components to install kubectl

> gcloud components install kubectl

confirming the kubectl is installed

Another 2 installations are required, which my computer has installed previously:
a. docker
b. git
These 2 installation guide are widely available on internet. I will not repeat here.

  1. Alternatively, you may use the Google Cloud native Shell (>_), ssh into your google cloud native shell from your powershell

> gcloud cloud-shell ssh

Note: Try to use your own local command prompt instead of Google Cloud native Shell if you need the port 80 for your web application. Otherwise you might need to troubleshot the “bind address already in use” error due to the port 80

Now we can start either with your local powershell or the Google Cloud Shell.

  1. Configurate local env variable and gcloud command line tool with the project ID

> export project_ID = “your-Project-ID”
> echo project_ID
>gcloud config set project $project_ID

Create a docker repository “os-erp” in asis-southeast1 region

> gcloud artifacts repositories create os-erp
–repository-format=docker
–location=asia-southeast1
–description=”open source ERP Docker repository”

clone the bitnami open source ERP docker images from github

> git clone https://github.com/bitnami/bitnami-docker-odoo.git

go into the folder where the dockerfile is available, either v12/13/14, build and tag the docker image to web app

docker build -t asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v1 .

Check if the docker image is available
> docker image ls
in the same folder, use the docker-compose to run the odoo application

> docker-compose up -d

or manually start run the odoo application using docker

a. create a network
docker network create odoo-network

b. Create a volume for PostgreSQL persistence and create a PostgreSQL container
docker volume create –name postgresql_data

docker run -d –name postgresql
–env ALLOW_EMPTY_PASSWORD=yes
–env POSTGRESQL_PASSWORD=bitnami
–network odoo-network
–volume postgresql_data:/bitnami/postgresql
bitnami/postgresql:latest

c. Create volumes for Odoo persistence and launch the container
docker volume create –name odoo_data

docker run -d –name odoo
-p 81:8069
–env ALLOW_EMPTY_PASSWORD=yes
–env ODOO_DATABASE_ADMIN_PASSWORD=bitnami
–network odoo-network
–volume odoo_data:/bitnami/odoo
bitnami/odoo:latest

  1. Pushing the Docker image to Artifact Registry

gcloud auth configure-docker “Your-REGION”-docker.pkg.dev

Push the Docker image that you just built to the repository:

> docker push asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v1

  1. Creating a GKE cluster

config the the GKE mode of operation as auto-pilot in the compute region

gcloud config set compute/region “Your-Region”

create a GKE auto-pilot cluster
gcloud container clusters create-auto os-erp-cluster

check if the cluster created successfully
kubectl get nodes

  1. Deploying the open source ERP web app to GKE

Ensure that you are connected to your GKE cluster.
gcloud container clusters get-credentials hello-cluster –zone COMPUTE_ZONE

Create a Kubernetes Deployment for your hello-app Docker image.

> kubectl create deployment erp-app –image=asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v1

Set the baseline number of Deployment replicas to 3.
> kubectl scale deployment erp-app –replicas=3

Create a HorizontalPodAutoscaler resource for your Deployment.
> kubectl autoscale deployment erp-app –cpu-percent=80 –min=1 –max=5

Check if the pods created
> kubectl get pods

  1. Exposing the sample app to the internet

kubectl expose deployment erp-app –name=erp-app-service –type=LoadBalancer –port 81 –target-port 8080

> kubectl get service

Note: Some GKE cluster information available via below command
> kubectl cluster-info

As the above deployment is mistakenly deploy with port 81, below we will continue with a rolling update

  1. Deploying a updated version of the application with rolling update

GKE’s rolling update feature lets you update your Deployments without downtime.

Build a new docker image (version 2 ) with latest update

> docker build -t asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v2 .

Push the image to Artifact Registry

> docker push asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v2

Apply a rolling update to the existing erp-app Deployment with an image update

> kubectl set image deployment/erp-app erp-app=asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v2

Monitor the rolling update
> kubectl get pods

Once the cluster has completed the rolling update, you should see the application is now updated.

Note: Remember to terminate the Google Kubernetes Clusters after this to avoid further charges

  1. Clean up of the cluster

a. Delete Services
> kubectl delete service erp-app-service

b. Delete Clusters
> gcloud container clusters delete os-erp-cluster –zone asia-southeast1

c. Delete images

> gcloud artifacts docker images delete asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v1 –delete-tags –quiet

> gcloud artifacts docker images delete asia-southeast1-docker.pkg.dev/gke-web-app/os-erp/erp-app:v2 –delete-tags –quiet

Evernote helps you remember everything and get organized effortlessly. Download Evernote.

c0b7db4c-ca4b-4448-8747-c178699de92e

Related Posts