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

Command Comment
kubectl cluster-info displays cluster endpoints and basic information
kubectl cluster-info dump displays cluster endpoints and very detailed  information
kubectl config get-contexts lists all defined contexts in the kubeconfig file
kubectl config current-context shows the currently active context
kubectl config use-context switches the active context to the specified one
kubectl config view displays the full configuration from the kubeconfig file
kubectl version  prints the version information for both the kubectl client and the Kubernetes server

Resource Management

command comment
kubectl apply -f creates or updates resources from a configuration file
kubectl delete -f deletes resources defined in a configuration file
kubectl get all lists all resources in the current namespace
kubectl get pods list the pods in the current namespace
kubectl get pods -A list all pods in all namespaces
kubectl get pods -A -o wide list all pods in all namespaces with detailed output including host
kubectl get services list the services in the current namespace
kubectl get services -A  list the services in all namespaces
kubectl get deployments list the deployments in the current namespace
kubectl get deployments -A list the deployments in all namespaces
kubectl get ds list the daemon sets in the current namespace
kubectl get ds -A list the daemon sets  in all namespaces
kubectl get statefulsets list the stateful sets in the current namespace
kubectl get statefulsets -A list the stateful sets in all namespaces
kubectl get secrets list the secrets in the current namespace
kubectl get secrets -A list the secrets in all namespaces
kubectl get nodes list all nodes
kubectl get configmaps list the config maps in the current namespace
kubectl get configmaps -A list the config maps in all namespaces
kubectl get -l filters resources by a specific label
kubectl get –all-namespaceskubectl get -A lists resources in all namespaces
kubectl get -n lists resources in a specific namespace
kubectl get –sort-by= 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

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

Deployment Management

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

Service Management

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

Node Management

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

Debugging and Monitoring

command comment
kubectl get events lists cluster events, useful for troubleshooting
kubectl get componentstatuses checks the status of cluster components
kubectl port-forward 8080:80 forwards a local port to a pod’s port for local access
kubectl debug -it –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.

Author Profile

12ww1160DevOps engineer & architect

Leave a Reply

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

11 + seventeen =