Patroni on Kubernetes
After this lesson, you will be able to:
- Deploy a Patroni cluster on Kubernetes.
- Configure StatefulSets and PersistentVolumes.
- Use the Patroni Kubernetes operator.
- Implement storage classes and volume management.
- Monitor and scale Patroni in a K8s environment.
1. Kubernetes Architecture for Patroni
1.1. Components
1.2. Advantages of K8s
- No separate etcd needed: Uses Kubernetes API for DCS.
- Built-in scheduling: K8s handles pod placement.
- Storage management: PVCs auto-provisioned.
- Service discovery: K8s Services for endpoints.
- Rolling updates: Native K8s feature.
- Resource limits: CPU/memory guaranteed.
2. Prerequisites
2.1. Kubernetes cluster
2.2. kubectl setup
2.3. Helm (optional)
3. Manual Deployment with StatefulSets
3.1. Create namespace
3.2. ConfigMap
3.3. Secret
3.4. StatefulSet
3.5. Services
3.6. RBAC (Service Account)
4. Verify Deployment
4.1. Check pods
4.2. Check StatefulSet
4.3. Check services
4.4. Check Patroni cluster
4.5. Test connection
5. Using Zalando Postgres Operator
5.1. Install operator
5.2. Create PostgreSQL cluster
5.3. Check cluster status
5.4. Connect to cluster
6. Storage Management
6.1. StorageClass for performance
6.2. Volume expansion
6.3. Backup volumes
7. Monitoring on Kubernetes
7.1. Prometheus ServiceMonitor
7.2. Grafana dashboard
7.3. Logs with Loki
8. Scaling and Updates
8.1. Scale cluster
8.2. Rolling update
8.3. Manual failover
9. Troubleshooting
9.1. Pod stuck in Pending
9.2. Replication not working
9.3. Leader election issues
10. Best Practices
✅ DO
- Use StatefulSets: Stable network identity.
- Set resource limits: Prevent OOM kills.
- Enable PV retention: Don't lose data on deletion.
- Use headless service: For StatefulSet discovery.
- Monitor with Prometheus: Track health.
- Use operators: Simplify management.
- Test failover: Regularly validate HA.
- Backup to external storage: S3, GCS, etc.
- Use anti-affinity: Spread pods across nodes.
- Document procedures: For operations team.
❌ DON'T
- Don't use Deployments: Use StatefulSets.
- Don't skip resource limits: Can crash node.
- Don't delete PVCs: Unless sure about data loss.
- Don't ignore pod affinity: All pods on same node = bad.
- Don't use emptyDir: Data lost on pod restart.
- Don't skip backups: K8s is not a backup solution.
11. Lab Exercises
Lab 1: Deploy Patroni with StatefulSets
Tasks:
- Create namespace and RBAC.
- Deploy ConfigMap and Secret.
- Create StatefulSet with 3 replicas.
- Deploy Services.
- Verify cluster status.
Lab 2: Test failover in Kubernetes
Tasks:
- Delete leader pod.
- Observe automatic failover.
- Verify new leader elected.
- Check application connectivity.
- Document RTO.
Lab 3: Use Zalando Postgres Operator
Tasks:
- Install operator.
- Create PostgreSQL cluster CR.
- Connect and create database.
- Scale cluster up/down.
- Test rolling update.
Lab 4: Monitor with Prometheus
Tasks:
- Deploy Prometheus Operator.
- Create ServiceMonitor.
- Query metrics in Prometheus.
- Create Grafana dashboard.
- Setup alerting rules.
12. Summary
Kubernetes vs Traditional
| Aspect | Traditional | Kubernetes |
|---|---|---|
| DCS | etcd cluster | K8s API |
| Storage | Local disks | PVCs |
| Service discovery | DNS/HAProxy | K8s Services |
| Scaling | Manual | kubectl scale |
| Updates | Manual SSH | Rolling updates |
| Monitoring | Separate setup | ServiceMonitor |
Key Concepts
- StatefulSet: Ordered pod creation/deletion.
- PVC: Persistent data storage.
- Service: Endpoint discovery (master/replica).
- ConfigMap: Patroni configuration.
- Secret: Passwords and credentials.
- RBAC: Kubernetes API access for Patroni.
Next Steps
Lesson 23 will cover Patroni Configuration Management:
- Dynamic configuration changes
- DCS-based config storage
patronictl edit-configusage- Zero-downtime updates
- Configuration validation