هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
apiVersion: v1
kind: Service
metadata:
name: pg
namespace: data
spec:
clusterIP: None
selector: { app: pg }
ports: [{ port: 5432 }]
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pg
namespace: data
spec:
serviceName: pg
replicas: 1
selector:
matchLabels: { app: pg }
template:
metadata:
labels: { app: pg }
spec:
securityContext: { fsGroup: 999 }
containers:
- name: postgres
image: postgres:16
env:
- name: POSTGRES_PASSWORD
valueFrom: { secretKeyRef: { name: pg-auth, key: password } }
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
ports: [{ containerPort: 5432 }]
readinessProbe:
exec: { command: ["pg_isready", "-U", "postgres"] }
resources:
requests: { cpu: 250m, memory: 512Mi }
limits: { memory: 1Gi }
volumeMounts:
- { name: data, mountPath: /var/lib/postgresql/data }
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ReadWriteOnce]
resources:
requests: { storage: 20Gi }Step by step
- Headless Service (
clusterIP: None) — gives each Pod a DNS name:pg-0.pg.data.svc.cluster.local. Apps connect to that name (or topgfor "any"). - volumeClaimTemplates — each Pod gets its own PVC (
data-pg-0). Whenpg-0is rescheduled, it gets the same PVC back. - PGDATA in a subfolder — the volume root may contain
lost+found, which PostgreSQL refuses; a subfolder avoids that. - Password from a Secret (first command) — never in the YAML.
- Connect and test:
kubectl -n data exec -it pg-0 -- psql -U postgres -c "SELECT version();"- Backups — a CronJob with
pg_dumpto object storage, plus volume snapshots; test restores. Deleting the StatefulSet does not delete the PVCs (on purpose); deleting the namespace does. - More than one replica is not replication —
replicas: 3gives three independent empty databases. For primary/replica with failover use an operator (CloudNativePG, Zalando Postgres Operator, Crunchy) — or a managed database (RDS, Azure Database), which is often the better choice for production. - Updates — changing the image rolls Pods one by one from the highest ordinal; for databases check the release notes (major versions need a dump/restore or
pg_upgrade).
Check that it worked
kubectl -n data get pods shows pg-0 Running and 1/1 ready; kubectl -n data get pvc shows data-pg-0 Bound; a table created in pg-0 is still there after kubectl -n data delete pod pg-0 (the Pod comes back with the same name and disk).
Common problems
| Problem | Cause | Fix |
|---|---|---|
initdb: directory … exists but is not empty | lost+found in the volume root. | Set PGDATA to a subfolder. |
Pod Pending | PVC cannot be provisioned. | Check the StorageClass; kubectl describe pvc data-pg-0. |
| Three replicas, different data | StatefulSet does not replicate data. | Use an operator or a managed database. |
| Data lost after deleting the namespace | PVCs deleted with it (reclaim Delete). | Backups; Retain class; protect the namespace. |
Pod stuck Terminating after node failure | StatefulSet waits to be sure the old Pod is gone (no two writers). | Confirm the node is dead, then force delete the Pod. |
App cannot reach pg-0.pg | Wrong serviceName or namespace. | serviceName must match the headless Service; use the full name. |
Go further
- Command pages: kubectl, psql, openssl.
- Next how-tos: Give a Pod persistent storage, Back up and restore PostgreSQL, Set up PostgreSQL replication, Back up Kubernetes with Velero.
- Learn it: CKA — Other workloads, CKA — Storage objects, CKA — Service DNS.
1What does a StatefulSet give a database that a Deployment does not?
Stable Pod names, one persistent volume per Pod that comes back after rescheduling, and ordered start and stop.
2Does replicas: 3 in a StatefulSet create a replicated database?
No — it creates three independent instances; replication needs the database's own setup, usually through an operator.