KUBECTL Commands Cheatsheet — Essential Reference
kubectl is the command-line tool for controlling Kubernetes clusters. It allows you to run commands against Kubernetes clusters to deploy applications, inspect and manage cluster resources, and view logs.
Master KUBECTL with Top Reference Books
Level up your DevOps skills. Find the best guide books and cheatsheet references for your engineering stack.
Commands
| Command | Description | Example |
|---|---|---|
kubectl get |
Lists resources (pods, deployments, services, etc.) in the cluster. | kubectl get pods -n default |
kubectl describe |
Shows detailed information about a specific resource. | kubectl describe pod my-pod-xyz123 |
kubectl apply -f |
Applies a configuration to a resource by filename or stdin. | kubectl apply -f my-deployment.yaml |
kubectl delete |
Deletes resources by filename, stdin, resource and name, or by selectors. | kubectl delete deployment my-app |
kubectl logs |
Prints the logs for a container in a pod. | kubectl logs my-pod-xyz123 -c my-container |
kubectl exec |
Execute a command in a container. | kubectl exec -it my-pod-xyz123 -- /bin/bash |
kubectl port-forward |
Forward one or more local ports to a pod. | kubectl port-forward service/my-service 8080:80 |
kubectl edit |
Edit a resource in your default editor. | kubectl edit deployment my-app |
Pro Tips
- Use `kubectl get all` to see a quick overview of common resources in a namespace.
- Alias `kubectl` to `k` for faster typing: `alias k=kubectl`.
- Use `--watch` or `-w` with `kubectl get` to continuously observe changes to resources.
- Explore output formats with `-o json`, `-o yaml`, or `-o wide` for more detailed or specific views.
- Leverage `kubectl explain <resource_type>` to understand the schema and fields of a resource.
Frequently Asked Questions
What is kubectl used for?
kubectl is the primary command-line interface for interacting with Kubernetes clusters. It allows users to deploy and manage applications, inspect and troubleshoot cluster resources, and perform various administrative tasks.
How do I install kubectl?
kubectl can be installed in several ways: via package managers (like `apt` on Debian/Ubuntu, `brew` on macOS), by downloading the binary directly, or as part of a Kubernetes distribution like Docker Desktop or Minikube. Refer to the official Kubernetes documentation for detailed installation instructions for your operating system.