readiness.sh


Overview

`readiness.sh` is a lightweight shell script designed to serve as a Kubernetes readiness probe or a similar health checking mechanism for a containerized service. Its primary function is to determine whether the service running locally (presumably on port 8545) is healthy and ready to accept traffic.

The script first checks for the presence of a sentinel file (`/root/disable_readiness`); if this file exists, the readiness probe is considered disabled, and the script exits successfully immediately. Otherwise, it performs an HTTP health check by querying the `/health` endpoint on `localhost:8545`. Based on the HTTP response, it signals success or failure to the orchestrator (e.g., Kubernetes), which can then make decisions about routing traffic or restarting the container.


File Details

#!/bin/bash

DISABLE_READINESS_PROBE=/root/disable_readiness

if [[ -f "$DISABLE_READINESS_PROBE" ]]; then
  echo "readiness probe disabled"
  exit 0
fi

curl -sf http://localhost:8545/health && exit 0 || exit 1

Script Explanation

Variables

Logic Flow

  1. Check for disable file

    The script uses a conditional if [[ -f "$DISABLE_READINESS_PROBE" ]] to check if the disable_readiness file exists.

    • If true:

      • Prints "readiness probe disabled" to standard output.

      • Exits with status code 0 (success).

  2. Perform health check

    If the disable file is not present:

    • Executes curl -sf http://localhost:8545/health

      • -s: Silent mode (no progress meter or error messages).

      • -f: Fail silently on server errors (e.g., HTTP 4xx or 5xx).

    • If the curl command succeeds (returns 0), the script exits with status code 0.

    • If curl fails, the script exits with status code 1.


Usage

This script is intended to be used as a readiness probe command in container orchestration platforms like Kubernetes.

Example Kubernetes readinessProbe configuration:

readinessProbe:
  exec:
    command:
      - /bin/bash
      - /path/to/readiness.sh
  initialDelaySeconds: 5
  periodSeconds: 10

In this example, Kubernetes will run the script periodically to check if the service is ready. If the script exits with a non-zero status, Kubernetes will mark the pod as not ready.


Important Implementation Details


Interaction with Other Components


Visual Diagram

flowchart TD
    A[Start readiness.sh script] --> B{Does /root/disable_readiness exist?}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0 (Success)]
    B -- No --> E[Invoke curl -sf http://localhost:8545/health]
    E -- Success --> D
    E -- Failure --> F[Exit 1 (Failure)]

Summary

`readiness.sh` is a simple but effective readiness probe script that:

Its minimalistic design reduces complexity and dependencies while providing a flexible mechanism to control readiness state reporting.