Mastering StatefulSets in OpenShift

Objectives:

  • Understand the use of StatefulSets for stateful applications.

  • Deploy a stateful application and manage its lifecycle.

  • Handle persistent storage in the context of StatefulSets.

Task 1: Deploy a StatefulSet and Explore its Components

Objective:

Deploy a StatefulSet and explore its components.

Explanation:

StatefulSets are valuable for applications that require stable network identifiers and ordered, graceful deployment and scaling.

create a statefulset.yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx
spec:
  serviceName: "nginx"
  replicas: 2                   # Number of replicas for the StatefulSet
  selector:
    matchLabels:
      app: nginx                # Label selector for pods in this StatefulSet
  template:
    metadata:
      labels:
        app: nginx              # Labels for the pod template
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: nginx           # Container name
          image: nginx         # Image to use for the container
          ports:
            - containerPort: 80 # Port to expose on the container
              name: web        # Name of the port
          volumeMounts:
            - name: www        # Name of the volume to mount
              mountPath: /usr/share/nginx/html # Path inside the container where volume is mounted
      volumes:
        - name: www             # Define the volume using existing PVC
          persistentVolumeClaim:
            claimName: nginx-hostpath-pvc # Reference to the existing PVC
oc create -f statefulsets.yaml

Task 2: Simulate Stateful Application Updates and Rollbacks

Objective:

Simulate stateful application updates and rollbacks.

Explanation:

Understanding how to update and rollback stateful applications is crucial for maintaining stability and ensuring data integrity.

oc set image statefulset/nginx nginx=nginx:1.22

To rollback to previous versions:

oc rollout undo statefulset/nginx

Task 3: Manage Persistent Volume Claims Associated with a StatefulSet

Objective:

Learn how to manage persistent volume claims associated with a StatefulSet.

Explanation:

StatefulSets often require persistent storage. Managing associated persistent volume claims is essential for handling data persistence.

Creating a persistent volume and persistent volume claim:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-hostpath-pv
spec:
  capacity:
    storage: 5Gi               # Specify the total storage capacity
  accessModes:
    - ReadWriteOnce             # Access mode for the volume
  hostPath:                     # Using hostPath for local storage
    path: /mnt/data/nginx-data  # Path on the host node where data will be stored
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-hostpath-pvc
spec:
  accessModes:
    - ReadWriteOnce             # Access mode to match the PV
  resources:
    requests:
      storage: 5Gi              # Requesting the same size as the PV
oc create -f persistentvolume.yaml
oc create -f persistentvolumeclaim.yaml
oc get statefulsets