Skip to content

Kubernetes Cheat Sheet

Pods & Deployments

##  Terminal into a pod
kubectl exec -it $pod_name -- bash # or PowerShell

##  Restart all pods in the current namespace
kubectl get deployments -o name | ForEach-Object { kubectl rollout restart $_ }

##  Delete pods in the specified state
kubectl get pods --field-selector 'status.phase==Failed' -o json | kubectl delete -f -
##  or
kubectl get pods --field-selector 'status.phase==Pending' -o json | kubectl delete -f -

Label a namespace (required for Network Policies)

$name = "minority"
kubectl label namespace $name networking/namespace=$name

Context & Namespace

##  list all available contexts
kubectl config get-contexts

##  switch to context
kubectl config use-context <context-name>

##  change namespace only
kubectl config set-context --current --namespace=<namespace>