Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone Google Container Cluster / Kubernetes cluster?

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.

like image 676
Paweł Szczur Avatar asked Jan 12 '17 09:01

Paweł Szczur


People also ask

How do you clone a Kubernetes cluster?

If you want to clone your cluster's Kubernetes configuration, you can use Heptio Velero. The Kubernetes etcd makes this straightforward, as the full configuration is stored there. But before you do that, you need to copy the cluster's virtual machines and the other resources on which Kubernetes runs.


2 Answers

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

like image 179
Camil Avatar answered Oct 04 '22 02:10

Camil


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
like image 33
Chronograph3r Avatar answered Oct 04 '22 02:10

Chronograph3r