Managing Container Storage with Kubernetes Volumes

Introduction

Kubernetes volumes offer a simple way to mount external storage to containers. This lab will test your knowledge of volumes as you provide storage to some containers according to a provided specification. This will allow you to practice what you know about using Kubernetes volumes.

Solution

Log in to the control plane server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Create a Pod That Outputs Data to the Host Using a Volume

  1. Create a Pod that will interact with the host file system by using vi maintenance-pod.yml.

  2. Enter in the first part of the basic YAML for a simple busybox Pod that outputs some data every 5 seconds to the host’s disk:

    apiVersion: v1
    kind: Pod
    metadata:
        name: maintenance-pod
    spec:
        containers:
        - name: busybox
          image: busybox
          command: ['sh', '-c', 'while true; do echo Success! >> /output/output.txt; sleep 5; done']
    
  3. Under the basic YAML, begin creating volumes, which should be level with the containers spec:

    volumes:
    - name: output-vol
      hostPath:
          path: /var/data
    
  4. In the containers spec of the basic YAML, add a line for volume mounts:

    volumeMounts:
    - name: output-vol
      mountPath: /output
    
  5. Save the file and exit by pressing the ESC key and using :wq.

  6. Finish creating the Pod by using kubectl create -f maintenance-pod.yml.

  7. Make sure the Pod is up and running by using kubectl get pods and check that maintenance-pod is running, so it should be outputting data to the host system.

  8. Log into the worker node server using the credentials provided:

    ssh cloud_user@<PUBLIC_IP_ADDRESS>
    
  9. Look at the output by using cat /var/data/output.txt to see whether the Pod setup was successful.

Create a Multi-Container Pod That Shares Data Between Containers Using a Volume

  1. Return to the control plane server.

  2. Create another YAML file for a shared-data multi-container Pod by using vi shared-data-pod.yml.

  3. Start with the basic Pod definition and add multiple containers, where the first container will write the output.txt file and the second container will read the output.txt file:

    apiVersion: v1
    kind: Pod
    metadata:
        name: shared-data-pod
    spec:
        containers:
        - name: busybox1
          image: busybox
          command: ['sh', '-c', 'while true; do echo Success! >> /output/output.txt; sleep 5; done']
        - name: busybox2
          image: busybox
          command: ['sh', '-c', 'while true; do cat /input/output.txt; sleep 5; done']
    
  4. Set up the volumes, again at the same level as containers with an emptyDir volume that only exists to share data between two containers in a simple way:

    volumes:
    - name: shared-vol
      emptyDir: {}
    
  5. Mount that volume between the two containers by adding the following lines under command for the busybox1 container:

    volumeMounts:
    - name: shared-vol
      mountPath: /output
    
  6. For the busybox2 container, add the following lines to mount the same volume under command to complete creating the shared file:

    volumeMounts:
    - name: shared-vol
      mountPath: /input
    
  7. Save the file and exit by pressing the ESC key and using :wq.

  8. Finish creating the multi-container Pod using kubectl create -f shared-data-pod.yml.

  9. Make sure the Pod is up and running by using kubectl get pods and check if both containers are running and ready.

  10. To make sure the Pod is working, check the logs for shared-data-pod.yml and specify the second container that is reading the data and printing it to the console, using kubectl logs shared-data-pod -c busybox2.

  11. If you see the series of “Success!” messages, you have successfully created both containers, one of which is using a host path volume to write some data to the host disk and the other of which is using an emptyDir volume to share a volume between two containers in the same Pod.