Topic outline

  • Shared Volume in Multi-Container Pods

    Sometimes wee need to share a volume with 2 or more containers running in a multi-container pod.

    Create a Multi-Container Pod

    Create a Pod with two containers, both with the image alpine running the command "echo hello; sleep 3600".
    Exec into the the second container and run 'ls'.

    The easiest way to do it is to create a pod with a single container and save its definition in a YAML file and extend it with an additional container:

    k run alpine-2-containers --image=alpine --restart=Never -o yaml --dry-run=client -- /bin/sh -c 'echo hello;sleep 3600' > alpine-pod.yaml
    Copy the generated apline-pod.yaml as apline-pod-2-containers.yaml and copy/paste the container related values, so your final YAML should contain the following two containers (make sure those containers have a different name):
    • Shared Volume

      We'll extend the above alpine-2-containers with a shared volume of type emptyDir named share with a volumeMount for each container with a different mountPath for each container, the /tmp/share1 and /tmp/share2 folders as follow:

      • Access and write to the shared volume from each container

        k apply -f alpine-pod-share-volumes.yaml
        k exec -it alpine-2-containers-share-volume -c alpine1 -- sh
        touch /tmp/share1/sharefile
        echo "test-share1" > /tmp/share1/sharefile
        cat /tmp/share1/sharefile
        k exec -it alpine-2-containers-share-volume -c alpine2 -- cat /tmp/share2/sharefile