Debug Statefulset

Debug StatefulSet in Kubernetes

Kubernetes

Kubernetes StatefulSets manage stateful workloads by preserving pod identity and storage. When pods in a StatefulSet misbehave, engineers struggle to trace the root cause. This guide shows you how to debug StatefulSets, list pods, inspect events, exec into containers, port-forward, use ephemeral containers, and dive into storage lifecycles, CSI drivers, access modes, quotas, and security contexts to restore healthy stateful workloads.


TL;DR

  • List StatefulSet pods with labels and observe ordering and identity.
  • Use kubectl describe to view events and PVC bindings for each pod.
  • Exec, port-forward, and attach ephemeral containers for live debugging.
  • Understand PV and PVC lifecycle: static vs. dynamic provisioning by CSI drivers.
  • Choose correct access modes (RWO, RWX) and set security contexts and resource quotas.
  • Refer to the workflow diagram for a step-by-step debug process.

Debug StatefulSet: List Pods

Start by listing all pods managed by your StatefulSet. Pods carry a stable network identity and storage. Use your app label and observe the ordinal suffix:

This confirms pod identity and ordering.


Debug StatefulSet: Inspect Pod Details and Events

For each pod, run kubectl describe to fetch events, volume mounts, and status conditions:

Key fields to watch:

  • Events: CrashLoopBackOff, FailedMount.
  • Volumes: PVC names bound to PVs – shows storage class and capacity.
  • Conditions: Ready, ContainersReady.

Debug StatefulSet: Exec and Port-Forward

Exec into a container for real-time inspection of processes, logs, or file system:

Use kubectl port-forward to expose container ports locally:

Combine with curl or grpcurl to test endpoints inside the pod’s network namespace.


Debug StatefulSet: Use Ephemeral Containers

Attach an ephemeral container if original images lack debugging tools:

Ephemeral containers share the PID namespace and let you inspect running processes.


Storage in StatefulSet: PVC and PV Lifecycle

StatefulSets mount PersistentVolumeClaims (PVCs) per pod. PVCs bind to PersistentVolumes (PVs) according to storage class and access mode. Understand these phases:

  • Pending: PVC waiting for PV binding.
  • Bound: PVC bound to matching PV by capacity and storage class.
  • Released: PVC deleted; PV enters released and then recycled or retained.
  • Available: PV ready for new claims if policy allows.

Check PVC status:


Provisioning and CSI Drivers in StatefulSet

Kubernetes uses Container Storage Interface (CSI) drivers for dynamic provisioning. StorageClass parameters choose the driver:

Dynamic provisioning creates PVs at runtime. CSI supports snapshot, clone, and encryption features for stateful workloads.


Access Modes and Security Contexts

Choose access modes based on workload needs:

  • ReadWriteOnce (RWO): Single node write access. Default for block storage.
  • ReadOnlyMany (ROX): Read-only across multiple nodes.
  • ReadWriteMany (RWX): Multiple nodes write access; often requires network file systems.

Apply security contexts to restrict permissions inside pods:

Restrict container capabilities and file system group to secure volume mounts against privilege escalation.


Resource Quotas and Limits

Set resource quotas at namespace level to prevent PVC overconsumption:

This ensures teams cannot provision more than allotted storage or PVC count.


Debugging Workflow

Debug Statefulset Workflow

References

Suggested Reading

PostHashID: f55db1f5a2bc13d4deea8e3ab7869f3e52c5120dbc0be52e724413ea54d1f959

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.