{"id":7867,"date":"2019-06-20T08:47:23","date_gmt":"2019-06-20T00:47:23","guid":{"rendered":"http:\/\/rmohan.com\/?p=7867"},"modified":"2019-06-20T08:47:25","modified_gmt":"2019-06-20T00:47:25","slug":"get-yaml-for-deployed-kubernetes","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7867","title":{"rendered":"Get YAML for deployed Kubernetes"},"content":{"rendered":"\n<p>ombining other answers, this is what I came up with for bash:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob) do     <br>mkdir -p $(dirname $n)     <br>kubectl get -o=yaml --export $n > $n.yaml <br>done<br><br><br><br><br> <br>kubectl get all --export=true -o yaml <br><br><br>!\/bin\/env bash<br> i=$((0))<br> for n in $(kubectl get -o=custom-columns=NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name pv,pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob --all-namespaces | grep -v 'secrets\/default-token')<br> do<br>     if (( $i &lt; 1 )); then<br>         namespace=$n<br>         i=$(($i+1))<br>         if [[ \"$namespace\" == \"PersistentVolume\" ]]; then<br>             kind=$n<br>             i=$(($i+1))<br>         fi<br>     elif (( $i &lt; 2 )); then<br>         kind=$n<br>         i=$(($i+1))<br>     elif (( $i &lt; 3 )); then<br>         name=$n<br>         i=$((0))<br>         if [[ \"$namespace\" != \"NAMESPACE\" ]]; then<br>             mkdir -p $namespace<br> <code>        yaml=$((kubectl get $kind -o=yaml $name -n $namespace ) 2>\/dev\/null)         if [[ $kind != 'Secret' || $yaml != *\"type: kubernetes.io\/service-account-token\"* ]]; then             echo \"Saving ${namespace}\/${kind}.${name}.yaml\"             kubectl get $kind -o=yaml --export $name -n $namespace > $namespace\/$kind.$name.yaml         fi     fi fi<\/code><br> done<br><br><br><br><br> <br>To get the yaml for a deployment (service, pod, secret, etc):<br><code>kubectl get deploy deploymentname -o yaml --export<\/code> <br><br><br><br> <br><code>kubectl get deployment,service,pod yourapp -o yaml --export <\/code><br>Answering @Sinaesthetic question:<br>any idea how to do it for the full cluster (all deployments)?<br><code>kubectl get deploy --all-namespaces -o yaml --export <\/code><br>The problem with this method is that\u00a0<em>export<\/em>\u00a0doesn't include the namespace. So if you want to export many resources at the same time, I recommend doing it per namespace:<br><code>kubectl get deploy,sts,svc,configmap,secret -n default -o yaml --export > default.yaml <\/code><br>Unfortunately kubernetes\u00a0<a href=\"https:\/\/github.com\/kubernetes\/kubectl\/issues\/527\">still doesn't support<\/a>\u00a0a true\u00a0<em>get all<\/em>\u00a0command, so you need to list manually the type of resources you want to export. You can get a list of resource types with<br><code>kubectl api-resources<\/code> <br><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"\n<p>ombining other answers, this is what I came up with for bash:<\/p>\n<p> 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 &#8211;export $n > $n.yaml done kubectl get all &#8211;export=true -o yaml !\/bin\/env bash i=$((0)) for n in $(kubectl get -o=custom-columns=NAMESPACE:.metadata.namespace,KIND:.kind,NAME:.metadata.name pv,pvc,configmap,ingress,service,secret,deployment,statefulset,hpa,job,cronjob &#8211;all-namespaces | grep -v &#8216;secrets\/default-token&#8217;) do [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7867"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7867"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7867\/revisions"}],"predecessor-version":[{"id":7868,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7867\/revisions\/7868"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}