How to clone Google Container Cluster / Kubernetes

2020-06-23 06:00发布

问题:

As in title. I want to clone (create a copy of existing cluster).

If it's not possible to copy/clone Google Container Engine cluster, then how to clone Kubernetes cluster?

If that's not possible, is there a way to dump the whole cluster config?

Note:

I try to modify the cluster's configs by calling:

kubectl apply -f some-resource.yaml

But nothing stops me/other employee modifying the cluster by running:

kubectl edit service/resource

Or setting properties from command line kubectl calls.

回答1:

I'm using a bash script from CoreOS team, with small adjustments, that works pretty good. By default it's excluding the kube-system namespace, but you can adjust this if you need. Also you can add or remove the resources you want to copy.

for ns in $(kubectl get ns --no-headers | cut -d " " -f1); do
  if { [ "$ns" != "kube-system" ]; }; then
  kubectl --namespace="${ns}" get --export -o=json svc,rc,rs,deployments,cm,secrets,ds,statefulsets,ing | \
jq '.items[] |
    select(.type!="kubernetes.io/service-account-token") |
    del(
        .spec.clusterIP,
        .metadata.uid,
        .metadata.selfLink,
        .metadata.resourceVersion,
        .metadata.creationTimestamp,
        .metadata.generation,
        .status,
        .spec.template.spec.securityContext,
        .spec.template.spec.dnsPolicy,
        .spec.template.spec.terminationGracePeriodSeconds,
        .spec.template.spec.restartPolicy
    )' >> "./my-cluster.json"
  fi
done

To restore it on another cluster, you have to execute kubectl create -f ./my-cluster.json



回答2:

You can now create/clone an existing cluster, On the Clusters page, click on create cluster and choose an existing cluster. But remember, this will not clone the api-resources you may have to use a third party tool such as Velero to help you backup the resources.

Here are some useful links

  • Cluster Creation
  • Velero
  • Medium Article on How to use Velero