Cloud Native 5 min read

Create a Custom Kubernetes Health‑Check Script that Auto‑Restarts Unhealthy Pods

To prevent service disruption caused by overly aggressive or sluggish Kubernetes liveness and readiness probes, this guide shows how to implement a custom health‑check script that periodically tests a service endpoint, exits the container on failure, and lets Kubernetes automatically restart the pod.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Create a Custom Kubernetes Health‑Check Script that Auto‑Restarts Unhealthy Pods

Problem: Kubernetes pods use liveness and readiness probes, but inappropriate probe intervals can cause unnecessary load or delayed detection of failures, leading to traffic being sent to unhealthy pods.

Requirements:

Custom health‑check mechanism that periodically verifies the service health inside the container.

Automatic container exit when the service is unhealthy.

Integration with Kubernetes so that the pod is automatically restarted or fail‑overed.

Solution overview: Run a lightweight monitoring process inside the container that calls the service’s health endpoint, exits with a non‑zero status on failure, and lets Kubernetes handle the restart.

Implementation steps:

1. Write the health‑check script

#!/bin/bash
# Custom health‑check script
while true; do
  # Send request to health endpoint
  response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/health)

  # If HTTP status is not 200, exit the container
  if [ "$response" != "200" ]; then
    echo "Service not healthy, exiting container..."
    exit 1
  fi

  # Check interval (1 ms)
  sleep 0.001
done

2. Add the script to the container image

FROM alpine:latest
# Install curl
RUN apk add --no-cache curl

# Copy the health‑check script
COPY health_check.sh /usr/local/bin/health_check.sh

# Make it executable
RUN chmod +x /usr/local/bin/health_check.sh

# Run the script when the container starts
CMD ["/usr/local/bin/health_check.sh"]

3. Deploy to Kubernetes

Build the image, push it to a registry, and create a pod or deployment that uses the image. Kubernetes will monitor the pod; if the script exits with status 1, the platform automatically restarts the container according to the pod’s restartPolicy.

DockerKubernetesHealth CheckCustom ScriptContainer Restart
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.