Kubernetes - Implementing Liveness and Readiness Probe for a Nodejs Application

Kubernetes - Implementing Liveness and Readiness Probe for a Nodejs Application

?? Liveness Probe

  1. Checks if your app is alive and working.
  2. If it’s NOT alive → Kubernetes restarts the app.


?? Readiness Probe

  1. Checks if your app is ready to handle requests.
  2. If it’s NOT ready → Kubernetes stops sending traffic to it.

Kind installation - you can refer this article for installation - https://www.dhirubhai.net/pulse/kubernetes-install-kind-create-multi-node-cluster-kiran-kulkarni-2ya8c

Step 1: Create a folder

mkdir nodejs-probes
cd nodejs-probes        

Step 2: Initialize the nodejs app

npm init -y        

Step 3: Install Express

npm install express        

Step 4: Create the Server File

const express = require('express');
const app = express();
let isReady = false;
let isHealthy = true;

// Simulate slow startup (e.g., loading data or connecting to a database)
setTimeout(() => {
  isReady = true;
  console.log('App is now ready!');
}, 20000); // 20-second startup delay

// Simulate failure after 30 seconds (e.g., memory leak or deadlock)
setTimeout(() => {
  isHealthy = false;
  console.log('App is now unhealthy!');
}, 30000); // 30-second failure trigger

// Readiness endpoint
app.get('/ready', (req, res) => {
  if (isReady) res.status(200).send('Ready');
  else res.status(503).send('Not Ready');
});

// Liveness endpoint
app.get('/health', (req, res) => {
  if (isHealthy) res.status(200).send('Healthy');
  else res.status(500).send('Unhealthy');
});

// Start server
app.listen(8080, () => {
  console.log('Server running on port 8080');
});        

Step 5: Create a docker file

# Use Node.js 18 Alpine as the base image
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy all files
COPY . .

# Start the app
CMD ["node", "server.js"]        

Step 6: Build the docker image

docker build -t my-node-app:latest .        

Step 7: Push the image to Docker Hub

docker tag my-node-app:latest kirankulkarni007/my-node-app:latest        
docker push kirankulkarni007/my-node-app:latest        


Step 9: Create a deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-probes-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodejs-probes-demo
  template:
    metadata:
      labels:
        app: nodejs-probes-demo
    spec:
      containers:
      - name: nodejs-app
        image: kirankulkarni007/my-node-app:latest  
        ports:
          - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5        
kubectl apply -f deployment.yml        

Step 10: Check Pod status

kubectl get pods -w        

Watch the status of the Pod

  • Pod starts but not ready yet (0/1).
  • After 20 seconds, it becomes ready (1/1).
  • After 30 seconds, it restarts as liveness probe fails and app will be unhealthy.

要查看或添加评论,请登录

Kiran Kulkarni的更多文章

社区洞察