Last active
January 12, 2023 11:18
-
-
Save juusujanar/6b6cdae633835383c532fb9319ff40b9 to your computer and use it in GitHub Desktop.
Kubectl useful commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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