Gentle Deep Dive Series Kubernetes: Persistent Volume
Other posts:
1. Fundamentals
2. Kubernetes Objectes
3. Ingress Controller
4. Persistent Volume
5. Github
Managing data in Kubernetes is not that straightforward process. In Kubernetes world pods are not persistent, they die and revive. So if you put some kind of DB into the pod, when the pod is dead data will be gone forever, because newly revived pod will be new from scratch. To overcome this problem kubernetes gives us Persistent Volume and Persistent Volume Claim objects to works with. According to kubernetes documentation ‘A PersistentVolume
(PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes’ and ‘A PersistentVolumeClaim
(PVC) is a request for storage by a user.’ So basically PV is storage resource in our cluster and PVC is actually request to use this storage. For more information check kubernetes documentation:https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Let’s create Postgress DB within our cluster which will persist data. The first thing that we need to do is to create Persistent Volume:
storageClassName: PV can have classes. If PV has some classname PVC need to have the same classname too so that it can actually request that storage.
storage: 1Gi means we are requesting 1GB storage in the cluster.
accessModes: There are different types of access in kubernetes.According to kubernetes documentation:
ReadWriteOnce — the volume can be mounted as read-write by a single node
ReadOnlyMany — the volume can be mounted read-only by many nodes
ReadWriteMany — the volume can be mounted as read-write by many nodes
Before running the apply command we need to create mnt/data in the cluster. So run minikube ssh
to get into minikube terminal and run sudo mkdir /mnt/data
to create that folder.
Now run kubectl apply -f pv.yml
to create PV and kubectl get pv
to get a list of created PV. Next step is to create PVC
As you can see PVC uses the same storageClassName to request 1GB storage from PV. Run apply command to create PVC. Next step would be to create secret for our PostGress DB.
Note that actual secret values are base64 encoded. If you run echo -n ‘admin’ | base64
In the terminal, it will give u base 64 encoded version of admin use this value as your secret.Run apply command to create secret.Next we need to create deployment for our Postgress (I will use docker hub image):
As you can I see I am using secret values from postgress-credentials secret which I created before and claimName is set as postgress-pvc which again was created in the step 2. Don’t forget to put that subPath value or else Postgress won’t start. Last step is actually to create Posgress Service:
Run apply the file to create service. Now run minikube dashboard
command click on the pod which runs postgress. On the top left, you can see the Exec button which allows u to execute commands inside of the pod. If everything is ok there should be postgress with DB name kubernetes_postgress running on the pod. Run psql -d kubernetes_postgress -U admin -W
inside of the pod. It will ask for a password which was set as admin. After typing the password you will be in postgress. In previous posts, we already talked about how to connect to different pods, so I won’t talk about that part.
Gentle Deep Dive Series Kubernetes: Persistent Volume
Managing data in Kubernetes is not that straightforward process. In Kubernetes world pods are not persistent, they die and revive. So if you put some kind of DB into the pod, when the pod is dead data will be gone forever, because newly revived pod will be new from scratch. To overcome this problem kubernetes gives us Persistent Volume and Persistent Volume Claim objects to works with. According to kubernetes documentation ‘A PersistentVolume
(PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes’ and ‘A PersistentVolumeClaim
(PVC) is a request for storage by a user.’ So basically PV is storage resource in our cluster and PVC is actually request to use this storage. For more information check kubernetes documentation:https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Let’s create Postgress DB within our cluster which will persist data. The first thing that we need to do is to create Persistent Volume:
storageClassName: PV can have classes. If PV has some classname PVC need to have the same classname too so that it can actually request that storage.
storage: 1Gi means we are requesting 1GB storage in the cluster.
accessModes: There are different types of access in kubernetes.According to kubernetes documentation:
ReadWriteOnce — the volume can be mounted as read-write by a single node
ReadOnlyMany — the volume can be mounted read-only by many nodes
ReadWriteMany — the volume can be mounted as read-write by many nodes
Before running the apply command we need to create mnt/data in the cluster. So run minikube ssh
to get into minikube terminal and run sudo mkdir /mnt/data
to create that folder.
Now run kubectl apply -f pv.yml
to create PV and kubectl get pv
to get a list of created PV. Next step is to create PVC
As you can see PVC uses the same storageClassName to request 1GB storage from PV. Run apply command to create PVC. Next step would be to create secret for our PostGress DB.
Note that actual secret values are base64 encoded. If you run echo -n ‘admin’ | base64
In the terminal, it will give u base 64 encoded version of admin use this value as your secret.Run apply command to create secret.Next we need to create deployment for our Postgress (I will use docker hub image):
As you can I see I am using secret values from postgress-credentials secret which I created before and claimName is set as postgress-pvc which again was created in the step 2. Don’t forget to put that subPath value or else Postgress won’t start. Last step is actually to create Posgress Service:
Run apply the file to create service. Now run minikube dashboard
command click on the pod which runs postgress. On the top left, you can see the Exec button which allows u to execute commands inside of the pod. If everything is ok there should be postgress with DB name kubernetes_postgress running on the pod. Run psql -d kubernetes_postgress -U admin -W
inside of the pod. It will ask for a password which was set as admin. After typing the password you will be in postgress. In previous posts, we already talked about how to connect to different pods, so I won’t talk about that part.
Gentle Deep Dive Series Kubernetes: Persistent Volume
Managing data in Kubernetes is not that straightforward process. In Kubernetes world pods are not persistent, they die and revive. So if you put some kind of DB into the pod, when the pod is dead data will be gone forever, because newly revived pod will be new from scratch. To overcome this problem kubernetes gives us Persistent Volume and Persistent Volume Claim objects to works with. According to kubernetes documentation ‘A PersistentVolume
(PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes’ and ‘A PersistentVolumeClaim
(PVC) is a request for storage by a user.’ So basically PV is storage resource in our cluster and PVC is actually request to use this storage. For more information check kubernetes documentation:https://kubernetes.io/docs/concepts/storage/persistent-volumes/
Let’s create Postgress DB within our cluster which will persist data. The first thing that we need to do is to create Persistent Volume:
storageClassName: PV can have classes. If PV has some classname PVC need to have the same classname too so that it can actually request that storage.
storage: 1Gi means we are requesting 1GB storage in the cluster.
accessModes: There are different types of access in kubernetes.According to kubernetes documentation:
ReadWriteOnce — the volume can be mounted as read-write by a single node
ReadOnlyMany — the volume can be mounted read-only by many nodes
ReadWriteMany — the volume can be mounted as read-write by many nodes
Before running the apply command we need to create mnt/data in the cluster. So run minikube ssh
to get into minikube terminal and run sudo mkdir /mnt/data
to create that folder.
Now run kubectl apply -f pv.yml
to create PV and kubectl get pv
to get a list of created PV. Next step is to create PVC
As you can see PVC uses the same storageClassName to request 1GB storage from PV. Run apply command to create PVC. Next step would be to create secret for our PostGress DB.
Note that actual secret values are base64 encoded. If you run echo -n ‘admin’ | base64
In the terminal, it will give u base 64 encoded version of admin use this value as your secret.Run apply command to create secret.Next we need to create deployment for our Postgress (I will use docker hub image):
As you can I see I am using secret values from postgress-credentials secret which I created before and claimName is set as postgress-pvc which again was created in the step 2. Don’t forget to put that subPath value or else Postgress won’t start. Last step is actually to create Posgress Service:
Run apply the file to create service. Now run minikube dashboard
command click on the pod which runs postgress. On the top left, you can see the Exec button which allows u to execute commands inside of the pod. If everything is ok there should be postgress with DB name kubernetes_postgress running on the pod. Run psql -d kubernetes_postgress -U admin -W
inside of the pod. It will ask for a password which was set as admin. After typing the password you will be in postgress. In previous posts, we already talked about how to connect to different pods, so I won’t talk about that part.