Skip to content

Instantly share code, notes, and snippets.

Last active Jun 27, 2022
What would you like to do?
Dump Kubernetes cluster resources as YAML
#!/usr/bin/env bash
set -e
if [[ -z ${CONTEXT} ]]; then
echo "Usage: $0 KUBE-CONTEXT"
exit 1
NAMESPACES=$(kubectl --context ${CONTEXT} get -o json namespaces|jq '.items[]'|sed "s/\"//g")
RESOURCES="configmap secret daemonset deployment service hpa"
for ns in ${NAMESPACES};do
for resource in ${RESOURCES};do
rsrcs=$(kubectl --context ${CONTEXT} -n ${ns} get -o json ${resource}|jq '.items[]'|sed "s/\"//g")
for r in ${rsrcs};do
mkdir -p "${dir}"
kubectl --context ${CONTEXT} -n ${ns} get -o yaml ${resource} ${r} > "${dir}/${r}.yaml"
Copy link

tilleryd commented Jan 29, 2020


Copy link

wuestkamp commented Apr 22, 2020

sweet! If you want all resources you could do:

RESOURCES=$(kubectl api-resources --namespaced -o name | tr "\n" " ")

and namespaces without jq:

kubectl get ns -o jsonpath="{.items[*]}"

Copy link

boweeb commented Nov 20, 2020

Quick jq tip:
If you use -r then you won't need to strip quotes with sed.

... | jq '.items[]' | sed "s/\"//g"

# is equivalent to

... | jq -r '.items[]'

Quote from man page:

o   --raw-output / -r:

    With this option, if the filter's result is a string then it will be written
    directly to standard output  rather  than  being formatted as a JSON string
    with quotes. This can be useful for making jq filters talk to non-JSON-based

Copy link

cig0 commented Dec 17, 2020

Saved me from a huge headache today

Copy link

artemkoru commented Dec 25, 2020


Copy link

WoozyMasta commented Feb 16, 2021

You can try this script

Copy link

RaymondHallquist commented Jun 7, 2022

This is a great script. I simply added statefulset to line 13 to get them too. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment