"Hello World!" with typescript, NodeJs, Express & Kubernetes

"Hello World!" with typescript, NodeJs, Express & Kubernetes

Well it all started with frustration of deploying a NodeJs and Express Application on Microsoft Azure, while deploying ASP.Net applications is simple and straight forward but not so when you are hosting a NodeJs Application, while i tried deployment multiple times and each with a success, the app never opened up and kept giving 500 error.

Armed with failure i was looking up for alternatives so thought of trying out Google's cloud platform, i do use kubernetes at work but somehow never liked the openshift platform, signing up was easy and you instantly get $300 worth of trial goodies, so lets get started with what i learned while deploying a node app on Google's kubernetes engine.

So what is kubernetes? a quick search will give you this definition:

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.

Fire up Visual Studio Code and create a basic typescript & nodejs app, i am using an amazing framework to work in ExpressJs the typescript way called ts-express-decorators

Prerequisites:

i have created a basic project and hosted it on github, download the sources from github => ApiBasic, once the app is up an running, create a Docker image to deploying on GKE, here's my docker ignore and docker file.

/*docker file*/
 
FROM node:8.4.0
EXPOSE 1337
ENV PORT=1337
WORKDIR /opt/app/
COPY . .
RUN npm install && npm run tsc
CMD ["npm","start"]
 
  


/* docker ignore */

build/
node_modules/
 
  


Build your docker image by running:

docker build --tag  apibasic:v1 .

Test your image:

docker run -p 8080:1337  apibasic:v1

Once the image is running, browse http://localhost:8080/hey , you should be greeted with Hello World.

Next , fire up your terminal and login to google cloud using

gcloud auth login

once logged in install kubernetes cli:

gcloud components install kubectl

Enable google container register from https://cloud.google.com/container-registry/ , next upload the docker image to google container registry, first create a project in google cloud (register with google cloud and create a project), then image needs to be tagged with gcr.io/[YOUR-PROJECT-NAME]/[IMAGE-NAME]

docker tag apibasic:v1 gcr.io/my-test-project-195415/apibasic:v1

once the image is tagged, push it to google container registry with gcloud cli:

gcloud docker -- push gcr.io/my-test-project-195415/apibasic:v1


now go google's container registry and you should see the docker image there




now back to terminal or stay in browser, create a kubernetes cluster (https://cloud.google.com/kubernetes-engine/docs/how-to/creating-a-container-cluster)

gcloud container  clusters create cluster-1 --zone us-central1-a --num-nodes 1

this will spin up a cluster which you can check in your google cloud console, move onto the next step once the cluster is ready, to deploy the image we need to use kubernetes cli "kubectl" , pass on the auth information from gcloud to kubectl

/* pass auth information to kubectl*/
gcloud container clusters get-credentials cluster-1 --zone us-central1-a

/*create deployment*/
kubectl run apibasic --image gcr.io/my-test-project-195415/apibasic:v1 --port=1337

/*expose port 80*/
kubectl expose deployment apibasic --type=LoadBalancer --port 80 --target-port 1337

/*check external ip*/
kubectl get services

go to the link http://[External-IP]/hey and you should see

hope this helps!!


To view or add a comment, sign in

More articles by Parvesh M.

Others also viewed

Explore content categories