# Kubernetes Cluster Simulator: Live HA, Failover & Fencing

Interactive Kubernetes + PostgreSQL 19 bunqueue cluster workbench: inject SIGTERM drains, SIGKILL crashes, split-brain partitions, DB restarts, node power-offs, and rolling deploys while verifying zero lost or duplicate jobs.

Canonical: https://bunqueue.dev/k8s-simulator/

---

A live discrete-event model of bunqueue's multi-broker Kubernetes runtime. Scale stateless replicas, inject `SIGTERM` drains, `SIGKILL` crashes, network partitions, node power-offs, and database restarts while watching `/ready` probes, lease handoffs, and stale ACK fencing in real time.

*[Interactive k8s simulator: open the HTML page to view it.]*

## How the simulation maps to real bunqueue v2.9.25

| Behavior | What you see here | Real bunqueue + Kubernetes runtime |
| --- | --- | --- |
| **Pod identity** | Each recreated pod gets a fresh `<pod>:<pid>:<hex>` broker ID | Omit static `BUNQUEUE_BROKER_ID` so restarted containers skip the 30 s fencing lockout |
| **Graceful drain (`SIGTERM`)** | `/ready` flips to `503`, `Service/bunqueue` removes the endpoint, owned leases hand off to siblings | `beginDrain()` stops lease reassignment onto draining pods; measured `4 s` drain |
| **Pod crash (`SIGKILL`)** | Pod dies with active leases; PostgreSQL DB clock expires leases and `RecoverySweeper` reclaims them | Surviving broker increments generation token and re-queues without manual intervention |
| **Split-brain partition** | Partitioned pod finishes locally and sends a late `ACK`; PostgreSQL rejects it and increments `Fenced ACKs` | Lease generation fencing guarantees two workers never both complete the same job |
| **PostgreSQL restart** | All pods flip `/ready 503` while `postgres-0` restarts, then auto-heal back to `N/N Ready` | `connectionTimeout: 5s` + bounded retry loop recovers in `5-6 s` |
| **PodDisruptionBudget** | Voluntary `SIGTERM` drains stop when `ready <= minAvailable` | `PodDisruptionBudget` (`minAvailable: 2`) protects cluster capacity during node drains |
| **Schema upgrade (`Recreate`)** | Drains old pods, holds `pg_advisory_lock(bunqueue:schema)`, boots new revision cleanly | Prevents mixed binary versions against one PostgreSQL namespace during schema bumps |

Ready to deploy this exact topology? Follow the [Step-by-Step Kubernetes Guide](https://bunqueue.dev/guide/kubernetes/) or explore the single-node [Queue Engine Simulator](https://bunqueue.dev/simulator/).
