Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shriomtri/d7ed6ff6c60eccddd9a8bfe4cf0f55fd to your computer and use it in GitHub Desktop.
Save shriomtri/d7ed6ff6c60eccddd9a8bfe4cf0f55fd to your computer and use it in GitHub Desktop.
Managing Container Storage with Kubernetes Volumes

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.

Additional Resources

Your company, BeeBox, is developing some applications that have storage needs beyond the short-lived storage of the container file system. One component, a simple maintenance script, needs to be able to interact with a directory on the host file system. Another needs to be able to share data between two containers in the same Pod.

Your task is to build a simple architecture to demonstrate how these tasks can be accomplished with Kubernetes volumes. Create Pods that meet the specified criteria.

Learning Objectives

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

    • One of the applications under development will be a maintenance script that needs to write data to the host's file system. Create a Pod that outputs some data every five seconds to the host's disk in the directory /var/data.
  2. Create a Multi-Container Pod That Shares Data between Containers Using a Volume

    • Another application component includes two pieces of software that need to collaborate using shared data. Create a multi-container Pod with a volume mounted to both containers.

Solution

  1. 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:

    vi maintenance-pod.yml
    
    
  2. Enter in the first part of the basic YAML for a simple busybox Pod that outputs some data every five 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. Check that the final maintenance-pod.yml file looks like this:

    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']
    
          volumeMounts:
          - name: output-vol
            mountPath: /output
    
        volumes:
        - name: output-vol
          hostPath:
            path: /var/data
    
    
  6. Save the file and exit by pressing the ESC key and using :wq.

  7. Finish creating the Pod:

    kubectl create -f maintenance-pod.yml
    
    
  8. Make sure the Pod is up and running:

    kubectl get pods
    
    

    Check that maintenance-pod is running, so it should be outputting data to the host system.

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

    ssh cloud_user@<PUBLIC_IP_ADDRESS>
    
    
  10. Look at the output to see whether the Pod setup was successful:

```
cat /var/data/output.txt

```

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:

    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. Make sure that the final shared-data-pod.yml file looks like this:

    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']
          volumeMounts:
          - name: shared-vol
            mountPath: /output
        - name: busybox2
          image: busybox
          command: ['sh', '-c', 'while true; do cat /input/output.txt; sleep 5; done']
          volumeMounts:
          - name: shared-vol
            mountPath: /input
        volumes:
        - name: shared-vol
          emptyDir: {}  
    
    
  8. Save the file and exit by pressing the ESC key and using :wq.

  9. Finish creating the multi-container Pod:

    kubectl create -f shared-data-pod.yml
    
    
  10. Make sure the Pod is up and running and check if both containers are running and ready:

```
kubectl get pods

```
  1. 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:
```
kubectl logs shared-data-pod -c busybox2

```
  1. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment