2016-12-07 01:22:21 +00:00
|
|
|
---
|
2017-06-08 18:48:28 +00:00
|
|
|
title: Configure a Pod to Use a Volume for Storage
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-20 04:43:52 +00:00
|
|
|
weight: 50
|
2016-12-07 01:22:21 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
This page shows how to configure a Pod to use a Volume for storage.
|
|
|
|
|
2018-08-22 18:37:45 +00:00
|
|
|
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
|
2016-12-07 01:22:21 +00:00
|
|
|
consistent storage that is independent of the Container, you can use a
|
2017-03-20 22:35:51 +00:00
|
|
|
[Volume](/docs/concepts/storage/volumes/). This is especially important for stateful
|
2018-08-22 18:37:45 +00:00
|
|
|
applications, such as key-value stores (such as Redis) and databases.
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2017-06-08 18:48:28 +00:00
|
|
|
## Configure a volume for a Pod
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
In this exercise, you create a Pod that runs one Container. This Pod has a
|
|
|
|
Volume of type
|
2017-03-20 22:35:51 +00:00
|
|
|
[emptyDir](/docs/concepts/storage/volumes/#emptydir)
|
2016-12-07 01:22:21 +00:00
|
|
|
that lasts for the life of the Pod, even if the Container terminates and
|
|
|
|
restarts. Here is the configuration file for the Pod:
|
|
|
|
|
2018-07-03 20:31:20 +00:00
|
|
|
{{< codenew file="pods/storage/redis.yaml" >}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
1. Create the Pod:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
2018-07-31 22:12:38 +00:00
|
|
|
kubectl create -f https://k8s.io/examples/pods/storage/redis.yaml
|
2018-07-17 07:45:38 +00:00
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
1. Verify that the Pod's Container is running, and then watch for changes to
|
|
|
|
the Pod:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
kubectl get pod redis --watch
|
|
|
|
```
|
|
|
|
|
2016-12-07 01:22:21 +00:00
|
|
|
The output looks like this:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
redis 1/1 Running 0 13s
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
1. In another terminal, get a shell to the running Container:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
kubectl exec -it redis -- /bin/bash
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-08-22 18:37:45 +00:00
|
|
|
1. In your shell, go to `/data/redis`, and then create a file:
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
root@redis:/data# cd /data/redis/
|
|
|
|
root@redis:/data/redis# echo Hello > test-file
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
1. In your shell, list the running processes:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
2018-07-18 20:21:39 +00:00
|
|
|
root@redis:/data/redis# apt-get update
|
|
|
|
root@redis:/data/redis# apt-get install procps
|
2018-07-17 07:45:38 +00:00
|
|
|
root@redis:/data/redis# ps aux
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
|
|
|
|
redis 1 0.1 0.1 33308 3828 ? Ssl 00:46 0:00 redis-server *:6379
|
|
|
|
root 12 0.0 0.0 20228 3020 ? Ss 00:47 0:00 /bin/bash
|
|
|
|
root 15 0.0 0.0 17500 2072 ? R+ 00:48 0:00 ps aux
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-08-22 18:37:45 +00:00
|
|
|
1. In your shell, kill the Redis process:
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
root@redis:/data/redis# kill <pid>
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-08-22 18:37:45 +00:00
|
|
|
where `<pid>` is the Redis process ID (PID).
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-08-22 18:37:45 +00:00
|
|
|
1. In your original terminal, watch for changes to the Redis Pod. Eventually,
|
2016-12-07 01:22:21 +00:00
|
|
|
you will see something like this:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
redis 1/1 Running 0 13s
|
|
|
|
redis 0/1 Completed 0 6m
|
|
|
|
redis 1/1 Running 1 6m
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
At this point, the Container has terminated and restarted. This is because the
|
2018-08-22 18:37:45 +00:00
|
|
|
Redis Pod has a
|
2018-05-05 16:00:51 +00:00
|
|
|
[restartPolicy](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#podspec-v1-core)
|
2016-12-07 01:22:21 +00:00
|
|
|
of `Always`.
|
|
|
|
|
|
|
|
1. Get a shell into the restarted Container:
|
|
|
|
|
2018-07-17 07:45:38 +00:00
|
|
|
```shell
|
|
|
|
kubectl exec -it redis -- /bin/bash
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
1. In your shell, goto `/data/redis`, and verify that `test-file` is still there.
|
2018-08-08 00:57:08 +00:00
|
|
|
```shell
|
|
|
|
root@redis:/data/redis# cd /data/redis/
|
|
|
|
root@redis:/data/redis# ls
|
|
|
|
test-file
|
|
|
|
```
|
|
|
|
|
|
|
|
1. Delete the Pod that you created for this exercise:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl delete pod redis
|
|
|
|
```
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
* See [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core).
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
* See [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core).
|
2016-12-07 01:22:21 +00:00
|
|
|
|
|
|
|
* In addition to the local disk storage provided by `emptyDir`, Kubernetes
|
|
|
|
supports many different network-attached storage solutions, including PD on
|
2018-08-22 18:37:45 +00:00
|
|
|
GCE and EBS on EC2, which are preferred for critical data and will handle
|
2016-12-07 01:22:21 +00:00
|
|
|
details such as mounting and unmounting the devices on the nodes. See
|
2017-03-20 22:35:51 +00:00
|
|
|
[Volumes](/docs/concepts/storage/volumes/) for more details.
|
2016-12-07 01:22:21 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
|
|
|
|
2016-12-07 01:22:21 +00:00
|
|
|
|