We need an app to deploy on our cluster. We will create a simple NodeJS app in this section. If you already have one, you can skip to the next section.
We start by installing the latest long term support version of NodeJS which is the current stable version.
Then we create a directory for our app:
Now we create the two following files:
server.js
file to store our app code:
package.json
file to store the metadata of our app and its dependencies:Finally, we can install the dependencies and run our app locally with:
Head over to localhost:3000 to check out our app.
Kubernetes is a container orchestration platform. This means that to deploy our app on the cluster, we must first package it as a container. Dockerizing our app is also useful for local development, as the containerized app does not depend on the version of the libraries installed on the developer’s computer.
We start by installing docker and docker-compose. Then, to dockerize our app, create the following files:
Dockerfile
file to describe the docker image of our application:.dockerignore
file to have docker ignore the node modules currently installed locally.:docker-compose.yml
file:To build the image described in the Dockerfile file and run our dockerized app with the configuration set in the docker-compose.yml
file, run:
We use docker-compose with a configuration file to avoid using directly the docker command with a lot of command line arguments.
Again, you can open localhost:3000 to check out our app.
The volumes
key in the docker-compose.yml
file and the nodemon dependency gives us live reload of our application in the development environment. This means that you can edit the server.js
file and reload the page in your browser to see the changes take effect without having to rebuild or restart the container.
Helm is a Kubernetes resources package manager, and a package is called a chart. Helm allows us to template our Kubernetes resources manifest files. A standard Helm chart has the following structure:
We will create the following files in a kubernetes directory. The Chart.yaml
file contains the chart’s metadata, it is the equivalent of the package.json
of a NodeJS package.
The values.yaml
file contains the values of the different variables used in our Kubernetes resources manifests templates.
The templates file contains the templates of our Kubernetes resources manifest files.
Let’s go ahead and create those files:
Chart.yaml
file:values.yaml
file where project_name
is your GCP project id:deployment.yaml
template file which describes how our container is to be run in a pod:configMap.yaml
template file in which we set the environment variables for our app:service.yaml
template file which describes the service that is used to expose our app:ingress.yaml
template file that exposes our service to the Internet:Don’t worry, you won’t have to create all those files manually every time you want to create a Helm chart, the helm create <my_chart_name>
command automatically creates a sample chart where you just have to remove or edit the files you don’t need.
Now to deploy the chart we created on our cluster, we need to install Helm and configure it with the cluster.
To create and update resources on the cluster, Helm needs a special system pod named Tiller. To install and configure Tiller on the cluster, create the following tiller.yaml
file:
The resources described in this file allow the tiller pod to create resources in the cluster, apply it with:
We can initialize Helm with:
Now before we can deploy our chart, we need to build the docker image of our app and push it to an online container our Kubernetes cluster can pull it from. We will use Google Container Registry:
And deploy our chart (run from the kubernetes directory):
Now you can find the IP address of your ingress with:
Paste it in your browser to check our app running on our Kubernetes cluster in Google Cloud Platform.
In this article, we have learned how to dockerize a NodeJS app and deploy it on a Kubernetes cluster with Helm. Check our next article in the series Kubernetes on Google Cloud Platform: Automate deployments with Cloud Build. The previous one was about how to build a Kubernetes cluster with Terraform.