Node.js Development Workflow for Kubernetes - Basic Setup (Part 1) | Coder Society

Node.js Development Workflow for Kubernetes - Basic Setup (Part 1)

Learn how to use kubectl and Minikube to run your containerized Node.js application in Kubernetes.

Kentaro Wakayama

Kentaro Wakayama

September 27, 2017

Node.js Development Workflow for Kubernetes - Basic Setup (Part 1)

Minikube is a single node Kubernetes cluster which runs on your laptop in a virtual machine. It provides the possibility to run applications in a production-like environment and avoids the need to use Docker Compose for local development.

In this article series we will demonstrate how to run, debug and set up live reload for your containerized Node.js application in Kubernetes.

You can find the code which we use in this article on GitHub: https://github.com/coder-society/node-minikube-workflow

Install Minikube and kubectl

On OS X you can install Minikube with Homebrew. You can find installation instructions for other platforms here.

$ brew cask install minikube

We use kubectl to interact with our Kubernetes cluster. It can also be installed with Homebrew. You can find installation instructions for other platforms here.

$ brew install kubectl

You should now be able to start Minikube:

$ minikube start Starting local Kubernetes v1.7.0 cluster... Starting VM... Getting VM IP address... Moving files into cluster... Setting up certs... Starting cluster components... Connecting to cluster... Setting up kubeconfig... Kubectl is now configured to use the cluster.

Verify the installation by using kubectl to get the running pods and nodes:

$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system heapster-5z549 1/1 Running 0 5m kube-system influxdb-grafana-f91mq 2/2 Running 0 5m kube-system kube-addon-manager-minikube 1/1 Running 0 5m kube-system kube-dns-910330662-8zdn7 3/3 Running 0 5m kube-system kubernetes-dashboard-645fn 1/1 Running 0 5m
$ kubectl get nodes NAME STATUS AGE VERSION minikube Ready 6m v1.7.0

Basic Setup

Clone the repository and cd into the 1-basic-setup directory. Have a look at our Makefile. It provides some convenient commands to interact with Docker and Minikube:

$ git clone [https://github.com/coder-society/node-minikube-workflow.git](https://github.com/coder-society/node-minikube-workflow.git) $ cd 1-basic-setup

$ make create

The make create command connects to the docker host running in the Minikube VM. It builds the docker image and applies the Kubernetes manifest files to the Minikube cluster.

$ make create Sending build context to Docker daemon 51.7MB Step 1 : FROM node:8-alpine ---> 7a779c246a41 Step 2 : WORKDIR /src ---> Using cache ---> bf82d455b228 Step 3 : COPY package.json /src/ ---> Using cache ---> 971f200e90a2 Step 4 : COPY package-lock.json /src/ ---> Using cache ---> 47fed9193df0 Step 5 : RUN npm install ---> Using cache ---> 142db6f23946 Step 6 : ADD . /src ---> b10ee47e048b Removing intermediate container 816dd54c1523 Step 7 : EXPOSE 3000 ---> Running in 1fa3c746d63d ---> 5bac6c4d188a Removing intermediate container 1fa3c746d63d Step 8 : CMD npm start ---> Running in e7e4fa77cb1a ---> 3063f2d0165e Removing intermediate container e7e4fa77cb1a Successfully built 3063f2d0165e kubectl apply -f kubernetes/ deployment "node-api" created service "node-api" created

Use can useminikube service node-api --url to retrieve the API URL. Make a curl request to the API:

$ curl $(minikube service node-api --url) Hello World

$ make update

The make update command builds the image with a timestamp in the tag name (e.g. tmp-1505147929). The Kubernetes deployment image gets updated via the kubectl set image command.

Open index.js and change the reply value of the route to 'Hello Kubernetes'.

server.route({ method: 'GET', path: '/', config: { handler: (res, reply) => { reply('Hello Kubernetes'); }, description: 'Hello World route', tags: ['api'], }, });

Run make update to update the application in Minikube.

$ make update docker build -t codersociety/node-api:tmp-1505148570 . Sending build context to Docker daemon 51.77MB Step 1 : FROM node:8-alpine ---> 016382f39a51 Step 2 : WORKDIR /src ---> Using cache ---> deff3e9a115e Step 3 : COPY package.json /src/ ---> Using cache ---> e2ab0b9845a9 Step 4 : COPY package-lock.json /src/ ---> Using cache ---> c815960d9ac8 Step 5 : RUN npm install ---> Using cache ---> b86c65ff6454 Step 6 : ADD . /src ---> 29c7a32c8d0a Removing intermediate container a12fd5b4f143 Step 7 : EXPOSE 3000 ---> Running in 782644f6b740 ---> d56d725f3f2b Removing intermediate container 782644f6b740 Step 8: CMD npm start ---> Running in 16d8f3d2d0dc ---> 6659294b267d Removing intermediate container 16d8f3d2d0dc Successfully built 6659294b267d kubectl set image -f ./kubernetes/deployment.yaml node-api=codersociety/node-api:tmp-1505148570 deployment "node-api" image updated

Make another request to verify your changes:

$ curl $(minikube service node-api --url) Hello Kubernetes

$ make delete

The make delete command simply removes the Kubernetes deployment and service which are defined in the kubernetes directory:

$ make delete kubectl delete -f kubernetes/ deployment "node-api" deleted service "node-api" deleted

In the next article we will show you how to debug your Node.js application in Minikube:

https://codersociety.com/articles/node.js-development-workflow-for-kubernetes-debugging-(part-2)

Content worth reading – right to your inbox.

By submitting you agree to us processing your personal data according to our privacy policy

A Coder Society member working at a cafe bar

For freelancers

Become part of Coder Society to work with us on innovative projects.

Recommended articles