emptyDir
Volume for Data SharingDurée estimée : 15 minutes
Objective: Create a Pod with two containers that share data using an emptyDir
volume.
Context: You need one container to generate data and another container in the same Pod to read and process that data.
Instructions:
data-sharing-pod
in the default
namespace.
emptyDir
volume named cache-volume
.data-generator
busybox:1.36
sh -c "echo \"Hello from data-generator $(date)\" > /cache/data.txt; sleep 10; echo \"Updated data from data-generator $(date)\" > /cache/data.txt; sleep 3600"
cache-volume
at /cache
.data-consumer
busybox:1.36
sh -c "while true; do cat /data/data.txt; sleep 5; done"
cache-volume
at /data
./opt/cka_exercises/stockage/Ex1/pod-data-sharing.yaml
and apply it.data-consumer
container. The logs should display the updated content written by the data-generator
container. Store the log output in /opt/cka_exercises/stockage/Ex1/consumer_logs.txt
.1. Création du Pod data-sharing-pod
:
Fichier /opt/cka_exercises/stockage/Ex1/pod-data-sharing.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: data-sharing-pod
namespace: default
spec:
volumes:
- name: cache-volume
emptyDir: {}
containers:
- name: data-generator
image: busybox:1.36
command: ["sh", "-c", "echo \"Hello from data-generator $(date)\" > /cache/data.txt; sleep 10; echo \"Updated data from data-generator $(date)\" > /cache/data.txt; sleep 3600"]
volumeMounts:
- name: cache-volume
mountPath: /cache
- name: data-consumer
image: busybox:1.36
command: ["sh", "-c", "while true; do cat /data/data.txt; sleep 5; done"]
volumeMounts:
- name: cache-volume
mountPath: /data
Appliquez-le :
mkdir -p /opt/cka_exercises/stockage/Ex1
kubectl apply -f /opt/cka_exercises/stockage/Ex1/pod-data-sharing.yaml
2. Vérification des logs du container data-consumer
:
Attendez que le Pod soit en cours d'exécution (Running
) et que le data-generator
ait eu le temps d'écrire et de mettre à jour le fichier (environ 20-30 secondes) :
# kubectl get pod data-sharing-pod -w
kubectl logs data-sharing-pod -c data-consumer > /opt/cka_exercises/stockage/Ex1/consumer_logs.txt
# Le contenu du fichier consumer_logs.txt devrait afficher la ligne mise à jour par data-generator,
# par exemple : "Updated data from data-generator [date et heure]"
(Note : La commande sleep 15
dans data-consumer
est là pour s'assurer qu'il lit après la mise à jour potentielle par data-generator
)
Durée estimée : 25 minutes
Objective: Manually create a PersistentVolume (PV) using hostPath
for testing purposes, then create a PersistentVolumeClaim (PVC) that binds to this PV, and finally deploy a Pod that uses the PVC.
Context: You are in a development environment where dynamic provisioning is not set up, and you need to provide persistent storage to a Pod using a specific directory on a node.
Instructions:
/mnt/cka-storage/pv-data1
on the node where your Pod will be scheduled.# On the Kubernetes node (e.g., minikube ssh then sudo su -)
# mkdir -p /mnt/cka-storage/pv-data1
# echo "Initial data in PV" > /mnt/cka-storage/pv-data1/initial.txt
manual-hostpath-pv
.
storageClassName
: manual-local
(this is a custom name for binding).capacity
: 1Gi
.accessModes
: ReadWriteOnce
.hostPath
: /mnt/cka-storage/pv-data1
.persistentVolumeReclaimPolicy
: Retain
./opt/cka_exercises/stockage/Ex2/pv-manual-hostpath.yaml
and apply it.my-app-pvc
in the default
namespace.
storageClassName
: manual-local
.accessModes
: ReadWriteOnce
.resources.requests.storage
: 500Mi
(should be less than or equal to PV capacity)./opt/cka_exercises/stockage/Ex2/pvc-my-app.yaml
and apply it.my-app-pvc
is Bound
to manual-hostpath-pv
. Write the command and its expected relevant output (status Bound) to /opt/cka_exercises/stockage/Ex2/pvc_status.txt
.app-with-persistent-storage
in the default
namespace.
nginx:1.27
.my-app-pvc
at /usr/share/nginx/html
./opt/cka_exercises/stockage/Ex2/pod-app-persistent.yaml
and apply it.initial.txt
file (if you created it in step 1). You can do this by exec'ing into the pod and checking the content or by other means if a Service is exposed (not required for this exercise).# kubectl exec app-with-persistent-storage -- cat /usr/share/nginx/html/initial.txt
Write this verification command to /opt/cka_exercises/stockage/Ex2/pod_verification.txt
.1. Prérequis sur le Nœud :
Cette étape doit être effectuée sur le nœud Kubernetes qui hébergera le Pod.
# Sur le nœud :
mkdir -p /mnt/cka-storage/pv-data1
echo "Initial data in PV for Ex2" > /mnt/cka-storage/pv-data1/initial.txt
(Si vous ne pouvez pas effectuer cette étape, le PV hostPath
ne fonctionnera pas comme prévu, mais vous pouvez toujours créer les objets k8s.)
2. Création du PersistentVolume manual-hostpath-pv
:
Fichier /opt/cka_exercises/stockage/Ex2/pv-manual-hostpath.yaml
:
apiVersion: v1
kind: PersistentVolume
metadata:
name: manual-hostpath-pv
spec:
storageClassName: manual-local
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/mnt/cka-storage/pv-data1"
Appliquez-le :
mkdir -p /opt/cka_exercises/stockage/Ex2
kubectl apply -f /opt/cka_exercises/stockage/Ex2/pv-manual-hostpath.yaml
3. Création du PersistentVolumeClaim my-app-pvc
:
Fichier /opt/cka_exercises/stockage/Ex2/pvc-my-app.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-app-pvc
namespace: default
spec:
storageClassName: manual-local
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
Appliquez-le :
kubectl apply -f /opt/cka_exercises/stockage/Ex2/pvc-my-app.yaml
4. Vérification du statut du PVC :
Attendez quelques instants pour que la liaison s'effectue.
kubectl get pvc my-app-pvc -n default > /opt/cka_exercises/stockage/Ex2/pvc_status.txt
# Le fichier pvc_status.txt devrait contenir une ligne similaire à :
# NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
# my-app-pvc Bound manual-hostpath-pv 1Gi RWO manual-local Xs
5. Création du Pod app-with-persistent-storage
:
Fichier /opt/cka_exercises/stockage/Ex2/pod-app-persistent.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: app-with-persistent-storage
namespace: default
spec:
containers:
- name: nginx-container
image: nginx:1.27
ports:
- containerPort: 80
volumeMounts:
- name: web-content
mountPath: /usr/share/nginx/html
volumes:
- name: web-content
persistentVolumeClaim:
claimName: my-app-pvc
Appliquez-le :
kubectl apply -f /opt/cka_exercises/stockage/Ex2/pod-app-persistent.yaml
6. Vérification du contenu du Pod :
Sauvegardez la commande de vérification :
echo "kubectl exec app-with-persistent-storage -- cat /usr/share/nginx/html/initial.txt" > /opt/cka_exercises/stockage/Ex2/pod_verification.txt
Exécutez la commande (après que le Pod soit Running
) :
kubectl get pod app-with-persistent-storage -w
kubectl exec app-with-persistent-storage -- cat /usr/share/nginx/html/initial.txt
# La sortie devrait être : Initial data in PV for Ex2
no-provisioner
StorageClass, Pod Deployment, and PVC Expansion AttemptDurée estimée : 35 minutes
Objective: Define a StorageClass using kubernetes.io/no-provisioner
, create a PersistentVolumeClaim (PVC), then manually create a PersistentVolume (PV) that binds to the PVC. Deploy a Pod that uses this PVC, and finally attempt to expand the PVC.
Context: You are in an environment without a dynamic storage provisioner, or you want to explicitly manage PersistentVolume (PV) creation. This exercise demonstrates how a PVC waits for a suitable PV when its StorageClass uses kubernetes.io/no-provisioner
, how to satisfy this PVC with a manual PV, and then observes the behavior of volume expansion.
Note: We will use a hostPath
PV for simplicity. Volume expansion success for hostPath
PVs depends on the underlying system and Kubernetes version and may not always result in an actual filesystem resize, but the PVC's requested size will change.
Instructions:
/mnt/cka-storage/pv-data-ex3
on the node.# On the Kubernetes node (e.g., minikube/killercoda ssh then sudo su -)
fast-storage
.
provisioner
: kubernetes.io/no-provisioner
.reclaimPolicy
: Delete
.allowVolumeExpansion
: true
.volumeBindingMode
: WaitForFirstConsumer
./opt/cka_exercises/stockage/Ex3/sc-fast-storage.yaml
and apply it.app-data-pvc
in the default
namespace.
100Mi
of storage from the fast-storage
StorageClass.accessModes
: ReadWriteOnce
./opt/cka_exercises/stockage/Ex3/pvc-app-data.yaml
and apply it.app-data-pvc
is initially in Pending
state. Store the command and output in /opt/cka_exercises/stockage/Ex3/pvc_pending_status.txt
.manual-pv-ex3
.
storageClassName
: fast-storage
.capacity
: 150Mi
(slightly larger than PVC request).accessModes
: ReadWriteOnce
.hostPath
: /mnt/cka-storage/pv-data-ex3
.persistentVolumeReclaimPolicy
: Retain
./opt/cka_exercises/stockage/Ex3/pv-manual-ex3.yaml
and apply it.data-intensive-app
that uses app-data-pvc
.
postgres:16
.POSTGRES_USER=myuser
, POSTGRES_PASSWORD=mypass
, POSTGRES_DB=mydb
.app-data-pvc
at /var/lib/postgresql/data
./opt/cka_exercises/stockage/Ex3/pod-data-intensive.yaml
and apply it.data-intensive-app
is Running
.app-data-pvc
current capacity (it should reflect the PV's capacity). Store command and output in /opt/cka_exercises/stockage/Ex3/pvc_initial_capacity.txt
.app-data-pvc
to 200Mi
.
/opt/cka_exercises/stockage/Ex3/pvc-app-data.yaml
to change storage: 100Mi
to storage: 200Mi
./opt/cka_exercises/stockage/Ex3/pvc-app-data-expanded.yaml
and apply it./opt/cka_exercises/stockage/Ex3/pvc_expanded_status.txt
.200Mi
. The actual PV capacity of 150Mi
for hostPath
won't change. Events might indicate FileSystemResizePending
or similar if the CSI sidecar is present, but the underlying hostPath
doesn't expand automatically.)1. Prérequis sur le Nœud :
# Sur le nœud :
mkdir -p /mnt/cka-storage/pv-data-ex3
2. Création de la StorageClass fast-storage
:
Fichier /opt/cka_exercises/stockage/Ex3/sc-fast-storage.yaml
:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast-storage
provisioner: kubernetes.io/no-provisioner
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
Appliquez-le :
mkdir -p /opt/cka_exercises/stockage/Ex3
kubectl apply -f /opt/cka_exercises/stockage/Ex3/sc-fast-storage.yaml
3. Création du PVC app-data-pvc
:
Fichier /opt/cka_exercises/stockage/Ex3/pvc-app-data.yaml
:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data-pvc
namespace: default
spec:
storageClassName: fast-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
Appliquez-le :
kubectl apply -f /opt/cka_exercises/stockage/Ex3/pvc-app-data.yaml
4. Vérification du statut Pending
du PVC :
kubectl get pvc app-data-pvc -n default > /opt/cka_exercises/stockage/Ex3/pvc_pending_status.txt
kubectl describe pvc app-data-pvc -n default >> /opt/cka_exercises/stockage/Ex3/pvc_pending_status.txt
# Le fichier pvc_pending_status.txt devrait montrer STATUS Pending.
# Les événements indiqueront "waiting for first consumer to be created before binding" ou similaire, ET ensuite, si aucun PV ne correspond une fois le Pod créé, des messages sur l'absence de PV.
5. Création du PersistentVolume manual-pv-ex3
:
Fichier /opt/cka_exercises/stockage/Ex3/pv-manual-ex3.yaml
:
apiVersion: v1
kind: PersistentVolume
metadata:
name: manual-pv-ex3
spec:
storageClassName: fast-storage
capacity:
storage: 150Mi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
hostPath:
path: "/mnt/cka-storage/pv-data-ex3"
Appliquez-le :
kubectl apply -f /opt/cka_exercises/stockage/Ex3/pv-manual-ex3.yaml
6. Déploiement du Pod data-intensive-app
:
Fichier /opt/cka_exercises/stockage/Ex3/pod-data-intensive.yaml
:
apiVersion: v1
kind: Pod
metadata:
name: data-intensive-app
namespace: default
spec:
containers:
- name: postgres-db
image: postgres:16
env:
- name: POSTGRES_USER
value: "myuser"
- name: POSTGRES_PASSWORD
value: "mypass"
- name: POSTGRES_DB
value: "mydb"
ports:
- containerPort: 5432
volumeMounts:
- name: db-data
mountPath: /var/lib/postgresql/data
volumes:
- name: db-data
persistentVolumeClaim:
claimName: app-data-pvc
Appliquez-le :
kubectl apply -f /opt/cka_exercises/stockage/Ex3/pod-data-intensive.yaml
7. Vérification du Pod et accès aux données :
# Attendre que le Pod soit Running
kubectl get pod data-intensive-app -w > /opt/cka_exercises/stockage/Ex3/pod_running_verification.txt
# Une fois Running (CTRL+C pour arrêter le watch), vérifier le statut :
kubectl get pod data-intensive-app >> /opt/cka_exercises/stockage/Ex3/pod_running_verification.txt
8. Vérification de la capacité initiale allouée au PVC :
kubectl get pvc app-data-pvc -o=jsonpath='{.spec.resources.requests.storage} (requested), Status Capacity: {.status.capacity.storage}' > /opt/cka_exercises/stockage/Ex3/pvc_initial_capacity.txt
# Le fichier pvc_initial_capacity.txt devrait contenir :
# 100Mi (requested), Status Capacity: 150Mi (ou la capacité réelle du PV)
9. Modification et application du PVC pour tentative d'expansion :
Fichier /opt/cka_exercises/stockage/Ex3/pvc-app-data-expanded.yaml
(basé sur pvc-app-data.yaml
modifié) :
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data-pvc
namespace: default
spec:
storageClassName: fast-storage
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 200Mi # Nouvelle taille demandée
Appliquez la modification :
kubectl apply -f /opt/cka_exercises/stockage/Ex3/pvc-app-data-expanded.yaml
10. Vérification du statut du PVC après la tentative d'expansion :
kubectl get pvc app-data-pvc -n default > /opt/cka_exercises/stockage/Ex3/pvc_expanded_status.txt
kubectl describe pvc app-data-pvc -n default >> /opt/cka_exercises/stockage/Ex3/pvc_expanded_status.txt
# Le fichier pvc_expanded_status.txt montrera que le PVC est toujours Bound.
# La sortie de 'get pvc' :
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
app-data-pvc Bound manual-pv-ex3 150Mi RWO fast-storage Xm
# La description (`describe pvc`) montrera que `.spec.resources.requests.storage` est maintenant `200Mi`.
# La section `.status.capacity.storage` restera probablement `150Mi` car le PV hostPath n'a pas été redimensionné.
# Les événements peuvent inclure FileSystemResizePending.
# La condition "FileSystemResizePending" peut apparaître si `allowVolumeExpansion: true` est dans la StorageClass.
# Cependant, avec un PV hostPath et no-provisioner, le redimensionnement effectif du volume sous-jacent ne se produit généralement pas automatiquement.
Fichier /opt/cka_exercises/stockage/Ex3/pvc_pending_troubleshooting.txt
(Révisé pour refléter le nouveau flux):
Si un PVC reste en état `Pending` après la création initiale (avant la création manuelle du PV):
1. **Décrire le PVC** : `kubectl describe pvc app-data-pvc -n default`.
Regardez attentivement la section `Events`. Elle contiendra typiquement un message indiquant :
* Si `volumeBindingMode: Immediate` (non utilisé ici) : `no persistent volumes available for this claim and no storage class is set up to dynamically provision them`.
* Si `volumeBindingMode: WaitForFirstConsumer` (utilisé ici) : `waiting for first consumer to be created before binding` ou similaire, ET ensuite, si aucun PV ne correspond une fois le Pod créé, des messages sur l'absence de PV.
Dans notre cas, le PVC attendra un PV correspondant car la StorageClass `fast-storage` utilise `kubernetes.io/no-provisioner`.
2. **Vérifier la StorageClass** : `kubectl describe storageclass fast-storage`.
* Confirmez que `Provisioner` est bien `kubernetes.io/no-provisioner`.
3. **Action corrective (qui fait partie de cet exercice)** : Créer manuellement un PersistentVolume (PV) qui :
* A le même `storageClassName` (`fast-storage`).
* Supporte les `accessModes` demandés (e.g., `ReadWriteOnce`).
* A une `capacity` suffisante pour la requête du PVC (e.g., >= 100Mi).
Une fois ce PV créé et disponible, le PVC devrait passer à l'état `Bound`. Si le Pod était déjà créé, il devrait alors commencer son initialisation.