22

I am getting error while copying the kubernetes secret from one namespace to another:

kubectl get secret secret1 --namespace=test --export -o=yaml | kubectl apply --namespace=test1 -f -

Error: unknown flag: --export
See 'kubectl get --help' for usage.
error: no objects passed to apply
cloudbud
  • 2,948
  • 5
  • 28
  • 54
  • can you please update the status of the question if your issue is resolved. it's due to version of K8s cluster you are running please check it once and update the command as @kiruba suggested for information you can also check: https://stackoverflow.com/a/64985982/5525824 – Harsh Manvar Dec 06 '20 at 07:05

3 Answers3

41

--export option has been deprecated in version 1.14 and removed in version 1.18. If you are using kubernetes version 1.18 or above, you can try using below command (using sed) to copy secret from one namespace to other.

kubectl get secret secret1 --namespace=test -o yaml | sed 's/namespace: test/namespace: test1/g' | kubectl create -f -  

Thanks,

Kiruba
  • 1,322
  • 8
  • 15
  • How to achieve the same use-case if I need to copy the secrets between two clusters? – AkshayBadri Jul 07 '21 at 17:22
  • @B.Akshay save the output from the sed command to a file instead of piping it back into kubectl. then switch contexts and then use kubectl apply on the file you created. Alternatively, use the `--server` and `--token` parameters to kubectl in the final command. – Segfault Aug 01 '21 at 17:55
0

Export is deprecated in latest version of Openshift. We can directly do it like below in openshift. Replace oc with kubectl if you are in kubernates.

oc get virtualservices --all-namespaces -o yaml > project.yaml  --> for all namespaces
oc get virtualservices -n <your-namespace>-all-namespaces -o yaml > project.yaml
Nilesh Kumar
  • 109
  • 1
  • 5
0

Actually you should be good just without --export This command works for me to copy secret between contexts:

kubectl get secret <secret> --context <context_1> -o yaml | kubectl apply --context <context_2> -f -