Skip to main content

Pregunta 6 - Storage, PV, PVC y 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.


Vamos a crear nuestro persistent volume, toma el ejemplo de la documentación

# /opt/course/6/pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: safari-pv
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce # Curiosidad: El hostpath solo utiliza ReadWriteOnce
hostPath: # Familiarízate con este
path: "/Volumes/Data"
# estando en la carpeta del archivo
kubectl apply -f pv.yaml

Ahora vamos a crear un pvc en el namespace especificado. Toma el ejemplo de la documentación.

# /opt/course/6/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: safari-pvc
namespace: project-tiger
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
# estando en la carpeta del archivo
kubectl apply -f pvc.yaml

# Y ya podemos verificar si hizo el 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 ...

# Y vamos a crear el deploy que fue pedido. Vamos a crear la plantilla y editarla después
k create deploy safari -n project-tiger --image=httpd:2.4.41-alpine --dry-run=client -o yaml > /opt/course/6/deploy.yaml

Vamos a editar el archivo.

# /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: # agregar
- name: vol # agregar
mountPath: /tmp/safari-data # agregar
volumes: # agregar
- name: vol # agregar
persistentVolumeClaim: # agregar
claimName: safari-pvc # agregar

Vamos a aplicar y verificar.

# estando en la carpeta del archivo
kubectl apply -f deploy.yaml
# Podemos hacer un describe en el pvc o en el pod para verificar si fue montado.