Skip to content

Instantly share code, notes, and snippets.

@Tharwat96
Last active November 2, 2023 10:35
Show Gist options
  • Save Tharwat96/57024497f2e25ff524259c02bd572da9 to your computer and use it in GitHub Desktop.
Save Tharwat96/57024497f2e25ff524259c02bd572da9 to your computer and use it in GitHub Desktop.
Kubernetes Cheat Sheet
# delete all pods that are evicted
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all pods that are erroring and sleep a bit to avoid rate limit
# you can add head or tail to work in batches
kubectl get pods --all-namespaces | grep 'Evicted\|Error\|ContainerStatusUnknown' | awk '{print $2 " --namespace=" $1}' | xargs -I % sh -c '{ kubectl delete pod %; sleep 0.1; }'
# print all failing pods with their describe reason of failiure
NS=production
IFS=$'\n' # make newlines the only separator
for DEF in $(kubectl -n $NS get pods --no-headers | grep -vi 'running\|evicted\|completed')
do
echo "\n------------\n${DEF}"
echo "${DEF}" | awk '{print $1}' | xargs kubectl -n $NS describe pod | grep -A 12 State
done
for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob)
do
mkdir -p $(dirname $n)
kubectl get -o=yaml --export $n > $n.yaml
done
for n in $(kubectl -n production get -o=name sealedsecret,configmap,ingress,service,deployment,statefulset,hpa,cronjob)
do
mkdir -p $(dirname $n)
kubectl get -o=yaml --export $n > $n.yaml
done
for OBJ in $(kubectl api-resources --verbs=list --namespaced -o name)
do
for NS in $(kubectl get namespaces --no-headers | awk '{print $1}')
do
mkdir -p $NS
for DEF in $(kubectl -n $NS get --show-kind --ignore-not-found $OBJ -o name)
do
mkdir -p $(dirname $DEF)
kubectl get $DEF -o yaml \
| yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' - > $DEF.yaml
done
done
done
for NS in $(kubectl get namespaces --no-headers | awk '{print $1}')
do
mkdir -p $NS
for OBJ in $(kubectl api-resources --verbs=list --namespaced -o name)
do
for DEF in $(kubectl -n $NS get --show-kind --ignore-not-found $OBJ -o name)
do
mkdir -p $NS/$(dirname $DEF)
kubectl -n $NS get $DEF -o yaml \
| kubectl neat - > $NS/$DEF.yaml
done
done
done
NS=production
mkdir -p $NS
for DEF in $(kubectl -n $NS get -o=name configmap,ingress,service,deployment,statefulset,hpa,cronjob,poddisruptionbudget,sealedsecret)
do
mkdir -p $NS/$(dirname $DEF)
kubectl -n $NS get $DEF -o yaml \
| kubectl neat - > $NS/$DEF.yaml
done
# Sealed secret controller update
kubectl get secret -n kube-system -l sealedsecrets.bitnami.com/sealed-secrets-key -o yaml >master.key
NS=production
mkdir -p $NS
for DEF in $(kubectl -n $NS get -o=name secret,sealedsecret)
do
mkdir -p $NS/$(dirname $DEF)
kubectl -n $NS get $DEF -o yaml \
| kubectl neat - > $NS/$DEF.yaml
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment