tech:notes_kubernetes_k8s_-_utilisation
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_kubernetes_k8s_-_utilisation [2025/11/07 11:45] – Jean-Baptiste | tech:notes_kubernetes_k8s_-_utilisation [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes Kubernetes k8s - Utilisation | ||
| + | |||
| + | Voir : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | |||
| + | |||
| + | ~~~bash | ||
| + | kubectl api-resources -o wide | ||
| + | kubectl explain deploy | ||
| + | |||
| + | kubectl get nodes | ||
| + | kubectl get pods --all-namespaces | ||
| + | kubectl --namespace=kube-system get all | ||
| + | kubectl --kubeconfig .kube/ | ||
| + | export KUBECONFIG=/ | ||
| + | #export KUBECONFIG=~/ | ||
| + | cp / | ||
| + | |||
| + | kubectl --server=https:// | ||
| + | kubectl get deployment --all-namespaces | ||
| + | kubectl get svc | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Administration | ||
| + | |||
| + | |||
| + | |||
| + | Diagnostiquer le dysfonctionnement d'un pod Kubernetes | ||
| + | ~~~bash | ||
| + | # Voir les logs | ||
| + | kubectl logs monpod | ||
| + | # Si le pod est en erreur | ||
| + | kubectl describe po monpod | ||
| + | # Si le pod n' | ||
| + | kubectl describe rs monreplicaset | ||
| + | # Si le replicaset n' | ||
| + | kubectl describe deploy mondeploiement | ||
| + | kubectl describe sts mon statefulset | ||
| + | ~~~ | ||
| + | |||
| + | Vous pouvez consulter ce diagramme pour le troubleshooting d'un déploiement | ||
| + | Consulter les logs de l'api server Kubernetes | ||
| + | |||
| + | On y trouvera notamment les requêtes récentes des utilisateurs et leur droits RBAC | ||
| + | ~~~bash | ||
| + | kubectl -n kube-system logs -l component=kube-apiserver --tail 1000 | ||
| + | ~~~ | ||
| + | |||
| + | Consulter les logs de plop | ||
| + | |||
| + | Peut permettre d' | ||
| + | ~~~bash | ||
| + | kubectl -n plop logs -l app=plop -c plop --tail 1000 | ||
| + | ~~~ | ||
| + | Note: on pourra passer plop en mode debug pour avoir des informations plus complètes (kubectl -n plop edit ds plop, puis "- --log-level=debug" | ||
| + | Lister les pvs de chaque node | ||
| + | |||
| + | Permet de voir les pvs (persistent volumes) disponibles sur chaque node, par exemple pour débugger un statefulset dont un pvc est en état " | ||
| + | ~~~bash | ||
| + | export JSONPATH=' | ||
| + | kubectl get pv -o jsonpath=" | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | Objets Kubernetes | ||
| + | |||
| + | Commandes pour retrouver le pod / daemonset / deploiement responsable du container " | ||
| + | ~~~bash | ||
| + | # À utiliser sur un master, les noeuds n'ont pas la commande " | ||
| + | kubectl get pod -A | grep plop | ||
| + | kubectl get ds -A | grep plop | ||
| + | kubectl get deploy -A | grep plop | ||
| + | ~~~ | ||
| + | |||
| + | Attendre un état - ex que les pods soient prêts | ||
| + | ~~~bash | ||
| + | |||
| + | |||
| + | An alternative approach is to use kubectl wait | ||
| + | |||
| + | You could: | ||
| + | ~~~bash | ||
| + | kubectl wait pod --all --for=condition=Ready --namespace=${ns} | ||
| + | |||
| + | kubectl wait pod --all --for=condition=Ready -A | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | Procédures | ||
| + | Restart des pods | ||
| + | |||
| + | |||
| + | Se placer dans le namespace des frontaux valplop | ||
| + | ~~~bash | ||
| + | kubectl config set-context $(kubectl config current-context) --namespace=zzz-valplop | ||
| + | ~~~ | ||
| + | |||
| + | Vérifier qu'on accède bien au cluster k8s en listant les pods | ||
| + | ~~~bash | ||
| + | kubectl get pods | ||
| + | ~~~ | ||
| + | |||
| + | Résultat attendu | ||
| + | ~~~ | ||
| + | NAME READY | ||
| + | bac-valplop-8686574455-bnplq | ||
| + | bac-valplop-8686574455-ddxcf | ||
| + | bac-valplop-8686574455-dr25b | ||
| + | [...] | ||
| + | ~~~ | ||
| + | |||
| + | Vérifier quelle ressource deployment on veut relancer | ||
| + | ~~~bash | ||
| + | kubectl get deployments.apps | ||
| + | ~~~ | ||
| + | |||
| + | Lancer un rollout (= restart des pods du deployment ciblé) | ||
| + | ~~~bash | ||
| + | kubectl rollout restart deployment <nom du deployment> | ||
| + | ~~~ | ||
| + | |||
| + | On peut surveiller le restart séquentiel des pods avec l' | ||
| + | |||
| + | Restart pod récalcitrant | ||
| + | ~~~bash | ||
| + | kubectl delete pod cassandra-cl02livebgl-a-0 --grace-period=0 --force | ||
| + | ~~~ | ||
| + | |||
| + | Je veux savoir ma conso sur mon namespace | ||
| + | ~~~bash | ||
| + | kubectl -n $namespace get ResourceQuota kaas-compute-resources-$namespace -o yaml | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ### Restart a resource | ||
| + | |||
| + | |||
| + | Resource rollout will be restarted. | ||
| + | ~~~bash | ||
| + | kubectl rollout restart RESOURCE | ||
| + | ~~~ | ||
| + | |||
| + | Examples | ||
| + | ~~~ | ||
| + | # Restart all deployments in the test-namespace namespace | ||
| + | kubectl rollout restart deployment -n test-namespace | ||
| + | |||
| + | # Restart a deployment | ||
| + | kubectl rollout restart deployment/ | ||
| + | |||
| + | # Restart a daemon set | ||
| + | kubectl rollout restart daemonset/ | ||
| + | |||
| + | # Restart deployments with the app=nginx label | ||
| + | kubectl rollout restart deployment --selector=app=nginx | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Notes install nodes | ||
| + | |||
| + | Clef SSH /root/.ssh/ | ||
| + | |||
| + | apt-get install curl | ||
| + | |||
| + | Install kubectl | ||
| + | Install kubeadm | ||
| + | Install Docker | ||
| + | |||
| + | ~~~bash | ||
| + | kubectl get nodes -o wide | ||
| + | kubectl version | ||
| + | kubectl cluster-info | ||
| + | ~~~ | ||
| + | |||
| + | To further debug and diagnose cluster problems, use `kubectl cluster-info dump` | ||
| + | ~~~bash | ||
| + | kubectl get namespaces | ||
| + | kubectl api-resources | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | # kubectl run hello --image=hello-world | ||
| + | pod/hello created | ||
| + | # kubectl get pods | ||
| + | NAME READY | ||
| + | hello | ||
| + | ~~~ | ||
| + | |||
| + | `CrashLoopBackOff` signifit que le container fait que de cracher, ce qui est normal pour le ' | ||
| + | |||
| + | ~~~bash | ||
| + | kubectl run nginx --image=nginx | ||
| + | kubectl describe pod nginx | ||
| + | ~~~ | ||
| + | |||
| + | Get a shell to the running container | ||
| + | ~~~bash | ||
| + | kubectl exec -ti nginx -- /bin/bash | ||
| + | ~~~ | ||
| + | |||
| + | Get a shell to a specific running container | ||
| + | ~~~bash | ||
| + | # Get containers list | ||
| + | kubectl get pod awx-task-777db545b8-7npjl -o jsonpath=' | ||
| + | |||
| + | kubectl exec -ti awx-task-777db545b8-7npjl --container awx-ee -- /bin/bash | ||
| + | ~~~ | ||
| + | |||
| + | Port mapping | ||
| + | ~~~bash | ||
| + | kubectl port-forward nginx 8080:80 | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | kubectl create deployment nginx-deployment --image=nginx | ||
| + | |||
| + | kubectl expose deployment nginx-deployment --port=8080 --target-port=80 | ||
| + | kubectl get services | ||
| + | ~~~ | ||
| + | |||
| + | ~~~ | ||
| + | # kubectl describe deployment nginx-deployment | grep ^Replicas: | ||
| + | Replicas: | ||
| + | |||
| + | # kubectl scale deployment nginx-deployment --replicas=2 | ||
| + | deployment.apps/ | ||
| + | ~~~ | ||
| + | |||
| + | Création d'un pod | ||
| + | |||
| + | `nginx.yaml` | ||
| + | ~~~yaml | ||
| + | apiVersion: v1 | ||
| + | kind: Pod | ||
| + | metadata: | ||
| + | name: www | ||
| + | spec: | ||
| + | containers: | ||
| + | - name: nginx | ||
| + | image: nginx: | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | kubectl create -f pod.yaml | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Autres | ||
| + | |||
| + | ~~~bash | ||
| + | export POD_NAME=$(kubectl get pods --namespace kube-system -l " | ||
| + | |||
| + | export CONTAINER_PORT=$(kubectl get pod --namespace kube-system $POD_NAME -o jsonpath=" | ||
| + | |||
| + | kubectl --namespace kube-system port-forward $POD_NAME 8080: | ||
| + | ~~~ | ||
