<p>How can I duplicate a namespace with all content with a new name in the same kubernetes cluster?</p>

<p>e.g. Duplicate default to my-namespace which will have the same content.</p>

<p>I'm interested just by services and deployments, so
when I try with method with kubectl get all and with api-resources i have error with services IP like :</p>

<pre><code>Error from server (Invalid): Service "my-service" is invalid: spec.clusterIP: Invalid value: "10.108.14.29": provided IP is already allocated
</code></pre>

Inforedaster
<p>As @coderanger mentioned in his answer, there is no straight way to make a copy of origin k8s resources to the separate namespace. </p>

<p>As was proposed, when you invoke <code>kubectl get all</code> command, k8s looks through resources catalog bounded to <strong><em>all</em></strong> category. Therefore, if you didn't add this category for each custom <a href="https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition" rel="nofollow noreferrer"><em>CRD</em></a> object, throughout specific <a href="https://kubernetes.io/docs/reference/using-api/#api-groups" rel="nofollow noreferrer">API group</a>, you might probably miss some relevant k8s resources in the command output. </p>

<p>Furthermore, if you desire to export all k8s resources from the particular namespace, besides user <a href="https://kubernetes.io/docs/concepts/#kubernetes-objects" rel="nofollow noreferrer"><em>workloads</em></a>, I would recommend exploring <a href="https://kubernetes.io/docs/reference/kubectl/cheatsheet/#resource-types" rel="nofollow noreferrer">API resources</a>, filtering out only namespace scoped objects, and then apply bash processing to generate manifest files across each resource group:</p>

<pre><code>kubectl api-resources --namespaced=true| awk '{print $1}'| sed '1d'| while read -r line; do echo "$(kubectl get $line -n namespace -o yaml &gt; $line.yaml)"; done
</code></pre>

<p>You can also consider using <a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a> (as @David Maze pointed out in the comment), in order to manage user workloads through Helm <a href="https://helm.sh/docs/developing_charts/#charts" rel="nofollow noreferrer"><em>Charts</em></a>, as a more flexible and structured way to describe k8s native resources.</p>
