Kubectl Cheatsheet

January 27, 2022

165 words 1 min read

Kubectl Cheatsheet

This page contains commonly used kubectl commands. Please note the choice is very much subjective and these are the ones I use the most myself, so some other ones you might find useful could be missing.

Note: All of them require Kubernetes version to be at least 1.21.

Secrets

Create secret from literal

kubectl create secret generic my-secret --from-literal='username=my-username' --from-literal='password=secret1'

Create secret from file

kubectl create secret generic my-secret --from-file=file.yaml

Copy secret between namespaces

kubectl get secret my-secret --namespace=old-namespace --export -o yaml | kubectl apply --namespace=new-namespace -f -

Pods/images

List all pulled images in cluster

kubectl get nodes -o json | jq '.items[].status.images[] | .names[1], .sizeBytes'

Delete evicted pods from all namespaces

kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod

ConfigMaps

Create ConfigMap from file

kubectl create configmap example-cm --from-file=file.txt -n namespace

Update ConfigMap from file in place

kubectl create configmap example-cm --from-file=file.txt -n namespace -o yaml --dry run | kubectl replace -f -