Kubernetes volumes offer a simple way to mount external storage to containers. This lab will test your knowledge of volumes as you provide storage to some containers according to a provided specification. This will allow you to practice what you know about using Kubernetes volumes.
Your company, BeeBox, is developing some applications that have storage needs beyond the short-lived storage of the container file system. One component, a simple maintenance script, needs to be able to interact with a directory on the host file system. Another needs to be able to share data between two containers in the same Pod.
Your task is to build a simple architecture to demonstrate how these tasks can be accomplished with Kubernetes volumes. Create Pods that meet the specified criteria.
-
Create a Pod That Outputs Data to the Host Using a Volume
- One of the applications under development will be a maintenance script that needs to write data to the host's file system. Create a Pod that outputs some data every five seconds to the host's disk in the directory
/var/data
.
- One of the applications under development will be a maintenance script that needs to write data to the host's file system. Create a Pod that outputs some data every five seconds to the host's disk in the directory
-
Create a Multi-Container Pod That Shares Data between Containers Using a Volume
- Another application component includes two pieces of software that need to collaborate using shared data. Create a multi-container Pod with a volume mounted to both containers.
-
Log in to the control plane server using the credentials provided:
ssh cloud_user@<PUBLIC_IP_ADDRESS>
-
Create a Pod that will interact with the host file system:
vi maintenance-pod.yml
-
Enter in the first part of the basic YAML for a simple busybox Pod that outputs some data every five seconds to the host's disk:
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']
-
Under the basic YAML, begin creating volumes, which should be level with the
containers
spec:volumes: - name: output-vol hostPath: path: /var/data
-
In the
containers
spec of the basic YAML, add a line for volume mounts:volumeMounts: - name: output-vol mountPath: /output
-
Check that the final
maintenance-pod.yml
file looks like this: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 by pressing the
ESC
key and using:wq
. -
Finish creating the Pod:
kubectl create -f maintenance-pod.yml
-
Make sure the Pod is up and running:
kubectl get pods
Check that
maintenance-pod
is running, so it should be outputting data to the host system. -
Log into the worker node server using the credentials provided:
ssh cloud_user@<PUBLIC_IP_ADDRESS>
-
Look at the output to see whether the Pod setup was successful:
```
cat /var/data/output.txt
```
-
Return to the control plane server.
-
Create another YAML file for a shared-data multi-container Pod:
vi shared-data-pod.yml
-
Start with the basic Pod definition and add multiple containers, where the first container will write the
output.txt
file and the second container will read theoutput.txt
file: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 the volumes, again at the same level as
containers
with anemptyDir
volume that only exists to share data between two containers in a simple way:volumes: - name: shared-vol emptyDir: {}
-
Mount that volume between the two containers by adding the following lines under
command
for thebusybox1
container:volumeMounts: - name: shared-vol mountPath: /output
-
For the
busybox2
container, add the following lines to mount the same volume undercommand
to complete creating the shared file:volumeMounts: - name: shared-vol mountPath: /input
-
Make sure that the final
shared-data-pod.yml
file looks 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 by pressing the
ESC
key and using:wq
. -
Finish creating the multi-container Pod:
kubectl create -f shared-data-pod.yml
-
Make sure the Pod is up and running and check if both containers are running and ready:
```
kubectl get pods
```
- To make sure the Pod is working, check the logs for
shared-data-pod.yml
and specify the second container that is reading the data and printing it to the console:
```
kubectl logs shared-data-pod -c busybox2
```
- If you see the series of
Success!
messages, you have successfully created both containers, one of which is using a host path volume to write some data to the host disk and the other of which is using anemptyDir
volume to share a volume between two containers in the same Pod.