Managing Container Storage with Kubernetes Volumes: A Practical Guide

Managing Container Storage with Kubernetes Volumes: A Practical Guide

In the ever-evolving world of containerized applications and microservices, efficient storage management is essential. Kubernetes, the leading container orchestration platform, offers a robust solution for this challenge through its Kubernetes Volumes. In this article, we'll explore how to effectively manage container storage with Kubernetes Volumes, with a hands-on approach.

Introduction

Kubernetes Volumes provide a straightforward method for attaching external storage to containers. In this lab, we will put your knowledge to the test by provisioning storage for containers as per a given specification. This practical exercise will help you sharpen your skills in working with Kubernetes Volumes.

Solution

1/ Creating a Pod That Outputs Data to the Host Using a Volume

* Start by logging in to the control plane server using the provided credentials:

ssh k8s-user@<PUBLIC_IP_ADDRESS>

* Create a Pod that interacts with the host file system by creating a YAML file named maintenance-pod.yml:

vi maintenance-pod.yml

* In the YAML file, add the basic configuration for a simple busybox Pod that outputs data to the host's disk every five seconds:


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']

* Below the container configuration, create a section for volumes. This should be at the same level as the containers section:

volumes:
- name: output-vol
hostPath:
path: /var/data

* Within the containers section, add a line for volume mounts:

volumeMounts:
- name: output-vol
mountPath: /output

>> Ensure that your maintenance-pod.yml file resembles the following:


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

>> Save the file and exit the editor (in Vi, press ESC and use :wq).

* Create the Pod using the YAML configuration:

kubectl create -f maintenance-pod.yml

* Verify that the Pod is running successfully:

kubectl get pods

>> Ensure that the maintenance-pod is running, which means it should be outputting data to the host system.

* To verify the output on the worker node server, log in using the provided credentials:

ssh k8s-user@<PUBLIC_IP_ADDRESS>

* Check the output to see if the Pod setup was successful:

cat /var/data/output.txt

2/ Creating a Multi-Container Pod That Shares Data Between Containers Using a Volume

Return to the control plane server for this next part.

* Create another YAML file for a multi-container Pod that shares data. Name it shared-data-pod.yml:

vi shared-data-pod.yml

>> Begin with the basic Pod definition and add multiple containers. The first container will write to the output.txt file, and the second container will read from it:


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']

* Set up volumes at the same level as the containers section. Use an emptyDir volume to facilitate data sharing between the two containers:

volumes:
- name: shared-vol
emptyDir: {}

>> For the busybox1 container, add the following lines under the command section to mount the shared volume:

volumeMounts:
- name: shared-vol
mountPath: /output

>> For the


container, add the following lines to mount the same volume under command to complete the configuration for the shared file:

volumeMounts:
- name: shared-vol
mountPath: /input

>> Your shared-data-pod.yml file should now look 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: {}

>> Save the file and exit the editor (in Vi, press ESC and use :wq).

* Create the multi-container Pod using the YAML configuration:

kubectl create -f shared-data-pod.yml

* Verify that the Pod is running successfully and check if both containers are running and ready:

kubectl get pods

* To confirm that the Pod is functioning correctly, check the logs for shared-data-pod.yml, specifying the second container that reads the data and prints it to the console:

kubectl logs shared-data-pod -c busybox2

If you see a series of "Success!" messages, you have successfully created a multi-container Pod. One of these containers uses a host path volume to write data to the host disk, while the other uses an emptyDir volume to share data between the two containers in the same Pod.

--->> In this article, we've walked through the process of managing container storage with Kubernetes Volumes, covering both.


Thank you.

Omar Barkallah

#kubernetes #volumes #devops

要查看或添加评论,请登录

Omar Barkallah的更多文章

社区洞察

其他会员也浏览了