Giter Club home page Giter Club logo

hello-nodejs-minikube's Introduction

Hello NodeJS example for Kubernetes

This repository contains a Hello NodeJS sample application that contains the necessary configuration files to be run on a Kubernetes cluster via Minikube.

Why?

On June 27th 2017 I attended a meetup organized by the Belgian Docker meetup group in Gent. The event was actually a Kubernetes workshop by Benjamin Henrion.

I attended the workshop and created a blog post and a vlog episode about it.

This repository actually contains a sample application that is used in the vlog. It's an extract from Benjamin's workshop. The course material for the workshop can be found on Benjamin's website

The code

The end goal is to run the NodeJS script below:

var handleRequest = function(request, response){
    console.log("rx request for url:" + request.url);
    response.writeHead(200)
    response.end('Hello World: ' +  process.env.HOSTNAME);
};

var www = http.createServer(handleRequest);
www.listen(8080);

It starts a basic webserver and prints Hello World:, followed by the value of the HOSTNAME environment variable. You could run this on your local computer. You can start the server as follows:

HOSTNAME=`hostname` node server.js

You can call the application by issuing a curl call:

curl localhost:8080

What about Docker?

You can deploy the server.js script to any machine you want, but nowadays running the application in a Docker container is very popular.

The Dockerfile contains the necessary instructions to turn this simple application into a Docker image. Just run the following command to build the image:

docker build -t hello-nodejs .

Eventually you could use docker run to run the application, but we'll let Kubernetes handle this.

What about Kubernetes?

Running a Docker container ain't that hard, but running it on a cluster of nodes brings some interesting challenges to the table:

  • Replication
  • High availability
  • Loadbalancing
  • Storage
  • Networking
  • ...

Kubernetes takes care of that and a lot more. To keep it simple, I'll just show you how to create a deployment and a service.

If all of this sounds confusing, go to the Kubernetes website and read the part that explains the core concepts.

The deployment

A deployment contains information about the Docker image that will be deployed and what parameters that will be used. The end result is one or more pods that will be scheduled on the Kubernetes cluster.

The hello_nodejs_deployment.yml configuration file contains all this information and is run as follows:

kubectl create -f hello_nodejs_deployment.yml

The service

In order to expose these containers to the outside world, we can use services. They our bound to a deployment and expose an endpoint.

The hello_nodejs_service.yml configuration file contains all this information and is run as follows:

kubectl create -f hello_nodejs_service.yml

What about Minikube?

Running Kubernetes in production will require at least 3 nodes. Installing all the required software and configuring all the settings is quite a task.

If you just want to run Kubernetes on your local computer without the hassle, there's a project called Minikube. Minikube runs Kubernetes within a virtual machine.

If you've installed Minikube, you can just run minikube start to get going. If you want your docker binary to point to the Docker installation running within Kubernetes, you can just run eval $(minikube docker-env).

Once you've deployed your services, it's not possible to call the endpoints directly, because Minikube runs Kubernetes in the virtual machine. Luckily Minikube can translate those by running the following command:

minikube service hello-nodejs-service --url

This endpoint can then be run via curl:

curl $(minikube service hello-nodejs-service --url)

hello-nodejs-minikube's People

Contributors

albertmarashi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.