Collabnix Team Follow
The Collabnix Team is a diverse collective of Docker, Kubernetes, and IoT experts united by a passion for cloud-native technologies. With backgrounds spanning across DevOps, platform engineering, cloud architecture, and container orchestration, our contributors bring together decades of combined experience from various industries and technical domains.
Kubectl Quick Reference 2025
2nd March 2025
3 min read
Kubectl is the command-line interface for interacting with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, and view logs. This cheatsheet provides a comprehensive reference of commonly used kubectl commands, organized by operation type.
Whether you’re new to Kubernetes or an experienced administrator, this guide will help you quickly find the right command for your task. Commands are presented with their descriptions and practical examples to make your Kubernetes workflow more efficient.
Cluster Management Commands
Command Description Example kubectl cluster-infoDisplay cluster info kubectl cluster-infokubectl versionShow kubectl and cluster version kubectl versionkubectl config viewShow kubeconfig settings kubectl config viewkubectl config current-contextDisplay current context kubectl config current-contextkubectl config use-context <context>Switch to another context kubectl config use-context minikubekubectl config set-contextSet a context parameter kubectl config set-context --current --namespace=myappkubectl api-resourcesList supported API resources kubectl api-resources
Namespace Operations
Command Description Example kubectl get namespacesList all namespaces kubectl get nskubectl create namespaceCreate a namespace kubectl create ns app-devkubectl delete namespaceDelete a namespace kubectl delete ns app-devkubectl config set-context --current --namespace=<ns>Set default namespace kubectl config set-context --current --namespace=app-dev
Pod Operations
Command Description Example kubectl get podsList all pods in current namespace kubectl get podskubectl get pods --all-namespacesList pods in all namespaces kubectl get pods -Akubectl get pods -o wideList pods with more details kubectl get pods -o widekubectl describe pod <pod-name>Show detailed pod information kubectl describe pod nginx-podkubectl run <name> --image=<image>Create and run a pod kubectl run nginx --image=nginxkubectl delete pod <pod-name>Delete a pod kubectl delete pod nginx-podkubectl logs <pod-name>Get pod logs kubectl logs nginx-podkubectl logs -f <pod-name>Stream pod logs kubectl logs -f nginx-podkubectl logs <pod-name> -c <container>Get container logs from multi-container pod kubectl logs webapp -c auth-servicekubectl exec -it <pod-name> -- <command>Execute command in pod kubectl exec -it nginx-pod -- /bin/bashkubectl port-forward <pod-name> <local-port>:<pod-port>Forward pod port to local kubectl port-forward nginx-pod 8080:80
Deployment Operations
Command Description Example kubectl get deploymentsList all deployments kubectl get deploykubectl describe deployment <name>Show deployment details kubectl describe deploy nginx-deploykubectl create deployment <name> --image=<image>Create a deployment kubectl create deploy nginx --image=nginxkubectl scale deployment <name> --replicas=<count>Scale a deployment kubectl scale deploy nginx --replicas=5kubectl rollout status deployment <name>Check rollout status kubectl rollout status deploy nginxkubectl rollout history deployment <name>View rollout history kubectl rollout history deploy nginxkubectl rollout undo deployment <name>Rollback deployment kubectl rollout undo deploy nginxkubectl rollout restart deployment <name>Restart deployment (for image refresh) kubectl rollout restart deploy nginxkubectl set image deployment/<name> <container>=<image>Update container image kubectl set image deployment/nginx nginx=nginx:latestkubectl delete deployment <name>Delete a deployment kubectl delete deploy nginx
Service Operations
Command Description Example kubectl get servicesList all services kubectl get svckubectl describe service <name>Show service details kubectl describe svc nginx-servicekubectl expose deployment <name> --port=<port> --type=<type>Create a service for deployment kubectl expose deploy nginx --port=80 --type=LoadBalancerkubectl delete service <name>Delete a service kubectl delete svc nginx-service
ConfigMap and Secret Operations
Command Description Example kubectl get configmapsList all configmaps kubectl get cmkubectl get secretsList all secrets kubectl get secretskubectl create configmap <name> --from-file=<path>Create configmap from file kubectl create cm app-config --from-file=config.propertieskubectl create configmap <name> --from-literal=<key>=<value>Create configmap from literal kubectl create cm app-config --from-literal=api.url=https://api.example.comkubectl create secret generic <name> --from-literal=<key>=<value>Create secret from literal kubectl create secret generic db-creds --from-literal=password=s3cr3tkubectl describe configmap <name>Show configmap details kubectl describe cm app-configkubectl describe secret <name>Show secret details kubectl describe secret db-creds
Persistent Volume Operations
Command Description Example kubectl get persistentvolumesList persistent volumes kubectl get pvkubectl get persistentvolumeclaimsList persistent volume claims kubectl get pvckubectl describe persistentvolumeclaim <name>Show PVC details kubectl describe pvc mysql-pvckubectl delete persistentvolumeclaim <name>Delete a PVC kubectl delete pvc mysql-pvc
Node Operations
Command Description Example kubectl get nodesList all nodes kubectl get nodeskubectl describe node <node-name>Show node details kubectl describe node worker-1kubectl cordon <node-name>Mark node as unschedulable kubectl cordon worker-1kubectl uncordon <node-name>Mark node as schedulable kubectl uncordon worker-1kubectl drain <node-name>Drain node in preparation for maintenance kubectl drain worker-1 --ignore-daemonsetskubectl taint nodes <node-name> <key>=<value>:<effect>Add a taint to node kubectl taint nodes worker-1 gpu=true:NoSchedule
Resource Management
Command Description Example kubectl top nodesShow CPU/Memory usage for nodes kubectl top nodeskubectl top podsShow CPU/Memory usage for pods kubectl top podskubectl get eventsShow events in the cluster kubectl get eventskubectl get allShow all resources kubectl get all
YAML Generation
Command Description Example kubectl create deployment <name> --image=<image> --dry-run=client -o yamlGenerate deployment YAML kubectl create deploy nginx --image=nginx --dry-run=client -o yaml > deploy.yamlkubectl run <name> --image=<image> --dry-run=client -o yamlGenerate pod YAML kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yamlkubectl expose deployment <name> --port=<port> --dry-run=client -o yamlGenerate service YAML kubectl expose deploy nginx --port=80 --dry-run=client -o yaml > svc.yaml
Apply and Diff
Command Description Example kubectl apply -f <file.yaml>Create/update resource from file kubectl apply -f deploy.yamlkubectl apply -f <directory>Create/update from all files in directory kubectl apply -f ./configskubectl diff -f <file.yaml>Show difference with live configuration kubectl diff -f deploy.yamlkubectl delete -f <file.yaml>Delete resources from file kubectl delete -f deploy.yaml
Context Switching
Command Description Example kubectl config get-contextsList all contexts kubectl config get-contextskubectl config current-contextShow current context kubectl config current-contextkubectl config use-context <context>Switch to context kubectl config use-context prod-clusterkubectl config rename-context <old> <new>Rename context kubectl config rename-context gke_proj_zone_name prod
Common Flags
Flag Description Example -n, --namespaceSpecify namespace kubectl get pods -n kube-system-A, --all-namespacesAll namespaces kubectl get pods -A-o, --outputOutput format (yaml/json/wide/custom) kubectl get pod nginx -o yaml--watch, -wWatch for changes kubectl get pods -w--selector, -lFilter by label kubectl get pods -l app=nginx--field-selectorFilter by field kubectl get pods --field-selector status.phase=Running--sort-bySort output kubectl get pods --sort-by=.metadata.creationTimestamp
Advanced Operations
Command Description Example kubectl auth can-i <verb> <resource>Check permissions kubectl auth can-i create deploymentskubectl explain <resource>Get documentation kubectl explain pods.spec.containerskubectl get componentstatusesCheck component health kubectl get cskubectl wait --for=condition=<condition> <resource>Wait for condition kubectl wait --for=condition=Ready pod/nginxkubectl patch <resource> <name> -p '<patch>'Patch resource kubectl patch deploy nginx -p '{"spec":{"replicas":3}}'kubectl kustomize <dir>Print kustomization kubectl kustomize ./overlay/dev
Have Queries? Join https://launchpass.com/collabnix