Durée estimée : 15 minutes
Objective: Understand how to create and manage namespaces to organize cluster resources.
Context: You need to segregate resources for different projects or environments (e.g., development, staging, production).
Instructions:
/opt/cka_exercises/gestion-cluster/Ex1/all_namespaces.txt.project-alpha and project-beta.
kubectl create namespace commands used in /opt/cka_exercises/gestion-cluster/Ex1/create_ns_commands.sh.nginx-dev (image nginx:1.29.4) in project-alpha.nginx-user (image nginx:1.29.4) in project-beta./opt/cka_exercises/gestion-cluster/Ex1/pod-alpha.yaml and /opt/cka_exercises/gestion-cluster/Ex1/pod-beta.yaml respectively, then apply them.kubectl get pods -n project-alpha output in /opt/cka_exercises/gestion-cluster/Ex1/pods_in_alpha.txt.kubectl get pods -n project-beta output in /opt/cka_exercises/gestion-cluster/Ex1/pods_in_beta.txt./opt/cka_exercises/gestion-cluster/Ex1/all_pods_all_namespaces.txt.project-beta namespace. What happens to the nginx-user Pod? Describe this in /opt/cka_exercises/gestion-cluster/Ex1/beta_deletion_effect.txt.Préparation :
mkdir -p /opt/cka_exercises/gestion-cluster/Ex1
1. Lister les Namespaces :
kubectl get namespaces > /opt/cka_exercises/gestion-cluster/Ex1/all_namespaces.txt
# ou kubectl get ns
2. Créer les Namespaces project-alpha et project-beta :
Fichier /opt/cka_exercises/gestion-cluster/Ex1/create_ns_commands.sh:
#!/bin/bash
kubectl create namespace project-alpha
kubectl create namespace project-beta
# Vérification (optionnelle)
kubectl get ns project-alpha project-beta
Exécutez bash /opt/cka_exercises/gestion-cluster/Ex1/create_ns_commands.sh.
3. Déployer les Pods Nginx :
Fichier /opt/cka_exercises/gestion-cluster/Ex1/pod-alpha.yaml:
apiVersion: v1
kind: Pod
metadata:
name: nginx-dev
namespace: project-alpha
spec:
containers:
- name: nginx
image: nginx:1.29.4
Fichier /opt/cka_exercises/gestion-cluster/Ex1/pod-beta.yaml:
apiVersion: v1
kind: Pod
metadata:
name: nginx-user
namespace: project-beta
spec:
containers:
- name: nginx
image: nginx:1.29.4
Appliquez-les :
kubectl apply -f /opt/cka_exercises/gestion-cluster/Ex1/pod-alpha.yaml
kubectl apply -f /opt/cka_exercises/gestion-cluster/Ex1/pod-beta.yaml
4. Vérifier les Pods dans leurs Namespaces :
# Attendre que les pods soient Running
kubectl get pods -n project-alpha > /opt/cka_exercises/gestion-cluster/Ex1/pods_in_alpha.txt
kubectl get pods -n project-beta > /opt/cka_exercises/gestion-cluster/Ex1/pods_in_beta.txt
5. Voir tous les Pods (tous Namespaces) :
kubectl get pods --all-namespaces > /opt/cka_exercises/gestion-cluster/Ex1/all_pods_all_namespaces.txt
6. Supprimer le Namespace project-beta :
kubectl delete namespace project-beta
Fichier /opt/cka_exercises/gestion-cluster/Ex1/beta_deletion_effect.txt:
Lorsque le namespace `project-beta` est supprimé, toutes les ressources qu'il contient, y compris le Pod `nginx-user`, sont également supprimées.
La suppression d'un namespace est une opération en cascade.
Pour vérifier (après un court instant) : `kubectl get pods -n project-beta` devrait retourner `No resources found in project-beta namespace.` ou une erreur indiquant que le namespace n'existe pas.
Commandes de Nettoyage :
kubectl delete namespace project-alpha --ignore-not-found=true
kubectl delete namespace project-beta --ignore-not-found=true # Au cas où il n'aurait pas été supprimé à l'étape 6
rm -rf /opt/cka_exercises/gestion-cluster/Ex1
Durée estimée : 15 minutes
Objective: Safely perform maintenance on a node by draining it and then bringing it back into service.
Context: You need to update the OS or hardware of a worker node without disrupting running applications more than necessary.
Instructions:
Assume you have at least two worker nodes. Identify one to be the target for maintenance (e.g., node-to-drain).
maintenance-test.maintenance-test, deploy:
app-deploy with 2 replicas of nginx:1.29.4. Label Pods app=mywebapp.
/opt/cka_exercises/gestion-cluster/Ex2/deployment-app.yaml.legacy-pod using busybox:1.36 with command sleep 3600.
/opt/cka_exercises/gestion-cluster/Ex2/pod-legacy.yaml.kubectl get pods -n maintenance-test -o wide in /opt/cka_exercises/gestion-cluster/Ex2/initial_pod_distribution.txt.your-worker-node-name).kubectl drain your-worker-node-name.
legacy-pod (not managed by a controller)./opt/cka_exercises/gestion-cluster/Ex2/drain_command.sh.your-worker-node-name (kubectl get nodes). It should be SchedulingDisabled.app-deploy Pods. They should have been rescheduled to other available nodes.legacy-pod. It should be gone.kubectl get outputs in /opt/cka_exercises/gestion-cluster/Ex2/post_drain_status.txt.kubectl uncordon your-worker-node-name.Ready again. Note if the app-deploy Pods automatically move back (they shouldn't).
kubectl get nodes output in /opt/cka_exercises/gestion-cluster/Ex2/post_uncordon_status.txt.Préparation :
mkdir -p /opt/cka_exercises/gestion-cluster/Ex2
1. Créer le Namespace :
kubectl create namespace maintenance-test
2. Déployer les Applications :
Fichier /opt/cka_exercises/gestion-cluster/Ex2/deployment-app.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-deploy
namespace: maintenance-test
spec:
replicas: 2
selector:
matchLabels:
app: mywebapp
template:
metadata:
labels:
app: mywebapp
spec:
containers:
- name: nginx
image: nginx:1.29.4
Fichier /opt/cka_exercises/gestion-cluster/Ex2/pod-legacy.yaml:
apiVersion: v1
kind: Pod
metadata:
name: legacy-pod
namespace: maintenance-test
spec:
containers:
- name: busybox
image: busybox:1.36
command: ["sleep", "3600"]
Appliquez-les :
kubectl apply -f /opt/cka_exercises/gestion-cluster/Ex2/deployment-app.yaml
kubectl apply -f /opt/cka_exercises/gestion-cluster/Ex2/pod-legacy.yaml
3. Identifier la Distribution Initiale des Pods :
# Attendre que les pods soient Running
kubectl get pods -n maintenance-test -o wide > /opt/cka_exercises/gestion-cluster/Ex2/initial_pod_distribution.txt
4. Choix du Nœud : Fait à l'étape de précédente
5. Drainer le Nœud :
Fichier /opt/cka_exercises/gestion-cluster/Ex2/drain_command.sh (adaptez your-worker-node-name):
#!/bin/bash
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data --force
# --delete-emptydir-data est souvent nécessaire si des pods utilisent emptyDir.
# --force est pour le legacy-pod qui n'est pas géré par un contrôleur.
6. Vérification Post-Drainage :
(
echo "=== Statut des Nœuds ===" &&
kubectl get nodes &&
echo "
=== Pods de l'application (devraient être sur d'autres nœuds) ===" &&
kubectl get pods -n maintenance-test -l app=mywebapp -o wide &&
echo "
=== Statut du Legacy Pod (devrait être supprimé) ===" &&
kubectl get pod legacy-pod -n maintenance-test --ignore-not-found
) > /opt/cka_exercises/gestion-cluster/Ex2/post_drain_status.txt
# Le nœud drainé devrait afficher Ready,SchedulingDisabled.
# Les pods de app-deploy devraient être sur d'autres nœuds.
# legacy-pod ne devrait plus exister.
7. Rendre le Nœud Disponible (Uncordon) :
# Remplacez your-worker-node-name par le nom réel du nœud.
kubectl uncordon your-worker-node-name
8. Vérification Post-Uncordon :
(
echo "=== Statut des Nœuds après Uncordon ===" &&
kubectl get nodes &&
echo "
=== Pods de l'application (ne devraient pas revenir automatiquement) ===" &&
kubectl get pods -n maintenance-test -l app=mywebapp -o wide
) > /opt/cka_exercises/gestion-cluster/Ex2/post_uncordon_status.txt
# Le nœud devrait être Ready. Les pods ne reviennent pas automatiquement sur le nœud fraîchement uncordonné.
# Le scheduler K8s ne déplace pas les pods existants à moins d'y être forcé (par ex. par des taints NoExecute, des pannes de nœuds, ou des deschedulers).
Commande de Nettoyage :
kubectl delete namespace maintenance-test --ignore-not-found=true
# Assurez-vous que le nœud est uncordonné si ce n'est pas déjà fait.
# Remplacez your-worker-node-name si nécessaire.
# kubectl uncordon your-worker-node-name --ignore-not-found=true
rm -rf /opt/cka_exercises/gestion-cluster/Ex2
kubeadmDurée estimée : 25 minutes.
Objectif : Comprendre le processus de mise à niveau d'un cluster Kubernetes géré par kubeadm. Simuler ou discuter les étapes de mise à niveau du control plane et des nœuds workers, y compris le troubleshooting de base.
Context: You need to upgrade your cluster from version v1.32.1 to v1.32.3 (ou des versions CKA pertinentes, ex: v1.28.x à v1.28.y).
Instructions: This exercise is primarily about documenting the command sequences. Assume you have a cluster with one control plane node (k8s-control) and one worker node (k8s-worker1), both initially at v1.32.1.
k8s-control):
/opt/cka_exercises/gestion-cluster/Ex3/01_control_plane_drain.sh.kubeadm to 1.32.3-1.1 (or target version). Store in /opt/cka_exercises/gestion-cluster/Ex3/02_control_plane_update_kubeadm.sh./opt/cka_exercises/gestion-cluster/Ex3/03_control_plane_upgrade_plan.sh.
kubeadm upgrade plan fails or shows unexpected versions, check network connectivity, repository configuration, and if kubeadm package was updated correctly./opt/cka_exercises/gestion-cluster/Ex3/04_control_plane_upgrade_apply.sh.
kubeadm upgrade apply fails, examine the output carefully. Check logs: journalctl -xeu kubelet or cat /var/log/pods/kube-system_kube-apiserver-... (or other control plane component logs if specified in the error). Check kubectl get pods -n kube-system for failing control plane components.kubelet and kubectl to 1.32.3-1.1 and restart kubelet. Store in /opt/cka_exercises/gestion-cluster/Ex3/05_control_plane_update_kubelet_kubectl.sh./opt/cka_exercises/gestion-cluster/Ex3/06_control_plane_uncordon.sh.k8s-worker1):
/opt/cka_exercises/gestion-cluster/Ex3/07_worker_drain.sh.k8s-worker1: Document commands to update kubeadm to 1.32.3-1.1. Store in /opt/cka_exercises/gestion-cluster/Ex3/08_worker_update_kubeadm.sh.k8s-worker1: Document command to upgrade the node configuration. Store in /opt/cka_exercises/gestion-cluster/Ex3/09_worker_upgrade_node.sh.
kubeadm upgrade node fails, check kubelet logs on the worker node: journalctl -xeu kubelet.k8s-worker1: Document commands to update kubelet and kubectl to 1.32.3-1.1 and restart kubelet. Store in /opt/cka_exercises/gestion-cluster/Ex3/10_worker_update_kubelet_kubectl.sh./opt/cka_exercises/gestion-cluster/Ex3/11_worker_uncordon.sh.Ready state after uncordon? Store the command and troubleshooting steps in /opt/cka_exercises/gestion-cluster/Ex3/12_verify_upgrade_status.sh.Préparation :
mkdir -p /opt/cka_exercises/gestion-cluster/Ex3
# Les versions cibles sont 1.32.3-1.1 pour les paquets et v1.32.3 pour kubeadm upgrade.
# Adaptez si vos versions de CKA sont différentes.
K8S_TARGET_VERSION_PKG="1.32.3-1.1"
K8S_TARGET_VERSION_UPGRADE="v1.32.3"
1. Mise à jour du Control Plane (k8s-control) :
Fichier /opt/cka_exercises/gestion-cluster/Ex3/01_control_plane_drain.sh:
#!/bin/bash
# Exécuté depuis une machine ayant kubectl configuré pour le cluster
kubectl drain k8s-control --ignore-daemonsets
Fichier /opt/cka_exercises/gestion-cluster/Ex3/02_control_plane_update_kubeadm.sh:
#!/bin/bash
# Exécuté sur k8s-control
# Assurez-vous que K8S_TARGET_VERSION_PKG est défini (ex: 1.32.3-1.1)
# sudo apt-mark unhold kubeadm # Si précédemment mis en hold
sudo apt-get update
sudo apt-get install -y --allow-change-held-packages kubeadm=${K8S_TARGET_VERSION_PKG}
sudo apt-mark hold kubeadm # Optionnel: pour éviter des mises à jour accidentelles
Fichier /opt/cka_exercises/gestion-cluster/Ex3/03_control_plane_upgrade_plan.sh:
#!/bin/bash
# Exécuté sur k8s-control
# Assurez-vous que K8S_TARGET_VERSION_UPGRADE est défini (ex: v1.32.3)
sudo kubeadm upgrade plan ${K8S_TARGET_VERSION_UPGRADE}
Fichier /opt/cka_exercises/gestion-cluster/Ex3/04_control_plane_upgrade_apply.sh:
#!/bin/bash
# Exécuté sur k8s-control
# Assurez-vous que K8S_TARGET_VERSION_UPGRADE est défini
sudo kubeadm upgrade apply ${K8S_TARGET_VERSION_UPGRADE} --yes
Fichier /opt/cka_exercises/gestion-cluster/Ex3/05_control_plane_update_kubelet_kubectl.sh:
#!/bin/bash
# Exécuté sur k8s-control
# Assurez-vous que K8S_TARGET_VERSION_PKG est défini
# sudo apt-mark unhold kubelet kubectl # Si précédemment mis en hold
sudo apt-get update
sudo apt-get install -y --allow-change-held-packages kubelet=${K8S_TARGET_VERSION_PKG} kubectl=${K8S_TARGET_VERSION_PKG}
sudo apt-mark hold kubelet kubectl # Optionnel
sudo systemctl daemon-reload
sudo systemctl restart kubelet
Fichier /opt/cka_exercises/gestion-cluster/Ex3/06_control_plane_uncordon.sh:
#!/bin/bash
# Exécuté depuis une machine ayant kubectl configuré
kubectl uncordon k8s-control
2. Mise à jour du Nœud Worker (k8s-worker1) :
Fichier /opt/cka_exercises/gestion-cluster/Ex3/07_worker_drain.sh:
#!/bin/bash
# Exécuté depuis une machine ayant kubectl configuré
kubectl drain k8s-worker1 --ignore-daemonsets --delete-emptydir-data --force
Fichier /opt/cka_exercises/gestion-cluster/Ex3/08_worker_update_kubeadm.sh:
#!/bin/bash
# Exécuté sur k8s-worker1
# Assurez-vous que K8S_TARGET_VERSION_PKG est défini
# sudo apt-mark unhold kubeadm # Si mis en hold
sudo apt-get update
sudo apt-get install -y --allow-change-held-packages kubeadm=${K8S_TARGET_VERSION_PKG}
sudo apt-mark hold kubeadm # Optionnel
Fichier /opt/cka_exercises/gestion-cluster/Ex3/09_worker_upgrade_node.sh:
#!/bin/bash
# Exécuté sur k8s-worker1
sudo kubeadm upgrade node
Fichier /opt/cka_exercises/gestion-cluster/Ex3/10_worker_update_kubelet_kubectl.sh:
#!/bin/bash
# Exécuté sur k8s-worker1
# Assurez-vous que K8S_TARGET_VERSION_PKG est défini
# sudo apt-mark unhold kubelet kubectl # Si mis en hold
sudo apt-get update
sudo apt-get install -y --allow-change-held-packages kubelet=${K8S_TARGET_VERSION_PKG} kubectl=${K8S_TARGET_VERSION_PKG}
sudo apt-mark hold kubelet kubectl # Optionnel
sudo systemctl daemon-reload
sudo systemctl restart kubelet
Fichier /opt/cka_exercises/gestion-cluster/Ex3/11_worker_uncordon.sh:
#!/bin/bash
# Exécuté depuis une machine ayant kubectl configuré
kubectl uncordon k8s-worker1
3. Vérifier le Statut de la Mise à Jour :
Fichier /opt/cka_exercises/gestion-cluster/Ex3/12_verify_upgrade_status.sh:
#!/bin/bash
# Exécuté depuis une machine ayant kubectl configuré
kubectl get nodes
# On s'attend à voir la nouvelle version (ex: v1.32.3) pour tous les nœuds et STATUS Ready.
# Si un nœud ne revient pas à Ready après uncordon:
# 1. Décrire le nœud : kubectl describe node <nom-noeud-probleme>
# Regarder les Conditions (surtout Ready, MemoryPressure, DiskPressure, NetworkUnavailable) et les Events.
# 2. Vérifier les logs du Kubelet sur le nœud en question: ssh <nom-noeud-probleme> -- sudo journalctl -xeu kubelet
# 3. Vérifier les pods du CNI sur ce nœud (souvent dans kube-system, ex: calico-node, weave-net):
# kubectl get pods -n kube-system -o wide | grep <nom-noeud-probleme>
# kubectl logs -n kube-system <pod-cni-sur-noeud>
# 4. Vérifier la configuration réseau du nœud et la connectivité avec le control plane.
# 5. S'assurer que le service containerd (ou docker) fonctionne correctement sur le nœud.
Commandes de Nettoyage :
rm -rf /opt/cka_exercises/gestion-cluster/Ex3