A custom Kubernetes Controller written in Go that automates the lifecycle management of PostgreSQL database clusters.
Unlike standard deployments, this Operator acts as a "Site Reliability Engineer in a box." It continuously watches the cluster state and enforces the desired configuration, offering Self-Healing capabilities that automatically recover deleted pods in under 5 seconds.
- Custom Resource Definition (CRD): Extends the Kubernetes API with a new
SimpleDBkind, allowing developers to request databases using native YAML. - Active Reconciliation: Implements a control loop that constantly compares the "Desired State" (YAML) vs "Actual State" (Cluster).
- Self-Healing Infrastructure: Automatically detects if a database Pod is deleted or crashes and recreates it instantly.
- Elastic Scaling: Supports real-time horizontal scaling via
kubectl patchor YAML updates. - Status Reporting: Provides real-time feedback on the database health status directly in the terminal.
The Operator follows the standard Kubernetes Controller pattern:
- User applies a
SimpleDBYAML. - API Server stores the request.
- Controller (My Code) detects the new event.
- Reconciler Loop creates and manages a Kubernetes Deployment for Postgres.
- Observer watches the Pods; if one dies, the Reconciler spins up a replacement.
- Go 1.22+
- Docker Desktop & Kind (Kubernetes in Docker)
kubectl
Clone the repo and install the Custom Resource Definitions (CRDs) into your cluster:
make install
Run the operator locally (it will connect to your current K8s context):
make run
Open a new terminal and apply the sample database configuration:
kubectl apply -f config/samples/database_v1_simpledb.yaml
Check the status of your database:
kubectl get simpledbs
# Output: NAME STATUS AGE
# simpledb-sample Ready 2m
🔥 The "Chaos Monkey" Test: Delete the pod manually to test resilience:
kubectl delete pod -l app=simpledb
kubectl get pods -w
# Result: You will see a new pod spin up instantly!
Scale your database from 1 replica to 3 replicas on the fly:
kubectl patch simpledb simpledb-sample --type='merge' -p '{"spec":{"replicas":3}}'
kubectl get pods
# Result: 3 Pods running
- Language: Go (Golang)
- Framework: Kubebuilder, Controller-Runtime
- Orchestration: Kubernetes (Kind)
Built by Hemraj