Cheat sheet · Systems and cloud

Kubernetes cheat sheet

The kubectl commands to see, change and debug a cluster, and what each core object is for.
Sections
6
Items
30
Area
Systems and cloud
Download
PNG, PDF

Cheat sheet

Kubernetes

The kubectl commands to see, change and debug a cluster, and what each core object is for.

/blogs

See what is running

kubectl get pods
List pods in the current namespace
kubectl get pods -A -o wide
All namespaces, with the node and IP
kubectl get deploy,svc,ingress
List several kinds at once
kubectl describe pod <name>
Full details and recent events
kubectl top pods
CPU and memory use

Debug

kubectl logs <pod> -f
Follow the logs
kubectl logs <pod> --previous
Logs of the crashed run before this one
kubectl exec -it <pod> -- sh
Open a shell inside a pod
kubectl port-forward <pod> 8080:80
Reach a pod on localhost:8080
kubectl get events --sort-by=.lastTimestamp
What happened recently

Change things

kubectl apply -f file.yaml
Create or update from a file
kubectl delete -f file.yaml
Remove what a file created
kubectl scale deploy <name> --replicas=3
Run three copies
kubectl rollout status deploy/<name>
Watch an update finish
kubectl rollout undo deploy/<name>
Go back to the last version
kubectl set image deploy/<name> app=img:2.0
Change the image

Context and namespace

kubectl config get-contexts
List clusters you can use
kubectl config use-context <name>
Switch cluster
kubectl -n <namespace> get pods
Use a namespace for one command
kubectl config set-context --current --namespace=<ns>
Change the default namespace

What each object is

Pod
One or more containers that run together
Deployment
Keeps a set number of identical pods and handles updates
Service
A steady address and load balancing for pods
Ingress
Rules that send outside web traffic to services
ConfigMap / Secret
Settings / sensitive settings for pods
Namespace
A separate area inside a cluster

YAML skeleton

apiVersion: apps/v1
Which API version
kind: Deployment
What you are creating
metadata: { name: web }
Its name and labels
spec: { replicas: 3, ... }
What you want it to be

Remember

  • A pod in CrashLoopBackOff keeps crashing. Start with kubectl logs --previous, then describe.
  • Keep desired state in files in git and use apply, so the cluster matches your repository.

Learn it properly

A cheat sheet helps you remember. These lessons explain the ideas step by step, with examples.

More cheat sheets

All cheat sheets →