Containerised app on Google Kubernetes - the easy way!
There are multiple ways to deploy an application to Google Kubernetes, with limited experience it can rather become overwhelming to go through the documentation. Hence, I compiled this step by step guide hoping it will help people who might be having similar trouble like me in getting started with Google Kubernetes deployments.
This guide assumes that you have a containerised application ready for deployment.
If you would like to learn the basic of Kubernetes, I would recommend this blog Kubernetes 101 (my absolute favourite!).
Here's my 5 step guide:
- Create a project in Google Cloud Platform (GCP) with billing enabled.
- Install/Enable pre-requisites packages.
- Prepare your application and push it to GitHub.
- Setup Google cloud build.
- Deploy container artefact to kubernetes cluster.
Create a GCP Project with billing
Visit create a project page on GCP and create a project and enable billing.
Install/Enable pre-requisites packages
This guide assumes you have setup docker on your local and you are able to build and run a docker image locally.
Install gcloud SDK on your computer, more details on Google cloud page.
Verify if you can run this command on your shell.
2. Kubernetes (
brew install kubernetes-cli
Verify installation using:
3. Enable gcloud kubernetes component This component will let kubectl communicate with gcloud.
gcloud components install kubectl
4. Set gcloud config to your project and compute zone
gcloud config set project rahul-my gcloud config set compute/zone asia-southeast1-b
Enable Kubernetes Engine API Go to https://console.cloud.google.com/apis/library/container.googleapis.com and enable the API, this may take upto a min. Let it run in the background.
Meantime validate you can see correct project_id and zone by running
gcloud config list.
After this step your computer is ready to create clusters, deploy applications and scale your kubernetes clusters using kubectl command.
Prepare your application and push it to GitHub
Let's setup our application and push it to github. You should verify you can run your application on your local using
docker run command.
I have compiled a quick demo application with Nodejs here:
https://github.com/rd4704/nestjs-api. Feel free to use it or deploy your own. Rest of the tutorial will work with deploying this sample application.
You can test if the application docker image is working fine in your local using following commands:
git clone https://github.com/rd4704/nestjs-api cd nestjs-api docker build -t nestjs-api . docker run -it -p 3000:3000 --name=nestjs-api nestjs-api
visit http://localhost:3000/docs to check if your API documentation is accessible in the browser.
After this step, you have your application ready to go live! Just push your application to your github account.
Setup Google cloud build
Visit https://console.cloud.google.com/cloud-build/builds and enable Cloud Build API.
Now Go to Triggers and
create a trigger. And select Github, you maybe asked to authenticate and give access to google cloud platform to your github account repository.
Then select the repository you would like to set a build trigger to. in our case it's
Next, trigger settings: Here we will set a trigger to build the docker image every time there is a push to master branch. We can name it anything in my case I did
Push to master branch and select the master branch as the main branch to trigger build and chose
Dockerfile as build configuration parameter.
Few important things here to note;
- The image name is created as the repo-path and tag version is set to the latest commit_sha in your git repository. So at anytime if you would like to deploy an older version you can do that by using the older commit_sha from your git repository, which makes it pretty easy to rollback in case of a bad build goes to production.
- You can manually trigger a build by clicking on the
Run Triggerbutton in the Google cloud build triggers menu.
- In the history section you can see list of all the builds.
- You can see list of all successful docker containers in the
Google Container Registry (GCR)(https://console.cloud.google.com/gcr). From there you can get the docker pull command by clicking on the build name for your latest built container which we will deploy to our kubernetes cluster.
- Keep the copy of docker pull image path; in our case
gcr.io/rahul-my/github.com/rd4704/nestjs-api:2a61420b42e202e31acd3e77c5c50362470ef567. Note: you can also copy this path from Cloud build history's artefacts section.
At this step your application artefact is ready for deployment.
Deploy container artefact to kubernetes cluster
Finally, you have everything you need to take your application to kubernetes cluster. Now we will follow theses steps:
Create a smallest available cluster with 3 micro-instances on GCP with name
api-cluster(you can name it anything). This step may take several minutes to complete.
gcloud container clusters create api-cluster --zone asia-southeast1-b --num-nodes 3 --machine-type f1-micro
It should output the name of cluster with master_ip and nodes status on completion. You can verify 3 micro nodes are running in our cluster by running:
gcloud compute instances list
Create a Kubectl deployment with our built container
kubectl create deployment nestjs-api --image=gcr.io/rahul-my/github.com/rd4704/nestjs-api:2a61420b42e202e31acd3e77c5c50362470ef567
It should output
- Expose your app to the internet using:
kubectl expose deployment nestjs-api --type=LoadBalancer --port 80 --target-port 3000
It should output
service/nestjs-api exposed. This step creates a Cloud Load Balancer and deploys the service onto it, which later is exposed to the pubic Internet using an external IP and port 80.
It will take few seconds before your application will be able on the Internet.
To check running pods and services you can use the following commands respectively:
kubectl get pods kubectl get service
From here, you can copy the External IP of your service
nestjs-api and paste it in your browser and your should be able to hit your api. In my case it is http://184.108.40.206/docs which outputs this:
Deploying a new build version
Once you have a new version of your application it is pretty simple to deploy using the following command:
kubectl set image deployment nestjs-api nestjs-api=gcr.io/untitled-48791/github.com/rd4704/nestjs-api:$NEW_COMMIT_SHA
It takes few seconds before your new application version starts serving, which can be checked by running
kubectl describe deployments
Which tells if the latest version image is deployed.
Cleaning up your deployment
- Delete the Service: This step will deallocate the Cloud Load Balancer created for your Service:
kubectl delete service nestjs-api
- Delete the container cluster: This step will delete the resources that make up the container cluster, such as the compute instances, disks and network resources.
gcloud container clusters delete api-cluster
Further Steps; deployment steps can be easily automated using a CI/CD tool where the tool gets the latest master branch version and runs the
kubectl set image command with the latest
COMMIT_SHA on every successful Google Cloud build.