kubectl — Kubernetes-Cluster über die Kommandozeile steuern

Praxis-Guide zu kubectl — Pods, Deployments, Services und Cluster verwalten, Logs lesen, Rollouts steuern und debuggen, mit Beispielen für jeden Befehl.

kubectl ist das zentrale Kommandozeilen-Werkzeug für Kubernetes: Jeder Befehl spricht über die kubeconfig mit dem API-Server des Clusters und steuert so Pods, Deployments, Services und alle anderen Ressourcen. Welchen Cluster du ansprichst, bestimmt der aktive Kontext – so wechselst du mit einem einzigen Befehl zwischen Entwicklung und Produktion. Dieser Guide führt dich durch die wichtigsten Befehle für den Alltag: Ressourcen anzeigen und anwenden, Rollouts steuern, Logs lesen und im laufenden Pod debuggen.

Cluster & Kontext

kubectl cluster-info — Zeigt Endpunkt-Informationen des Clusters an.

kubectl cluster-info

kubectl config get-contexts — Listet alle verfügbaren Kontexte (Cluster) auf.

kubectl config get-contexts

kubectl config current-context — Zeigt den aktuell aktiven Kontext.

kubectl config current-context

kubectl config use-context <name> — Wechselt zu einem anderen Kontext (Cluster).

kubectl config use-context production

kubectl config set-context --current --namespace=<ns> — Setzt den Standard-Namespace für den aktuellen Kontext.

kubectl config set-context --current --namespace=my-app

kubectl version — Zeigt die Client- und Server-Version.

kubectl version -o yaml

kubectl api-resources — Listet alle verfügbaren Ressourcentypen und ihre Kurznamen auf.

kubectl api-resources

Ressourcen anzeigen

kubectl get <resource> — Listet Ressourcen eines Typs im aktuellen Namespace auf.

kubectl get pods

kubectl get <resource> -A — Listet Ressourcen über alle Namespaces hinweg auf.

kubectl get pods -A

kubectl get <resource> -n <namespace> — Listet Ressourcen in einem bestimmten Namespace auf.

kubectl get pods -n production

kubectl get <resource> -o wide — Zeigt zusätzliche Details wie Node und IP.

kubectl get pods -o wide

kubectl get <resource> -o yaml — Gibt die Ressourcen-Definition als YAML aus.

kubectl get deployment nginx -o yaml

kubectl get <resource> -o json — Gibt die Ressourcen-Definition als JSON aus.

kubectl get pod my-pod -o json

kubectl get all — Listet alle gängigen Ressourcen auf (Pods, Services, Deployments usw.).

kubectl get all -n my-app

kubectl describe <resource> <name> — Zeigt detaillierte Informationen zu einer Ressource inklusive Events.

kubectl describe pod my-pod

kubectl get <resource> -l <label>=<value> — Filtert Ressourcen über einen Label-Selektor.

kubectl get pods -l app=nginx

kubectl get <resource> --sort-by=<field> — Sortiert die Ausgabe nach einem bestimmten Feld.

kubectl get pods --sort-by=.metadata.creationTimestamp

Erstellen & Anwenden

kubectl apply -f <file> — Erstellt oder aktualisiert Ressourcen aus einer YAML-/JSON-Datei.

kubectl apply -f deployment.yaml

kubectl apply -f <directory> — Wendet alle Ressourcen-Dateien in einem Verzeichnis an.

kubectl apply -f ./k8s/

kubectl apply -f <url> — Wendet eine Ressource von einer Remote-URL an.

kubectl apply -f https://raw.githubusercontent.com/org/repo/main/deploy.yaml

kubectl create deployment <name> --image=<image> — Erstellt ein Deployment imperativ.

kubectl create deployment nginx --image=nginx:alpine

kubectl create namespace <name> — Erstellt einen neuen Namespace.

kubectl create namespace staging

kubectl run <name> --image=<image> — Erstellt und startet einen Pod.

kubectl run debug --image=busybox --rm -it -- sh

kubectl expose deployment <name> --port=<port> --type=<type> — Erstellt einen Service für ein Deployment.

kubectl expose deployment nginx --port=80 --type=LoadBalancer

Bearbeiten & Aktualisieren

kubectl edit <resource> <name> — Bearbeitet eine Ressource im Standard-Editor.

kubectl edit deployment nginx

kubectl set image deployment/<name> <container>=<image> — Aktualisiert das Image eines Containers in einem Deployment.

kubectl set image deployment/nginx nginx=nginx:1.25

kubectl scale deployment <name> --replicas=<n> — Skaliert ein Deployment auf n Replicas.

kubectl scale deployment nginx --replicas=5

kubectl rollout status deployment/<name> — Beobachtet den Rollout-Status eines Deployments.

kubectl rollout status deployment/nginx

kubectl rollout history deployment/<name> — Zeigt die Rollout-Historie (Revisionen) eines Deployments.

kubectl rollout history deployment/nginx

kubectl rollout undo deployment/<name> — Setzt ein Deployment auf die vorherige Revision zurück.

kubectl rollout undo deployment/nginx

kubectl rollout undo deployment/<name> --to-revision=<n> — Setzt auf eine bestimmte Revisionsnummer zurück.

kubectl rollout undo deployment/nginx --to-revision=3

kubectl label <resource> <name> <key>=<value> — Fügt ein Label zu einer Ressource hinzu oder aktualisiert es.

kubectl label pod my-pod env=production

kubectl annotate <resource> <name> <key>=<value> — Fügt eine Annotation zu einer Ressource hinzu oder aktualisiert sie.

kubectl annotate deployment nginx description='Web server'

