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-infokubectl config get-contexts — Listet alle verfügbaren Kontexte (Cluster) auf.
kubectl config get-contextskubectl config current-context — Zeigt den aktuell aktiven Kontext.
kubectl config current-contextkubectl config use-context <name> — Wechselt zu einem anderen Kontext (Cluster).
kubectl config use-context productionkubectl config set-context --current --namespace=<ns> — Setzt den Standard-Namespace für den aktuellen Kontext.
kubectl config set-context --current --namespace=my-appkubectl version — Zeigt die Client- und Server-Version.
kubectl version -o yamlkubectl api-resources — Listet alle verfügbaren Ressourcentypen und ihre Kurznamen auf.
kubectl api-resourcesRessourcen anzeigen
kubectl get <resource> — Listet Ressourcen eines Typs im aktuellen Namespace auf.
kubectl get podskubectl get <resource> -A — Listet Ressourcen über alle Namespaces hinweg auf.
kubectl get pods -Akubectl get <resource> -n <namespace> — Listet Ressourcen in einem bestimmten Namespace auf.
kubectl get pods -n productionkubectl get <resource> -o wide — Zeigt zusätzliche Details wie Node und IP.
kubectl get pods -o widekubectl get <resource> -o yaml — Gibt die Ressourcen-Definition als YAML aus.
kubectl get deployment nginx -o yamlkubectl get <resource> -o json — Gibt die Ressourcen-Definition als JSON aus.
kubectl get pod my-pod -o jsonkubectl get all — Listet alle gängigen Ressourcen auf (Pods, Services, Deployments usw.).
kubectl get all -n my-appkubectl describe <resource> <name> — Zeigt detaillierte Informationen zu einer Ressource inklusive Events.
kubectl describe pod my-podkubectl get <resource> -l <label>=<value> — Filtert Ressourcen über einen Label-Selektor.
kubectl get pods -l app=nginxkubectl get <resource> --sort-by=<field> — Sortiert die Ausgabe nach einem bestimmten Feld.
kubectl get pods --sort-by=.metadata.creationTimestampErstellen & Anwenden
kubectl apply -f <file> — Erstellt oder aktualisiert Ressourcen aus einer YAML-/JSON-Datei.
kubectl apply -f deployment.yamlkubectl 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.yamlkubectl create deployment <name> --image=<image> — Erstellt ein Deployment imperativ.
kubectl create deployment nginx --image=nginx:alpinekubectl create namespace <name> — Erstellt einen neuen Namespace.
kubectl create namespace stagingkubectl run <name> --image=<image> — Erstellt und startet einen Pod.
kubectl run debug --image=busybox --rm -it -- shkubectl expose deployment <name> --port=<port> --type=<type> — Erstellt einen Service für ein Deployment.
kubectl expose deployment nginx --port=80 --type=LoadBalancerBearbeiten & Aktualisieren
kubectl edit <resource> <name> — Bearbeitet eine Ressource im Standard-Editor.
kubectl edit deployment nginxkubectl set image deployment/<name> <container>=<image> — Aktualisiert das Image eines Containers in einem Deployment.
kubectl set image deployment/nginx nginx=nginx:1.25kubectl scale deployment <name> --replicas=<n> — Skaliert ein Deployment auf n Replicas.
kubectl scale deployment nginx --replicas=5kubectl rollout status deployment/<name> — Beobachtet den Rollout-Status eines Deployments.
kubectl rollout status deployment/nginxkubectl rollout history deployment/<name> — Zeigt die Rollout-Historie (Revisionen) eines Deployments.
kubectl rollout history deployment/nginxkubectl rollout undo deployment/<name> — Setzt ein Deployment auf die vorherige Revision zurück.
kubectl rollout undo deployment/nginxkubectl rollout undo deployment/<name> --to-revision=<n> — Setzt auf eine bestimmte Revisionsnummer zurück.
kubectl rollout undo deployment/nginx --to-revision=3kubectl label <resource> <name> <key>=<value> — Fügt ein Label zu einer Ressource hinzu oder aktualisiert es.
kubectl label pod my-pod env=productionkubectl 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-podkubectl delete -f <file> — Löscht alle in einer Datei definierten Ressourcen.
kubectl delete -f deployment.yamlkubectl delete <resource> -l <label>=<value> — Löscht alle Ressourcen, die einem Label-Selektor entsprechen.
kubectl delete pods -l app=old-versionkubectl delete namespace <name> — Löscht einen Namespace und alle darin enthaltenen Ressourcen.
kubectl delete namespace stagingkubectl delete pod <name> --grace-period=0 --force — Erzwingt das sofortige Löschen eines hängenden Pods.
kubectl delete pod stuck-pod --grace-period=0 --forceLogs & Debugging
kubectl logs <pod> — Gibt die Logs eines Pods aus.
kubectl logs my-podkubectl logs -f <pod> — Streamt (folgt) die Logs eines Pods in Echtzeit.
kubectl logs -f my-podkubectl logs <pod> -c <container> — Gibt die Logs eines bestimmten Containers in einem Multi-Container-Pod aus.
kubectl logs my-pod -c sidecarkubectl logs <pod> --previous — Gibt die Logs der vorherigen Container-Instanz aus (nach einem Absturz).
kubectl logs my-pod --previouskubectl logs -l <label>=<value> — Gibt die Logs aller Pods aus, die einem Label entsprechen.
kubectl logs -l app=nginx --all-containerskubectl logs <pod> --tail=<n> — Zeigt nur die letzten n Log-Zeilen.
kubectl logs my-pod --tail=50kubectl logs <pod> --since=<duration> — Zeigt Logs aus dem letzten Zeitraum (z. B. 1h, 30m).
kubectl logs my-pod --since=1hkubectl exec -it <pod> -- <command> — Führt einen interaktiven Befehl in einem laufenden Pod aus.
kubectl exec -it my-pod -- /bin/bashkubectl exec <pod> -- <command> — Führt einen nicht-interaktiven Befehl in einem Pod aus.
kubectl exec my-pod -- cat /etc/config.yamlkubectl top pods — Zeigt CPU- und Speicherauslastung der Pods (erfordert metrics-server).
kubectl top pods -n my-appkubectl top nodes — Zeigt CPU- und Speicherauslastung der Nodes.
kubectl top nodeskubectl get events --sort-by=.lastTimestamp — Zeigt Cluster-Events nach Zeit sortiert (nützlich zum Debuggen).
kubectl get events --sort-by=.lastTimestamp -n my-appPort-Forwarding & Proxy
kubectl port-forward <pod> <local>:<remote> — Leitet einen lokalen Port an einen Port eines Pods weiter.
kubectl port-forward my-pod 8080:80kubectl port-forward svc/<service> <local>:<remote> — Leitet einen lokalen Port an einen Service weiter.
kubectl port-forward svc/my-service 3000:80kubectl proxy — Startet einen Proxy zum Kubernetes-API-Server auf localhost:8001.
kubectl proxyDateien 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.logkubectl cp <local_path> <pod>:<path> — Kopiert eine lokale Datei in einen Pod.
kubectl cp ./config.yaml my-pod:/app/config.yamlkubectl cp <ns>/<pod>:<path> <local_path> — Kopiert aus einem Pod in einem bestimmten Namespace.
kubectl cp production/my-pod:/data/export.csv ./export.csvSecrets & 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=s3cretkubectl 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.pemkubectl 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 -dkubectl 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=productionkubectl 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.
Weiterführende Links
- kubectl – offizielle Referenz – alle Befehle und Optionen im Detail (englisch)
- kubectl Quick Reference – kompakte Befehlsübersicht (englisch)
- Kubernetes – Wikipedia – Hintergrund und Architektur
Verwandte Kommandos
- ddev – lokale PHP-Entwicklungsumgebungen mit Docker
- docker – einzelne Container bauen und betreiben
- docker-compose – Multi-Container-Anwendungen lokal orchestrieren