Kubefeeds Team A dedicated and highly skilled team at Kubefeeds, driven by a passion for Kubernetes and Cloud-Native technologies, delivering innovative solutions with expertise and enthusiasm.

Deploy Backend API to Google Kubernetes Engine

2 min read

The goal is simply to write a NodeJS API and turn it into a replicated application running on Kubernetes, which is running on Google Kubernetes Engine.

Important Note: This article uses Google Cloud Console, gcloud shell, Compute Engine, Google Container Registry, Google Kubernetes Engine.

System Architecuture

Image description

What is Kubernetes

Kubernetes, also abbreviated as K8s (with ‘8’ representing the number of letters between ‘K’ and ‘s’), is an open-source project originally developed by Google. It is now managed by the Cloud Native Computing Foundation (CNCF). Kubernetes provides a robust way to run scalable and highly available applications.

From the Beginning

Let’s build the API

  1. Create your app file e.g app.js
  2. Put the code below
var http = require('http');
var handleRequest = function(request, response) {
  response.writeHead(200);
  response.end("Hello World!n");
}
var www = http.createServer(handleRequest);
www.listen(8080);
  1. Execute it by running “node

we chose the file name to be app.js, therefore we will run node app.js and we can find the app running on port 8080

Output:
Image description

Supercharge with Docker.

Kubernetes can run Docker containers; therefore, we need to containerize our app. To run a containarize application we need a Dockerfile.

  • Simple container lifecycle: Dockerfile > Docker Image > Docker Container. A built dockerfile produces a docker image and a running docker image produces a docker container. In this article, the container will be running on cloud and managed by kubernetes.
  1. Create Docker file with: a file with name Dockerfile and put the below code.
FROM node:6.9.2
EXPOSE 8080
COPY server.js .
CMD node server.js
  1. Build the docker file to get a docker image
  2. Run the docker image to get a docker container to test out locally.

Output on the cloudshell:
Image description

Output on the browser:
Image description

Pushing the image to the registry

  1. Authenticate your registry be it GCR,ECR, Dockerhub or ACR
gcloud auth configure-docker
  1. Push to the registry
docker push gcr.io/PROJECT_ID/hello-node:v1

Output on the cloudshell:
Image description

The image pushed to the registry can be confirmed by checking the registry via the Google cloud console.

Output on th cloud console registry:

Image description

Image description

Creating the Kubernetes cluster

  1. Confirm project ID config
gcloud config set project PROJECT_ID
  1. Creating the nodes
gcloud container clusters create hello-world 
                --num-nodes 2 
                --machine-type e2-medium 
                --zone "us-central1-c"

2 nodes with machine type to be e2-medium and the us-central1-c zone
Output:

Image description

this will take 2-3 minutes to complete depending on my factors, so be patient.

Creating a pod

Running nodejs image stored in the registry serving content at port 8080. We need away to bring the docker image stored in the registry into the kubernetes cluster created and this can be done by creating a pod and run the docker image inside of it.

  1. kubectl command to create a deployment pod
kubectl create deployment hello-node 
    --image=gcr.io/PROJECT_ID/hello-node:v1

the command create a deployment in the cluster referencing the docker image stored in the registry.

Output:
Image description

  1. show the pod created
kubectl get pods

Output:
Image description

Let’s allow external traffic from customers and internet

In order to make the hello-node container accessible from outside the Kubernetes virtual network, we need to expose the pod as a Kubernetes service.

  1. Add a load balancer to add an external IP
kubectl expose deployment hello-node --type="LoadBalancer" --port=8080

Output:
Image description

  1. Finding the IP address accessible everywhere
kubectl get svc

Output with a focus on the external IP on the hello-node:
Image description

  1. Check the browser with the IP.
    > If you put the IP address in the browser, you will get something like below which is the response from the API we are deploying

Output:
Image description

Scale up the services easily

Suppose you suddenly need more capacity. You can tell the replication controller to manage a new number of replicas for your pod with the kubectl scale command.

  1. Increase the number of pods to 4
kubectl scale deployment hello-node --replicas=4

Output:
Image description

  1. Confirm the number of replicas
kubectl get deployment

Output:
Image description

  1. See the running pods managed by the deployment
kubectl get pods

Output:
Image description

  1. Where we are
    Everything we’ve done so far is replicated in the diagram below for easy understand and a help to build the right mental model.

Image description

Roll out an upgrade to your service

This is done without impacting users, that’s the power of kubernetes

  1. Add the feature: we added another endpoint return
  2. Rebuild the docker image
  3. push new version to docker registry

a. To interact with running cluster, you edit the k8s

kubectl edit deployment hello-node

Point the spec.template.spec.containers.image to the new version of the image just pushed to the registry.

Image description

Image description

While this is happening, the users using the services wouldn’t see any interruption. After a little while they’ll start accessing the new version of the application i.e the API responding with Hello Kubernetes World!.
These deployment, scaling, and updated features, are managed by K8s once Kubernetes Engine cluster is set up, It’ll be clear that Kubernetes will help you focus on the application rather than the infrastructure.

This is a part of a series. take note!

Kubefeeds Team A dedicated and highly skilled team at Kubefeeds, driven by a passion for Kubernetes and Cloud-Native technologies, delivering innovative solutions with expertise and enthusiasm.