docs/doc/source/storage/kubernetes/storage-configuration-mount-persistent-volumes-in-containers.rst
Keane Lim ceea9dda0d Storage Guide update
Applied review feedback.

Fixed 'grey bars' formatting.
Fixed gerunds and lists
Added abbreviations and references

Change-Id: I104d678ce3ea52bddcbc141f8aad49ea1e1971db
Signed-off-by: Keane Lim <keane.lim@windriver.com>
2021-02-09 12:50:24 -05:00

5.7 KiB

Mount Persistent Volumes in Containers

You can launch, attach, and terminate a busybox container to mount in your cluster.

This example shows how a volume is claimed and mounted by a simple running container. It is the responsibility of an individual micro-service within an application to make a volume claim, mount it, and use it. For example, the stx-openstack application will make volume claims for mariadb and rabbitmq via their helm charts to orchestrate this.

You must have created the persistent volume claims. This procedure uses PVCs with names and configurations created in : Create Persistent Volume Claims <storage-configuration-create-persistent-volume-claims>.

  1. Create the busybox container with the persistent volumes created from the PVCs mounted.

    1. Create a yaml file definition for the busybox container.

      % cat <<EOF > busybox.yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: busybox
        namespace: default
      spec:
        progressDeadlineSeconds: 600
        replicas: 1
        selector:
          matchLabels:
            run: busybox
        template:
          metadata:
            labels:
              run: busybox
          spec:
            containers:
            - args:
              - sh
              image: busybox
              imagePullPolicy: Always
              name: busybox
              stdin: true
              tty: true
              volumeMounts:
              - name: pvc1
                mountPath: "/mnt1"
              - name: pvc2
                mountPath: "/mnt2"
            restartPolicy: Always
            volumes:
            - name: pvc1
              persistentVolumeClaim:
                claimName: test-claim1
            - name: pvc2
              persistentVolumeClaim:
                claimName: test-claim2
      EOF
    2. Apply the busybox configuration.

      % kubectl apply -f busybox.yaml
  2. Attach to the busybox and create files on the persistent volumes.

    1. List the available pods.

      % kubectl get pods
      NAME                       READY   STATUS    RESTARTS   AGE
      busybox-5c4f877455-gkg2s   1/1     Running   0          19s
    2. Connect to the pod shell for CLI access.

      % kubectl attach busybox-5c4f877455-gkg2s -c busybox -i -t
    3. From the container's console, list the disks to verify that the persistent volumes are attached.

      # df
      Filesystem           1K-blocks      Used Available Use% Mounted on
      overlay               31441920   3239984  28201936  10% /
      tmpfs                    65536         0     65536   0% /dev
      tmpfs                 65900776         0  65900776   0% /sys/fs/cgroup
      /dev/rbd0               999320      2564    980372   0% /mnt1
      /dev/rbd1               999320      2564    980372   0% /mnt2
      /dev/sda4             20027216   4952208  14034624  26%
      ...

      The PVCs are mounted as /mnt1 and /mnt2.

  3. Create files in the mounted volumes.

    # cd /mnt1
    # touch i-was-here
    # ls /mnt1
    i-was-here lost+found
    #
    # cd /mnt2
    # touch i-was-here-too
    # ls /mnt2
    i-was-here-too lost+found
  4. End the container session.

    # exit
    Session ended, resume using 'kubectl attach busybox-5c4f877455-gkg2s -c busybox -i -t' command when the pod is running
  5. Terminate the busybox container.

    % kubectl delete -f busybox.yaml
  6. Recreate the busybox container, again attached to persistent volumes.

    1. Apply the busybox configuration.

      % kubectl apply -f busybox.yaml
    2. List the available pods.

      % kubectl get pods
      NAME                       READY   STATUS    RESTARTS   AGE
      busybox-5c4f877455-jgcc4   1/1     Running   0          19s
    3. Connect to the pod shell for CLI access.

      % kubectl attach busybox-5c4f877455-jgcc4 -c busybox -i -t
    4. From the container's console, list the disks to verify that the PVCs are attached.

      # df
      Filesystem           1K-blocks      Used Available Use% Mounted on
      overlay               31441920   3239984  28201936  10% /
      tmpfs                    65536         0     65536   0% /dev
      tmpfs                 65900776         0  65900776   0% /sys/fs/cgroup
      /dev/rbd0               999320      2564    980372   0% /mnt1
      /dev/rbd1               999320      2564    980372   0% /mnt2
      /dev/sda4             20027216   4952208  14034624  26%
      ...
  7. Verify that the files created during the earlier container session still exist.

    # ls /mnt1
    i-was-here lost+found
    # ls /mnt2
    i-was-here-too lost+found