Deploying a Node.js app to the Google Kubernetes Engine (GKE)

Tomorrow Tech Reviews
ITNEXT
Published in
3 min readJun 29, 2019

--

I wanna show you how quickly and easily you can deploy your app to the Kubernetes on Google Kubernetes Engine (GKE).

Create a new Kubernetes cluster

Enter a name for your cluster, location, number of nodes and machine type. Then click More Options.

If you need to automatically add new nodes to the cluster, then click Enable autoscaling.

You can allow access to some APIs from the cluster. For example, I allow access to Google Cloud Storage (GCS).

After your cluster has been created, click Connect.

Now you’ll see a Cloud Shell with the command, press Enter to execute it and get GKE credentials. After that, you can execute any commands with kubectl.

For example, let’s check the nodes of a cluster.

Push docker image to Google Container Registry

I’ll use my GitHub repo which includes a simple Node.js app, Dockerfile, and deployment.yaml. All these commands I’ll enter in Cloud Shell.

git clone https://github.com/sonufrienko/gke-simple-appcd gke-simple-app

Replace [PROJECT_ID] in deployment.yaml with your GCP project ID.

Get credentials to Google Container Registry

gcloud auth configure-docker

Build and push the docker image to Google Container Registry

docker build -t gcr.io/[PROJECT_ID]/app:v1 .
docker push gcr.io/[PROJECT_ID]/app:v1

Deploy docker image to Kubernetes

Deployment YAML file contains two parts:

  • Deployment - describe containers to be deployed
  • Service - will create a LoadBalancer to expose our containers to the internet

Create deployment and service.

kubectl apply -f deployment.yaml --record

Check deployment process

kubectl get deployments

Check pods (containers)

kubectl get pods

Check service and copy external IP address (LoadBalancer)

kubectl get services

Now, you can open in your browser this URL

http://<EXTERNAL-IP>/encrypt?secret=abc&message=i-love-you

How to release a new version?

Push a new version of docker image to the Container Registry, change docker image version in deployment.yaml and then run this command to set desired deployment state.

kubectl apply -f deployment.yaml --record

How to rollback new release?

kubectl rollout undo deployment/my-app-deployment

Useful tips

Check container logs

kubectl logs <POD NAME>

Go inside container

kubectl exec -it <POD NAME> bash

Delete a whole deployment

kubectl delete deployment my-app-deployment

Set horizontal pod autoscaling policy for deployment

kubectl autoscale deployment <DEPLOYMENT_NAME> --max 6 --min 1 --cpu-percent 60

Check horizontal pod autoscaling policy (HorizontalPodAutoscaler object)

kubectl get hpa

Check pod Events to investigate issues with pods deployment, polling docker image, etc.

kubectl get pods
kubectl describe pods/<POD_NAME>

What’s next?

Now you know how to create a GKE cluster, deploy containers, release a new version and rollback, set autoscale policy and how to investigate problems.

You can also read more about these topics:

  1. Manage Secrets
  2. Labels
  3. Namespace
  4. Application health check
  5. other Service types (ClusterIP, NodePort, ExternalName)
  6. Ingress

--

--