Persistent Storage in Minikube

Persistent Storage in Minikube

In this lesson, we will explore how to manage persistent storage in Minikube. Persistent storage is crucial for applications that need to retain data across Pod restarts or between different versions of your application. You’ll learn how to create Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) in Minikube, and how to use them in your Kubernetes applications. By the end of this lesson, you’ll understand how to implement and manage persistent storage in your Minikube environment.

1. Understanding Persistent Storage in Kubernetes

Kubernetes uses two main abstractions to manage storage:

  • Persistent Volume (PV): A piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVs are independent of the Pod lifecycle and can exist beyond the life of a Pod.

  • Persistent Volume Claim (PVC): A request for storage by a user. PVCs specify the size, access modes, and other requirements for the storage they request. Kubernetes binds a PVC to a suitable PV that meets the criteria.

2. Creating Persistent Volumes in Minikube

Minikube provides local storage by default, which can be used to create PVs. These PVs can then be used by your applications to persist data.

Step 1: Define a Persistent Volume

Create a YAML file named pv.yaml with the following content:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: minikube-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
  • Explanation:

    • capacity.storage: Specifies the amount of storage available (10Gi in this case).

    • accessModes: Defines how the volume can be accessed. ReadWriteOnce means the volume can be mounted as read-write by a single node.

    • hostPath.path: Specifies the path on the Minikube node where the data will be stored.

Step 2: Apply the Persistent Volume

Use the following command to create the Persistent Volume:

kubectl apply -f pv.yaml
  • Explanation:

    • kubectl apply -f: Applies the configuration from the specified YAML file.

Step 3: Verify the Persistent Volume

Check that the Persistent Volume has been created:

kubectl get pv
  • Expected Output:

    • You should see the minikube-pv listed with a status of Available, indicating that it’s ready to be used by a PVC.

3. Creating Persistent Volume Claims

Once you have a Persistent Volume, you need to create a Persistent Volume Claim to request storage for your application.

Step 1: Define a Persistent Volume Claim

Create a YAML file named pvc.yaml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: minikube-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  • Explanation:

    • accessModes: Requests the same access mode as defined in the PV (ReadWriteOnce).

    • resources.requests.storage: Specifies the amount of storage requested (5Gi in this case).

Step 2: Apply the Persistent Volume Claim

Use the following command to create the PVC:

kubectl apply -f pvc.yaml
  • Explanation:

    • This command creates the PVC based on the configuration in the YAML file.

Step 3: Verify the Persistent Volume Claim

Check that the PVC has been created and bound to the PV:

kubectl get pvc
  • Expected Output:

    • You should see minikube-pvc with a status of Bound, indicating that it has been successfully matched to a PV.

4. Using Persistent Storage in a Pod

Now that you have a PV and PVC, you can use them in your Pods to persist data.

Step 1: Define a Pod that Uses the PVC

Create a YAML file named pod-with-pvc.yaml with the following content:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: busybox
    image: busybox
    command: [ "sleep", "3600" ]
    volumeMounts:
    - mountPath: "/mnt/storage"
      name: storage
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: minikube-pvc
  • Explanation:

    • volumeMounts.mountPath: Specifies where the volume will be mounted inside the container (/mnt/storage).

    • volumes.persistentVolumeClaim.claimName: References the PVC that the Pod will use.

Step 2: Apply the Pod Configuration

Create the Pod using the following command:

kubectl apply -f pod-with-pvc.yaml
  • Explanation:

    • This command creates a Pod that uses the PVC to mount the Persistent Volume.

Step 3: Verify the Pod and Storage

Check that the Pod is running:

kubectl get pods
  • Expected Output:

    • You should see test-pod listed with a status of Running.

Step 4: Access the Persistent Storage

You can access the persistent storage inside the Pod by using kubectl exec:

kubectl exec -it test-pod -- sh
  • Navigate to the mounted directory:

    cd /mnt/storage
  • You can now create files or directories in this location, and they will persist across Pod restarts because they are stored in the Persistent Volume.

5. Cleaning Up Resources

Once you’re done experimenting, it’s important to clean up the resources you created to free up system resources.

Step 1: Delete the Pod

kubectl delete pod test-pod

Step 2: Delete the Persistent Volume Claim

kubectl delete pvc minikube-pvc

Step 3: Delete the Persistent Volume

kubectl delete pv minikube-pv

Conclusion

In this lesson, you learned how to configure and manage persistent storage in Minikube using Persistent Volumes and Persistent Volume Claims. You now understand how to create storage that persists across Pod restarts and how to integrate this storage with your Kubernetes applications. This is a crucial skill for running stateful applications in Kubernetes. In the next lesson, we’ll explore advanced Minikube features and how to use Minikube for local development workflows.

Last updated