Get Your First Application on Kubernetes

DZone 's Guide to

Get Your First Application on Kubernetes

A quick and easy tutorial for getting a Node.js application on Kubernetes.

· Cloud Zone ·
Free Resource

This article talks about the technical details on deploying your first application on Kubernetes.

What Is This Application?

This is a very simple Node.js application. This application exposes a GET endpoint "/", and when invoked will return "Hello World" message!

Though this example is simple, this will help in understanding all the basic stuff that is needed to run a Dockerized application on Kubernetes.


The following are the pre-requisites to run this application. Refer to their official site to install these, if you do not have them already.

  1. Node: https://nodejs.org/en/
  2. Docker: https://docs.docker.com/install/
  3. Dockerhub account.
  4. Kubernetes: https://kubernetes.io/docs/setup/pick-right-solution/#local-machine-solutions: Use the packages like "minikube" or "microk8s for Ubuntu" for easier setup.

Steps to Follow

cd nodejs-docker-hw docker build -t abhishekjv/nodejs-hw .

The docker build command will refer to the Dockerfile in present working directory, and create the Docker image. This Docker image is the portable executable application that can run on a machine that has Docker and even on the production environments like Kubernetes, Amazon ECS, OpenShift etc.

The -t option for the docker build command is used to give a name to the Docker image. If this option is not specified, a random name will be provided which might not be friendly! When tagging the image with the name, it is recommended to use the format "<REPOSITORY-ID>/<IMAGE-NAME>."

  • REPOSITORY ID is the Docker hub ID or the private registry ID you may be using.
  • IMAGE-NAME is the simple name of the image.

Here, the image is tagged as "abhishekjv/nodejs-hw."

2.1: Quick look at Dockerfile

FROM node:4.6
ADD . /app
RUN npm install
CMD npm start

FROM node:4.6: Says what is the source image needed to run this application.

WORKDIR /app: Describes what should be the working directory of the application inside the image.

ADD . /app : Tells to put the contents of the current directory into the working directory. Current directory is the location where Dockerfile is located.

RUN  npm install  : Tells Docker to run this command once files are copied. This will install the npm dependencies.

EXPOSE 3000: Tells Docker to expose port 3000 from the container. (This port is also defined in index.js)

CMD  npm start : Finally, Docker will execute the command defined here to start the application.

Verify if the image is built by executing Docker images command:

docker images

$ docker images

Execute the below command to run the Docker image locally.

docker run -p 3000:3000 929654e7274a

The host system port 3000 is being mapped to the container port 3000.

Go to http://localhost:3000 to verify the working app

docker push abhishekjv/nodejs-hw
kubectl create -f pod-nodejs-docker-hw.yml

This will create a pod in K8s setup. Verify the status by executing the command kubectl get pod

6. To Access the Application Deployed On K8s:

Once the application is deployed to K8s cluster, the port of K8s has to be exposed to host machine to access the application. This can be done in 2 ways in local environment: (When running in AWS or other production setup, it is recommended to configure and use load balancers)

kubectl port-forward nodejs-docker-hw 8081:3000
Forwarding from 3000
Forwarding from [::1]:8081 300

The port 3000 of the container is exposed on port 8081 on the host system.

Go to http://localhost:8081 to access the application.

kubectl expose pod nodejs-docker-hw --type=NodePort --name nodejs-hw-service

To get the port number:

kubectl describe service nodejs-hw-service

Look for the port number defined for NodePort:

NodePort: <unset> 30469/TCP

Use the above port from local system to access the application: http://localhost:30469

Finally, here is the link to Git repo.

cloud, cloud computing, kubernetes, node js, tutorial

Published at DZone with permission of Abhishek Jawali . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}