Close this search box.
Deploying and exposing a Node.js application on Kubernetes 

Deploying and exposing a Node.js application on Kubernetes 

In this tutorial, we will walk through the process of deploying a simple “Hello World” Node.js application on a Kubernetes cluster. Kubernetes is a powerful container orchestration platform that helps manage and scale containerized applications.


  • Basic understanding of containers and Docker. 
  • Docker desktop installed.   
  • A running Kubernetes cluster (locally using tools like Minikube or remotely). 
  • Start your Minikube cluster.  
  • Kubernetes command-line tool (kubectl) installed and configured to interact with your cluster.

Create the Node.js Application: 

For this tutorial, we’ll create a simple Node.js application that responds with “Hello, Kubernetes!” when accessed. 

npm init -y 

Create a app.js file and add the following code, 

// app.js 

const http = require('http'); 

const server = http.createServer((req, res) => { 

  res.writeHead(200, { 'Content-Type': 'text/plain' }); 

  res.end('Hello, Kubernetes!\n'); 


const PORT = process.env.PORT || 3000; 

server.listen(PORT, () => { 

  console.log(`Server listening on port ${PORT}`); 


Now, you can run the application with the following command:

node app.js 

You should see your application running on your localhost 3000. 

Let’s Dockerize the application: 

Create a Dockerfile in your application’s root directory to define how the application should be packaged as a Docker container. 

FROM node:14 


COPY package*.json ./ 

RUN npm install 

COPY . . 

EXPOSE 3000 

CMD [ "node", "app.js" ] 

Build the Docker image: 

docker build -t hello-k8s-app . 

Tag and push the Docker image to DockerHub 

docker tag hello-k8s-app your-dockerhub-username/hello-k8s-app 

Replace your-dockerhub-username with your actual Docker Hub username. 

You need to authenticate with Docker Hub to be able to push images. 

docker login 

Push the tagged image to your Docker Hub repository. 

docker push your-dockerhub-username/hello-k8s-app 

Deploy to Kubernetes: 

Create a Kubernetes deployment configuration to manage the application’s replicas. 

# deployment.yaml 

apiVersion: apps/v1 

kind: Deployment 


  name: hello-k8s-deployment 


  replicas: 3 



      app: hello-k8s 




        app: hello-k8s 



        - name: hello-k8s-container 

          image: your-dockerhub-username/hello-k8s-app 


            - containerPort: 3000 

Apply the deployment to your cluster: 

kubectl apply -f deployment.yaml

Expose the application: 

To make the application accessible from outside the cluster, you can expose it using a Kubernetes service. 

# service.yaml 

apiVersion: v1 

kind: Service 


  name: hello-k8s-service 



    app: hello-k8s 


    - protocol: TCP 

      port: 80 

      targetPort: 3000 

  type: LoadBalancer 

Apply the service configuration: 

kubectl apply -f service.yaml 

Access the application: 

Once the service is created and an external IP is assigned, you can access the application in your browser using the external IP. 


To clean up resources after you’re done: 

kubectl delete deployment hello-k8s-deployment 
kubectl delete service hello-k8s-service

Concluding the Node.js deployment

Congratulations! You’ve successfully deployed a simple Node.js application on Kubernetes and exposed it to the internet. 

Kubernetes has revolutionized the way we deploy and manage applications, providing a powerful and flexible platform for container orchestration. In this and the previous tutorial, we’ve covered the basics of setting up a local Kubernetes cluster using Minikube, creating deployments, managing pods, and exposing applications through services. This of course is just the tip of the iceberg – Kubernetes offers a wealth of advanced features for handling complex scenarios in production environments. 

As you continue your journey with Kubernetes, consider exploring features such as namespaces, persistent storage, configuration management, and more. The Kubernetes documentation and online tutorials are excellent resources to deepen your knowledge and become a proficient Kubernetes user. With practice and dedication, you’ll be well on your way to mastering the art of container orchestration and streamlining your application deployment processes. 

If you have remarks or questions let me know. We also provide free health checks for companies using Kubernetes or OKD, OpenShift, Rancher so if you would like to have a chat, let me know.

We are hiring!
Are you our new