duplicate kubernetes namespace with the content

10/14/2019

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

e.g. Duplicate default to my-namespace which will have the same content.

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 :

Error from server (Invalid): Service "my-service" is invalid: spec.clusterIP: Invalid value: "10.108.14.29": provided IP is already allocated
-- Inforedaster
kubernetes
namespaces
testing

2 Answers

10/14/2019

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

As was proposed, when you invoke kubectl get all command, k8s looks through resources catalog bounded to all category. Therefore, if you didn't add this category for each custom CRD object, throughout specific API group, you might probably miss some relevant k8s resources in the command output.

Furthermore, if you desire to export all k8s resources from the particular namespace, besides user workloads, I would recommend exploring API resources, filtering out only namespace scoped objects, and then apply bash processing to generate manifest files across each resource group:

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

You can also consider using Helm (as @David Maze pointed out in the comment), in order to manage user workloads through Helm Charts, as a more flexible and structured way to describe k8s native resources.

-- mk_sta
Source: StackOverflow

10/14/2019

There is no specific way to do this. You could probably get close with something like kubectl get all -n sourcens -o yaml | sed -e 's/namespace: sourcens/namespace: destns/' | kubectl apply -f - but get all is always a bit wonky and this could easily miss weird edge cases.

-- coderanger
Source: StackOverflow