Création Cluster Kubernetes via Magnum¶
Introduction¶
Création et gestion d'un cluster Kubernetes avec Magnum : provisioning, scaling, accès kubectl, et opérations courantes.
Prérequis¶
Points à apprendre¶
Création d'un cluster¶
# Cluster simple (1 master, 1 worker)
openstack coe cluster create k8s-dev-cluster \
--cluster-template k8s-dev \
--master-count 1 \
--node-count 1 \
--keypair my-keypair
# Cluster production (3 masters, 3+ workers)
openstack coe cluster create k8s-prod-cluster \
--cluster-template k8s-prod \
--master-count 3 \
--node-count 5 \
--keypair my-keypair \
--docker-volume-size 100
# Avec labels supplémentaires au niveau cluster
openstack coe cluster create k8s-staging-cluster \
--cluster-template k8s-staging \
--master-count 1 \
--node-count 3 \
--keypair my-keypair \
--labels "min_node_count=1,max_node_count=10"
Suivi de la création¶
stateDiagram-v2
[*] --> CREATE_IN_PROGRESS : cluster create
CREATE_IN_PROGRESS --> CREATE_COMPLETE : Success
CREATE_IN_PROGRESS --> CREATE_FAILED : Error
CREATE_COMPLETE --> UPDATE_IN_PROGRESS : cluster resize
CREATE_COMPLETE --> DELETE_IN_PROGRESS : cluster delete
CREATE_COMPLETE --> ROLLBACK_IN_PROGRESS : rollback
UPDATE_IN_PROGRESS --> UPDATE_COMPLETE : Success
UPDATE_IN_PROGRESS --> UPDATE_FAILED : Error
DELETE_IN_PROGRESS --> DELETE_COMPLETE : Success
DELETE_IN_PROGRESS --> DELETE_FAILED : Error
DELETE_COMPLETE --> [*]
# Suivre la progression
watch openstack coe cluster show k8s-dev-cluster -c status -c status_reason
# Voir les événements Heat
STACK_ID=$(openstack coe cluster show k8s-dev-cluster -f value -c stack_id)
openstack stack event list $STACK_ID
# Logs détaillés en cas d'erreur
openstack stack failures list $STACK_ID
Accès au cluster¶
# Générer le kubeconfig
openstack coe cluster config k8s-dev-cluster --dir ~/kube
# Utiliser le kubeconfig
export KUBECONFIG=~/kube/config
kubectl get nodes
kubectl cluster-info
# Ou avec merge
openstack coe cluster config k8s-dev-cluster --dir ~/.kube --force --merge
kubectl config get-contexts
kubectl config use-context k8s-dev-cluster
Diagramme d'architecture cluster¶
graph TB
subgraph internet[Internet]
users[Users/Devs<br/>kubectl]
end
subgraph openstack[OpenStack Cloud]
subgraph lb[Load Balancer]
api_lb[API LB<br/>Octavia<br/>6443]
end
subgraph masters[Master Nodes x3]
m1[master-0<br/>kube-apiserver<br/>etcd<br/>controller-manager<br/>scheduler]
m2[master-1]
m3[master-2]
end
subgraph workers[Worker Nodes x3]
w1[node-0<br/>kubelet<br/>kube-proxy<br/>containerd]
w2[node-1]
w3[node-2]
end
subgraph storage[Ceph Storage]
ceph[Ceph Cluster<br/>PersistentVolumes]
end
end
users -->|HTTPS 6443| api_lb
api_lb --> m1
api_lb --> m2
api_lb --> m3
m1 -->|Schedule pods| w1
m2 --> w2
m3 --> w3
w1 -->|CSI| ceph
w2 -->|CSI| ceph
w3 -->|CSI| ceph
Scaling du cluster¶
# Scale up workers
openstack coe cluster update k8s-dev-cluster replace node_count=5
# Scale down workers (avec précaution)
openstack coe cluster update k8s-dev-cluster replace node_count=3
# Note: Le scaling des masters n'est pas supporté via update
# Il faut créer un nouveau cluster pour changer master_count
Autoscaling (si activé)¶
# Vérifier que l'autoscaler est déployé
kubectl get deployment cluster-autoscaler -n kube-system
# Logs de l'autoscaler
kubectl logs -f deployment/cluster-autoscaler -n kube-system
# Configurer via labels du template
# min_node_count: nombre minimum de workers
# max_node_count: nombre maximum de workers
Opérations sur le cluster¶
# Lister les clusters
openstack coe cluster list
# Détails du cluster
openstack coe cluster show k8s-dev-cluster
# Rafraîchir les credentials (rotation certificats)
openstack coe cluster config k8s-dev-cluster --dir ~/kube --force
# Supprimer le cluster
openstack coe cluster delete k8s-dev-cluster
# Forcer la suppression (si bloqué)
openstack coe cluster delete k8s-dev-cluster --force
Accès SSH aux nodes¶
# Récupérer les floating IPs
openstack server list | grep k8s-dev-cluster
# SSH sur un master (user dépend de l'image)
ssh -i ~/.ssh/my-keypair.pem core@<master-floating-ip>
# SSH sur un worker
ssh -i ~/.ssh/my-keypair.pem core@<worker-floating-ip>
# Vérifier les services
sudo systemctl status kubelet
sudo crictl ps
Configuration kubectl après création¶
# Vérifier la connectivité
kubectl cluster-info
# Voir les composants système
kubectl get pods -n kube-system
# Vérifier les nodes
kubectl get nodes -o wide
# Tester le scheduling
kubectl create deployment nginx --image=nginx --replicas=3
kubectl get pods -o wide
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get svc nginx
Labels de nodegroup¶
# Voir les labels des nodes
kubectl get nodes --show-labels
# Les nodes Magnum ont des labels automatiques:
# - magnum.openstack.org/role=master ou =worker
# - topology.kubernetes.io/zone=<availability_zone>
Exemples pratiques¶
Script de création complète¶
#!/bin/bash
# create-cluster.sh
CLUSTER_NAME=$1
TEMPLATE=${2:-k8s-staging}
MASTER_COUNT=${3:-1}
WORKER_COUNT=${4:-3}
KEYPAIR=${5:-default-keypair}
echo "Creating cluster: $CLUSTER_NAME"
echo "Template: $TEMPLATE"
echo "Masters: $MASTER_COUNT, Workers: $WORKER_COUNT"
# Créer le cluster
openstack coe cluster create $CLUSTER_NAME \
--cluster-template $TEMPLATE \
--master-count $MASTER_COUNT \
--node-count $WORKER_COUNT \
--keypair $KEYPAIR
# Attendre la création
echo "Waiting for cluster creation..."
while true; do
STATUS=$(openstack coe cluster show $CLUSTER_NAME -f value -c status)
echo "Status: $STATUS"
if [ "$STATUS" == "CREATE_COMPLETE" ]; then
echo "Cluster created successfully!"
break
elif [ "$STATUS" == "CREATE_FAILED" ]; then
echo "Cluster creation failed!"
openstack coe cluster show $CLUSTER_NAME -c status_reason
exit 1
fi
sleep 30
done
# Récupérer kubeconfig
mkdir -p ~/.kube
openstack coe cluster config $CLUSTER_NAME --dir ~/.kube --force --merge
# Vérifier
export KUBECONFIG=~/.kube/config
kubectl config use-context $CLUSTER_NAME
kubectl get nodes
Vérification santé cluster¶
#!/bin/bash
# check-cluster.sh
CLUSTER=$1
echo "=== Cluster Status ==="
openstack coe cluster show $CLUSTER -c status -c health_status
echo -e "\n=== Kubernetes Nodes ==="
kubectl get nodes
echo -e "\n=== System Pods ==="
kubectl get pods -n kube-system -o wide
echo -e "\n=== API Server Health ==="
kubectl get --raw='/healthz'
echo -e "\n=== Cluster Resources ==="
kubectl top nodes 2>/dev/null || echo "Metrics server not available"
Troubleshooting¶
# Si le cluster est bloqué en CREATE_IN_PROGRESS
STACK_ID=$(openstack coe cluster show $CLUSTER_NAME -f value -c stack_id)
# Voir les ressources du stack
openstack stack resource list $STACK_ID
# Voir les erreurs
openstack stack failures list $STACK_ID --long
# Logs des VMs via console
openstack console log show <node-server-id>
Ressources¶
Checkpoint¶
- Cluster créé avec succès (status: CREATE_COMPLETE)
- kubeconfig généré et fonctionnel
-
kubectl get nodesretourne tous les nodes Ready - Pods système en Running dans kube-system
- Scaling testé (augmentation workers)
- Accès SSH aux nodes possible