Simple Kubernetes minikube example

There are a lot of great Kubernetes and Docker tutorials on the Internet, but I have had difficulty finding the perfect one that gets you from source code to a deployed app running on minikube. In this tutorial, I will show how to deploy a simple NodeJS app to Kubernetes. It involves two main steps: build a Docker image from the app, and deploy the app to minikube. Let’s begin.

Assumptions

  • you have a Docker Hub account (example: your_docker_hub_username)
  • you have NodeJS installed
  • you have Docker Desktop installed
  • you have Kubernetes enabled in Docker Desktop
  • you have minikube installed

There are lots of great tutorials for the above preliminary setup procedures. The focus of this article is to deploy a simple NodeJS app to Kubernetes minikube. Minikube is the local Kubernetes cluster emulator for your laptop or desktop machine. It saves the hassle and expense of setting up a full-blown Kubernetes cluster on a cloud provider, and works almost the same. Once you have everything set up, head on over to my GitHub repository to retrieve the files for this tutorial.

Files

The files for this Kubernetes tutorial are located in my GitHub repo at the following URL: https://github.com/cyndi5/cynthia-docker. Here is a brief description of the purpose of each file.

FileDescription
.dockerignoreTells Docker to ignore node_modules
DockerfileTells Docker how to build an image of the app
package.json NodeJS installation file
server.jsThe app source code
Purpose of each Kubernetes tutorial file

How to install locally

npm install

How to test locally

npm start

How to build Docker image

docker build -t your_docker_hub_username/node-web-app .

Tag the image with a version (1.0)

docker tag your_docker_hub_username/node-web-app your_docker_hub_username/node-web-app:1.0

Run the container locally

 docker run -d -p 30125:8080 your_docker_hub_username/node-web-app:1.0

-d = detach, lets the command exit while still running the container.

you’ll get a response such as the following which is a container ID: 27947fc4fe8d609a9204be03091b5df1a26907a0109bd8cd18ff1e5b43194b85

Visit http://localhost:30125 in your browser. You should see Hello World.

Stop the container and remove it with the following, using your actual container ID:

docker stop 27947fc4fe8d609a9204be03091b5df1a26907a0109bd8cd18ff1e5b43194b85
docker rm 27947fc4fe8d609a9204be03091b5df1a26907a0109bd8cd18ff1e5b43194b85

Upload (push) your Docker image to Docker Hub

docker login
docker push your_docker_hub_username/node-web-app:1.0

Deploy to Kubernetes locally using minikube

Now run the Kubernetes dashboard. From the terminal, run the following:

minikube dashboard

Eventually you’ll be taken to the browser and to a locally-hosted page, and the dashboard will appear

Create a deployment using Kubernetes dashboard

Click “Deployments” on the left-hand side.

Click the + at the upper-right-hand side to create a resource.

Click the tab “Create from form.”

Fill in the information, substituting your image name / tag instead of mine. I arbitrarily chose “cynthias-app” as the app name.

Click “Deploy.” After a while, you will see the deployment is successful.

Expose Kubernetes service with minikube

First, get the pod name that was just deployed. From the terminal use kubectl get pods.

kubectl get pods

NAME                           READY   STATUS    RESTARTS   AGE
cynthias-app-bb7f5889b-4jrlm   1/1     Running   0          103s

Expose the service. I arbitrarily chose the service name somename-service. I randomly chose port=30123, but target-port=8080 is the port on which the node server is running.

kubectl expose pod cynthias-app-bb7f5889b-4jrlm --type="LoadBalancer" --name="somename-service" --port=30123 --target-port=8080
service/somename-service exposed

Tell minikube to make the node port available

minikube service somename-service

A table showing which URL is exposed will appear, and your browser will open to the random port on localhost, in this case 56401. URL in my example happens to be the following: 127 0 0 1 port 56401. (I removed the parts that would make that a link. You can see the link in the figure below. Yours will be similar, but perhaps with a different port.)

View the deployed NodeJS app

Your browser should open to the above URL (in my case 127 0 0 1 port 56401). If not, open a browser window and enter that second URL. You will see the NodeJS app. Congratulations! You’ve deployed a NodeJS app onto Kubernetes using minikube on your local machine!

Your browser window when it opens to the URL selected by minikube, in my case 127 0 0 1 port 56401

Thank you for visiting!

-Cyndi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: