Configure a Pod to Use a Volume for Storage

This page shows how to configure a Pod to use a Volume for storage.

A Container’s file system lives only as long as the Container does. So when a Container terminates and restarts, filesystem changes are lost. For more consistent storage that is independent of the Container, you can use a Volume. This is especially important for stateful applications, such as key-value stores (such as Redis) and databases.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

Configure a volume for a Pod

In this exercise, you create a Pod that runs one Container. This Pod has a Volume of type emptyDir that lasts for the life of the Pod, even if the Container terminates and restarts. Here is the configuration file for the Pod:

pods/storage/redis.yamlConfigure a Pod to Use a Volume for Storage - 图1

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: redis
  5. spec:
  6. containers:
  7. - name: redis
  8. image: redis
  9. volumeMounts:
  10. - name: redis-storage
  11. mountPath: /data/redis
  12. volumes:
  13. - name: redis-storage
  14. emptyDir: {}
  1. Create the Pod:

    1. kubectl apply -f https://k8s.io/examples/pods/storage/redis.yaml
  2. Verify that the Pod’s Container is running, and then watch for changes to the Pod:

    1. kubectl get pod redis --watch

    The output looks like this:

    1. NAME READY STATUS RESTARTS AGE
    2. redis 1/1 Running 0 13s
  3. In another terminal, get a shell to the running Container:

    1. kubectl exec -it redis -- /bin/bash
  4. In your shell, go to /data/redis, and then create a file:

    1. root@redis:/data# cd /data/redis/
    2. root@redis:/data/redis# echo Hello > test-file
  5. In your shell, list the running processes:

    1. root@redis:/data/redis# apt-get update
    2. root@redis:/data/redis# apt-get install procps
    3. root@redis:/data/redis# ps aux

    The output is similar to this:

    1. USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
    2. redis 1 0.1 0.1 33308 3828 ? Ssl 00:46 0:00 redis-server *:6379
    3. root 12 0.0 0.0 20228 3020 ? Ss 00:47 0:00 /bin/bash
    4. root 15 0.0 0.0 17500 2072 ? R+ 00:48 0:00 ps aux
  6. In your shell, kill the Redis process:

    1. root@redis:/data/redis# kill <pid>

    where <pid> is the Redis process ID (PID).

  7. In your original terminal, watch for changes to the Redis Pod. Eventually, you will see something like this:

    1. NAME READY STATUS RESTARTS AGE
    2. redis 1/1 Running 0 13s
    3. redis 0/1 Completed 0 6m
    4. redis 1/1 Running 1 6m

At this point, the Container has terminated and restarted. This is because the Redis Pod has a restartPolicy of Always.

  1. Get a shell into the restarted Container:

    1. kubectl exec -it redis -- /bin/bash
  2. In your shell, go to /data/redis, and verify that test-file is still there.

    1. root@redis:/data/redis# cd /data/redis/
    2. root@redis:/data/redis# ls
    3. test-file
  3. Delete the Pod that you created for this exercise:

    1. kubectl delete pod redis

What’s next

  • See Volume.

  • See Pod.

  • In addition to the local disk storage provided by emptyDir, Kubernetes supports many different network-attached storage solutions, including PD on GCE and EBS on EC2, which are preferred for critical data and will handle details such as mounting and unmounting the devices on the nodes. See Volumes for more details.