kubectl commands
kubectl is the official Kubernetes client and the main tool for working with your cluster. This page summarises the most commonly used commands.
Connection and Context
Once you have downloaded the configuration file, use --kubeconfig to specify its location:
kubectl --kubeconfig=/<pfad-zum-verzeichnis>/<cluster-name>-kubeconfig.yaml get nodes
To avoid having to specify the path every time the programme is run, set the environment variable KUBECONFIG:
export KUBECONFIG=/<pfad-zum-verzeichnis>/<cluster-name>-kubeconfig.yaml
Check connection
The following commands confirm that the connection to the cluster is working:
| Command | Purpose |
|---|---|
kubectl config get-contexts | Lists the cluster name, users and namespaces |
kubectl cluster-info | Displays the addresses of the control plane and cluster services |
kubectl version | Displays the client and server versions |
kubectl get nodes | Lists all nodes in the cluster |
kubectl help | Displays the available commands |
View resources
# Nodes mit zusätzlichen Details
kubectl get nodes -o wide
# Pods im aktuellen Namespace
kubectl get pods
# Pods über alle Namespaces hinweg
kubectl get pods --all-namespaces
# Fortlaufende Aktualisierung der Anzeige
kubectl get pods --watch
# Deployments, Services und Ingresses
kubectl get deployments
kubectl get services
kubectl get ingress
# Alle gängigen Ressourcen eines Namespace
kubectl get all -n <namespace>
Details and Troubleshooting
# Ausführliche Informationen inklusive Events
kubectl describe pod <pod-name>
kubectl describe node <node-name>
# Logs eines Pods
kubectl logs <pod-name>
# Logs fortlaufend mitlesen
kubectl logs -f <pod-name>
# Logs eines bestimmten Containers in einem Pod
kubectl logs <pod-name> -c <container-name>
# Logs der vorherigen, abgestürzten Instanz
kubectl logs <pod-name> --previous
# Events des Clusters nach Zeit sortiert
kubectl get events --sort-by=.metadata.creationTimestamp
Switch to a container
kubectl exec -it <pod-name> -- /bin/sh
Forward a local port
kubectl port-forward <pod-name> 8080:80
kubectl port-forward service/<service-name> 8080:80
Managing workloads
# Manifest anwenden
kubectl apply -f manifest.yaml
# Alle Manifeste eines Verzeichnisses anwenden
kubectl apply -f ./manifeste/
# Ressource löschen
kubectl delete -f manifest.yaml
kubectl delete pod <pod-name>
# Deployment skalieren
kubectl scale deployment <deployment-name> --replicas=3
# Neustart eines Deployments erzwingen
kubectl rollout restart deployment <deployment-name>
# Status eines Rollouts verfolgen
kubectl rollout status deployment <deployment-name>
# Rollout zurücknehmen
kubectl rollout undo deployment <deployment-name>
Capacity utilisation
kubectl top nodes
kubectl top pods --all-namespaces
kubectl top pods --all-namespaces --sort-by=cpu
Namespaces
# Namespaces auflisten
kubectl get namespaces
# Namespace anlegen
kubectl create namespace <namespace>
# Standard-Namespace für den aktuellen Kontext setzen
kubectl config set-context --current --namespace=<namespace>
Preparing nodes for maintenance
# Keine neuen Pods mehr auf dem Node platzieren
kubectl cordon <node-name>
# Vorhandene Pods verlagern
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
# Node wieder für Pods freigeben
kubectl uncordon <node-name>
Storage
kubectl get pvc
kubectl get pv
kubectl get storageclass
kubectl get storageclass lists the storage classes available for persistent volumes (CSI/PVC). See Storage Features.
Version compatibility
kubectl supports a minor version difference of up to one version in either direction from the cluster version. For a cluster with version 1.33, the kubectl versions 1.32 to 1.34 are therefore suitable.
You can compare the client and server versions using:
kubectl version
Further reading
You can find the full reference in the official kubectl documentation.