kubectl
Last updated
Last updated
kubectl
is the primary command-line tool for interacting with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, and view logs. Mastery of kubectl
is essential for DevOps engineers working with AWS EKS, Azure AKS, GCP GKE, NixOS, and WSL environments.
macOS (Homebrew):
Linux (Debian/Ubuntu):
NixOS (declarative):
Add to your /etc/nixos/configuration.nix
:
Then run:
Windows (WSL): Install via Chocolatey or manually download the binary from the .
kubectl cluster-info
– Show cluster endpoints
kubectl version
– Show client/server versions
kubectl config view
– Show kubeconfig
kubectl get all --all-namespaces
– List all resources in all namespaces
kubectl get namespaces
– List all namespaces
kubectl get pods
– List all pods in current namespace
kubectl get pods -o wide
– Detailed pod info
kubectl get pods --field-selector=spec.nodeName=<node>
– Pods on a node
kubectl get rc,services
– List replication controllers and services
kubectl get deployment
– List deployments
kubectl describe deployment <name>
– Deployment details
kubectl edit deployment <name>
– Edit deployment
kubectl create deployment <name> --image=<image>
– Create deployment
kubectl delete deployment <name>
– Delete deployment
kubectl rollout status deployment <name>
– Rollout status
kubectl rollout history deployment/<name>
– Rollout history
kubectl rollout undo deployment/<name>
– Rollback deployment
kubectl rollout restart deployment/<name>
– Restart deployment
kubectl get pod
– List pods
kubectl describe pod <name>
– Pod details
kubectl logs <pod>
– Pod logs
kubectl logs -f <pod>
– Follow logs
kubectl exec -it <pod> -- /bin/sh
– Shell into pod
kubectl delete pod <name>
– Delete pod
kubectl create namespace <name>
– Create namespace
kubectl get namespace
– List namespaces
kubectl describe namespace <name>
– Namespace details
kubectl delete namespace <name>
– Delete namespace
kubectl get nodes
– List nodes
kubectl describe node <name>
– Node details
kubectl cordon <node>
– Mark node unschedulable
kubectl drain <node>
– Prepare node for maintenance
kubectl uncordon <node>
– Mark node schedulable
kubectl top node
– Node resource usage
kubectl get daemonset
– List daemonsets
kubectl describe ds <name> -n <namespace>
– DaemonSet details
kubectl edit daemonset <name>
– Edit DaemonSet
kubectl delete daemonset <name>
– Delete DaemonSet
kubectl get events
– List events
kubectl get events --field-selector type=Warning
– List warnings
kubectl logs <pod>
– Pod logs
kubectl logs -c <container> <pod>
– Container logs
kubectl logs --since=1h <pod>
– Last hour logs
kubectl logs --tail=20 <pod>
– Last 20 lines
kubectl logs --previous <pod>
– Previous pod logs
kubectl get services
– List services
kubectl describe service <name>
– Service details
kubectl expose deployment <name>
– Expose as service
kubectl get serviceaccounts
– List service accounts
kubectl describe serviceaccount <name>
– Service account details
kubectl create secret generic <name> --from-literal=key=value
– Create secret
kubectl get secrets
– List secrets
kubectl describe secret <name>
– Secret details
kubectl delete secret <name>
– Delete secret
Use kubectl --context
and --namespace
to avoid mistakes in multi-cluster/multi-namespace environments
Use kubectl explain <resource>
to discover resource fields
Use kubectl apply -f <file.yaml>
for declarative resource management
Integrate kubectl
with CI/CD (GitHub Actions, Azure Pipelines, GitLab CI)
Never run destructive commands (delete
, drain
) without double-checking the context/namespace
Tip: Use shell aliases and prompt tools (e.g., kube-ps1) to display current context/namespace and avoid costly mistakes.
Use for fast context/namespace switching