Discovering Kubernetes
Discovering Kyma
Working with Kubernetes Workloads
Using Services to Create Internal and External Communication with Kubernetes
Using Service Meshes with Istio
Using Kyma Eventing
Using Kubernetes Storage and StatefulSets
Using SAP BTP Service Management
Performing Observability and Monitoring
Using ConfigMaps and Secrets

Connecting Storage to StatefulSets

Objective

After completing this lesson, you will be able to connect storage to StatefulSets

Deploy a Postgres Database Using a StatefulSet

Usage Scenario

You, as a developer, have been tasked with creating a PostgreSQL database in Kubernetes that persists data on a PersistentVolume.

Task Flow

In this exercise, you will perform the following steps:

  1. Create a PersistentVolume.
  2. Create a PersistentVolumeClaim.
  3. Deploy a Postgres database using a StatefulSet.
  4. Verify that the PersistentVolumeClaim is bound.

Prerequisites

Before you begin, ensure that the following prerequisites are met:

Task 1: Create a PersistentVolume

Steps

  1. Create a PersistentVolume for the Postgres database.

    In this task, you will take on the cluster administrator role and create a PersistentVolume for the cluster.

    1. Create a file called pv.yaml with the following content:

      YAML
      12345678910111213
      apiVersion: v1 kind: PersistentVolume metadata: name: my-persistent-volume spec: storageClassName: default volumeMode: Filesystem capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
    2. Apply the file to the cluster:

      Code Snippet
      1
      kubectl apply -f pv.yaml
    3. Check if the PersistentVolume was created:

      Code Snippet
      1
      kubectl get pv my-persistent-volume

      If successful, the PersistentVolume my-persistent-volume status is Available.

  2. Create PersistentVolumeClaim for the Postgres database

    Let's switch back to the role of a developer. Now you need to request a piece of storage from the cluster's PersistentVolume. To do this, create a PersistentVolumeClaim.

    1. Create a file called pvc.yaml with the following content:

      YAML
      1234567891011
      apiVersion: v1 kind: PersistentVolumeClaim metadata: name: postgres-claim spec: storageClassName: default accessModes: - ReadWriteOnce resources: requests: storage: 5Gi
    2. Apply the file to the cluster:

      Code Snippet
      1
      kubectl apply -f pvc.yaml
    3. Check if the PersistentVolumeClaim is created:

      Code Snippet
      1
      kubectl get pvc postgres-claim
      The status of the PersistentVolumeClaim postgres-claim is set to Pending. This is because it is waiting for the first consumer to bind it. You will create this first consumer in the next task.

  3. Create a StatefulSet for the Postgres database.

    1. Create a new file called postgres.yaml and specify the StatefulSet to create a postgres database.

      Code Snippet
      1234567891011121314151617181920212223242526272829303132333435
      apiVersion: apps/v1 kind: StatefulSet metadata: name: postgres spec: serviceName: postgres selector: matchLabels: app: postgres replicas: 1 template: metadata: labels: app: postgres spec: containers: # This is the container that runs the postgres image - name: postgres image: postgres:15.1 imagePullPolicy: "IfNotPresent" ports: - containerPort: 5432 env: # Environment variables that define a user - name: POSTGRES_DB value: "postgresdb" - name: POSTGRES_USER value: "admin" - name: POSTGRES_PASSWORD value: "password" volumeMounts: # Mount a volume to the path where postgres will store data - name: data mountPath: /var/lib/postgresql/data volumes: # This is the volume mounted to the container - name: data persistentVolumeClaim: claimName: postgres-claim
    2. Apply the file to create the StatefulSet.

      Code Snippet
      1
      kubectl apply -f postgres.yaml
    3. Check if the StatefulSet is successfully created.

      Code Snippet
      1
      kubectl get statefulset postgres
    4. Check if the Pod postgres-0 is successfully created.

      Code Snippet
      1
      kubectl get pod postgres-0
  4. Verify that the PersistentVolumeClaim is bound.

    1. Check if the PersistentVolumeClaim is bound to the PersistentVolume.

      Code Snippet
      1
      kubectl get pvc postgres-claim

      Now, the PersistentVolumeClaim's status is Bound.

      Note

      You can also play around with the postgres database. You can connect to it locally by port-forwarding the postgres-0 Pod to your local machine.
      Code Snippet
      1
      kubectl port-forward postgres-0 5432:5432

      Note

      With the Postgres client of your choice, you can now connect to the database with the credentials specified in the postgres.yaml file.

      Insert data into the database, remove the Pod, wait for it to be recreated and check if the data is still there.

Result

You have created a stateful application using a StatefulSet and used Kubernetes storage primitives to persist data.