The Volume which would NOT be destroyed along with POD. After re-creating pod, same volume would be re-used (if deployed on same node).
-
Create a new POD template using following code:
apiVersion: v1 kind: Pod metadata: name: volume-pod2 labels: name: volume-pod2 spec: volumes: - name: vol1 ## Directory on Node's Filesystem ## Deleting POD does not affect this directory ## Allows Sharing Volume between PODS in same NODE! hostPath: path: /var/myvol containers: - name: myapp2 image: mahendrshinde/myweb:latest volumeMounts: - name: vol1 mountPath: "/data" readOnly: false resources: limits: memory: "64Mi" cpu: "100m" ports: - containerPort: 80
Click here for complete YAML file.
-
Save the file using name
pod-volume2.yaml
and deploy usingkubectl
$ kubectl apply -f pod-volume2.yaml $ kubectl get po volume-pod2
-
Execute
bash
on running pod (Enter inside pod container to verify volume)$ kubectl exec -it volume-pod2 -- sh $ cd /data $ echo "Testing the filesystem" > file1 $ exit
-
Try deleting and re-creating the pod and then test the
/data
directory to check if volume was deleted along with pod.$ kubectl delete -f volume-pod2.yaml # wait for 1 minutes and then $ kubectl apply -f volume-pod2.yaml # Execute bash on container $ kubectl exec -it volume-pod2 -- sh $ cd /data # Try display content of file1 $ cat file1 # EXPECTED: File should display `Testing the filesystem` # Now quit the bash and delete pod $ exit $ kubectl delete -f volume-pod2.yaml