Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save simonswine/6bf3b665e4117f42b550c3ea12dd171a to your computer and use it in GitHub Desktop.
Save simonswine/6bf3b665e4117f42b550c3ea12dd171a to your computer and use it in GitHub Desktop.
Copying kubernetes resources accross namespaces
kubectl get rs,secrets -o json --namespace old | jq '.items[].metadata.namespace = "new"' | kubectl create-f -
@slomek
Copy link

slomek commented Jul 31, 2017

There's a typo: should be kubectl create -f instead of kubectl create-f near the end.

@marcellodesales
Copy link

@slomek That's right! @simonswine thank you for providing this!

@Eyjafjallajokull
Copy link

Version without jq:

kubectl get secrets -n old -o yaml | sed 's/namespace: old/namespace: new/'  | kubectl create -f -

@cgrinker
Copy link

cgrinker commented May 3, 2018

Reads corrected

kubectl get rs,secrets -o json --namespace old | jq '.items[].metadata.namespace = "new"' | kubectl create -f  -

@ST-DDT
Copy link

ST-DDT commented Jul 16, 2018

It might be neccessary to delete some metadata in order to apply/create it. I'm not sure which values I had to delete to make it work, but the UUID is probably a good start.

@dilumr
Copy link

dilumr commented Jul 25, 2018

Also consider using the --export option on get command:

--export=false: If true, use 'export' for the resources.  Exported resources are stripped of cluster-specific information.

@ST-DDT
Copy link

ST-DDT commented Jun 27, 2019

Export is now deprecated 😢

@cmoulliard
Copy link

The command kubectl get secret www-snowdrop-dev-tls -n snowdrop-site -o yaml | sed 's/namespace: snowdrop-site/namespace: generator-site/' | kubectl create -n generator-site -f - is failing

for: "STDIN": Operation cannot be fulfilled on secrets "www-snowdrop-dev-tls": the object has been modified; please apply your changes to the latest version and try again

@jeff1985
Copy link

So what is the alternative to --export ?

@Sureshravi40
Copy link

I wanted too make a copy all my resources running in sandbox namespace to staging namespace , Hence tired running this following cmd

kubectl get rs,secrets -o json --namespace sandbox | jq '.items[].metadata.namespace = "staging"' | kubectl create -f -

A copy of my resources gets created inside staging namespace, all the pods fails with an error:

authorization-55b866f8c4-7nkxp 0/1 CreateContainerConfigError 0 77s
consults-6b796cf5f4-wrjbf 0/1 CreateContainerConfigError 0 77s
core-api-8dd575db9-9bgth 0/1 CreateContainerConfigError 0 77s

what I am doing wrong? I just wanted to make a copy of this resources into staging namespace and play around

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment