Skip to content

Exercise

Exercise: Storage

  1. Setting up the default StorageClass

    SKS defines a StorageClass to provision storage dynamically, it is named exoscale-sbs. This StorageClass is not set as the default one, run the following command to do so:

    kubectl patch storageclass exoscale-sbs -p '{"metadata": {"annotations": {"storageclass.kubernetes.io/is-default-class": "true"}}}'
    
  2. In a file named pvc-redis.yaml, define the specification for a PersistentVolumeClaim with the following characteristics:

    • name: redis
    • ReadWriteOnce mode
    • request for 1OG storage

    Warning

    10Gi if the minimum capacity we can require on Exoscale

    Then modify the redis Deployment specification by adding a volume based on this PersistentVolumeClaim, and use the volumeMounts instruction to ensure that the associated PersistentVolume is mounted in the /data directory of the redis container.

  3. In a file named pvc-db.yaml containing the specification for a PersistentVolumeClaim with the following characteristics:

    • name: db
    • ReadWriteOnce mode
    • request for 10G of storage

    Then modify the db Deployment specification by adding a volume based on this PersistentVolumeClaim, and use the volumeMounts instruction to ensure that the associated PersistentVolume is mounted in the /var/lib/postgresql/data directory of the postgres container.

  4. Deploy the application defined in these specifications and verify that it is working correctly.

  5. List the PersistentVolumeClaim resources. What do you observe?

  6. Delete the application.

Tip
  1. already done

  2. The specification to define the PersistentVolumeClaim named redis:

    pvc-redis.yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata: 
      name: redis
    spec: 
      accessModes:
        - ReadWriteOnce
      resources:
        requests: 
          storage: 10Gi
    

    The redis Deployment is modified as follows:

    deploy-redis.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: redis
      name: redis
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: redis
      template:
        metadata:
          labels:
            app: redis
        spec:
          containers:
            - image: redis:7.0.8-alpine3.17
              name: redis
              volumeMounts:
              - name: data
                mountPath: /data
          volumes:
          - name: data
            persistentVolumeClaim:
              claimName: redis
    
  3. The specification to define the PersistentVolumeClaim named db:

    pvc-db.yaml
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata: 
      name: db
    spec: 
      accessModes:
        - ReadWriteOnce
      resources:
        requests: 
          storage: 10Gi
    

    The db Deployment is modified as follows:

    deploy-db.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: db
      name: db
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: db
      template:
        metadata:
          labels:
            app: db
        spec:
          containers:
            - image: postgres:15.1-alpine3.17
              name: postgres
              env:
                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: db
                      key: password
              volumeMounts:
              - name: data
                mountPath: /var/lib/postgresql/data
              ports:
                - containerPort: 5432
                  name: postgres
          volumes:
          - name: data
            persistentVolumeClaim: 
              claimName: db
    
  4. Deploy the application using the following command from the manifests directory:

    kubectl apply -f .
    

    Using the IP address of one of the cluster nodes, you can access the vote and result interfaces via ports 31000 and 31001 respectively.

  5. You can list the PersistentVolumeClaim resources and observe that a PersistentVolume has been created for each of the 2 PVCs.

    List of PersistentVolumeClaims:

    $ kubectl get pvc
    NAME                          STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    persistentvolumeclaim/redis   Bound    pvc-789e3c5c-4402-4b96-b09d-ee441e8ade1d   10Gi       RWO            local-path     39s
    persistentvolumeclaim/db      Bound    pvc-75b9a32c-eab5-4452-a9b8-12d41dd74e7a   10Gi       RWO            local-path     39s
    

    List of PersistentVolumes:

    $ kubectl get pv
    NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM           STORAGECLASS   REASON   AGE
    persistentvolume/pvc-789e3c5c-4402-4b96-b09d-ee441e8ade1d   10Gi       RWO            Delete           Bound    default/redis   local-path              32s
    persistentvolume/pvc-75b9a32c-eab5-4452-a9b8-12d41dd74e7a   10Gi       RWO            Delete           Bound    default/db      local-path              32s
    

    The volumes can be listed from the Exoscale console

    Console

  6. Delete the application using the following command from the manifests directory:

    kubectl delete -f .