{{announcement.body}}
{{announcement.title}}

How to Run a Basic Node.js Application in Kubernetes Minikube Cluster

DZone 's Guide to

How to Run a Basic Node.js Application in Kubernetes Minikube Cluster

Learn how to create and start a simple Hello World Node.js application with Minikube, as well as an introduction to some stock Kubernetes plugins.

· Cloud Zone ·
Free Resource

Cubes

Learn to use Minikube with a Node.js app

This tutorial shows you how to run a simple "Hello World" Node.js app on Kubernetes using Minikube. Minikube provides a single master Kubernetes cluster. Minikube is used for developer workstations.

In this article, we will:

  1. Deploy a "Hello World" application to Minikube.
  2. Run the app.
  3. View application logs.

Create an Application and Image

  • Create a directory called Minikube.
  • Inside the directory create two files, a server.js file, and a Dockerfile.
  • Content of server.js
JavaScript




x


1
var http = require('http');
2
 
          
3
var handleRequest = function(request, response) {
4
  console.log('Received request for URL: ' + request.url);
5
  response.writeHead(200);
6
  response.end('Hello World!');
7
};
8
var www = http.createServer(handleRequest);
9
www.listen(8080);


  • Content of Dockerfile
Dockerfile




xxxxxxxxxx
1


 
1
FROM node:6.14.2 
2
EXPOSE 8080 
3
COPY server.js . 
4
CMD node server.js


Start Minikube Cluster

  1. Click Launch Terminal

    Note: If you have installed Minikube locally, run minikube start.

  2. Open the Kubernetes dashboard in a browser:

    Shell




    xxxxxxxxxx
    1


     
    1
    minikube dashboard

Create Deployment

A Kubernetes pod is a group of one or more containers, tied together for the purposes of administration and networking. The pod in this tutorial has only one container. A Kubernetes deployment checks on the health of your pod and restarts the pod’s container if it terminates. Deployments are the recommended way to manage the creation and scaling of pods.

  1. Use the kubectl create command to create a deployment that manages a pod. The pod runs a container based on the provided Docker image.

    Shell




    xxxxxxxxxx
    1


     
    1
    kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node


    View the Deployment:

  2. Shell




    xxxxxxxxxx
    1


     
    1
    kubectl get deployments


    Output:

    Plain Text




    x


    1
    NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    2
    hello-node   1         1         1            1           1m


  3. View the Pod:

    Shell




    xxxxxxxxxx
    1


     
    1
    kubectl get pods


    Output:

    Java




    xxxxxxxxxx
    1


     
    1
    NAME                          READY     STATUS    RESTARTS   AGE
    2
    hello-node-5f76cf6ccf-br9b5   1/1       Running   0          1m


    View cluster events:

  4. Shell




    xxxxxxxxxx
    1


     
    1
    kubectl get events


    View the kubectl configuration:

  5. Shell




    xxxxxxxxxx
    1


     
    1
    kubectl config view


Create Service

By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster. To make the hello-node container accessible from outside the Kubernetes virtual network, you have to expose the pod as a Kubernetes Service.

You may also enjoy: Get Your First Application on Kubernetes

  1. Expose the pod to the public internet using the kubectl expose command:

    Shell




    xxxxxxxxxx
    1


     
    1
    kubectl expose deployment hello-node --type=LoadBalancer --port=8080


    The --type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster.

  2. View the Service you just created:

    Shell




    xxxxxxxxxx
    1


     
    1
    kubectl get services


    Output:

    Java




    x


    1
    NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
    2
    hello-node   LoadBalancer   10.108.144.78   <pending>     8080:30369/TCP   21s
    3
    kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          23m


    On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On Minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

  3. Run the following command:

    Shell




    xxxxxxxxxx
    1


     
    1
    minikube service hello-node


    Katacoda environment only: Click the plus sign, and then click Select port to view on Host 1.

  4. Katacoda environment only: Type 30369 (see port opposite to 8080 in services output), and then click. This opens up a browser window that serves your app and shows the “Hello World” message.

Enable Addons

Minikube has a set of built-in add-ons that can be enabled, disabled and opened in the local Kubernetes environment.

  1. List the currently supported addons:

    Shell




    xxxxxxxxxx
    1


     
    1
    minikube addons list


    Output:

    Shell




    x
    16


    1
    addon-manager: enabled
    2
    coredns: disabled
    3
    dashboard: enabled
    4
    default-storageclass: enabled
    5
    efk: disabled
    6
    freshpod: disabled
    7
    heapster: disabled
    8
    ingress: disabled
    9
    kube-dns: enabled
    10
    metrics-server: disabled
    11
    nvidia-driver-installer: disabled
    12
    nvidia-gpu-device-plugin: disabled
    13
    registry: disabled
    14
    registry-creds: disabled
    15
    storage-provisioner: enabled
    7
    heapster: disabled


    Enable an addon, for example, heapster:

  2. Shell




    xxxxxxxxxx
    1


     
    1
    minikube addons enable heapster


    Output:

    Shell




    xxxxxxxxxx
    1


    1
    heapster was successfully enabled


    View the Pod and Service you just created:

  3. Shell




    xxxxxxxxxx
    1


     
    1
    kubectl get pod,svc -n kube-system
    2
     
                


    Output:

    Shell




    xxxxxxxxxx
    1
    14


     
    1
    NAME                                        READY     STATUS    RESTARTS   AGE
    2
    pod/heapster-9jttx                          1/1       Running   0          26s
    3
    pod/influxdb-grafana-b29w8                  2/2       Running   0          26s
    4
    pod/kube-addon-manager-minikube             1/1       Running   0          34m
    5
    pod/kube-dns-6dcb57bcc8-gv7mw               3/3       Running   0          34m
    6
    pod/kubernetes-dashboard-5498ccf677-cgspw   1/1       Running   0          34m
    7
    pod/storage-provisioner                     1/1       Running   0          34m
    8
     
                
    9
    NAME                           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
    10
    service/heapster               ClusterIP   10.96.241.45    <none>        80/TCP              26s
    11
    service/kube-dns               ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP       34m
    12
    service/kubernetes-dashboard   NodePort    10.109.29.1     <none>        80:30000/TCP        34m
    13
    service/monitoring-grafana     NodePort    10.99.24.54     <none>        80:30002/TCP        26s
    14
    service/monitoring-influxdb    ClusterIP   10.111.169.94   <none>        8083/TCP,8086/TCP   26s


    Disable heapster:

  4. Shell




    xxxxxxxxxx
    1


     
    1
    minikube addons disable heapster


    Output:

    Shell




    xxxxxxxxxx
    1


    1
    heapster was successfully disabled


Clean Up

Now you can clean up the resources you created in your cluster:

Shell




xxxxxxxxxx
1


 
1
kubectl delete service hello-node
2
kubectl delete deployment hello-node


Optionally, stop the Minikube virtual machine (VM):

Shell




xxxxxxxxxx
1


 
1
minikube stop


Optionally, delete the Minikube VM:

Shell


xxxxxxxxxx
1
 
1
minikube delete

Further Reading

Deploying a Node.js/Angular 5 Application to Kubernetes With Docker

Topics:
kubernetes ,minikube ,nodejs ,tutorial ,plugins ,kubernetes plugins ,cloud ,logging

Published at DZone with permission of Aditya Bhuyan . See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}