Kubernetes (kubectl) Reference

Essential kubectl commands for Kubernetes management - from pods to deployments

Quick Start

Deploy Your First App

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80
kubectl get pods

Apply from YAML

kubectl apply -f deployment.yaml
kubectl get deployments
kubectl logs -f <pod-name>

Cluster Information

kubectl cluster-infoDisplay cluster information
kubectl versionShow kubectl and cluster version
kubectl config viewView kubeconfig settings
kubectl config current-contextShow current context
kubectl config use-context <name>Switch to different context
kubectl get nodesList all nodes in cluster
kubectl describe node <name>Show node details
kubectl top nodesShow node resource usage

Pod Management

kubectl get podsList pods in current namespace
kubectl get pods -AList pods in all namespaces
kubectl get pods -o wideList pods with more details
kubectl describe pod <name>Show pod details and events
kubectl logs <pod>View pod logs
kubectl logs -f <pod>Follow pod logs in real-time
kubectl logs <pod> -c <container>View logs from specific container
kubectl exec -it <pod> -- bashExecute interactive shell in pod
kubectl exec <pod> -- <command>Execute command in pod
kubectl delete pod <name>Delete a pod
kubectl delete pod <name> --grace-period=0 --forceForce delete pod immediately
kubectl top podsShow pod resource usage
kubectl port-forward <pod> 8080:80Forward local port to pod

Deployments

kubectl get deploymentsList deployments
kubectl describe deployment <name>Show deployment details
kubectl create deployment <name> --image=<image>Create deployment
kubectl scale deployment <name> --replicas=3Scale deployment to N replicas
kubectl set image deployment/<name> <container>=<image>Update deployment image
kubectl rollout status deployment/<name>Check rollout status
kubectl rollout history deployment/<name>View rollout history
kubectl rollout undo deployment/<name>Rollback to previous version
kubectl rollout restart deployment/<name>Restart deployment
kubectl delete deployment <name>Delete deployment

Services

kubectl get servicesList services
kubectl get svcList services (short)
kubectl describe service <name>Show service details
kubectl expose deployment <name> --port=80Expose deployment as service
kubectl delete service <name>Delete service

Namespaces

kubectl get namespacesList all namespaces
kubectl get nsList namespaces (short)
kubectl create namespace <name>Create namespace
kubectl delete namespace <name>Delete namespace
kubectl config set-context --current --namespace=<name>Switch default namespace
kubectl get pods -n <namespace>List pods in specific namespace

ConfigMaps

kubectl get configmapsList ConfigMaps
kubectl get cmList ConfigMaps (short)
kubectl describe configmap <name>Show ConfigMap details
kubectl create configmap <name> --from-literal=key=valueCreate ConfigMap from literal
kubectl create configmap <name> --from-file=<file>Create ConfigMap from file
kubectl delete configmap <name>Delete ConfigMap

Secrets

kubectl get secretsList secrets
kubectl describe secret <name>Show secret details
kubectl create secret generic <name> --from-literal=key=valueCreate secret from literal
kubectl create secret generic <name> --from-file=<file>Create secret from file
kubectl delete secret <name>Delete secret

Apply & Manage Resources

kubectl apply -f <file.yaml>Apply configuration from file
kubectl apply -f <directory>/Apply all configs in directory
kubectl apply -f <url>Apply configuration from URL
kubectl delete -f <file.yaml>Delete resources from file
kubectl diff -f <file.yaml>Show diff before applying
kubectl get -f <file.yaml>Get resources defined in file

Debugging & Troubleshooting

kubectl get eventsView cluster events
kubectl get events --sort-by=.metadata.creationTimestampView events sorted by time
kubectl describe pod <name>Debug pod issues
kubectl logs <pod> --previousView logs from previous container
kubectl get pod <name> -o yamlExport pod definition as YAML
kubectl explain pod.specShow documentation for resource field
kubectl run debug --rm -it --image=busybox -- shCreate temporary debug pod

Other Useful Commands

kubectl get allList all resources in namespace
kubectl api-resourcesList all resource types
kubectl label pod <name> key=valueAdd label to pod
kubectl annotate pod <name> key=valueAdd annotation to pod
kubectl get pods -l app=myappFilter pods by label
kubectl cp <pod>:/path/file ./fileCopy file from pod to local
kubectl cp ./file <pod>:/path/fileCopy file from local to pod

Need time management tools?

Try Focus 25 →