Deploying your NodeJS app with Docker and Kubernetes on GCP

Campion
Google Cloud - Community
3 min readFeb 23, 2018
Google Cloud Platform

This tutorial will be fairly easy to follow along, but some technical experience is recommended!

Assumptions

  • You already have a NodeJS app
  • You are either using the GCP Cloud Shell or have gcloud , kubectl , and docker installed on your local shell
  • You have a GCP project with the Kubernetes Engine API enabled

Configure your Project

First, we need to configure some settings for you project:

gcloud config set project kube-tutorial-194819
gcloud config set compute/zone us-west1-a

This sets the current project to your project (in this case, mine is kube-tutorial-194819 and then it sets the default compute zone to us-west1-a

Dockerize Your App

Now we’ll need to put your app into a Docker container so we can deploy it to the Kubernetes cluster that we’ll create.

Head to the root directory of your Node app and create a Dockerfile and .dockerignore file using your favorite shell editor:

nano Dockerfile and put this as its contents:

FROM node:carbon

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

This Docker image gets the latest version of Node, then changes directory, copies the package.json file, runs npm install , copies everything over, and runs npm start to start your app listening on port 8080. You can find more details about this here.

Next, we’ll make our .dockerignore file:

nano .dockerignore

node_modules
npm-debug.log

Like .gitignore , a .dockerignore file tells the system which files and directories to ignore when building.

Upload your Docker Image to GCR

Now let’s build our image and deploy it to the Google Container Registry!

docker build -t gcr.io/kube-tutorial-194819/kube-tutorial:latest .

This builds our image with the tag:

gcr.io/kube-tutorial-194819/kube-tutorial:latest

The format for this tag is gcr.io/[PROJECT_ID]/[APP_NAME][:version]

It is important that we start the tag for the image withgcr.io/[PROJECT_ID] so that Docker knows where we will push our image.

Next we’ll go ahead and push to our private Container Registry:

gcloud docker -- push gcr.io/kube-tutorial-194819/kube-tutorial

Deploy to Google Kubernetes Engine

Now that our app is Dockerized, we can set up our deployment.

gcloud container clusters create kube-tutorial
gcloud container clusters get-credentials kube-tutorial

This creates a cluster with the name kube-tutorial and then authenticates our shell to use the cluster.

Finally, we’ll run a deployment on our cluster:

kubectl run kube-tutorial \
--image gcr.io/kube-tutorial-194819/kube-tutorial:latest \
--port 8080

This creates the Deployment and names it kube-tutorial using the image we just pushed to the Container Registry, then specifies the port that we’ll be exposing as 8080. Now let’s expose our Deployment to the world:

kubectl expose deployment kube-tutorial --type "LoadBalancer"

Now our app is deployed! To find the external IP that the Load Balancer gave us, run the command:

kubectl get service kube-tutorial

You’ll may see <pending> as the External-IP, but just wait a minute and re-run the command and eventually you’ll see it. Go ahead and enter that External-IP in to your browser, appending :8080 to specify the port.

Summary

  • In this tutorial, we learned how to Dockerize your NodeJS app.
  • Then we uploaded our Docker image to the Google Container Registry.
  • Finally, we deployed this image to a Kubernetes cluster on Google Kubernetes Engine.

Moving Forward

Next time we’ll learn about versioning with Kubernetes and how easy it is to deploy updates to your application!

If you have any questions, comment below!

--

--