liveness.sh

Overview

`liveness.sh` is a lightweight Bash script designed as a Kubernetes liveness probe for a blockchain node daemon, specifically interacting with an Ethereum-compatible JSON-RPC endpoint. Its primary purpose is to verify if the blockchain daemon is actively progressing by monitoring the increment of the blockchain's latest block number. If the block number advances over time, the daemon is considered healthy ("running"); otherwise, it is flagged as stalled.

The script also supports a manual override to disable the liveness probe by placing a specific file (`/data/disable_liveness`), allowing administrators to temporarily bypass health checks.


Detailed Explanation

Purpose


Script Workflow

  1. Disable Check
    Checks if the file /data/disable_liveness exists.

    • If yes, prints "liveness probe disabled" and exits with status 0 (success), skipping further checks.

  2. Fetch Current Block Number
    Uses curl to send a JSON-RPC request to http://localhost:8545 to get the latest Ethereum block number (eth_blockNumber method).

    • If the request fails, the script exits with status 1 (failure).

  3. Parse and Convert Block Number
    Extracts the block number in hexadecimal from the JSON response using jq, then converts it to a decimal integer.

  4. First Run Initialization
    Checks if the file /data/.block_number exists.

    • If not, writes the current block number to this file and exits with status 1.

    • This ensures the script has a baseline block number for future comparisons.

  5. Compare Block Numbers
    Reads the previous block number from /data/.block_number and updates it with the current block number.

    • If the current block number is greater than the previous, prints "daemon is running" and exits with status 0.

    • Otherwise, prints "daemon is stalled" and exits with status 1.


Important Variables and Files

Variable/File

Description

DISABLE_LIVENESS_PROBE

Path to the disable file (`/data/disable_liveness`)

FILE

Path to the stored block number file (`/data/.block_number`)

ETH_BLOCK_NUMBER

Raw JSON response from Ethereum node's [eth_blockNumber](/projects/291/69210) call

CURRENT_BLOCK_NUMBER_HEX

Hexadecimal block number extracted from JSON

CURRENT_BLOCK_NUMBER

Decimal block number converted from hex

PREVIOUS_BLOCK_NUMBER

Last recorded block number read from file


Usage Example

This script is typically invoked as a Kubernetes liveness probe command:

livenessProbe:
  exec:
    command:
      - /bin/bash
      - /path/to/liveness.sh
  initialDelaySeconds: 30
  periodSeconds: 10
  failureThreshold: 3

**Manual disable example:** To disable the liveness probe temporarily (e.g., for maintenance):

touch /data/disable_liveness
# The next liveness check will print "liveness probe disabled" and succeed.

Remove the file to re-enable:

rm /data/disable_liveness

Implementation Details


Interaction with Other System Components


Mermaid Flowchart Diagram

flowchart TD
    Start([Start])
    CheckDisable{Is /data/disable_liveness present?}
    ExitDisabled["Print 'liveness probe disabled'\nExit 0"]
    FetchBlock["Fetch block number via\ncurl JSON-RPC"]
    CurlFail{Curl success?}
    ParseBlock["Parse blockNumber from JSON\nConvert hex to decimal"]
    CheckFile{Does /data/.block_number exist?}
    InitFile["Write current block number\nto /data/.block_number\nExit 1"]
    ReadPrev["Read previous block number\nfrom /data/.block_number"]
    WriteCurr["Write current block number\nto /data/.block_number"]
    CompareBlocks{Is current block > previous?}
    Running["Print 'daemon is running'\nExit 0"]
    Stalled["Print 'daemon is stalled'\nExit 1"]

    Start --> CheckDisable
    CheckDisable -- Yes --> ExitDisabled
    CheckDisable -- No --> FetchBlock
    FetchBlock --> CurlFail
    CurlFail -- No --> Stalled
    CurlFail -- Yes --> ParseBlock
    ParseBlock --> CheckFile
    CheckFile -- No --> InitFile
    CheckFile -- Yes --> ReadPrev
    ReadPrev --> WriteCurr
    WriteCurr --> CompareBlocks
    CompareBlocks -- Yes --> Running
    CompareBlocks -- No --> Stalled

Summary

`liveness.sh` is a concise and effective script to monitor the health of an Ethereum node by checking block progression. It provides a simple mechanism for Kubernetes or other orchestrators to determine if the blockchain daemon is alive, with built-in support for disabling the probe and persisting state across runs. The script’s minimal dependencies and straightforward logic make it well-suited for containerized environments requiring reliable liveness checking.