peter (admin) -

Kubernetes Volumes
In this section, we will learn how volumes work in Kubernetes and how they help us store and keep data safe even when containers restart.
Checking the Kubernetes Cluster
We are already logged into both the master node and the worker node.
To verify the cluster, run:
kubectl get nodes
Both the master and worker nodes should be in the Ready state. This means the cluster is running and both nodes are properly connected and working.
Creating the Pod Configuration
Create a configuration file for the pod and give it the name volume-test. The pod will use the NGINX image so that a simple web container can run inside it.
The pod is configured with a restart policy of Never, so it will not continuously restart if it stops.
Generate the full configuration in YAML format and save it to a file so that it can be edited to add the volume configuration.
For example:
kubectl run volume-test
--image=nginx
--restart=Never
--dry-run=client
-o yaml > volume.yaml
Configuring the Volume
Edit the YAML file to add the volume configuration.
By default, the pod does not have persistent storage attached. If the container stops or is deleted, data stored only inside the container can be lost.
By adding a volume, we provide the container with storage that exists independently of the container's filesystem.
The basic pod configuration contains the following elements:
-
apiVersionspecifies the Kubernetes API version being used. -
kinddefines the resource as aPod. -
metadatacontains the pod name and labels. -
specdefines how the pod should run. -
The container uses the NGINX image.
-
The container is given a name for identification.
-
The
resourcessection contains no resource limits. -
The DNS policy is set to
ClusterFirst. -
The restart policy is set to
Never.
At this point, the configuration defines a simple NGINX pod without storage attached.
Adding the Volume Mount
Inside the container configuration, add the volumeMounts section.
The volume is mounted at:
/data
This tells Kubernetes where the volume should appear inside the container.
For example:
volumeMounts:
- name: data-volume
mountPath: /data
The volume mount has a name that connects it to the actual volume definition.
Defining the Volume
The actual volume is defined inside the spec section, but outside the containers block.
For this example, a hostPath volume is used:
volumes:
- name: data-volume
hostPath:
path: /tmp/data
type: DirectoryOrCreate
The volume has the same name as the volume mount:
data-volume
The hostPath connects the volume to a directory on the Kubernetes node.
The configured path is:
/tmp/data
Kubernetes uses this directory on the worker node to store the data.
The DirectoryOrCreate type means that Kubernetes will create the directory if it does not already exist.
The complete configuration therefore provides the container with storage from the node and makes that storage available inside the container at /data.
Applying the Pod Configuration
Save and exit the YAML file, then apply it to the cluster:
kubectl apply -f volume.yaml
The pod should be created successfully using the updated configuration.
Verify the pod:
kubectl get pods
Writing Data to the Volume
Access the container so that we can work with the attached storage:
kubectl exec -it volume-test -- /bin/bash
Inside the container, write some data to a file in the mounted volume:
echo "persistent data" > /data/file.txt
The file is stored at:
/data/file.txt
Verify the contents:
cat /data/file.txt
The output should be:
persistent data
This confirms that the data has successfully been written to the mounted volume.
Exit the container:
exit
Deleting and Recreating the Pod
Now delete the pod to test whether the data stored in the volume remains available:
kubectl delete pod volume-test
After deleting the pod, apply the same configuration again:
kubectl apply -f volume.yaml
This creates a new pod using the same volume configuration.
The purpose of this test is to determine whether the data stored earlier is still available after the original container has been removed.
Verifying Persistent Data
Access the newly created pod:
kubectl exec -it volume-test -- /bin/bash
Read the file from the mounted volume:
cat /data/file.txt
The output should again be:
persistent data
This confirms that the data is still available even though the original pod was deleted and recreated.
How Kubernetes Volumes Work
This example demonstrates how Kubernetes volumes can provide storage that survives the lifecycle of a container.
Without a volume, data stored inside the container's filesystem can be lost when the container is removed.
With the hostPath volume used in this example, the data is stored in /tmp/data on the Kubernetes node and made available inside the container through the /data mount point.
The basic relationship is:
Kubernetes Worker Node
|
| /tmp/data
|
v
HostPath Volume
|
| mounted as /data
|
v
Container
|
| /data/file.txt
|
v
"persistent data"
When the pod is deleted and recreated with the same volume configuration, the new container can access the data stored in the node's /tmp/data directory.
Summary
Kubernetes volumes provide a way to make data available independently of a container's own filesystem.
In this example:
-
The pod runs an NGINX container.
-
A
hostPathvolume is configured. -
The node directory
/tmp/datais used for storage. -
The volume is mounted inside the container at
/data. -
Data is written to
/data/file.txt. -
The pod is deleted.
-
A new pod is created using the same configuration.
-
The original data is still available.
This demonstrates how volumes can help keep application data available even when containers are deleted and recreated.
Comments (0)
There are no comments yet. Be the first to comment.
Log in or register to leave a comment. all comments are subject to admin approval