🗄️ StatefulSets = Database on Kubernetes
Databases need special handling. StatefulSets provide stable storage and network identities for stateful applications like databases.
📝 PostgreSQL StatefulSet Configuration
apiVersion: v1
kind: Secret
metadata:
name: postgres-secret
type: Opaque
data:
postgres-password: c2VjcmV0X3Bhc3N3b3Jk
---
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-config
data:
postgresql.conf: |
max_connections = 100
shared_buffers = 256MB
effective_cache_size = 1GB
work_mem = 16MB
maintenance_work_mem = 128MB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
default_statistics_target = 100
---
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
selector:
app: postgres
ports:
- port: 5432
name: postgres
clusterIP: None
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgres
spec:
serviceName: postgres
replicas: 3
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:15
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: postgres-password
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
- name: POSTGRES_INITDB_ARGS
value: --data-checksums
ports:
- containerPort: 5432
name: postgres
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
- name: postgres-config
mountPath: /etc/postgresql
resources:
requests:
memory: 512Mi
cpu: 500m
limits:
memory: 1Gi
cpu: 1000m
livenessProbe:
exec:
command:
- /bin/sh
- -c
- pg_isready -U postgres
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
readinessProbe:
exec:
command:
- /bin/sh
- -c
- pg_isready -U postgres
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3
volumes:
- name: postgres-config
configMap:
name: postgres-config
volumeClaimTemplates:
- metadata:
name: postgres-storage
spec:
accessModes: [ ReadWriteOnce ]
resources:
requests:
storage: 50Gi
storageClassName: standard
🎯 Database Operations on StatefulSets
# Access PostgreSQL
# Connect to primary pod
kubectl exec -it postgres-0 -- psql -U postgres
# Get pod information
kubectl get pods -l app=postgres -o wide
# Check replication status
kubectl exec -it postgres-0 -- psql -U postgres -c SELECT * FROM pg_stat_replication;
# Backup database
kubectl exec -it postgres-0 -- pg_dump -U postgres mydb > backup.sql
# Restore database
kubectl exec -i postgres-0 -- psql -U postgres mydb < backup.sql
# Increase replicas
kubectl scale statefulset postgres --replicas=5
# Rolling Update
kubectl patch statefulset postgres --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"postgres:15.1"}]'
# View persistent volumes
kubectl get pvc
# Manual failover
kubectl delete pod postgres-0
# Kubernetes will recreate with same storage
# Migration Script
kubectl exec -it postgres-0 -- psql -U postgres <
💡 StatefulSet Best Practices
- Use separate storage class for databases
- Implement regular backup schedule
- Use pod anti-affinity for high availability
- Monitor disk usage and performance
- Test failover procedures regularly
StatefulSets bring the power of Kubernetes to stateful applications. With proper configuration, you get high availability and scalability for your databases.