kubectl patch <resource> <name> -p '<json>' — Aktualisiert einzelne Felder einer Ressource über einen JSON-Patch.

kubectl patch deployment nginx -p '{"spec":{"replicas":3}}'

Ressourcen löschen

kubectl delete <resource> <name> — Löscht eine bestimmte Ressource anhand ihres Namens.

kubectl delete pod my-pod

kubectl delete -f <file> — Löscht alle in einer Datei definierten Ressourcen.

kubectl delete -f deployment.yaml

kubectl delete <resource> -l <label>=<value> — Löscht alle Ressourcen, die einem Label-Selektor entsprechen.

kubectl delete pods -l app=old-version

kubectl delete namespace <name> — Löscht einen Namespace und alle darin enthaltenen Ressourcen.

kubectl delete namespace staging

kubectl delete pod <name> --grace-period=0 --force — Erzwingt das sofortige Löschen eines hängenden Pods.

kubectl delete pod stuck-pod --grace-period=0 --force

Logs & Debugging

kubectl logs <pod> — Gibt die Logs eines Pods aus.

kubectl logs my-pod

kubectl logs -f <pod> — Streamt (folgt) die Logs eines Pods in Echtzeit.

kubectl logs -f my-pod

kubectl logs <pod> -c <container> — Gibt die Logs eines bestimmten Containers in einem Multi-Container-Pod aus.

kubectl logs my-pod -c sidecar

kubectl logs <pod> --previous — Gibt die Logs der vorherigen Container-Instanz aus (nach einem Absturz).

kubectl logs my-pod --previous

kubectl logs -l <label>=<value> — Gibt die Logs aller Pods aus, die einem Label entsprechen.

kubectl logs -l app=nginx --all-containers

kubectl logs <pod> --tail=<n> — Zeigt nur die letzten n Log-Zeilen.

kubectl logs my-pod --tail=50

kubectl logs <pod> --since=<duration> — Zeigt Logs aus dem letzten Zeitraum (z. B. 1h, 30m).

kubectl logs my-pod --since=1h

kubectl exec -it <pod> -- <command> — Führt einen interaktiven Befehl in einem laufenden Pod aus.

kubectl exec -it my-pod -- /bin/bash

kubectl exec <pod> -- <command> — Führt einen nicht-interaktiven Befehl in einem Pod aus.

kubectl exec my-pod -- cat /etc/config.yaml

kubectl top pods — Zeigt CPU- und Speicherauslastung der Pods (erfordert metrics-server).

kubectl top pods -n my-app

kubectl top nodes — Zeigt CPU- und Speicherauslastung der Nodes.

kubectl top nodes

kubectl get events --sort-by=.lastTimestamp — Zeigt Cluster-Events nach Zeit sortiert (nützlich zum Debuggen).

kubectl get events --sort-by=.lastTimestamp -n my-app

Port-Forwarding & Proxy

kubectl port-forward <pod> <local>:<remote> — Leitet einen lokalen Port an einen Port eines Pods weiter.

kubectl port-forward my-pod 8080:80

kubectl port-forward svc/<service> <local>:<remote> — Leitet einen lokalen Port an einen Service weiter.

kubectl port-forward svc/my-service 3000:80

kubectl proxy — Startet einen Proxy zum Kubernetes-API-Server auf localhost:8001.

kubectl proxy

Dateien kopieren

kubectl cp <pod>:<path> <local_path> — Kopiert eine Datei aus einem Pod in das lokale Dateisystem.

kubectl cp my-pod:/var/log/app.log ./app.log

kubectl cp <local_path> <pod>:<path> — Kopiert eine lokale Datei in einen Pod.

kubectl cp ./config.yaml my-pod:/app/config.yaml

kubectl cp <ns>/<pod>:<path> <local_path> — Kopiert aus einem Pod in einem bestimmten Namespace.

kubectl cp production/my-pod:/data/export.csv ./export.csv

Secrets & ConfigMaps

kubectl create secret generic <name> --from-literal=<key>=<value> — Erstellt ein Secret aus literalen Schlüssel-Wert-Paaren.

kubectl create secret generic db-creds --from-literal=username=admin --from-literal=password=s3cret

kubectl create secret generic <name> --from-file=<path> — Erstellt ein Secret aus einer Datei.

kubectl create secret generic tls-cert --from-file=cert.pem --from-file=key.pem

kubectl get secret <name> -o jsonpath='{.data.<key>}' | base64 -d — Dekodiert und zeigt einen Secret-Wert an.

kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d

kubectl create configmap <name> --from-literal=<key>=<value> — Erstellt eine ConfigMap aus literalen Schlüssel-Wert-Paaren.

kubectl create configmap app-config --from-literal=APP_ENV=production

kubectl create configmap <name> --from-file=<path> — Erstellt eine ConfigMap aus einer Datei oder einem Verzeichnis.

kubectl create configmap nginx-conf --from-file=nginx.conf

Fazit

kubectl ist die Drehscheibe für den Cluster-Alltag: Vom schnellen get bis zum gezielten rollout undo erledigst du fast jede Aufgabe ohne Dashboard. Wer regelmäßig damit arbeitet, legt sich einen Alias (k für kubectl), die Shell-Completion und --dry-run=client -o yaml für sauberes GitOps zu – so wird aus Einzelbefehlen ein reproduzierbarer Workflow.

Verwandte Kommandos

  • ddev – lokale PHP-Entwicklungsumgebungen mit Docker
  • docker – einzelne Container bauen und betreiben
  • docker-compose – Multi-Container-Anwendungen lokal orchestrieren