Skip to content

Instantly share code, notes, and snippets.

@juusujanar
Last active January 12, 2023 11:18
Show Gist options
  • Save juusujanar/6b6cdae633835383c532fb9319ff40b9 to your computer and use it in GitHub Desktop.
Save juusujanar/6b6cdae633835383c532fb9319ff40b9 to your computer and use it in GitHub Desktop.
Kubectl useful commands
#!/bin/bash
set -e
# Export all secrets and configmaps in all namespaces to files
# Every file will have name <namespace>__<type>__<name>.yaml
# e.g. default__secret__database.yaml
namespaces=$(kubectl get namespace | awk '{print $1}' | tail -n +2)
for ns in $namespaces
do
if [[ "$ns" == *"kube-system"* ]]; then
echo "Skipping namespace $ns"
continue
fi
echo "Checking namespace $ns for secrets and configmaps"
# Add other objects (statefulset,svc,deployment etc) here to also be exported
for n in $(kubectl get -o=name -n=$ns configmap,secret)
do
if [[ "$n" == *"kube-root-ca"* ]] || [[ "$n" == *"default-token"* ]]; then
echo "Skipping $n"
continue
fi
echo "Exporting $n"
filename=$(sed s:/:__:g <<< $n)
kubectl get -o=yaml -n=$ns $n > "${ns}__${filename}.yaml"
# Delete creationTimestamp, resourceVersion and uid lines
sed -i '/creationTimestamp:/d;/resourceVersion:/d;/uid:/d' "${ns}__${filename}.yaml"
done
done
# Show all secrets being used by pods in all namespaces
# Does not filter out null values
kubectl get pods --all-namespaces -o json | jq '.items[] | [{pod:.metadata.name , secrets: [.spec.containers[].env[]?.valueFrom.secretKeyRef.name] }]' > pod-secrets.json
# Show pods in all namespaces that run on given node
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=
# Show every node's CPU and memory requests and limits
kubectl describe nodes | grep 'Name:\| cpu\| memory'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment