Saltar al contenido principal

Lab 6.1 - Volúmenes Efímeros y emptyDir

Objetivos​

Al final de este laboratorio, usted será capaz de:

  • Crear y utilizar volúmenes emptyDir
  • Utilizar volúmenes tmpfs (RAM)
  • Compartir volúmenes entre contenedores en un Pod
  • Comprender el ciclo de vida de los volúmenes efímeros
  • Comparar emptyDir y tmpfs

Prerrequisitos​

  • Cluster Kubernetes funcional
  • kubectl configurado
  • Comprensión de Pods y volúmenes

Ejercicio 1: Volumen emptyDir Básico​

Crear un Pod con emptyDir​

Cree pod-emptydir.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "echo 'Hello' > /cache/data.txt && cat /cache/data.txt && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir: {}

Desplegar y Verificar​

kubectl apply -f pod-emptydir.yaml
kubectl logs emptydir-pod
kubectl exec emptydir-pod -- ls -la /cache

Ejercicio 2: emptyDir con Límite de Tamaño​

Crear un Pod con Límite de Tamaño​

Cree pod-emptydir-limit.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-limit-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "dd if=/dev/zero of=/cache/bigfile bs=1M count=100 && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 50Mi

Desplegar y Observar​

kubectl apply -f pod-emptydir-limit.yaml
kubectl get pod emptydir-limit-pod
# El Pod debería fallar porque 100Mi > 50Mi

Ejercicio 3: Volumen Compartido entre Contenedores​

Crear un Pod Multi-Contenedor​

Cree pod-shared-volume.yaml:

apiVersion: v1
kind: Pod
metadata:
name: shared-volume-pod
spec:
containers:
- name: writer
image: busybox:1.35
command: ["sh", "-c", "echo 'Data from writer' > /shared/data.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /shared
- name: reader
image: busybox:1.35
command: ["sh", "-c", "sleep 5 && cat /shared/data.txt && sleep 3600"]
volumeMounts:
- name: shared
mountPath: /shared
volumes:
- name: shared
emptyDir: {}

Desplegar y Verificar​

kubectl apply -f pod-shared-volume.yaml
kubectl logs shared-volume-pod -c writer
kubectl logs shared-volume-pod -c reader

Ejercicio 4: Volumen tmpfs (RAM)​

Crear un Pod con tmpfs​

Cree pod-tmpfs.yaml:

apiVersion: v1
kind: Pod
metadata:
name: tmpfs-pod
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "echo 'Fast storage' > /tmp/data.txt && cat /tmp/data.txt && sleep 3600"]
volumeMounts:
- name: tmpfs-volume
mountPath: /tmp
volumes:
- name: tmpfs-volume
emptyDir:
medium: Memory
sizeLimit: 100Mi

Desplegar y Verificar​

kubectl apply -f pod-tmpfs.yaml
kubectl logs tmpfs-pod
kubectl exec tmpfs-pod -- df -h /tmp

Ejercicio 5: Comparación emptyDir vs tmpfs​

Crear Dos Pods para Comparación​

Cree pod-comparison.yaml:

apiVersion: v1
kind: Pod
metadata:
name: emptydir-bench
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "time dd if=/dev/zero of=/cache/test bs=1M count=10 && sleep 3600"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir: {}
---
apiVersion: v1
kind: Pod
metadata:
name: tmpfs-bench
spec:
containers:
- name: app
image: busybox:1.35
command: ["sh", "-c", "time dd if=/dev/zero of=/tmp/test bs=1M count=10 && sleep 3600"]
volumeMounts:
- name: tmpfs-vol
mountPath: /tmp
volumes:
- name: tmpfs-vol
emptyDir:
medium: Memory

Comparar el Rendimiento​

kubectl apply -f pod-comparison.yaml
kubectl logs emptydir-bench
kubectl logs tmpfs-bench
# Comparar los tiempos de escritura

Ejercicio 6: Caso de Uso Real - Caché​

Crear un Pod con Caché​

Cree pod-cache.yaml:

apiVersion: v1
kind: Pod
metadata:
name: cache-pod
spec:
containers:
- name: web-server
image: nginx:1.20
volumeMounts:
- name: cache
mountPath: /var/cache/nginx
- name: cache-warmup
image: busybox:1.35
command: ["sh", "-c", "while true; do echo 'Cache data' > /cache/data.txt; sleep 60; done"]
volumeMounts:
- name: cache
mountPath: /cache
volumes:
- name: cache
emptyDir:
sizeLimit: 500Mi

Desplegar y Verificar​

kubectl apply -f pod-cache.yaml
kubectl exec cache-pod -c web-server -- ls -la /var/cache/nginx
kubectl exec cache-pod -c cache-warmup -- cat /cache/data.txt

Limpieza​

kubectl delete pod emptydir-pod emptydir-limit-pod shared-volume-pod tmpfs-pod emptydir-bench tmpfs-bench cache-pod

Preguntas de Reflexión​

  1. ¿Cuál es la diferencia principal entre emptyDir y tmpfs?
  2. ¿Cuándo utilizaría tmpfs en lugar de emptyDir?
  3. ¿Por qué definir un sizeLimit para los volúmenes emptyDir?
  4. ¿Qué sucede con los datos en un volumen emptyDir cuando el Pod es eliminado?

Desafío Extra​

Cree un Pod con tres contenedores que comparten un volumen emptyDir:

  • Un contenedor que escribe datos
  • Un contenedor que lee y procesa los datos
  • Un contenedor que monitorea el tamaño del volumen

Laboratorio creado el: Diciembre 2024