Is there a way to export a resource from a kube cluster so that it can be applied to another cluster

4/16/2020

I'm trying to export some kube resources (configmaps, deployments, services) from one cluster so that it can be applied to another.

Something like this ....

kubectl --kubeconfig ${src} -n ${ns} get configmaps ${cm} -o yaml | \ kubectl --kubeconfig ${dst} -n ${ns} apply -f -

It kind works, once. The first time where the resource hasn't been created yet. If I run it a second time, trying to update the destination I get error like...

the object has been modified; please apply your changes to the latest version and try again.

I suspect it has something to do with the resourceVersion or timestamp fields that appear in the output.

Is there away to output a resource so that is striped of all server side specific info? Other that a bunch of sed delete and grep -v statements.

TIA

-- J.E.
kubernetes

3 Answers

4/16/2020

So, with export being depricated, I came up with this....

sub_string_A="resourceVersion|selfLink|uid|creationTimestamp"

sub_string_B="resourceVersion|selfLink|uid|creationTimestamp|annotations|last-applied-configuration|status|loadBalancer|clusterIP"

namespace=test

src=clusterA.yml
dst=clusterB.yml

dry_run=false

kubectl --kubeconfig ${src} -n ${namespace} get ingress -o yaml \
  | grep -v -E "${sub_string_A}" \
  | kubectl --kubeconfig ${dst} -n ${namespace} --dry-run=${dry_run} apply -f -

kubectl --kubeconfig ${src} -n ${namespace} get statefulsets -o yaml \
  | grep -v -E "${sub_string_B}" \
  | kubectl --kubeconfig ${dst} -n ${namespace} --dry-run=${dry_run} apply -f -

Ingress seems to behave differently than other reources. So it get a different pattern.

-- J.E.
Source: StackOverflow

4/16/2020

You need to edit the output data to remove some of the metadata fields like UID, creation time, etc.

-- coderanger
Source: StackOverflow

4/16/2020

You can use below command with --export flag to strip of cluster-specific information.

kubectl get cm cmname -o yaml --export > cm.yaml

A word of caution that --export is deprecated and going to be removed in future. So use it at your own risk.

-- Arghya Sadhu
Source: StackOverflow