Cloud & KubernetesJA · Japanese

Avoiding Data Loss Pitfalls When Running PostgreSQL StatefulSets on kind Kubernetes Clusters

Field notes on deploying a PostgreSQL StatefulSet on kind v1.37.0: resolving PGDATA path pitfalls that lead to data loss, sideloading local container images, and configuring a Headless Service.

Curated by Vibe DevOps · approx. 534 words

Key Takeaways

  • The default PGDATA environment variable in PostgreSQL 18 images targets a version-specific subdirectory, causing writes to bypass the mounted volume unless a nested subpath is explicitly configured.
  • The PGDATA path must point one level deeper into a subdirectory within the mount point to preserve data integrity and avoid root-level filesystem conflicts.
  • kind cluster nodes do not share the host Docker daemon's image cache, requiring local images to be explicitly loaded into the nodes for offline testing.
  • StatefulSets require a Headless Service (clusterIP: None) to provide deterministic network identities and direct Pod-level DNS resolution.

From Original Source

Storage Path Pitfalls and Image Sideloading in kind

When provisioning a PostgreSQL 18 StatefulSet on a kind cluster, mounting a volume directly to the default data root can cause data to vanish upon Pod termination. PostgreSQL 18 uses a versioned directory convention by default, which can cause the engine to write to the container's ephemeral root filesystem instead of the persistent volume. Setting the PGDATA variable to a nested subdirectory within the mount path ensures data is committed to persistent storage while preventing non-empty directory initialization errors.

Additionally, kind worker nodes run in isolated containers and do not inherit locally cached images from the host Docker daemon. Unless images are sideloaded directly into the nodes using CLI tooling or pulled from a container registry, Pods will get stuck in an ErrImagePull state.

From Original Source

Network Identity and Credential Hardening

Unlike stateless workloads, StatefulSets require an accompanying Headless Service to maintain stable network identities. Configuring clusterIP: None allows intra-cluster services to resolve individual replicas directly using deterministic Pod DNS names rather than routing through a virtual cluster IP.

For administrative credentials, generating Kubernetes Secrets from an environment file protects sensitive credentials from leaking into shell history and avoids duplicate-key collisions inherent in inline manifest definitions.

Vibe DevOps Editorial Commentary

Operational Recommendations for Databases on Kubernetes

Vibe DevOps perspective: When testing stateful databases locally, engineers should proactively simulate failure domains by killing Pods and verifying data consistency post-recovery. This practice surfaces volume mount misconfigurations and DNS resolution failures early before promoting configs into production CI/CD pipelines.

Commands & Configuration Snippets

Verbatim extract for quick lookup. Verify against your infrastructure before running.

kind create cluster --name <name>

Initialize a local Kubernetes cluster with a custom cluster name using the kind tool.

kind load docker-image postgres:18-alpine --name <name>

Directly load a container image from the host into the kind node without going through a registry.

kubectl create secret generic postgresql-secrets --from-env-file=.env

Create a generic Secret from an environment file to securely pass credentials.

kubectl delete pod db-0

Delete the database Pod to test data recovery capabilities after re-initialization.

⚠️ Operational Caveats & Limitations

  • Based on local experiments running PostgreSQL 18 on kind targeting Kubernetes v1.37.0.
  • Production cloud deployments require robust CSI-backed block storage and advanced RBAC/IAM permission boundaries.

Read original post at Zenn — Topic Kubernetes ↗


Explore Next on Vibe DevOps

More Global DevOps News