kuberneteskubectlk8s
MDstable
NoteSnippetChecklistPlaybook
kubectl — Référence pratique
Commandes kubectl essentielles pour administrer un cluster Kubernetes
snippetintermediate 2025-05-10 4 min read
kuberneteskubectlk8spodsdeploymentsdevops
Contextes et namespaces
bash
Variables
{{CONTEXT}}
{{NAMESPACE}}
# Contexteskubectl config get-contextskubectl config current-contextkubectl config use-context {{CONTEXT}}# Namespace par défautkubectl config set-context --current --namespace={{NAMESPACE}}# Alias utilesalias kkubectlalias kn'kubectl config set-context --current --namespace'
Pods
bash
Variables
{{NAMESPACE}}
{{SERVICE}}
{{CONTAINER}}
# Listerkubectl get podskubectl get pods -n {{NAMESPACE}}kubectl get pods -A # tous les namespaceskubectl get pods -o wide # avec IP et node# Détailkubectl describe pod {{SERVICE}}kubectl describe pod {{SERVICE}} -n {{NAMESPACE}}# Logskubectl logs {{SERVICE}}kubectl logs {{SERVICE}} -f # suivrekubectl logs {{SERVICE}} --previous # crash précédentkubectl logs {{SERVICE}} -c {{CONTAINER}} # multi-conteneurkubectl logs -l app{{SERVICE}} --all-containers # par label# Execkubectl exec -it {{SERVICE}} -- bashkubectl exec -it {{SERVICE}} -c {{CONTAINER}} -- sh# Copier des fichierskubectl cp {{SERVICE}}/path/to/file /local-filekubectl cp /local-file {{SERVICE}}/path/to/file# Supprimerkubectl delete pod {{SERVICE}}kubectl delete pod {{SERVICE}} --force --grace-period=0
Deployments
bash
Variables
{{NAMESPACE}}
{{SERVICE}}
{{CONTAINER}}
# Listerkubectl get deploymentskubectl get deploy -n {{NAMESPACE}}# Détailkubectl describe deployment {{SERVICE}}# Scalerkubectl scale deployment {{SERVICE}} --replicas=3# Mettre à jour l'imagekubectl set image deployment{{SERVICE}} {{CONTAINER}}imagetag# Rolloutkubectl rollout status deployment{{SERVICE}}kubectl rollout history deployment{{SERVICE}}kubectl rollout undo deployment{{SERVICE}} # rollbackkubectl rollout undo deployment{{SERVICE}} --to-revision=2# Redémarrer les pods (sans downtime)kubectl rollout restart deployment{{SERVICE}}
Services
bash
Variables
{{NAMESPACE}}
{{SERVICE}}
kubectl get serviceskubectl get svc -n {{NAMESPACE}}kubectl describe service {{SERVICE}}# Port-forward (debug local)kubectl port-forward service{{SERVICE}} 808080kubectl port-forward pod{{SERVICE}} 80803000
Configuration
bash
Variables
{{SERVICE}}
# ConfigMapskubectl get configmapkubectl describe configmap {{SERVICE}}kubectl create configmap {{SERVICE}} --from-file=config.yamlkubectl create configmap {{SERVICE}} --from-literal=key=value# Secretskubectl get secretskubectl describe secret {{SERVICE}}kubectl create secret generic {{SERVICE}}--from-literal=username=admin--from-literal=password=s3cr3t# Décoder un secretkubectl get secret {{SERVICE}} -o jsonpath'{.data.password}' | base64 -d
Manifests YAML
Deployment type
yaml
Variables
{{SERVICE}}
{{NAMESPACE}}
apiVersion: apps/v1kind: Deploymentmetadata:name: {{SERVICE}}namespace: {{NAMESPACE}}labels:app: {{SERVICE}}spec:replicas: 3selector:matchLabels:app: {{SERVICE}}strategy:type: RollingUpdaterollingUpdate:maxSurge: 1maxUnavailable: 0template:metadata:labels:app: {{SERVICE}}spec:containers:- name: {{SERVICE}}image: myapp:latestports:- containerPort: 3000env:- name: DB_URLvalueFrom:secretKeyRef:name: app-secretskey: db-urlresources:requests:memory: "128Mi"cpu: "100m"limits:memory: "512Mi"cpu: "500m"livenessProbe:httpGet:path: /healthport: 3000initialDelaySeconds: 30periodSeconds: 10readinessProbe:httpGet:path: /readyport: 3000initialDelaySeconds: 5periodSeconds: 5
Appliquer et gérer les ressources
bash
Variables
{{SERVICE}}
# Appliquer un manifestkubectl apply -f deploymentyamlkubectl apply -f /k8s/ # tout un répertoirekubectl apply -k /kustomize/ # kustomize# Voir avant d'appliquer (dry-run)kubectl apply -f deploymentyaml --dry-run=clientkubectl diff -f deploymentyaml# Supprimerkubectl delete -f deploymentyamlkubectl delete deployment {{SERVICE}}# Éditer en directkubectl edit deployment {{SERVICE}}
Nodes et cluster
bash
Variables
{{SERVICE}}
{{NAMESPACE}}
# Nodeskubectl get nodeskubectl get nodes -o widekubectl describe node {{SERVICE}}kubectl top nodes # CPU/RAM (metrics-server requis)# Cordon / drain (maintenance)kubectl cordon {{SERVICE}} # plus de schedulingkubectl drain {{SERVICE}} --ignore-daemonsets --delete-emptydir-datakubectl uncordon {{SERVICE}} # réactiver# Pods par nodekubectl get pods -A -o wide | grep {{SERVICE}}# Events du clusterkubectl get events -A --sort-by='.lastTimestamp' | tail -20kubectl get events -n {{NAMESPACE}} --field-selector reasonBackOff
Troubleshooting
bash
Variables
{{SERVICE}}
{{CONTAINER}}
# Pod en CrashLoopBackOffkubectl logs {{SERVICE}} --previouskubectl describe pod {{SERVICE}} # Events en bas# Pod en Pendingkubectl describe pod {{SERVICE}} # chercher "Insufficient cpu/memory"kubectl get events | grep {{SERVICE}}# Image pull errorkubectl describe pod {{SERVICE}} # "ErrImagePull" ou "ImagePullBackOff"# Vérifier le secret de registry :kubectl get secret regcred# Debug avec un pod éphémèrekubectl debug -it {{SERVICE}} --image=busybox --target={{CONTAINER}}kubectl run debug --image=nicolaka/netshoot -it --rm
💡 Tip —
Toujours définir resources.requests et resources.limits sur les containers de production. Sans requests, le scheduler ne peut pas placer correctement les pods. Sans limits, un pod peut monopoliser les ressources du node et impacter les autres.
OPS·BRAIN v1.03 notes · DevOpslocal