Cloud Native with Node.js, Docker and Kubernetes

I decided to get my hands dirty and use a bunch of technologies I’ve had some exposure to but haven’t tried to setup personally. I began by creating a website with Node.js and the Express framework for the Node Meetup group I co-organize. It’s a simple website which explains what the group is about, has an events calendar, provides a feedback page and showcases some of the member’s work. Here is the development site up and running on my server and here is the gitlab repository where the code lives.

From there I created a docker image by going to CloudNativejs.io and going to their Docker git repo to pull down the Dockerfiles. I tried building the various Dockerfiles to see how each of them worked and used the Dockerfile-run template since it was the leanest option. From there I ran it to check that it worked. I then tagged it to prepare it for publishing and published it on dockerhub. Here are the commands I used:

#build
docker build -t nodepghserver-run -f Dockerfile-run .
#verify that it's a smaller size!
docker images
#run
docker run -i -p 3000:3000 -t nodepghserver-run
#stopped it with ctrl+c

#Tag it
docker tag nodepghserver-run nodeserver:1.0.0

#Publish
docker login
#retag image so it lives in your username namespace
docker tag nodepghserver-run ephemeralwaves/nodeserver:1.0.0
#push
docker push ephemeralwaves/nodepghserver

The next part of my journey involved using Helm and Kubernetes. I installed Microk8s by Canonical which was a snap (no pun intended!) and enabled Helm. I got a template Helm chart from CloudNativejs.io‘s Helm repo. I got stuck here for about an hour until I realized that the chart was outdated and a pull request was pending that updated the charts (it has since been updated!).

#install micro8s from https://microk8s.io/#quick-start 
sudo snap install microk8s --classic
sudo microk8s.enable helm
sudo microk8s.helm init

#get and put helm chart into project folder
git clone https://github.com/CloudNativeJS/helm
mv chart/ myappfolder/

#change name of repo in values.yaml

#then go into repo and install/deploy
sudo microk8s.helm install --name nodepghserver chart/nodeserver
#check with
sudo microk8s.helm status nodepghserver
#open browser to yourLocalIPAddress:Port listed in helm status

For seamless upgrades and rollbacks just use the helm rollback and upgrade commands so no up time is lost.

#seamless upgrade so no downtime!
sudo microk8s.helm upgrade nodepghserver chart/nodeserver

#if you need to rollback
#check history
sudo microk8s.helm history nodepghserver
#rolling back to revision 1
sudo microk8s.helm rollback nodepghserver 1

More to come about Prometheus integration and health checks!