Kubectl Cheat Sheet Commands & Examples

Part 3 of 4 in the Learning Kubernetes series
- Kubernetes Basics: Kubernetes Tutorial for Beginners
- Kubernetes Network Policy: A Beginner’s Guide
- Kubectl Cheat Sheet Commands & Examples
- Guide to the Kubernetes Load Balancer Service
Things can be overwhelming when you are new to Kubernetes. You’d think that understanding all the different objects Kubernetes has to offer would be the end of it. But when it comes to actually interacting with a cluster, things get even more confusing for beginners. This is where a kubectl cheat sheet comes in handy.
Introduction to kubectl
The most common tool to interact with a Kubernetes cluster is kubectl. It lets you work with the objects you create, view logs, and much more. But as anyone who’s just starting to learn Kubernetes will agree, there are too many kubectl commands to keep track of.
When I was preparing for the Certified Kubernetes Administrator exam, I kept a diary of all the commands I came across and reviewed it every day. This cheat sheet is my attempt to save you that work: it goes over the commands you’ll find yourself using most often.
If you’re completely new to Kubernetes, you might want to read our Kubernetes for Beginners post first.
Cluster Info and Configuration
Often you need information, not about the application but about the cluster itself. These commands help with that:
kubectl version: Shows the version of Kubernetes of the client and the server.kubectl cluster-info: Displays the endpoint of the Kubernetes control plane.kubectl config get-contexts: Lists all the contexts present in the current kubeconfig.kubectl config use-context <context name>: Sets the default context for all the kubectl commands you’ll run.
Namespaces
Namespaces play a very important role in clusters. They organize and isolate the different resources your application might need. By default, you’re in the default namespace, and the resources Kubernetes needs to work are in the kube-system namespace. These are the commands you’ll use most often with namespaces:
kubectl get namespaces: Lists all the namespaces of the cluster.kubectl create namespace <namespace name>: Creates a new namespace. It doesn’t make it the default one: the resources you create still go to the current namespace unless you pass the--namespaceflag.kubectl config set-context --current --namespace=<namespace name>: Changes the default namespace for all the commands you’ll run, while keeping the same context. To switch back, the default namespace is calleddefault.kubectl delete namespace <namespace name>: Deletes the namespace and all the resources in it! The deletion is asynchronous, so the namespace may stay in theTerminatingstate for a while before it’s completely gone.
Pods, Deployments, Secrets, etc
There is a set of commands you’ll use with most Kubernetes resources. Replace X with the type of resource (pod, deployment, secret, and so on):
kubectl get X: Lists all the resources of that type in the current namespace. Add-o widefor more columns, or-o yamlfor the full definition of a resource.kubectl create X <name> <other properties>: Creates a resource of that type in the current namespace with the given name. Most types need some additional flags.kubectl delete X <name>: Deletes the resource with the given name in the current namespace.kubectl describe X <name>: Shows the details and the events of the resource. You’ll use this one a lot when debugging.kubectl edit X <name>: Opens the YAML of the resource in your editor and applies your changes when you save it. Copy your changes to your own YAML files too: an edit doesn’t persist once the resource is deleted and created again.
Nodes
Nodes are the machines that make up your cluster, so you’ll often find yourself interacting with them. These commands are worth keeping a note of:
kubectl get nodes: Lists all the nodes, with their role (control-plane, or<none>for the worker nodes) and the version of Kubernetes they run.kubectl top node <node name>: Shows the CPU and memory usage of the node.kubectl top poddoes the same for pods. Both need the metrics server installed in the cluster.kubectl cordon <node name>andkubectl uncordon <node name>: Cordoning a node marks it as unschedulable, so no new pods are placed on it. Use it to isolate a node, for example to upgrade or debug it. Uncordoning makes it schedulable again.kubectl drain <node name> --ignore-daemonsets: You’ll use this command for maintenance tasks. Draining a node cordons it and evicts its pods. The pods managed by a Deployment, a StatefulSet, or another controller are recreated on the remaining nodes. The pods of a DaemonSet stay on the node, which is why the command needs--ignore-daemonsets.
Other Common Kubectl Commands
These commands don’t fit into any single category, but you’ll use them a lot:
kubectl apply -f <filename.yaml>: Creates the resources defined in a YAML file. If a resource already exists, it updates it with the configuration of the file.kubectl delete -f <filename.yaml>: Deletes all the resources defined in a YAML file.kubectl port-forward <pod name> <local port>:<remote port>: Exposing your application with a service isn’t always convenient, especially when you’re debugging. This command forwards a local port to a port of the pod. It also works with a service:kubectl port-forward svc/<service name> <local port>:<remote port>.kubectl logs <pod name>: Prints the logs of the container running in the pod. If the pod runs more than one container, pass--containerto choose one. Add-fto follow the logs as they’re written, and--previousto see the logs of the last container that crashed and restarted.kubectl exec -it <pod name> -- sh: Opens a shell in the container of the pod, to inspect it from inside.kubectl get events --sort-by=.metadata.creationTimestamp: Lists the recent events of the current namespace, in order. It’s a good first step when a pod doesn’t start.kubectl rollout status deployment/<name>: Waits until the latest rollout of a Deployment finishes, and shows its progress.kubectl rollout restart deployment/<name>restarts all the pods of a Deployment, one by one.
Who Are These Kubectl Commands For?
These commands are what you need to inspect and debug what runs in a cluster. Getting the application running is a different matter: it shouldn’t take a list of kubectl commands, for a developer or for a coding agent. A good platform gives them their own copy of the application with a single command, and leaves kubectl for when they need to look under the hood.
