Static Persistent Volumes
Course: Kubernetes Part 2 - chapter 22
Creating a NFS
Section titled “Creating a NFS”NFS is one famous implementation you can use to deploy your own persistent volume
I’m running one on my aws server -
version: '3.7'
services: nfs-server: image: itsthenetwork/nfs-server-alpine:latest container_name: nfs-server privileged: true environment: SHARED_DIRECTORY: /exports volumes: - ./data:/exports:rw ports: - "2049:2049" restart: unless-stopped💡 Make sure the 2049 port on your machine is open
Creating a pv and pvc
Section titled “Creating a pv and pvc”Create a persistent volume claim and persistent volume
apiVersion: v1kind: PersistentVolumemetadata: name: nfs-pvspec: capacity: storage: 10Gi accessModes: - ReadWriteMany storageClassName: nfs nfs: path: /exports server: 52.66.197.168---apiVersion: v1kind: PersistentVolumeClaimmetadata: name: nfs-pvcspec: accessModes: - ReadWriteMany resources: requests: storage: 10Gi storageClassName: nfsCreate a pod
Section titled “Create a pod”apiVersion: v1kind: Podmetadata: name: mongo-podspec: containers: - name: mongo image: mongo:4.4 command: ["mongod", "--bind_ip_all"] ports: - containerPort: 27017 volumeMounts: - mountPath: "/data/db" name: nfs-volume volumes: - name: nfs-volume persistentVolumeClaim: claimName: nfs-pvcTry it out
Section titled “Try it out”- Put some data in mongodb
kubectl exec -it mongo-pod -- mongouse mydbdb.mycollection.insert({ name: "Test", value: "This is a test" })exit- Delete and restart the pod
kubectl delete pod mongo-podkubectl apply -f mongo.yml- Check if the data persists
kubectl exec -it mongo-pod -- mongouse mydbdb.mycollection.find()