
Deploying a Node.js API to Kubernetes with Minikube: A Beginner’s Guide
Kubernetes is a powerful tool for managing applications, and Minikube makes it easy to try it out locally. In this beginner-friendly guide, we’ll walk through deploying a simple Node.js API to Kubernetes using Minikube. By the end, you’ll have a running API that says "Hello from Kubernetes!" Let’s dive in!
What You’ll Need
- Node.js: Installed on your computer (version 18 or later recommended).
- Docker: To build and manage container images.
- Minikube: A tool to run Kubernetes locally.
- kubectl: The Kubernetes command-line tool to interact with your cluster.
- A code editor (like VS Code) and a terminal.
Don’t worry if these terms sound new—we’ll explain each step clearly!
Step 1: Create Your Node.js App
First, let’s build a simple Node.js API using the Express framework.
Create a project folder called
node-apiand navigate to it in your terminal:mkdir node-api cd node-apiInitialize a Node.js project:
npm init -yInstall Express:
npm install expressCreate the server code in a file named
server.js:const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello from Kubernetes!'); }); app.listen(port, () => { console.log(`Listening on port ${port}`); });Update
package.jsonto include the start script and Express dependency:{ "name": "node-api", "version": "1.0.0", "main": "server.js", "scripts": { "start": "node server.js" }, "dependencies": { "express": "^4.18.2" } }Test your app locally:
npm startOpen
http://localhost:3000in your browser. You should see "Hello from Kubernetes!" If it works, you’re ready for the next step!
Step 2: Create a Dockerfile
To run our app in Kubernetes, we need to package it into a Docker container. A Dockerfile defines how to build this container.
Create a file named
Dockerfilein yournode-apifolder:FROM node:18 WORKDIR /app COPY . . RUN npm install CMD ["npm", "start"]
What’s happening here?
FROM node:18: Uses the official Node.js 18 image as the base.WORKDIR /app: Sets the working directory inside the container.COPY . .: Copies your project files into the container.RUN npm install: Installs dependencies.CMD ["npm", "start"]: Runs the app when the container starts.
Step 3: Build the Docker Image
Now, let’s build the Docker image for our app.
- Run the build command in your terminal (make sure you’re in the
node-apifolder):docker build -t node-api:1.0 .
What’s this doing?
docker build: Creates a Docker image from the Dockerfile.-t node-api:1.0: Tags the image with the namenode-apiand version1.0..: Tells Docker to use the Dockerfile in the current directory.
You can check if the image was created by running:
docker images
Step 4: Load the Image into Minikube
Minikube runs a local Kubernetes cluster, and we need to make our Docker image available to it.
Ensure Minikube is running:
minikube startLoad the Docker image into Minikube:
minikube image load node-api:1.0
This transfers the node-api:1.0 image to Minikube’s internal Docker registry.
Step 5: Create a Kubernetes Deployment
A Kubernetes Deployment ensures our app runs reliably. We’ll define it in a YAML file.
Create a file named
node-api-deploy.yaml:apiVersion: apps/v1 kind: Deployment metadata: name: node-api-deploy spec: replicas: 1 selector: matchLabels: app: node-api template: metadata: labels: app: node-api spec: containers: - name: node-api image: node-api:1.0 ports: - containerPort: 3000
What’s in this file?
kind: Deployment: Defines a Kubernetes Deployment.metadata.name: Names the deploymentnode-api-deploy.replicas: 1: Runs one instance (pod) of the app.selectorandlabels: Links the deployment to pods with the labelapp: node-api.containers: Specifies the Docker image (node-api:1.0) and the port (3000).
Apply the deployment:
kubectl apply -f node-api-deploy.yamlVerify the deployment:
kubectl get deploymentsYou should see
node-api-deploylisted.
Step 6: Expose the Deployment with a Service
To access the app, we need a Kubernetes Service to expose it.
Create a file named
node-api-service.yaml:apiVersion: v1 kind: Service metadata: name: node-api-service spec: type: NodePort selector: app: node-api ports: - port: 3000 targetPort: 3000 nodePort: 30036
What’s happening here?
kind: Service: Defines a Kubernetes Service.type: NodePort: Exposes the app on a specific port.selector: Connects the service to pods with the labelapp: node-api.ports: Maps port3000in the service to port3000in the container, and exposes it externally on port30036.
Apply the service:
kubectl apply -f node-api-service.yamlVerify the service:
kubectl get services
Step 7: Access Your App
Finally, let’s make the app accessible from your browser.
Set up port forwarding:
kubectl port-forward service/node-api-service 8080:3000This maps port
8080on your local machine to port3000on the Kubernetes service.Open your browser and visit:
http://localhost:8080You should see:
Hello from Kubernetes!
Congratulations! 🎉
You’ve successfully deployed a Node.js API to Kubernetes using Minikube! You created a simple app, containerized it with Docker, and used Kubernetes to manage and expose it. From here, you can explore scaling your app, adding more routes, or deploying to a cloud provider.
Cleanup (Optional): To stop Minikube and free up resources:
minikube stop
minikube delete
Happy coding, and welcome to the world of Kubernetes! 🚀