Kubernetes Commands

Shardul | Dec 20, 2025 min read

Managing Kubernetes efficiently requires knowing the right commands at the right time. This cheat sheet consolidates the most commonly used kubectl and cluster management commands into one structured reference. From initializing a cluster to debugging deployments and configuring RBAC, this guide serves as a quick and reliable command handbook.

Cluster Setup (kubeadm)

Initialize control plane:

kubeadm init

Configure kubectl:

mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config

Install CNI (example: Calico):

kubectl apply -f <calico-manifest>

Join worker node:

kubeadm join <IP>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Namespace

Create, List, Delete:

kubectl create namespace
kubectl get ns
kubectl delete ns

Resources for Specific Namespace:

kubectl get pods -n <namespace>

Pods

List, Describe, Logs, Delete:

kubectl get pods
kubectl get pods -o wide
kubectl describe pod <pod>
kubectl logs <pod>
kubectl logs -f <pod>
kubectl exec -it <pod> -- /bin/sh
kubectl delete pods <pod>
kubectl cp pod:/file /local/path

Create pod quickly:

kubectl run nginx --image=nginx

Generate YAML:

kubectl run nginx --image=nginx --dry-run=client -o yaml

Deployments

Create, List, Describe, scale and Rollout:

kubectl create deployment nginx --image=nginx
kubectl get deploy
kubectl describe deploy nginx
kubectl scale deploy nginx --replicas=3
kubectl set image deploy nginx nginx=nginx:1.25
kubectl delete deploy nginx

Rollout:

kubectl rollout status deploy nginx
kubectl rollout undo deploy nginx
kubectl rollout history deploy nginx

Services

kubectl expose deploy nginx --port=80 --type=ClusterIP
kubectl expose pod nginx --port=80 --type=ClusterIP
kubectl get svc
kubectl describe svc nginx
kubectl delete svc <name>
kubectl get svc <name> -o yaml

NodePort:

kubectl expose deploy nginx --port=80 --type=NodePort

Ingress

kubectl get ingress
kubectl describe ingress <name>
kubectl delete ingress <name>

ConfigMaps & Secrets

kubectl create configmap app-config --from-literal=key=value
kubectl create secret generic db-secret --from-literal=password=1234
kubectl get configmap
kubectl get secret
kubectl describe cm <name>
kubectl describe secret <name>
kubectl delete configmap <name>
kubectl delete secret <name>

Extract specific key:

kubectl get configmap <name> -o jsonpath='{.data.keyname}'

Decode secret value:

kubectl get secret <name> -o jsonpath='{.data.password}' | base64 -d

Storage (PV / PVC)

kubectl get pv
kubectl get pvc
kubectl describe pv <name>
kubectl describe pvc <name>

Create PVC YAML fast:

kubectl create pvc myclaim --storage=1Gi --access-mode=ReadWriteOnce --dry-run=client -o yaml

RBAC

kubectl create serviceaccount sa1
kubectl create role role1 --verb=get,list --resource=pods
kubectl create rolebinding rb1 --role=role1 --serviceaccount=default:sa1

Check permissions:

kubectl auth can-i create pods
kubectl auth can-i delete pods --as=system:serviceaccount:default:sa1

Label

kubectl label pod nginx env=prod
kubectl label pod nginx env=dev --overwrite
kubectl label pod nginx env-
kubectl get pods <pod> -o yaml

Show pods labels:

kubectl get pods --show-labels
Select Resources Using Labels:

Equal:

kubectl get pods -l app=nginx

Not equal:

kubectl get pods -l app!=nginx

Multiple labels:

kubectl get pods -l app=nginx,env=prod

Exists:

kubectl get pods -l app

Context Switching

kubectl config get-contexts
kubectl config use-context <context>
kubectl config current-context

Troubleshooting

kubectl get all
kubectl describe <resource> <name>
kubectl logs <pod>
kubectl exec -it <pod> -- sh
kubectl get events

Node issues:

kubectl get nodes
kubectl describe node <node>
kubectl get nodes --show-labels=true

System pods:

kubectl get pods -n kube-system

Change namespace fast:

kubectl config set-context --current --namespace=dev

Debugging

kubectl debug pod/<pod-name> -it --image=busybox --target=<container-name>
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
kubectl cordon <node-name>
kubectl uncordon <node-name>
kubectl taint nodes <node-name> key=value:NoSchedule
kubectl taint nodes <node-name>  key=value:NoSchedule-

Annotate:

kubectl annotate pod <pod-name> key=value
kubectl annotate pod <pod-name> key=newvalue --overwrite
kubectl annotate pod <pod-name> key-

kubectl Explain:

kubectl explain pod
kubectl explain deployment.spec

kubectl explain

kubectl explain pod
kubectl explain deployment.spec

YAML Editing Strategy

Don’t need to write full YAML from scratch.

Generate template:

kubectl create deploy nginx --image=nginx --dry-run=client -o yaml > nginx.yaml

Edit:

vi nginx.yaml

Apply:

kubectl apply -f nginx.yaml

Delete:

kubectl delete -f nginx.yaml

Direct edit:

kubectl edit deploy <deployment-name>

Save → Kubernetes updates automatically.

Patch:

kubectl patch deploy nginx -p '{"spec":{"replicas":3}}'

Plugin & Misc:

kubectl plugin
kubectl version
kubectl completion
kubectl proxy

Tip use:

kubectl --help
kubectl get --help

Checkout list of all the resources available in K8s. Kubernetes Resources