How to backup Kubernetes' Config

Jan. 20, 2022

The easiest solution seems to be following script by Stackoverflow user, Timothy Perez

:

  <pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">#!/bin/bash

# NAMESPACED EXPORTS
 for  ns in  $( kubectl get ns --no-headers | cut -d &#34; &#34;  -f1 ) ;  do
  kubectl --namespace = &#34; ${ ns} &#34;  get -o = json bindings,cm,ep,ev,limits,pvc,po,podtemplates,rc,quota,secrets,sa,svc,controllerrevisions,ds,deploy,rs,sts,localsubjectaccessreviews,hpa,cj,jobs,leases,ev,ds,deploy,ing,netpol,rs,pods,netpol,pdb,roles,rolebindings |  \
       jq '.items[] |
     select(.type!=&#34;kubernetes.io/service-account-token&#34;) |
     del(
         .spec.clusterIP,
         .metadata.uid,
         .metadata.selfLink,
         .metadata.resourceVersion,
         .metadata.creationTimestamp,
         .metadata.generation,
         .status,
         .spec.template.spec.securityContext,
         .spec.template.spec.dnsPolicy,
         .spec.template.spec.terminationGracePeriodSeconds,
         .spec.template.spec.restartPolicy
     )'  >> &#34;./ ${ ns} .json&#34;
 done

# NON-NAMESPACED EXPORTS
kubectl get -o = json cs,ns,no,pv,mutatingwebhookconfigurations,validatingwebhookconfigurations,crds,apiservices,tokenreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,csr,psp,nodes,psp,clusterrolebindings,clusterroles,pc,sc,volumeattachments |  \
       jq '.items[] |
     select(.type!=&#34;kubernetes.io/service-account-token&#34;) |
     del(
         .spec.clusterIP,
         .metadata.uid,
         .metadata.selfLink,
         .metadata.resourceVersion,
         .metadata.creationTimestamp,
         .metadata.generation,
         .status,
         .spec.template.spec.securityContext,
         .spec.template.spec.dnsPolicy,
         .spec.template.spec.terminationGracePeriodSeconds,
         .spec.template.spec.restartPolicy
     )'  >> &#34;./cluster_non-namespaced_export.json&#34;

Script copied here under CC BY-SA 4.0 license

.