<p>I would like to create a new namespace which is identical to the old one.</p>
<p>My approach would look something like this (full command below):</p>
<pre><code>kubectl get &lt;resources&gt; -o json --namespace OLD-NAMESPACE | jq '.items[].metadata.namespace = &quot;NEW-NAMESPACE&quot;' | kubectl create -f  -
</code></pre>
<p>This basically gets all resource definitions in a json format, replaces the old namespace with the new one, and applies everything.</p>
<p>Unfortunately, this does not work, since the old definitions contain namespace specific things. To avoid this, there used to be an <code>--export</code> flag, which has been deprecated <a href="https://stackoverflow.com/questions/43941772/get-yaml-for-deployed-kubernetes-services">see here</a>.</p>
<p>Any Idea how I could do this?</p>
<p><strong>Full command:</strong></p>
<pre><code>kubectl get bindings,configmaps,endpoints,limitranges,persistentvolumeclaims,persistentvolumes,pods,podtemplates,replicationcontrollers,resourcequotas,secrets,mutatingwebhookconfigurations,validatingwebhookconfigurations,controllerrevisions,daemonsets,deployments,replicasets,statefulsets,tokenreviews,localsubjectaccessreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,horizontalpodautoscalers,cronjobs,jobs,certificatesigningrequests,leases,endpointslices,events,ingressclasses,networkpolicies,runtimeclasses,poddisruptionbudgets,rolebindings,roles,debugattachments,csidrivers,volumeattachments -o json --namespace OLD-NAMESPACE | jq '.items[].metadata.namespace = &quot;NEW-NAMESPACE&quot;' | kubectl create -f  -
</code></pre>

User12547645
<p>you can try this</p>
<pre><code>kubectl get all -n OLD_NAMESPACE -o yaml | sed -e 's/namespace: OLD_NAMESPACE/namespace:  NEW_NAMESPACE/' | kubectl apply -f -
</code></pre>
<p>this will work</p>
<p>if you are using a higher K8s version above 1.14 <strong>sed</strong> will work to replace the namespaces</p>
<p>if <code>kubectl get all</code> not adding all resources we can apply like</p>
<pre><code>kubectl get bindings,configmaps,endpoints,limitranges,persistentvolumeclaims,persistentvolumes,pods,podtemplates,replicationcontrollers,resourcequotas,secrets,mutatingwebhookconfigurations,validatingwebhookconfigurations,controllerrevisions,daemonsets,deployments,replicasets,statefulsets,tokenreviews,localsubjectaccessreviews,selfsubjectaccessreviews,selfsubjectrulesreviews,subjectaccessreviews,horizontalpodautoscalers,cronjobs,jobs,certificatesigningrequests,leases,endpointslices,events,ingressclasses,networkpolicies,runtimeclasses,poddisruptionbudgets,rolebindings,roles,debugattachments,csidrivers,volumeattachments -n OLD_NAMESPACE -o yaml | sed -e 's/namespace: OLD_NAMESPACE/namespace:  NEW_NAMESPACE/' | kubectl apply -f -
</code></pre>
