Kubectl cheat sheet

I am dealing with Kubernetes on a daily basis. Although I am using the very capable headlamp as GUI tool, sometimes there are moments when it comes to using kubectl after all. Since I am not using it every day, I keep forgetting commands. so therefore I have written down the most important ones below for reference.

Cluster and Context Management

CommandComment
kubectl cluster-infodisplays cluster endpoints and basic information
kubectl cluster-info dumpdisplays cluster endpoints and very detailed  information
kubectl config get-contextslists all defined contexts in the kubeconfig file
kubectl config current-contextshows the currently active context
kubectl config use-context <context-name>switches the active context to the specified one
kubectl config viewdisplays the full configuration from the kubeconfig file
kubectl version prints the version information for both the kubectl client and the Kubernetes server

Resource Management

commandcomment
kubectl apply -f <file.yaml>creates or updates resources from a configuration file
kubectl delete -f <file.yaml>deletes resources defined in a configuration file
kubectl get alllists all resources in the current namespace
kubectl get podslist the pods in the current namespace
kubectl get pods -Alist all pods in all namespaces
kubectl get pods -A -o widelist all pods in all namespaces with detailed output including host
kubectl get serviceslist the services in the current namespace
kubectl get services -A list the services in all namespaces
kubectl get deploymentslist the deployments in the current namespace
kubectl get deployments -Alist the deployments in all namespaces
kubectl get dslist the daemon sets in the current namespace
kubectl get ds -Alist the daemon sets  in all namespaces
kubectl get statefulsetslist the stateful sets in the current namespace
kubectl get statefulsets -Alist the stateful sets in all namespaces
kubectl get secretslist the secrets in the current namespace
kubectl get secrets -Alist the secrets in all namespaces
kubectl get nodeslist all nodes
kubectl get configmapslist the config maps in the current namespace
kubectl get configmaps -Alist the config maps in all namespaces
kubectl get <resource> -l <label>filters resources by a specific label
kubectl get <resource> –all-namespaceskubectl get <resource> -Alists resources in all namespaces
kubectl get <resource> -n <namespace>lists resources in a specific namespace
kubectl get <resource> –sort-by=<field>sorts the output by a specified field, such as `status.containerStatuses.restartCount`

Seen my other posts about Kubernetes?
create-a-configuration-backup-with-kubectl
kubernetes-quickie-extract-the-ca-bundle

Pod Management

commandcomment
kubectl get podslists all pods in the current namespace
kubectl get pods -n <namespace>lists pods in the specified namespace
ubectl describe pod <pod-name> -n <namespace>describes specified pod in the specified namespace including status, events, and container details
kubectl logs <pod-name> streams logs from a pod’s container 
kubectl logs -f <pod-name> follows the logs in real-time
kubectl logs -p <pod-name>retrieves logs from a previous container instance
kubectl logs -l <label>gets logs for pods matching a label selector
kubectl exec -it <pod-name> — /bin/shstarts an interactive shell session inside a pod
kubectl exec <pod-name> -c <container-name> — <command>executes a command in a specific container within a pod
kubectl run <pod-name> –image=<image>creates and runs a pod with the specified image
kubectl delete pod <pod-name>deletes a specific pod
kubectl delete pod <pod-name> –force –grace-period=0deletes a specific pod forcefully and without grace from the API server

Deployment Management

commandcomment
kubectl create deployment <name> –image=<image>creates a new deployment using the specified image
kubectl get deploymentslists all deployments in the current namespace
kubectl get deployments -n <namespace>lists all deployments in the specified namespace
kubectl describe deployment <name>provides detailed information about a deployment
kubectl scale deployment <name> –replicas=<number>adjusts the number of replicas for a deployment
kubectl set image deployment/<name> <container>=<image>updates the container image in a deployment
kubectl rollout restart deployment/<name>`rolling restarts a deployment 
kubectl rollout status deployment/<name>monitors the rollout status of a deployment
kubectl rollout pause deployment/<name>pauses a deployment rollout
kubectl rollout resume deployment/<name>resumes a paused rollout
kubectl rollout undo deployment/<name>reverts the deployment to the previous revision
kubectl rollout undo deployment/<name> –to-revision=<revision>rolls back to a specific revision
kubectl delete deployment <name>deletes a deployment and its managed pods

Service Management

commandcomment
kubectl get serviceslists all services in the current namespace
kubectl get services -Alists all services in all namespaces
kubectl get services -n <namespace>lists all services in specified namespace
kubectl expose deployment <name> –port=<port>creates a service that exposes a deployment
kubectl create service <type> –tcp=<port>creates a new service of a specified type (e.g., ClusterIP, NodePort)
kubectl describe service <name>provides detailed information about a service
kubectl delete service <name>removes a service from the cluster
kubectl get endpoints <name>lists the endpoints (pod IPs) backing a service

Node Management

commandcomment
kubectl get nodeslists all nodes in the cluster
kubectl describe node <node-name>provides detailed node-level information, including resource pressure and taints
kubectl cordon <node-name>marks a node as unschedulable
kubectl uncordon <node-name>`marks a node as schedulable again
kubectl drain <node-name>prepares a node for maintenance by evicting all pods
kubectl label node <node-name> <key>=<value>adds or updates a label on a node
kubectl label node <node-name> <key>-removes a label from a node
kubectl taint node <node-name> <key>=<value>:<effect>applies a taint to a node
kubectl top node <node-name>displays resource usage metrics (CPU and memory) for a node

Debugging and Monitoring

commandcomment
kubectl get eventslists cluster events, useful for troubleshooting
kubectl get componentstatuseschecks the status of cluster components
kubectl port-forward <pod-name> 8080:80forwards a local port to a pod’s port for local access
kubectl debug <pod-name> -it –image=<image>enables debugging by launching a shell in a new container

Best Practices

– Use namespaces to isolate environments, teams, or applications

– Apply labels and annotations to resources for easier management and filtering

– Use declarative configurations (YAML files) with `kubectl apply` to define desired states

– Set resource requests and limits on containers to prevent overprovisioning or starvation 

In case I am losing this blog post, I have it written down in my wiki too for good measure.

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

17 − fifteen =