k8s print to yaml

  • When you need to quickly find information about the state of resources, you can import Kubernetes definitions into YAML files. These files allow for full-text search across all resources.
  • For example, you can quickly search by domain name to find the service deployed on it and check its environment variables.
  • This method can also be used as a tool for saving the state of the cluster.
#!/bin/bash
 
ROOT=${HOME}/clusterstate
NAMESPACE="webpush"  # needent namespace
 
while read -r resource
do
    echo "  scanning resource '${resource}'"
    while read -r item x
    do
        mkdir -p "${ROOT}/${NAMESPACE}/${resource}"
        echo "    exporting item '${NAMESPACE} ${item}'"
        kubectl get "$resource" -n "$NAMESPACE" "$item" -o yaml > "${ROOT}/${NAMESPACE}/${resource}/$item.yaml" &
    done < <(kubectl get "$resource" -n "$NAMESPACE" 2>&1 | tail -n +2)
done < <(kubectl api-resources --namespaced=true 2>/dev/null | grep -v "events" | tail -n +2 | awk '{print $1}')
wait