AKS 15 – Stateful Containers and why need

A stateful container is like a bank account — it remembers your balance, transactions, and details even if you log out and come back later.

  • Imagine you’re using a diary app. If it forgets everything every time you close it, that’s stateless.
  • But if it remembers your notes, even after restarting your phone, that’s stateful — it stores data somewhere safe.

💻 Technical Explanation:

A stateful container in Kubernetes is typically part of a StatefulSet, which is used when:

  • The application needs persistent storage (e.g., databases).
  • Each Pod must have a stable identity (hostname, network).
  • Pods are created and deleted in order.
  • Data must survive Pod restarts.

🔑 Key Features:

  • Uses PersistentVolumeClaim (PVC) for storage.
  • Each Pod gets a unique name like mysql-0, mysql-1.
  • Ideal for apps like PostgreSQL, MongoDB, Kafka, etc.

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: postgres-db

spec:

  serviceName: “postgres”

  replicas: 1

  selector:

    matchLabels:

      app: postgres

  template:

    metadata:

      labels:

        app: postgres

    spec:

      containers:

      – name: postgres

        image: postgres:14

        volumeMounts:

        – name: pgdata

          mountPath: /var/lib/postgresql/data

  volumeClaimTemplates:

  – metadata:

      name: pgdata

    spec:

      accessModes: [“ReadWriteOnce”]

      resources:

        requests:

          storage: 10Gi

🧪 What Happens:

  • A Pod postgres-db-0 is created.
  • It gets a persistent volume (pgdata) that stores the database files.
  • Even if the Pod crashes or restarts, the data is retained.

Leave a Reply

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