Kubernetes Operator & Read Balancer

Deploy SpydDB CRDs on Kubernetes and balance SpydWire traffic across replicas with spyd-balancer.

3 min read Clustering & Operations

SpydDB ships production deployment artifacts for single-writer StatefulSets, declarative Kubernetes Custom Resources (SpydDbCluster), and a weight-aware TCP load balancer (spyd-balancer) for SpydWire connections.


1. Kubernetes Packaging (Kustomize & Helm)#

Under deploy/kubernetes/, SpydDB provides both a raw Kustomize base (deploy/kubernetes/base) and a Helm chart (deploy/kubernetes/helm/spyddb) sharing identical security hardening:

  • Non-root, read-only container with service-account token automounting disabled
  • PersistentVolumeClaim per pod for WAL and SSTable/columnar storage
  • TLS enforced on every non-loopback listener (:8080 HTTPS, :5432 PGWire, :5433 SpydWire)
  • Loopback metrics listener (127.0.0.1:9090) for local sidecar or exec scraping
  • Readiness probe (GET /v1/health): returns HTTP 503 while draining or if the storage engine degrades, removing the pod from Service endpoints automatically

Deploying with Kustomize#

bash
kubectl create namespace spyddb

# 1. Mount TLS certs and configuration Secret before applying manifests
kubectl -n spyddb create secret tls spyddb-tls \
  --cert=tls.crt --key=tls.key

cp deploy/kubernetes/server.json.example server.json
# Replace tokens / configure SCRAM verifiers in server.json, then create Secret:
kubectl -n spyddb create secret generic spyddb-config \
  --from-file=server.json=server.json

# 2. Apply StatefulSet + headless Service
kubectl apply -k deploy/kubernetes/base
kubectl -n spyddb rollout status statefulset/spyddb

Deploying with Helm#

bash
helm lint deploy/kubernetes/helm/spyddb
helm install spyddb deploy/kubernetes/helm/spyddb \
  --namespace spyddb \
  --create-namespace

Override image tags, storage size, and secret names in a values.yaml:

yaml
image:
  repository: registry.example.com/spyddb/server
  tag: "1.0.0"
persistence:
  size: 100Gi
config:
  secretName: spyddb-config
tls:
  secretName: spyddb-tls

2. Declarative Kubernetes Operator (SpydDbCluster)#

While a Helm chart renders manifests once at install time, spyddb-operator continuously watches SpydDbCluster resources (apiVersion: spyddb.dev/v1, plural spyddbclusters, shortname sdb) and reconciles drift in real time.

Install the CRD & Apply a Cluster#

bash
kubectl apply -f deploy/kubernetes/crd.yaml
kubectl apply -f deploy/kubernetes/example-cluster.yaml
kubectl get sdb bench -o wide

Example SpydDbCluster manifest (deploy/kubernetes/example-cluster.yaml):

yaml
apiVersion: spyddb.dev/v1
kind: SpydDbCluster
metadata:
  name: bench
  namespace: default
spec:
  image: nayem7663/spyddb:1.0.0
  replicas: 3
  # Written to a Secret and injected via env var — never passed on pod CLI args
  token: change-this-token-before-deploying
  # TLS Secret holding tls.crt and tls.key mounted at /etc/spyd/tls
  tlsSecret: bench-tls
  storage: 20Gi
  storageClass: ""
  config:
    max_connections: 256
    pgwire_bind: "0.0.0.0:5432"
    metrics_bind: "127.0.0.1:9090"
  resources:
    requests:
      cpu: 500m
      memory: 1Gi
    limits:
      memory: 4Gi

Reconciled Objects & Contracts#

For every SpydDbCluster named <name>, the operator reconciles three idempotent Kubernetes objects and writes readiness status (.status.phase, .status.readyReplicas) back onto the CRD:

Reconciled ObjectPurpose
Secret <name>-configHolds serialized server.json (read via spyd-server --config) plus the client credential
StatefulSet <name>Runs replicas ordered pods (<name>-0, <name>-1, ...) with stable PVC identities, hardened SecurityContext, and /etc/spyd/tls mount
Service <name>Headless Service exposing SpydWire (5433) so clients can address specific pods cleanly

To prevent misconfiguration, the operator owns data_dir, bind_address, and port; passing those fields inside spec.config is explicitly rejected rather than silently overwritten. Note also that multi-node Raft membership (raft peer config) is configured inside spec.config per pod, while the operator manages StatefulSet/Service/Secret lifecycle.


3. SpydWire Load Balancing (spyd-balancer)#

spyd-balancer is a low-overhead TCP proxy designed specifically for pools of SpydDB servers speaking SpydWire (:5433). Because authentication, prepared statements, transaction state, and query cancellation belong to a connection, spyd-balancer splices client and backend sockets byte-for-byte without parsing or rewriting protocol frames.

Selection, Health Checks & Failover#

  • Weighted Least-Connections (connections / weight): Each backend tracks active proxied connections and a positive integer weight. New clients route to the healthy backend with the lowest connections / weight score (so a weight: 2 instance carries twice as many connections as a weight: 1 instance at equal load).
  • Round-Robin Tie-Break Rotation: Equal scores break via an atomic rotation index rather than always picking backend 0, preventing serial short-lived connections from skewing to a single node.
  • Active Health Checks: Periodic TCP connect checks run at health_interval (default 2s). Unhealthy backends stop taking new connections immediately and re-enter rotation automatically as soon as health checks recover.
  • Connect Failover: If dialing the chosen backend fails while opening a client stream, spyd-balancer transparently tries the next healthy backend before returning an error to the client.
  • Isolated HTTP Prometheus Metrics: When metrics_listen is configured on a separate address/port, scrapers read balancer connection, failure, and per-backend counters over HTTP without sending malformed non-SpydWire traffic to the proxy port.

4. spyd-server Operational CLI Flags#

The standalone spyd-server binary also exposes deployment utilities for CI pipelines and systemd/container entrypoints:

bash
# Validate server.json schema, TLS paths, and bounds without starting listeners
spyd-server --check --config /etc/spyd/server.json

# Generate a SCRAM-SHA-256 verifier JSON snippet (reads password from stdin)
printf '%s\n' "$APP_PASSWORD" | spyd-server --hash-password app_user

# Print the empty initial Raft bootstrap state digest (and group digests)
spyd-server --print-bootstrap-digest --config /etc/spyd/server.json