Deploying Stateful Applications on Kubernetes

Deploying Stateful Applications on Kubernetes

Prerequisites

  • A Kubernetes cluster
  • A basic understanding of Kubernetes concepts
  • A stateful application that you want to deploy

Step 1: Create a Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  storageClassName: my-storage-class
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  hostPath:
    path: /mnt/data
kubectl apply -f pv.yaml

Step 2: Create a Persistent Volume Claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  storageClassName: my-storage-class
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
kubectl apply -f pvc.yaml

Step 3: Create a StatefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  serviceName: my-app
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image
        volumeMounts:
        - name: my-persistent-storage
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: my-persistent-storage
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: my-storage-class
      resources:
        requests:
          storage: 10Gi
kubectl apply -f statefulset.yaml

Step 4: Verify Your Deployment

kubectl get statefulsets
kubectl get pods

Leave a comment

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