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
DISABLE_READINESS_PROBEType: String (file path)
Value:
/root/disable_readinessPurpose: This file acts as a toggle switch to disable the readiness probe. If this file exists, the probe will not perform the HTTP health check and will instead exit immediately with success.
Logic Flow
Check for disable file
The script uses a conditional
if [[ -f "$DISABLE_READINESS_PROBE" ]]to check if thedisable_readinessfile exists.If true:
Prints
"readiness probe disabled"to standard output.Exits with status code
0(success).
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
Toggle via sentinel file:
The use of a sentinel file (/root/disable_readiness) provides an easy way to disable readiness checks without modifying the script or container configuration. This can be useful during maintenance or debugging.HTTP health check endpoint:
The script depends on the HTTP endpointhttp://localhost:8545/healthbeing available and returning a successful status code (2xx). This endpoint must be implemented by the service running inside the container.Exit codes matter:
The script exits with0to indicate readiness and1to indicate unready state. This convention is crucial for orchestrators to interpret the container state correctly.Minimal dependencies:
The script relies onbashandcurl, common utilities available in most Linux-based containers.
Interaction with Other Components
Service running on port 8545:
This script queries the/healthendpoint of a service listening onlocalhostport8545. The health endpoint is expected to return a successful HTTP response if the service is healthy.Container orchestration platform:
The script is typically invoked by Kubernetes or a similar system as part of a readiness probe to decide whether the container can receive traffic.Disabling mechanism:
External processes or users can create or remove the/root/disable_readinessfile to control the readiness check behavior dynamically.
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:
Checks for a disable file to optionally bypass health checks.
Performs an HTTP health check on a local service.
Returns appropriate exit codes to signal readiness.
Integrates seamlessly with container orchestration platforms like Kubernetes.
Its minimalistic design reduces complexity and dependencies while providing a flexible mechanism to control readiness state reporting.