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.

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 startStarting 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-namespacesNAMESPACE NAME READY STATUS RESTARTS AGEkube-system heapster-5z549 1/1 Running 0 5mkube-system influxdb-grafana-f91mq 2/2 Running 0 5mkube-system kube-addon-manager-minikube 1/1 Running 0 5mkube-system kube-dns-910330662-8zdn7 3/3 Running 0 5mkube-system kubernetes-dashboard-645fn 1/1 Running 0 5m
$ kubectl get nodesNAME STATUS AGE VERSIONminikube 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 createSending build context to Docker daemon 51.7MBStep 1 : FROM node:8-alpine---> 7a779c246a41Step 2 : WORKDIR /src---> Using cache---> bf82d455b228Step 3 : COPY package.json /src/---> Using cache---> 971f200e90a2Step 4 : COPY package-lock.json /src/---> Using cache---> 47fed9193df0Step 5 : RUN npm install---> Using cache---> 142db6f23946Step 6 : ADD . /src---> b10ee47e048bRemoving intermediate container 816dd54c1523Step 7 : EXPOSE 3000---> Running in 1fa3c746d63d---> 5bac6c4d188aRemoving intermediate container 1fa3c746d63dStep 8 : CMD npm start---> Running in e7e4fa77cb1a---> 3063f2d0165eRemoving intermediate container e7e4fa77cb1aSuccessfully built 3063f2d0165ekubectl apply -f kubernetes/deployment "node-api" createdservice "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 updatedocker build -t codersociety/node-api:tmp-1505148570 .Sending build context to Docker daemon 51.77MBStep 1 : FROM node:8-alpine---> 016382f39a51Step 2 : WORKDIR /src---> Using cache---> deff3e9a115eStep 3 : COPY package.json /src/---> Using cache---> e2ab0b9845a9Step 4 : COPY package-lock.json /src/---> Using cache---> c815960d9ac8Step 5 : RUN npm install---> Using cache---> b86c65ff6454Step 6 : ADD . /src---> 29c7a32c8d0aRemoving intermediate container a12fd5b4f143Step 7 : EXPOSE 3000---> Running in 782644f6b740---> d56d725f3f2bRemoving intermediate container 782644f6b740Step 8: CMD npm start---> Running in 16d8f3d2d0dc---> 6659294b267dRemoving intermediate container 16d8f3d2d0dcSuccessfully built 6659294b267dkubectl set image -f ./kubernetes/deployment.yaml node-api=codersociety/node-api:tmp-1505148570deployment "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 deletekubectl delete -f kubernetes/deployment "node-api" deletedservice "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)
For our latest insights and updates, follow us on LinkedIn
Kentaro is CEO and Solutions Architect at Coder Society. With his in-depth knowledge of software development and cloud technologies, Kentaro often takes on the lead engineer's role. His analytical, organized, and people-oriented nature makes him an apt advisor on software projects and flexible staffing.