Connecting Storage to StatefulSets

Objectives
After completing this lesson, you will be able to:

After completing this lesson, you will be able to:

  • Connect storage to StatefulSets

Deploy a Postgres Database Using a StatefulSet

Business Scenario

You, as a developer, have been tasked with creating a PostgreSQL (or simply Postgres) database on Kubernetes that persists data on a PersistentVolume.

Exercise Options

You can perform this exercise in two ways:

  1. Platform Simulation – follow the step-by-step instructions within the simulation.
  2. Live Environment - by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
Note
We strongly recommend first performing the steps in the Live Environment.

Platform Simulation

Choose the Start Exercise button below to open a simulation of the platform. Then follow the step-by-step instructions to deploy a Postgres database using a StatefulSet.

Live Environment

In this exercise, you will perform the following steps:

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

Prerequisites

  • You have successfully created a SAP BTP, Kyma runtime instance in the SAP BTP subaccount.
  • You have configured kubectl to work with your SAP BTP, Kyma runtime instance.

Result

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

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:

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

      Code snippet
      kubectl apply -f pv.yaml
      Copy code
    3. Check if the PersistentVolume was created:

      Code snippet
      kubectl get pv my-persistent-volume
      Copy code

      The PersistentVolume my-persistent-volume status should now be Available. Let's switch back to the role of a developer.

  2. Create PersistentVolumeClaim for the Postgres database

    Now you need to request a piece of storage from the clusters PersistentVolume. This is done by creating a PersistentVolumeClaim.

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

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

      Code snippet
      kubectl apply -f pvc.yaml
      Copy code
    3. Check if the PersistentVolumeClaim was created:

      Code snippet
      kubectl get pvc postgres-claim
      Copy code
      The status of the PersistentVolumeClaim postgres-claim should now be Pending. This is because it is waiting for the first consumer to bind it. This first consumer will be created 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
      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 will run the postgres image
              - name: postgres
                image: postgres:15.1
                imagePullPolicy: "IfNotPresent"
                ports:
                - containerPort: 5432
                env: # Some environment variables to 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 that will be mounted to the container
            - name: data
              persistentVolumeClaim:
                claimName: postgres-claim
      Copy code
    2. Apply the file to create the StatefulSet.

      Code snippet
      kubectl apply -f postgres.yaml
      Copy code
    3. Check if the StatefulSet was created.

      Code snippet
      kubectl get statefulset postgres
      Copy code
    4. Check if the Pod postgres-0 was created.

      Code snippet
      kubectl get pod postgres-0
      Copy code
  4. Verify that the PersistentVolumeClaim is bound.

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

      Code snippet
      kubectl get pvc postgres-claim
      Copy code

      It should now be in the status 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
      kubectl port-forward postgres-0 5432:5432
      Copy code
      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 via a StatefulSet and used Kubernetes storage primitives to persist data.

Log in to track your progress & complete quizzes