Kubernetes cheatsheet#
In each of the commands below, the -n or --namespace option specifies the namespace to operate on.
Often this will be default, which is the default, or your own personal namespace if you are testing within that.
There is no environment variable to set the current namespace, but you can set it via a kubectl command that will record your preference in ~/.kube/config:
# Set current namespace
kubectl config set-context --current --namespace=<namespace>
# Report which namespace is current
kubectl config view | grep namespace:
If the latter shows no results, there’s no namespace setting so it’s still the default default.
Show nodes in the cluster#
kubectl [-n <namespace>] get pods
View logs#
kubectl [-n <namespace>] logs [options] <pod>
Useful options include:
--since=6hto show logs for the last six hours--tail=10to show the most recent 10 lines-fto show log lines as they appear, as withtail -f
If there are no logs, e.g., because a pod is failing during startup, the following can be informative:
kubectl [-n <namespace>] describe pods
Log in to a node#
kubectl [-n <namespace>] exec -it <pod> -- bash
Restart a node#
kubectl [-n <namespace>] delete pod <pod>
The node will be deleted and a new one started in its place.