Create & Apply
kubectl apply -f <file>
Create or update from file
kubectl apply -f <dir>
Apply every manifest in a dir
kubectl create deployment <name>
--image=<img>
New deployment
kubectl run <name> --image=<img>
Start a single pod
kubectl run <name> --image=<img>
--dry-run=client -o yaml
Generate YAML only
Redirect to a file to bootstrap a manifest.
kubectl create job <name> --image=<img> --
<cmd>
Run a one-off job
kubectl create cronjob <name> --image=<img>
--schedule="<cron>" -- <cmd>
Scheduled job
kubectl expose deployment <name>
--port=<port>
Create a service
kubectl diff -f <file>
Preview changes before apply
Inspect
kubectl get pods
List pods
kubectl get pods -o wide
Add node and IP
kubectl get pods -n <ns>
List in a namespace
kubectl get all
Common resources in namespace
Not truly all: skips configmaps, secrets, ingresses and CRDs.
kubectl get <type> <name> -o yaml
Full object as YAML
kubectl describe <type> <name>
Details and recent events
kubectl get pods -l <key>=<value>
Filter by label
kubectl get pods -w
Watch for changes
kubectl get events
--sort-by=.metadata.creationTimestamp
Events by time
kubectl events --types=Warning
Only warnings
kubectl get pods --sort-by=.status.startTime
Sort by a field
kubectl get <type> <name> -o
jsonpath='{.<path>}'
Extract one field
kubectl get pods -o
custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[*].image
Custom table
kubectl get <type> -o name
Names only
Pods & Debug
kubectl logs <pod>
Show pod logs
kubectl logs -f <pod>
Follow logs
kubectl logs <pod> -c <container>
Logs of one container
kubectl logs <pod> --previous
Logs of crashed container
kubectl logs -l <key>=<value>
--all-containers
Logs by label
kubectl exec -it <pod> -- sh
Interactive shell
Everything after -- is the command run in the container.
kubectl exec <pod> -- <cmd>
Run one command
kubectl port-forward <pod>
<local>:<remote>
Forward a local port
kubectl port-forward svc/<name>
<local>:<port>
Forward to a service
kubectl cp <pod>:<path> <local>
Copy file from pod
Needs tar inside the container image.
kubectl cp <local> <pod>:<path>
Copy file to pod
kubectl debug <pod> -it --image=busybox:1.28
Debug container in a pod
kubectl debug node/<node> -it
--image=busybox:1.28
Debug on a node
kubectl top pod
Pod CPU and memory
Needs metrics-server in the cluster.
kubectl attach <pod> -i
Attach to main process
Update & Rollout
kubectl set image deployment/<name>
<container>=<img>
Change an image
kubectl rollout status deployment/<name>
Wait for rollout
kubectl rollout undo deployment/<name>
Roll back
kubectl rollout restart deployment/<name>
Restart all pods
kubectl rollout history deployment/<name>
Revision history
kubectl scale deployment/<name>
--replicas=<n>
Set replica count
kubectl autoscale deployment <name> --min=2
--max=10
Create an autoscaler
kubectl edit <type> <name>
Edit in $EDITOR
KUBE_EDITOR overrides the editor.
kubectl patch <type> <name> -p
'<json>'
Patch fields (strategic merge)
kubectl label <type> <name>
<key>=<value>
Add or change a label
Append - to the key to remove it; add --overwrite to replace.
kubectl annotate <type> <name>
<key>=<value>
Add an annotation
kubectl replace --force -f <file>
Delete and recreate
Causes downtime for the object.