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.
- you have a Docker Hub account (example:
- 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.
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.
|.dockerignore||Tells Docker to ignore node_modules|
|Dockerfile||Tells Docker how to build an image of the app|
|package.json||NodeJS installation file|
|server.js||The app source code|
How to install locally
How to test locally
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:
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 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:
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
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
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!
Thank you for visiting!