Deploying a simple node.js App to Kubernetes

Hassan Ajan
Oracle Developers
Published in
3 min readMar 23, 2019

A few days ago I was presenting Kubernetes to a small IT Team, as always I started with explaining the concept of Containers, Docker Hub, Docker build files and then went on to talk about deploying multi-tier apps using Helm Charts. I ended it with showing how to setup CI/CD with Wercker. It didn’t go as I expected.

The IT Team I was presenting to were overwhelmed with the many tools and I can fully understand that. Trying to understand Kubernetes and the full ecosystem while being new to the concept of containers can be quite difficult.

We took a few steps back and instead showed a really simple example:

  1. Build a simple App that exposes a REST API
  2. Packaged the App and dependencies in a Docker Image
  3. Push the Docker Image to a registry, in this case the Oracle Cloud Infrastructure Registry (OCIR)
  4. Deploy the App to Kubernetes

Pre-Requisites:

There are a few pre-requistes to this tutorial. You can read more about those here.

  • Create a Oracle Kubernetes Cluster
  • Install OCI Tools
  • Install Kubectl
  • Download the OKE Configuration
  • Create a Repository on OCIR

I wasn’t intending of building an app from scratch even though it wouldn’t take long. I headed to github and tried a few different apps, eventually I found this app and cloned it:

Clone the app from github:

git clone git@github.com:haj/rest-api-sample.git

Go to the folder:

cd rest-api-sample

Build the Container:

docker build -t fra.ocir.io/<tenancyname>/<reponame>/node-web-app .

Create Credentials for Oracle Container Registry

Kubernetes need to access the container registry to pull the image from there.

kubectl create secret docker-registry regcred --docker-server=fra.ocir.io --docker-username=<tenancyname>/myemail@email.com  --docker-password=’very secret’ --docker-email=myemail@email.com

Create a deployment.yaml file which defines our Deployment and Serivce. We will use this in the next step to deploy the container and create a loadbalancer to expose the service:

cat <<EOF >> deployment.yaml
apiVersion: apps/v1beta1
kind: Deploymentmetadata:name: hello-worldspec:replicas: 1 # tells deployment to run 2 pods matching the templatetemplate: # create pods using pod definition in this templatemetadata:# unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is# generated from the deployment namelabels:app: hello-worldspec:containers:- name: hello-worldimage: fra.ocir.io/<tenancyname>/<reponame>/node-web-app:latestports:- containerPort: 8081imagePullSecrets:# enter the name of the secret you created- name: regcred---apiVersion: v1kind: Servicemetadata:name: hello-worldspec:type: LoadBalancerports:- port: 80protocol: TCPtargetPort: 8081selector:app: hello-world
EOF

Deploy to Kubernetes

kubectl create -f deployment.yaml

Check the status of the deployment created

kubectl get pods

Check the status of service created

kubectl get service

Post some content to the App deployed:

Change the ip below with the external ip of the loadbalancer created.

curl -d '{"body":"This is amazing", "feel":"happy"}' -H "Content-Type: application/json" -X POST http://<IP>:80/api/v1/tweet/

That’s it, you have now packaged an app in a container, pushed it to a container registry and deployed it to Kubernetes.

Check out helm charts to learn how to deploy container to Kubernetes in a more manageable way.

--

--

Hassan Ajan
Oracle Developers

In quest of perfect IT-Infrastructure | Currently exploring Kubernetes | Love building automated Infra when I’m not laying on the beach in Morocco