Kubernetes is a container orchestration tool that incorporates the idea of pods as ephemeral items. This temporary nature would have us assume that storage would be difficult to manage. In reality, the opposite is true. Kubernetes allows you to natively create and manage storage decoupled from pods.
To show you what I mean, let’s go through an exercise that will provision a persistent disk in Google Cloud using a Cloud Sandbox environment in Linux Academy’s Cloud Playground. Try it for yourself and follow along by starting your free 7-day trial here.
Spinning up the Environment
When the Google Cloud Platform Sandbox is ready, open the console in a private browser session and use your given username and password to login (provided by Linux Academy!).
Creating a Kubernetes Cluster
Within the menu on the left, click on Kubernetes Engine. Then, click on Create cluster.
Select standard cluster, allow the defaults and click on Create. Once the cluster has been created, it will have a green checkmark next to the cluster name. Once you see the connect button, click on it, then click on Run in Cloud Shell.
When the Cloud Shell starts up, it will auto-fill a command, starting with
gcloud container clusters.... Hit the enter key on your keyboard in order to connect to your cluster. To verify that you’ve successfully connected to your cluster, type the following command:
Now that we’re connected to our cluster, let’s go ahead and create a persistent disk. In order to provide storage to our pods, we have to place the disk in the same region as our cluster. So let’s find out our region by running the following command:
gcloud container clusters list
You will see an output similar to this, where the “location” is what we are trying to obtain:
Creating Persistent Storage
Now that we have the location of our cluster, we can now proceed to create the persistent disk. Let’s create a 1 GiB disk named “volume1” with the following command:
gcloud compute disks create --size=1GiB --zone=us-central1-a volume1
You should see an output similar to this:
Now let’s go ahead and create the YAML for a pod that will use this persistent disk that we just created in Google Cloud with the following command:
kubectl run pod1 --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c 'sleep 3600' > pod.yaml
Now that we’ve created the YAML, let’s open the file and add the volumes to the pod YAML:
Add the following YAML under the container spec:
volumeMounts: - name: vol1 mountPath: /data/ volumes: - name: vol1 gcePersistentDisk: pdName: volume1 fsType: ext4
The YAML above will create a volume named vol1 and mount it to the
/data path with the same name, attached to the container within the pod.
Create a Pod Mounting the Volume
Let’s create and start the pod by running the following command:
kubectl apply -f pod.yaml
To verify that the pod is running, perform the following command:
kubectl get pods
Now that our pod is up and running, let’s connect directly to the container and write a file to that volume that is mounted to that container. Run this command to connect directly to the container:
kubectl exec pod1 -it -- sh
Now that we have connected directly to the container, lets write a file to the persistent volume mounted at
/data with the following command:
echo "hello" > /data/file1.txt
Let’s verify it’s there by listing the contents of that directory with the following command:
Delete the Pod to Demonstrate Persistent Storage
Ok, now that we’ve created a file in that directory and we’ve verified that it’s there, let’s go ahead and delete the pod. First, let’s exit the shell that we entered on the container with a simple
exit command. Then use the following command to delete the pod:
kubectl delete pod pod1
Are you wondering whether the data will still be there after the pod has been deleted? Well, let’s find out by creating an entirely new pod and checking if that file still exists.
So let’s open our pod YAML again and modify the name of the pod from “pod1” to “pod2”:
Here is the screenshot of the line in the YAML where I changed the name:
Create a New Pod to View the Pre-Written File
Now let’s create an entirely new pod with that YAML file using the following command:
kubectl create -f pod.yaml
Let’s verify that it’s running by running the following command:
kubectl get pods
Now, let’s connect to the container directly and see if the file
file1.txt is still there. Let’s use the following command to directly connect to the new container:
kubectl exec pod2 -it -- sh
Now, let’s list the contents of the
/data/ directory with this command:
You will now see the file that we created earlier while connected to the first container. It’s still there! Amazing, right?
Congratulations! Want More?
Congratulations on completing this short lesson on managing storage in Kubernetes! (If you haven’t yet, try it for yourself and follow along by starting your free 7-day trial here.)
If you’d like more demonstrations like this and like to learn more about using Kubernetes, check out the Cloud Native Kubernetes Course on Linux Academy! Also, check out our many hands-on labs that are your own pre-built environments, including Google Cloud Platform, that we spin up for you and allow you to practice what you learn! Happy learning!
On the Topic of Kubernetes
If you’re looking to learn more, here are some other helpful, free articles on Kubernetes.
- Deploying Services in Kubernetes
- Kubernetes Cheat Sheet
- Building a Three-Node Kubernetes Cluster | Quick Guide
- Getting Started with Kubernetes Using Minikube