Skip to main content

Questão 6 - Storage, PV, PVC e Pod Volume

Question 6 | Storage, PV, PVC, Pod volume

Use context: kubectl config use-context k8s-c1-H

Create a new PersistentVolume named safari-pv. It should have a capacity of 2Gi, accessMode ReadWriteOnce, hostPath /Volumes/Data and no storageClassName defined.

Next create a new PersistentVolumeClaim in Namespace project-tiger named safari-pvc . It should request 2Gi storage, accessMode ReadWriteOnce and should not define a storageClassName. The PVC should bound to the PV correctly.

Finally create a new Deployment safari in Namespace project-tiger which mounts that volume at /tmp/safari-data. The Pods of that Deployment should be of image httpd:2.4.41-alpine.


Let's create our persistent volume, get the example from the documentation

# /opt/course/6/pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: safari-pv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce # Fun fact: hostPath only uses ReadWriteOnce
hostPath: # Be familiar with this
path: "/Volumes/Data"
# being in the file directory
kubectl apply -f pv.yaml

Now let's create a pvc in the specified namespace. Get the example from the documentation.

# /opt/course/6/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: safari-pvc
namespace: project-tiger
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
# being in the file directory
kubectl apply -f pvc.yaml

# And we can already check if it did the binding
k get pv,pvc -n project-tiger
NAME CAPACITY ... STATUS CLAIM ...
persistentvolume/safari-pv 2Gi ... Bound project-tiger/safari-pvc ...

NAME STATUS VOLUME CAPACITY ...
persistentvolumeclaim/safari-pvc Bound safari-pv 2Gi ...

# And let's create the deployment that was requested. Let's create the template and edit it later
k create deploy safari -n project-tiger --image=httpd:2.4.41-alpine --dry-run=client -o yaml > /opt/course/6/deploy.yaml

Let's edit the file.

# /opt/course/6/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: safari
name: safari
namespace: project-tiger
spec:
replicas: 1
selector:
matchLabels:
app: safari
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: safari
spec:
containers:
- image: httpd:2.4.41-alpine
name: container
volumeMounts: # add
- name: vol # add
mountPath: /tmp/safari-data # add
volumes: # add
- name: vol # add
persistentVolumeClaim: # add
claimName: safari-pvc # add

Let's apply and check.

# being in the file directory
kubectl apply -f deploy.yaml
# We can do a describe on the pvc or pod to check if it was mounted.