In this article, you’ll learn how to deploy a simple Node.js application on Kubernetes with at least 3 instances (replicas). We’ll use Kubernetes objects like Deployments, Services, and ConfigMaps to manage and expose the application. The article is designed to be beginner-friendly with detailed steps.
Prerequisites
- Kubernetes Cluster:
- A working Kubernetes cluster, such as the one provided by Docker Desktop or Minikube.
- kubectl:
- The command-line tool for interacting with Kubernetes.
- Docker Installed:
- To build and push your Node.js application container image.
Step 1: Create a Simple Node.js Application
Create a basic Node.js application to serve as our project.
1.1 Create the Project Directory:
mkdir nodejs-k8s-app
cd nodejs-k8s-app
1.2 Initialize the Node.js Application:
npm init -y
1.3 Create the Application File:
Create a file named app.js
:
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("Hello, Kubernetes!");
});
app.listen(port, () => {
console.log(`App is running on http://localhost:${port}`);
});
1.4 Install Dependencies:
npm install express
1.5 Test the Application:
Run the application locally:
node app.js
Visit http://localhost:3000
in your browser, and you should see “Hello, Kubernetes!”.
Step 2: Containerize the Node.js Application
We’ll use Docker to create a container for the Node.js application.
2.1 Create a Dockerfile
:
In the root of your project, create a Dockerfile
:
# Base image
FROM node:16
# Set the working directory
WORKDIR /usr/src/app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy application code
COPY . .
# Expose the application port
EXPOSE 3000
# Command to run the app
CMD ["node", "app.js"]
2.2 Build the Docker Image:
docker build -t nodejs-k8s-app:1.0 .
2.3 Test the Docker Image:
Run the image locally to verify:
docker run -p 3000:3000 nodejs-k8s-app:1.0
Step 3: Push the Image to a Container Registry
Push the image to a container registry like Docker Hub or any other registry.
3.1 Log in to Docker Hub:
docker login
3.2 Tag and Push the Image:
Replace <username>
with your Docker Hub username.
docker tag nodejs-k8s-app:1.0 <username>/nodejs-k8s-app:1.0
docker push <username>/nodejs-k8s-app:1.0
Step 4: Create Kubernetes YAML Files
We’ll now define Kubernetes manifests to deploy and expose the application.
4.1 Deployment Manifest
Create a file named deployment.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-app
labels:
app: nodejs-app
spec:
replicas: 3
selector:
matchLabels:
app: nodejs-app
template:
metadata:
labels:
app: nodejs-app
spec:
containers:
- name: nodejs-app
image: <username>/nodejs-k8s-app:1.0
ports:
- containerPort: 3000
This deployment:
- Deploys 3 replicas of the Node.js application.
- Uses the Docker image you created and pushed.
4.2 Service Manifest
Create a file named service.yaml
:
apiVersion: v1
kind: Service
metadata:
name: nodejs-service
spec:
type: LoadBalancer
selector:
app: nodejs-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
This service:
- Exposes the application to external users using a LoadBalancer.
- Maps port 80 to the container’s port 3000.
Step 5: Deploy to Kubernetes
- Apply the Deployment and Service manifests:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
2. Verify the deployment:
kubectl get deployments
kubectl get pods
3. Verify the service:
kubectl get services
#Note the EXTERNAL-IP for the nodejs-service.
Step 6: Access the Application
- Open a browser and navigate to the
EXTERNAL-IP
of your service. - You should see “Hello, Kubernetes!”.
Optional: Scale the Application
You can easily scale the number of replicas up or down.
- Scale to 5 replicas:
kubectl scale deployment nodejs-app --replicas=5
2. Verify the updated replicas:
kubectl get pods
Step 7: Clean Up
To delete the deployment and service:
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
Conclusion
This guide demonstrated how to deploy a simple Node.js application on Kubernetes using multiple replicas. Kubernetes’ declarative approach simplifies scaling, updating, and managing applications. This foundational knowledge prepares you to explore advanced topics like ConfigMaps, Secrets, and Ingress for real-world use cases.