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

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.

Live Environment

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

  • 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:

      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

      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:

      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 was created:

      Code Snippet
      1
      kubectl get pvc postgres-claim
      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
      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 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
    2. Apply the file to create the StatefulSet.

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

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

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

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

      Code Snippet
      1
      kubectl get pvc postgres-claim

      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
      Expand

      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