Skip to main content

kubectl

The command-line tool for interacting with Kubernetes clusters.

Contexts (Clusters)โ€‹

kubectl config get-contexts # list all clusters
kubectl config current-context # which cluster you're on
kubectl config use-context <name> # switch cluster
kubectl config set-context --current --namespace=staging # set default namespace

Get Resourcesโ€‹

kubectl get pods
kubectl get pods -n <namespace>
kubectl get pods -A # all namespaces
kubectl get pods -w # watch (live updates)
kubectl get pods -o wide # show node, IP
kubectl get pods -o yaml # full YAML output

kubectl get deployments
kubectl get services
kubectl get ingress
kubectl get configmaps
kubectl get secrets
kubectl get nodes
kubectl get all # pods, services, deployments in one shot

Describe (Details + Events)โ€‹

kubectl describe pod <pod-name>
kubectl describe deployment <name>
kubectl describe node <node-name>

describe is your first stop when something isn't working โ€” check the Events section at the bottom.


Apply & Deleteโ€‹

kubectl apply -f manifest.yaml # create or update
kubectl apply -f ./k8s/ # apply entire directory
kubectl delete -f manifest.yaml
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --force # skip graceful shutdown

Logsโ€‹

kubectl logs <pod-name>
kubectl logs <pod-name> -c <container> # multi-container pod
kubectl logs <pod-name> -f # follow (tail -f)
kubectl logs <pod-name> --previous # logs from crashed container
kubectl logs <pod-name> --tail=100 # last 100 lines

Exec (Shell into a Pod)โ€‹

kubectl exec -it <pod-name> -- bash
kubectl exec -it <pod-name> -- sh # if no bash
kubectl exec -it <pod-name> -c <container> -- bash # specific container

Port Forward (Local Access to a Pod/Service)โ€‹

kubectl port-forward pod/<pod-name> 8080:80
kubectl port-forward svc/<service-name> 8080:80

Opens localhost:8080 โ†’ forwards to port 80 on the pod/service.


Scaleโ€‹

kubectl scale deployment <name> --replicas=5
kubectl scale deployment <name> --replicas=0 # stop all pods

Rolloutsโ€‹

kubectl rollout status deployment/<name> # check rollout progress
kubectl rollout history deployment/<name> # see revision history
kubectl rollout undo deployment/<name> # roll back to previous
kubectl rollout undo deployment/<name> --to-revision=2
kubectl rollout restart deployment/<name> # force restart all pods

Copy Filesโ€‹

kubectl cp <pod-name>:/path/to/file ./local-file # pod โ†’ local
kubectl cp ./local-file <pod-name>:/path/to/file # local โ†’ pod

Resource Usageโ€‹

kubectl top pods
kubectl top pods -n <namespace>
kubectl top nodes

Requires metrics-server to be installed in the cluster.


Labels & Selectorsโ€‹

kubectl get pods -l app=my-app
kubectl get pods -l env=production,app=my-app
kubectl label pod <pod-name> env=production

Quick Editโ€‹

kubectl edit deployment <name> # opens YAML in $EDITOR

Dry Run (Preview Without Applying)โ€‹

kubectl apply -f manifest.yaml --dry-run=client
kubectl delete pod <name> --dry-run=client

Useful Aliasesโ€‹

alias k=kubectl
alias kgp='kubectl get pods'
alias kgpa='kubectl get pods -A'
alias kl='kubectl logs'
alias kex='kubectl exec -it'