Cluster & Context
kubectl cluster-infoCluster endpoint
kubectl config get-contextsList contexts
kubectl config use-context CTXSwitch context
kubectl config current-contextCurrent ctx
kubectl versionClient/server ver
Pods
kubectl get pods [-A] [-w]List pods
kubectl describe pod PODPod details
kubectl logs POD [-f] [-c]View logs
kubectl exec -it POD -- shShell into pod
kubectl port-forward POD 8080:80Forward port
kubectl delete pod PODDelete pod
Deployments
kubectl create deploy D --image=ICreate
kubectl scale deploy D --replicas=NScale
kubectl set image deploy/D C=IUpdate image
kubectl rollout status deploy/DRollout status
kubectl rollout undo deploy/DRollback
kubectl rollout restart deploy/DRolling restart
Services & Networking
kubectl expose deploy D --port=80Create svc
kubectl get svc [-A]List services
kubectl get endpoints SVCEndpoints
kubectl get ingressList ingress
kubectl get netpolNetwork policies
Resources & Config
kubectl apply -f FILEApply manifest
kubectl delete -f FILEDelete from file
kubectl diff -f FILEDiff changes
kubectl edit TYPE NAMEEdit resource
kubectl get cm / secretsConfigMaps/Secrets
Debugging
kubectl describe TYPE NAMEResource details
kubectl get events --sort-by=.lastTimestampEvents
kubectl top pod / nodeResource usage
kubectl debug POD -it --image=busyboxDebug pod
kubectl api-resourcesAll resources
kubectl explain TYPEResource docs
Cluster Information
kubectl cluster-info
kubectl cluster-info dump
kubectl version
kubectl config view
Context Management
kubectl config get-contexts
kubectl config current-context
kubectl config use-context <context-name>
kubectl config set-context <context-name> --namespace=<namespace>
kubectl config rename-context <old-name> <new-name>
kubectl config delete-context <context-name>
Kubeconfig Structure
apiVersion: v1
kind: Config
clusters:
- name: my-cluster
cluster:
server: https://cluster-api-endpoint
certificate-authority-data: <base64-cert>
contexts:
- name: my-context
context:
cluster: my-cluster
user: my-user
namespace: default
current-context: my-context
users:
- name: my-user
user:
token: <auth-token>
Pod Lifecycle States
Pending
→
Running
→
Succeeded
|
Failed
|
Unknown
|
CrashLoopBackOff
Listing & Inspecting Pods
kubectl get pods
kubectl get pods -A
kubectl get pods -o wide
kubectl get pods --watch
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
kubectl get pods --show-labels
kubectl describe pod <pod-name>
kubectl get pod <pod-name> -o yaml
kubectl get pod <pod-name> -o json
Pod Logs
kubectl logs <pod-name>
kubectl logs <pod-name> -f
kubectl logs <pod-name> -c <container>
kubectl logs <pod-name> --previous
kubectl logs <pod-name> --since=1h
kubectl logs <pod-name> --since-time=2026-02-09T10:00:00Z
kubectl logs <pod-name> --tail=100 --timestamps
kubectl logs <pod-name> --all-containers=true
Exec, Port-Forward & Copy
kubectl exec -it <pod-name> -- /bin/bash
kubectl exec -it <pod-name> -- /bin/sh
kubectl exec <pod-name> -- ls /app
kubectl exec -it <pod-name> -c <container> -- sh
kubectl port-forward <pod-name> 8080:80
kubectl port-forward --address 0.0.0.0 <pod-name> 8080:80
kubectl port-forward service/<svc-name> 8080:80
kubectl cp /local/path <pod-name>:/container/path
kubectl cp <pod-name>:/container/path /local/path
Creating & Deleting Pods
kubectl run <name> --image=<image>
kubectl run <name> --image=<image> --env="KEY=VALUE" --port=8080 -l app=myapp
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --grace-period=0 --force
kubectl top pod
kubectl top pod <pod-name> --containers
Create & View Deployments
kubectl create deployment <name> --image=<image>
kubectl create deployment <name> --image=<image> --replicas=3
kubectl create deployment <name> --image=<image> --dry-run=client -o yaml
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get deploy -A
kubectl describe deployment <name>
Scaling
kubectl scale deployment <name> --replicas=5
kubectl autoscale deployment <name> --min=2 --max=10 --cpu-percent=80
Rollouts & Updates
kubectl set image deployment/<name> <container>=<new-image>
kubectl rollout status deployment/<name>
kubectl rollout history deployment/<name>
kubectl rollout history deployment/<name> --revision=2
kubectl rollout undo deployment/<name>
kubectl rollout undo deployment/<name> --to-revision=2
kubectl rollout restart deployment/<name>
kubectl rollout pause deployment/<name>
kubectl rollout resume deployment/<name>
Edit & Delete
kubectl edit deployment <name>
kubectl delete deployment <name>
| Type | Scope | Use Case |
ClusterIP | Internal only (default) | Inter-service communication within cluster |
NodePort | External via node IP:port | Development, direct node access |
LoadBalancer | External cloud LB | Production traffic from internet |
ExternalName | DNS CNAME alias | Mapping to external services |
Creating Services
kubectl expose deployment <name> --port=80 --target-port=8080
kubectl expose deployment <name> --port=80 --type=NodePort
kubectl expose deployment <name> --port=80 --type=LoadBalancer
kubectl expose pod <pod-name> --port=80 --name=<svc-name>
Viewing & Deleting Services
kubectl get services
kubectl get svc -A
kubectl describe service <name>
kubectl get endpoints <name>
kubectl get svc <name> -o yaml
kubectl delete service <name>
ConfigMaps
kubectl create configmap <name> \
--from-literal=key1=val1 \
--from-literal=key2=val2
kubectl create configmap <name> --from-file=<path>
kubectl create configmap <name> --from-env-file=.env
kubectl get cm
kubectl describe configmap <name>
kubectl get cm <name> -o yaml
kubectl edit configmap <name>
kubectl delete configmap <name>
Secrets
kubectl create secret generic <name> \
--from-literal=user=admin \
--from-literal=pass=s3cret
kubectl create secret generic <name> --from-file=<path>
kubectl create secret tls <name> --cert=c.pem --key=k.pem
kubectl create secret docker-registry <name> \
--docker-server=<reg> --docker-username=<u> \
--docker-password=<p>
kubectl get secrets
kubectl get secret <name> -o yaml
kubectl get secret <name> \
-o jsonpath='{.data.pass}' | base64 -d
kubectl delete secret <name>
Security Note
Secrets are base64-encoded, not encrypted, by default. For production, enable encryption at rest in etcd or use external secret management (Vault, AWS Secrets Manager, etc.).
kubectl create namespace <name>
kubectl get namespaces
kubectl get ns
kubectl describe namespace <name>
kubectl get pods -n <namespace>
kubectl get all -n <namespace>
kubectl config set-context --current --namespace=<namespace>
kubectl config view --minify | grep namespace:
kubectl delete namespace <name>
Namespace Best Practices
Use namespaces for environment separation (dev/staging/prod). Apply ResourceQuotas and LimitRanges per namespace. Note that Secrets and ConfigMaps are namespace-scoped and cannot be shared across namespaces.
Apply, Create & Validate
kubectl apply -f <file.yaml>
kubectl apply -f <directory>/
kubectl apply -f https://example.com/resource.yaml
kubectl apply -f a.yaml -f b.yaml
kubectl create -f <file.yaml>
kubectl apply -f <file.yaml> --dry-run=client
kubectl apply -f <file.yaml> --dry-run=server
kubectl diff -f <file.yaml>
Edit, Patch & Replace
kubectl edit <type> <name>
kubectl patch <type> <name> -p '{"spec":{"replicas":3}}'
kubectl replace -f <file.yaml>
kubectl replace --force -f <file.yaml>
Delete Resources
kubectl delete <type> <name>
kubectl delete -f <file.yaml>
kubectl delete pods -l app=myapp
kubectl delete <type> --all
kubectl delete pod <name> --grace-period=30
kubectl delete pod <name> --grace-period=0 --force
Describe & Events
kubectl describe <type> <name>
kubectl describe pod <pod>
kubectl describe node <node>
kubectl get events
kubectl get events --sort-by='.lastTimestamp'
kubectl get events --watch
kubectl get events --field-selector type=Warning
kubectl get events --field-selector involvedObject.name=<name>
Debug Containers
kubectl debug <pod> -it --image=busybox
kubectl debug <pod> -it --image=ubuntu --target=<container>
kubectl debug node/<node-name> -it --image=ubuntu
kubectl debug <pod> -it --copy-to=<debug-pod> --image=<image>
Resource Metrics & API
kubectl top node
kubectl top pod
kubectl top pod -n <namespace>
kubectl top pod --sort-by=cpu
kubectl top pod --sort-by=memory
kubectl top pod <pod> --containers
kubectl api-resources
kubectl api-resources -o wide
kubectl api-versions
kubectl explain pod
kubectl explain pod.spec.containers
kubectl explain pod.spec.containers --recursive
kubectl get --raw='/healthz'
kubectl get --raw='/readyz'
1. kubectl get pods -- check pod status
2. kubectl describe pod <name> -- events & conditions
3. kubectl logs <name> -- application logs
4. kubectl logs <name> --previous -- logs from crashed container
5. kubectl exec -it <name> -- sh -- shell access
6. kubectl get events --sort-by=.lastTimestamp -- cluster events
7. kubectl top pod <name> -- resource consumption
Managing Labels
kubectl label pod <name> env=prod tier=frontend
kubectl label pod <name> env=staging --overwrite
kubectl label pod <name> env-
kubectl label pods --all version=v1
kubectl get pods --show-labels
Filtering with Selectors
kubectl get pods -l app=myapp
kubectl get pods -l 'env=prod,tier=frontend'
kubectl get pods -l 'env in (prod,staging)'
kubectl get pods -l 'env notin (dev,test)'
kubectl get pods -l 'release'
kubectl get pods -l '!release'
kubectl get pods -l 'app=myapp,env!=dev'
Field Selectors & Annotations
kubectl get pods --field-selector status.phase=Running
kubectl get pods --field-selector status.phase=Running,spec.nodeName=<node>
kubectl get pods --field-selector status.phase!=Running
kubectl get pods -l app=myapp --field-selector status.phase=Running
kubectl annotate pod <name> description="My pod"
kubectl annotate pod <name> description="Updated" --overwrite
kubectl annotate pod <name> description-
StatefulSets
kubectl apply -f statefulset.yaml
kubectl get statefulsets
kubectl describe statefulset <name>
kubectl scale statefulset <name> --replicas=5
kubectl rollout restart statefulset/<name>
kubectl rollout status statefulset/<name>
kubectl delete statefulset <name> --cascade=orphan
DaemonSets
kubectl apply -f daemonset.yaml
kubectl get daemonsets
kubectl get ds -A
kubectl describe daemonset <name>
kubectl rollout restart daemonset/<name>
kubectl delete daemonset <name>
Jobs & CronJobs
kubectl create job <name> --image=<image>
kubectl create job <name> --from=cronjob/<cj-name>
kubectl get jobs
kubectl logs job/<name>
kubectl delete job <name>
kubectl create cronjob <name> --image=<image> --schedule="*/5 * * * *"
kubectl get cronjobs
kubectl describe cronjob <name>
kubectl patch cronjob <name> -p '{"spec":{"suspend":true}}'
kubectl patch cronjob <name> -p '{"spec":{"suspend":false}}'
kubectl create job <job-name> --from=cronjob/<cj-name>
Networking & Storage
Ingress
kubectl apply -f ingress.yaml
kubectl get ingress
kubectl get ing -A
kubectl describe ingress <name>
kubectl edit ingress <name>
kubectl delete ingress <name>
Ingress NGINX Retiring March 2026
Ingress NGINX will be retired in March 2026 with no further releases or security patches. Consider migrating to Gateway API. Check usage: kubectl get pods -A --selector app.kubernetes.io/name=ingress-nginx
PersistentVolumes & Claims
kubectl get persistentvolumes
kubectl get persistentvolumeclaims
kubectl describe pv <name>
kubectl describe pvc <name>
kubectl apply -f pvc.yaml
kubectl delete pvc <name>
NetworkPolicies
kubectl apply -f networkpolicy.yaml
kubectl get networkpolicies
kubectl describe networkpolicy <name>
kubectl delete networkpolicy <name>
kubectl exec -it <source-pod> -- curl <target-ip>
CNI Required
NetworkPolicies require a CNI plugin that supports them (Calico, Cilium, Weave Net). Policies are additive -- the union of all matching policies is applied.
Horizontal Pod Autoscaler
kubectl autoscale deployment <name> --cpu-percent=50 --min=1 --max=10
kubectl get hpa
kubectl describe hpa <name>
kubectl get hpa --watch
kubectl edit hpa <name>
kubectl delete hpa <name>
Output Formats (-o)
| Flag | Description | Example |
-o wide | Additional columns (node, IP) | kubectl get pods -o wide |
-o yaml | Full YAML output | kubectl get pod X -o yaml |
-o json | Full JSON output | kubectl get pod X -o json |
-o name | Resource type/name only | kubectl get pods -o name |
-o jsonpath | JSONPath expression | kubectl get pods -o jsonpath='{..name}' |
-o custom-columns | Custom table columns | kubectl get pods -o custom-columns=N:.metadata.name |
-o go-template | Go template formatting | kubectl get pods -o go-template='...' |
--no-headers | Omit table headers | kubectl get pods --no-headers |
JSONPath Queries
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'
kubectl get pod <name> -o jsonpath='{.status.podIP}'
kubectl get pod <name> -o jsonpath='{.spec.containers[0].image}'
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
kubectl get pods -o jsonpath='{.items[?(@.status.phase=="Running")].metadata.name}'
Custom Columns & Sorting
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName,IP:.status.podIP
kubectl get pods -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[*].image
kubectl get pods --sort-by=.metadata.name
kubectl get pods --sort-by=.metadata.creationTimestamp
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
kubectl get nodes --sort-by=.status.capacity.cpu
Go Templates
kubectl get pods -o go-template='{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
kubectl get pods -o go-template='{{range .items}}{{if eq .status.phase "Running"}}{{.metadata.name}}{{"\n"}}{{end}}{{end}}'
kubectl Aliases
alias k='kubectl'
alias kg='kubectl get'
alias kd='kubectl describe'
alias kdel='kubectl delete'
alias kl='kubectl logs'
alias kex='kubectl exec -it'
alias ka='kubectl apply -f'
alias kgp='kubectl get pods'
alias kgd='kubectl get deployments'
alias kgs='kubectl get services'
alias kgn='kubectl get nodes'
alias kgpa='kubectl get pods --all-namespaces'
alias kns='kubectl config set-context --current --namespace'
alias kctx='kubectl config current-context'
Shell Completion
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc
kubectl completion fish | source
Dry Run & Explain
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deploy.yaml
kubectl apply -f deploy.yaml --dry-run=server
kubectl explain pods
kubectl explain pod.spec.containers
kubectl explain deployment.spec.strategy
Tooling
Kustomize
kubectl apply -k <directory>
kubectl kustomize <directory>
kubectl apply -k <directory> --dry-run=client
kubectl kustomize <directory> > output.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yaml
namespace: production
namePrefix: prod-
labels:
- pairs:
env: production
Helm Basics
helm repo add <name> <url>
helm repo update
helm search repo <chart>
helm install <release> <chart>
helm install <release> <chart> -f values.yaml
helm install <release> <chart> --set key=value
helm upgrade <release> <chart>
helm rollback <release> <revision>
helm list
helm status <release>
helm template <release> <chart>
helm uninstall <release>
kubectl Plugins (krew)
kubectl krew install <plugin>
kubectl krew list
kubectl krew upgrade
kubectl krew install ctx
kubectl krew install ns
kubectl krew install tree
kubectl krew install neat
Resource Quotas & Limits
kubectl create quota <name> --hard=cpu=10,memory=20Gi,pods=10
kubectl get resourcequota
kubectl describe quota
kubectl apply -f limitrange.yaml
kubectl get limitranges
kubectl describe limitrange <name>
kubectl set resources deployment <name> --limits=cpu=200m,memory=512Mi
kubectl set env deployment/<name> ENV=production
Essential One-Liners
kubectl get pods -o jsonpath='{.items[*].status.podIP}'
kubectl get pods -A -o jsonpath='{range .items[*]}{.spec.containers[*].image}{"\n"}{end}' | sort -u
kubectl get pods -A --no-headers | awk '{print $4}' | sort | uniq -c
kubectl get pods -A --field-selector spec.nodeName=<node>
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.containerStatuses[0].restartCount}{"\n"}{end}'
kubectl get pods -A --field-selector status.phase!=Running
kubectl get pods,services,deployments
Pro Tips Summary
Use kubectl explain instead of searching docs.
Always test with --dry-run before applying changes.
Use kubectl diff to preview what will change.
Set up shell completion and aliases for speed.
Use labels extensively for organization.
Monitor events with kubectl get events -w.
Combine -o jsonpath with jq for complex queries.
Use Kustomize for environment-specific configs.
Use kubectl debug for non-intrusive troubleshooting.