Skip to main content

Running Databases

Why Databases on Kubernetes

Picture a traveling circus. Performers (applications) can move easily from city to city, but the elephants (databases) are heavy, require special care, and don’t move as easily. Kubernetes was designed for stateless workloads, but enterprises quickly realized they needed to run stateful databases inside clusters.

Running databases on Kubernetes became the elephant management system, ensuring heavy, stateful workloads can thrive alongside lightweight microservices.


Challenges of Databases

  • Persistence: Pods are ephemeral, but databases need durable storage.
  • Consistency: Databases must maintain ACID properties even when pods restart.
  • Scaling: Stateless apps scale easily; databases require careful sharding or replication.
  • Networking: Databases need stable endpoints and low‑latency connections.
Analogy: Running databases in Kubernetes is like keeping elephants in a circus tent so you need strong foundations, careful feeding, and constant monitoring.

Running Databases

  • StatefulSets: Provide stable identities and persistent storage for database pods.
  • Operators: Automate lifecycle tasks (backups, upgrades, failover).
  • External Storage: Use Persistent Volumes backed by cloud disks (AWS EBS, GCP PD, Azure Disks).
  • Hybrid Models: Some enterprises run databases outside Kubernetes but connect them via services.

Popular Database Operators

  • MySQL Operator: Automates MySQL clusters.
  • Postgres Operator: Manages Postgres deployments with backups and scaling.
  • MongoDB Operator: Handles sharding and replica sets.
  • Vitess: Scales MySQL horizontally for massive workloads.

Analogy: Operators are like specialized elephant trainers, ensuring each database species thrives in Kubernetes.


Global Context

  • Enterprises: Run mission‑critical databases in Kubernetes for consistency across environments.
  • Cloud Providers: Offer managed operators (e.g. Amazon RDS on Kubernetes, Azure Arc Data Services).
  • Community: Database operators are CNCF projects, widely adopted for production workloads.

Hands‑On Exercise

  1. Reflect: How do StatefulSets, PVs, and Operators work together to keep databases reliable inside Kubernetes?

Verify pods and persistent volumes:

kubectl get pods
kubectl get pvc

Create a Postgres cluster:

apiVersion: acid.zalan.do/v1
kind: postgresql
metadata:
  name: demo-db
spec:
  teamId: "demo"
  volume:
    size: 1Gi
  numberOfInstances: 2
  users:
    demo: []
  databases:
    demo: demo
  postgresql:
    version: "14"

Deploy a Postgres Operator:

kubectl apply -f https://github.com/zalando/postgres-operator/releases/download/v1.10.0/postgres-operator.yaml

The Hacker’s Notebook

  • Databases are like elephants heavy, stateful, persistent.
  • StatefulSets provide stable identities.
  • Operators automate lifecycle tasks.
  • Lesson for engineers: Don’t fear stateful apps just embrace them with the right tools.
  • Hacker’s mindset: Treat databases as first‑class citizens. With Kubernetes, you can run them globally at scale.

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 30, 2025