Table of contents
๐ Congratulations to you for conquering ConfigMaps and Secrets in Kubernetes yesterday.
๐ฅ You're on fire! ๐ฅ
What is Persistent Volume in K8s?
Persistent Volume (PV) is a storage resource that can be dynamically provisioned or manually configured to provide storage to applications running in the cluster. Persistent Volumes decouple storage provision from the application, making it easier to manage and share storage resources in a cluster.
Key-points
It allows you to define and manage storage resources independently of the applications that use them.
It has a life cycle that includes the creation, deletion and management of storage resources.
It provides storage that persists data even if the application or pod using it is terminated or rescheduled.
PVs support different access modes, such as ReadWriteOnce (RWO), ReadOnlyMany (ROX), and ReadWriteMany (RWX), to accommodate various use cases.
TASK 1
Add a Persistent Volume to your Deployment todo app.
Create a Persistent Volume using a file on your node.
Create a Persistent Volume Claim that references the Persistent Volume.
Update your deployment.yml file to include the Persistent Volume Claim. After Applying pv.yml pvc.yml your deployment file.
Apply the updated deployment using the command.
Verify that the Persistent Volume has been added to your Deployment by checking the status of the Pods and Persistent Volumes in your cluster.
Steps:
Create a persistent volume file and add the following content to it
#persistentvolume.yml apiVersion: v1 kind: PersistentVolume metadata: name: todo-app-pv namespace: my-app spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce hostPath: path: /path/to/your/host
Give the path to your host in the hostPath section. And apply it to the Kubernetes Cluster by using the command
kubectl apply -f persistentvolume.yml -n my-app
Claim the persistent volume by creating the persistent volume claim file and adding the content to it
#persistentvolumeclaim.yml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: todo-app-pvc namespace: my-app spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
Apply PVC to your cluster
kubectl apply -f persistentvolumeclaim.yml -n my-app
Update deployment file
#deployment.yml apiVersion: apps/v1 kind: Deployment metadata: name: todo-app namespace: my-app spec: replicas: 1 template: metadata: labels: app: todo-app spec: containers: - name: todo-app-container image: your-image:tag ports: - containerPort: 80 volumeMounts: - name: persistent-volume-claim mountPath: /path/in/container volumes: - name: persistent-volume-claim persistentVolumeClaim: claimName: todo-app-pvc
Add your actual image tag and mountPath.
Apply the updated deployment file to the cluster
kubectl apply -f deployment.yml -n my-app
Verify that the persistent volume was added successfully to the deployment by checking the status of pods and persistent volumes:
#to check the status of pods kubectl get pods -n my-app #to check the status of persistent volume kubectl get pv -n my-app
Make sure that the pods are running and persistent volume is in a bound state.
TASK 2
Accessing data in the Persistent Volume,
Connect to a Pod in your Deployment using the command.
Verify that you can access the data stored in the Persistent Volume from within the Pod
Steps:
Check the status of pods
kubectl get pods -n my-app
It will show you the lists of all the running pods.
Go to your worker node and check the latest container running
#in worker node docker ps
Then go inside the latest container which should be running by using the command
docker exec -it <container-id> bash
It will allow you to access the running docker container.
Inside the container, access your my-SQL database by using the command
#inside the container mysql -u root -p
It will ask you password which you have set in Secrets. Enter that password to access my SQL database.
To check the databases you have set in an earlier task
#in mysql show databases;
It shows the database name you have set in earlier steps.
To verify that you can access the data stored in persistent volume. Go to the path which you have given in the persistent volume and do the following command to check the data.
#show the list of data stored in persistent volume ls
You can also read and change the data as you need.
<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>