Lab Adv 02 : Déployer Longhorn sur OKS
Objectif du lab
Section intitulée « Objectif du lab »À la fin de ce lab, vous serez capable de :
- Déployer Longhorn sur un cluster OKS
- Créer des volumes persistants via Longhorn
- Tester un volume ReadWriteMany
- Valider la persistance des données en environnement multi-AZ
Préparation de l’environnement
Section intitulée « Préparation de l’environnement »Avant de commencer ce TP, assurez-vous de disposer des éléments suivants :
- Un cluster OKS. 👉 Cliquez ici pour accéder au TP de création d’un cluster OKS si ce n’est pas encore fait.
- Helm installé .
Création du NodePool dédié Longhorn
Section intitulée « Création du NodePool dédié Longhorn »Créez un fichier nodepool.yaml :
apiVersion: oks.dev/v1beta2kind: NodePoolmetadata: name: nodepool-01spec: desiredNodes: 1 nodeType: tinav7.c4r8p2 zones: - eu-west-2a - eu-west-2b - eu-west-2c volumes: - device: root size: 100 type: "gp2" dir: '/' - device: xvdl size: 100 type: "gp2" dir: "/var/lib/longhorn"Appliquez la configuration :
kubectl apply -f nodepool.yamlInstallation iSCSI sur les nœuds
Section intitulée « Installation iSCSI sur les nœuds »Créez le fichier configuration-iscsi.yaml :
apiVersion: apps/v1kind: DaemonSetmetadata: name: longhorn-iscsi-installation labels: app: longhorn-iscsi-installation annotations: command: &cmd OS=$(grep -E "^ID_LIKE=" /etc/os-release | cut -d '=' -f 2); if [[ -z "${OS}" ]]; then OS=$(grep -E "^ID=" /etc/os-release | cut -d '=' -f 2); fi; if [[ "${OS}" == *"debian"* ]]; then sudo apt-get update -q -y && sudo apt-get install -q -y open-iscsi nfs-common cryptsetup && sudo systemctl -q enable iscsid && sudo systemctl start iscsid && sudo modprobe iscsi_tcp; elif [[ "${OS}" == *"suse"* ]]; then sudo zypper --gpg-auto-import-keys -q refresh && sudo zypper --gpg-auto-import-keys -q install -y open-iscsi && sudo systemctl -q enable iscsid && sudo systemctl start iscsid && sudo modprobe iscsi_tcp; else sudo yum makecache -q -y && sudo yum --setopt=tsflags=noscripts install -q -y iscsi-initiator-utils && echo "InitiatorName=$(/sbin/iscsi-iname)" > /etc/iscsi/initiatorname.iscsi && sudo systemctl -q enable iscsid && sudo systemctl start iscsid && sudo modprobe iscsi_tcp; fi && if [ $? -eq 0 ]; then echo "iscsi install successfully"; else echo "iscsi install failed error code $?"; fispec: selector: matchLabels: app: longhorn-iscsi-installation template: metadata: labels: app: longhorn-iscsi-installation spec: nodeSelector: node-pool: nodepool-01 hostNetwork: true hostPID: true initContainers: - name: iscsi-installation command: - nsenter - --mount=/proc/1/ns/mnt - -- - bash - -c - *cmd image: alpine:3.17 securityContext: privileged: true containers: - name: sleep image: registry.k8s.io/pause:3.1 updateStrategy: type: RollingUpdateAppliquez le DaemonSet :
kubectl apply -f configuration-iscsi.yamlInstallation de Longhorn
Section intitulée « Installation de Longhorn »Créez le namespace :
kubectl create ns longhorn-systemAjoutez le depôt helm :
helm repo add longhorn https://charts.longhorn.iohelm repo updateInstallez longhorn :
helm install longhorn longhorn/longhorn --namespace longhorn-systemVérifiez les pods :
kubectl get pods -n longhorn-systemCréation de la StorageClass Longhorn
Section intitulée « Création de la StorageClass Longhorn »Créez le fichier longhorn-storageclass.yaml :
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: longhorn-3replprovisioner: driver.longhorn.ioallowVolumeExpansion: truereclaimPolicy: DeletevolumeBindingMode: Immediateparameters: numberOfReplicas: "3" staleReplicaTimeout: "2880" replicaAutoBalance: "least-effort" fsType: "ext4" nfsOptions: "vers=4.2,noresvport,softerr,timeo=600,retrans=5"Appliquez la StorageClass :
kubectl apply -f longhorn-storageclass.yamlCréation du PersistentVolumeClaim
Section intitulée « Création du PersistentVolumeClaim »Créez le fichier pvc.yaml :
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: rwx-test-claim namespace: longhorn-systemspec: accessModes: - ReadWriteMany storageClassName: longhorn-3repl resources: requests: storage: 3Gikubectl apply -f pvc.yamlVérification des volumes Longhorn
Section intitulée « Vérification des volumes Longhorn »kubectl get volumes.longhorn.io -n longhorn-system## Résultat attendu :
NAME DATA ENGINE STATE ROBUSTNESS SCHEDULED SIZEpvc-d1359fcd-9267-4b28-903a-1912a1d1af10 v1 detached unknown 3221225472kubectl get replicas.longhorn.io -n longhorn-system
## Résultat attendu:
NAME DATA ENGINE STATE NODE DISKpvc-d1359fcd-9267-4b28-903a-1912a1d1af10-r-0baeabda v1 stopped ip-10-50-110-132 202e26f6-0f49-4733-8b13-5787febd0b16pvc-d1359fcd-9267-4b28-903a-1912a1d1af10-r-0efa26c4 v1 stopped ip-10-50-65-223 b325f185-de59-4a5a-98f3-daa3e83eea72pvc-d1359fcd-9267-4b28-903a-1912a1d1af10-r-aa17101c v1 stopped ip-10-50-32-166 b7dc5742-2297-46b7-8904-d5a5440c5d8bAccès à l’interface Longhorn
Section intitulée « Accès à l’interface Longhorn »kubectl port-forward -n longhorn-system svc/longhorn-frontend 8080:80Accédez à : 👉 http://localhost:8080 Résultat attendu :
- Volume rwx-test-claim Healthy
- 3 replicas sur des nœuds différents
- Les replicas apparaissent en état
stoppedtant qu’aucun pod n’a monté le volume
Déploiement d’une application utilisant le volume RWX
Section intitulée « Déploiement d’une application utilisant le volume RWX »Créez le fichier deployment-nginx.yaml :
apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-rwx-demo namespace: longhorn-systemspec: replicas: 3 selector: matchLabels: app: nginx-rwx-demo template: metadata: labels: app: nginx-rwx-demo spec: nodeSelector: node-pool: nodepool-01 containers: - name: nginx image: nginx:alpine volumeMounts: - name: shared-data mountPath: /usr/share/nginx/html command: ["/bin/sh", "-c"] args: - | echo "Welcome!" > /usr/share/nginx/html/index.html; while true; do echo "$(date) — $(hostname)" >> /usr/share/nginx/html/dates.log; sleep 60; done volumes: - name: shared-data persistentVolumeClaim: claimName: rwx-test-claimkubectl apply -f deployment-nginx.yamlLes replicas doivent etre maintenant en RUNNING:
kubectl get replicas.longhorn.io
# Résultat attendu :
NAME DATA ENGINE STATE NODE DISKpvc-f6d331a7-002f-4a26-b4bc-0042ae816836-r-13f4eaf0 v1 running ip-10-50-110-132 202e26f6-0f49-4733-8b13-5787febd0b16pvc-f6d331a7-002f-4a26-b4bc-0042ae816836-r-bbe908b3 v1 running ip-10-50-32-166 b7dc5742-2297-46b7-8904-d5a5440c5d8bpvc-f6d331a7-002f-4a26-b4bc-0042ae816836-r-c3fa3af1 v1 running ip-10-50-65-223 b325f185-de59-4a5a-98f3-daa3e83eea72Vérification du partage du volume
Section intitulée « Vérification du partage du volume »kubectl exec -it <pod-name> -- tail -f /usr/share/nginx/html/dates.log
## Résultat attendu:# Pod 1 :kubectl exec -it nginx-rwx-demo-6b777c9db8-n29n5 -- tail -f /usr/share/nginx/html/dates.logMon Jan 26 12:53:23 UTC 2026 — nginx-rwx-demo-6b777c9db8-xvrd2Mon Jan 26 12:53:24 UTC 2026 — nginx-rwx-demo-6b777c9db8-w298mMon Jan 26 12:53:25 UTC 2026 — nginx-rwx-demo-6b777c9db8-n29n5
# Pod 2 :
kubectl exec -it nginx-rwx-demo-6b777c9db8-w298m -- tail -f /usr/share/nginx/html/dates.logMon Jan 26 12:53:23 UTC 2026 — nginx-rwx-demo-6b777c9db8-xvrd2Mon Jan 26 12:53:24 UTC 2026 — nginx-rwx-demo-6b777c9db8-w298mMon Jan 26 12:53:25 UTC 2026 — nginx-rwx-demo-6b777c9db8-n29n5Mon Jan 26 12:54:23 UTC 2026 — nginx-rwx-demo-6b777c9db8-xvrd2Test de résilience multi-AZ
Section intitulée « Test de résilience multi-AZ »kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-dataLe pod est recréé automatiquement sur un autre nœud. Vérification :
kubectl exec -it <new-pod> -- tail /usr/share/nginx/html/dates.logRésultat attendu :
—> données toujours présentes