Managing Persistent Volumes in Your Deployment ๐ฅ
Table of contents
- Beginner's Guide to Kubernetes Persistent Volumes (PVs)
Beginner's Guide to Kubernetes Persistent Volumes (PVs)
In Kubernetes, Persistent Volumes (PVs) are like virtual hard drives that help pods store data. They make it easy to manage storage for your applications. Let's break it down:
What Are Persistent Volumes (PVs)?
Imagine a Hard Drive: Think of a PV as a virtual hard drive for your applications running in Kubernetes. It's a safe place where you can store data.
Decoupled Storage: PVs allow you to separate storage management from your application pods. This means you can manage your data independently.
How Do PVs Work?
Creating PVs: Cluster administrators create PVs. They set up the storage and make it available for pods to use.
Requesting Storage with PVCs: Pods that need storage ask for it by creating something called a Persistent Volume Claim (PVC). It's like ordering storage.
Matching PVs and PVCs: Kubernetes finds a suitable PV for the PVC based on the requested size and other preferences. This PV is then "attached" to the pod.
Using Storage: The pod can now use the storage from the PV, just like you'd use files on a hard drive.
Simple Examples of PV Usage
Database Storage
Imagine a Database: Say you have a database running in Kubernetes. Databases need a safe place to store data. You create a PV for it.
Claiming Storage: When the database pod starts, it requests storage by creating a PVC. The PVC gets connected to the PV.
Data Safety: Even if the database pod stops or gets replaced, the data is safe on the PV.
Shared File Storage
Think of Shared Files: In some cases, multiple pods need access to the same files, like configuration files or user uploads.
Creating a Shared PV: You can create a PV and attach it to multiple pods using PVCs. Now, all pods can access the same files.
Log Storage
Logs for Apps: Many apps generate logs. Instead of losing those logs when a pod restarts, you can store them safely in a PV.
Claiming Log Storage: Pods claim log storage with PVCs. The logs remain even if pods come and go.
Types of PV Reclaim Policies
PVs have different policies to decide what happens when they are no longer needed. Here are the main ones:
Retain: The PV is not deleted. It's like storing important things in a box that you can open later.
Delete: The PV is automatically deleted when it's not needed anymore, like cleaning up after you finish using something.
Recycle (Deprecated): This used to be an option, but it's not recommended anymore.
Best Practices
Use Retain when you want to keep the PV and its data safe, like important files.
Use Delete when you're done with the PV and want it to be cleaned up automatically.
Remember, PVs are like storage lockers for your apps in Kubernetes. You choose how to use them based on your app's needs.
Certainly! Let's break down the tasks using the provided configurations:
Task 1: Adding a Persistent Volume to Your Deployment (todo app)
Step 1: Create a Persistent Volume (PV)
A Persistent Volume (PV) is like a virtual hard drive. It's where you store data for your applications. In this step, we're creating a PV using the provided configuration.
Here's the PV configuration (pv.yml):
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-todo-app
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/home/srahul/volume"
name: pv-todo-app
is the name of the PV.capacity
specifies the storage size (2GiB in this case).accessModes
indicate that only one node can read and write to this PV at a time.persistentVolumeReclaimPolicy: Retain
means that even if the PV is no longer used, it won't be automatically deleted. The data is retained.hostPath
tells Kubernetes to use storage on the host machine at the specified path.
Apply this PV configuration using the command:
kubectl apply -f pv.yml
# verify the setup
kubectl get pv -n <namespace>
Step 2: Create a Persistent Volume Claim (PVC)
A Persistent Volume Claim (PVC) is like placing an order for storage. Pods use PVCs to get access to PVs.
Here's the PVC configuration (pvc.yaml):
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-todo-app
namespace: mysql
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
name: pvc-todo-app
is the name of the PVC.accessModes
specify that only one node can read and write to this PVC at a time.resources
request 1GiB of storage.
Apply this PVC configuration using the command:
kubectl apply -f pvc.yaml
# verify the setup
kubectl get pvc -n <namespace>
Step 3: Update your Deployment to use the PVC
Now, you need to update your deployment configuration (deployment.yaml) to use the PVC. This means your application pods will have access to the storage.
Here's the updated Deployment configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app-deployment
spec:
replicas: 1
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: srahul0502/todoapp:v1
ports:
- containerPort: 8000
volumeMounts:
- name: todo-app-data
mountPath: /app
volumes:
- name: todo-app-data
persistentVolumeClaim:
claimName: pvc-todo-app
volumeMounts
specify where the storage will be mounted inside the pod (mountPath).volumes
link the PVC (claimName: pvc-todo-app
) to your pod.
Apply the updated Deployment configuration using the command:
kubectl apply -f deployment.yaml
kubectl get pods -n <namespace>
Step 5: Verify the Setup
Check if everything is set up correctly. Run these commands to verify:
kubectl get pods -n <namespace>
kubectl get pv -n <namespace>
kubectl get pods
will show the status of your pods. You should see yourtodo-app
pod running.kubectl get pv
will show the status of your Persistent Volumes. You should seepv-todo-app
in the list.You can use
kubectl describe deployment <deployment-name> -n <namespace>
You've successfully added a Persistent Volume to your Deployment!
Task 2: Accessing Persistent Volume Data Within a Kubernetes Pod and include the additional steps you provided:
Step 1: Connect to a Pod in your Deployment
You can access the pod by running the following command:
kubectl exec -it <pod-name> -- /bin/bash
Replace <pod-name>
with the name of your todo-app
pod.
Step 2: Inside the App Folder, Create a New File (some.txt)
Once you are inside the pod, navigate to the /app
folder, and create a new file named some.txt
. You can create the file using the following commands:
cd /app
touch some.txt
This command creates an empty some.txt
file within the /app
folder.
Step 3: Verify Access to Data Stored in the Persistent Volume
To verify that you can access the data stored in the Persistent Volume (PV) from within the pod, you can add some content to the some.txt
file. For example:
echo "Hello, Persistent Volume!" > some.txt
This command adds the text "Hello, Persistent Volume!" to the some.txt
file.
Step 4: Delete the Pod
Now, you can delete the pod you created earlier. When you delete it, Kubernetes automatically creates a new pod to replace it.
kubectl delete pod <pod-name>
Replace <pod-name>
with the name of the todo-app
pod.
Step 5: Access the New Pod and Check for some.txt
After the new pod is created, you can access it using the following command:
kubectl exec -it <new-pod-name> -- /bin/bash
Replace <new-pod-name>
with the name of the newly created pod.
Once you are inside the new pod, navigate to the /app
folder and check if the some.txt
file is still there:
cd /app
ls
You should see the some.txt
file. You can also check its contents using cat some.txt
.
These steps will confirm that the data you stored in the Persistent Volume is accessible even after deleting and recreating the pod.
I hope you understood the concept of persistent volumes and now you can create one for your own !!
Happy Learning :D