From 892e74bb296b66b93b0040ef2dc0ab3a85063a94 Mon Sep 17 00:00:00 2001 From: Mircea Cosbuc Date: Tue, 6 Dec 2022 16:06:49 +0100 Subject: [PATCH] Add multi-cluster CLI GitOps example --- samples/multi-cluster-cli-gitops/README.md | 19 ++++++++++ .../argocd/application.yaml | 23 ++++++++++++ .../argocd/project.yaml | 23 ++++++++++++ .../resources/job.yaml | 36 +++++++++++++++++++ .../resources/replica-set.yaml | 25 +++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 samples/multi-cluster-cli-gitops/README.md create mode 100644 samples/multi-cluster-cli-gitops/argocd/application.yaml create mode 100644 samples/multi-cluster-cli-gitops/argocd/project.yaml create mode 100644 samples/multi-cluster-cli-gitops/resources/job.yaml create mode 100644 samples/multi-cluster-cli-gitops/resources/replica-set.yaml diff --git a/samples/multi-cluster-cli-gitops/README.md b/samples/multi-cluster-cli-gitops/README.md new file mode 100644 index 0000000..1e251d6 --- /dev/null +++ b/samples/multi-cluster-cli-gitops/README.md @@ -0,0 +1,19 @@ +# Multi-Cluster CLI GitOps Samples + +This is an example of using the `multi-cluster-cli` in a [GitOps](https://www.weave.works/technologies/gitops/) operating model to perform a recovery of the dataplane +in a multi-cluster deployment scenario. For more details on managing multi-cluster resources with the kubernetes operator see [the official documentation](https://www.mongodb.com/docs/kubernetes-operator/master/multi-cluster/). The example is applicable for an [ArgoCD](https://argo-cd.readthedocs.io/) configuration. + +## ArgoCD configuration +The files in the [argocd](./argocd) contain an [AppProject](./argocd/project.yaml) and an [Application](./argocd/application.yaml) linked to it which allows the synchronization of `MongoDBMulti` resources from a Git repo. + +## Multi-Cluster CLI Job setup +To enable the manual disaster recovery using the CLI, this sample provides a [Job](./resources/job.yaml) which runs the recovery subcommand as a [PreSync hook](https://argo-cd.readthedocs.io/en/stable/user-guide/resource_hooks/). This ensures that the multicluster environment is configured before the application of the modified [`MongoDBMulti`](./resources/replica-set.yaml) resource. The `Job` mounts the same `kubeconfig` that the operator is using to connect to the clusters defined in your architecture. + +### Build the multi-cluster CLI image +You can build a minimal image containing the CLI executable using the `Dockerfile` [provided in this repo](./../../tools/multicluster/Dockerfile). +``` shell +git clone https://github.com/mongodb/mongodb-enterprise-kubernetes +cd mongodb-enterprise-kubernetes/tools/multicluster +docker build . -t "your-registry/multi-cluster-cli:latest" +docker push "your-registry/multi-cluster-cli:latest" +``` diff --git a/samples/multi-cluster-cli-gitops/argocd/application.yaml b/samples/multi-cluster-cli-gitops/argocd/application.yaml new file mode 100644 index 0000000..870170e --- /dev/null +++ b/samples/multi-cluster-cli-gitops/argocd/application.yaml @@ -0,0 +1,23 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: multi-cluster-replica-set + namespace: argocd + finalizers: + - resources-finalizer.argocd.argoproj.io + labels: + name: database +spec: + project: my-project + source: + repoURL: https://github.com/mongodb/mongodb-enterprise-kubernetes + targetRevision: "fix/ubi-8-repo-names" + path: samples/multi-cluster-cli-gitops + destination: + server: https://central.mongokubernetes.com + namespace: mongodb + syncPolicy: + automated: + prune: true + syncOptions: + - CreateNamespace=true diff --git a/samples/multi-cluster-cli-gitops/argocd/project.yaml b/samples/multi-cluster-cli-gitops/argocd/project.yaml new file mode 100644 index 0000000..90a0df8 --- /dev/null +++ b/samples/multi-cluster-cli-gitops/argocd/project.yaml @@ -0,0 +1,23 @@ +apiVersion: argoproj.io/v1alpha1 +kind: AppProject +metadata: + name: my-project + namespace: argocd + finalizers: + - resources-finalizer.argocd.argoproj.io +spec: + description: Example Project + sourceRepos: + - '*' + destinations: + - namespace: mongodb + server: https://central.mongokubernetes.com + clusterResourceWhitelist: + # Allow MongoDBMulti resources to be synced + - group: '' + kind: MongoDBMulti + # Allow Jobs to be created (used for sync hooks in this example) + - group: '' + kind: Job + - group: '' + kind: Namespace diff --git a/samples/multi-cluster-cli-gitops/resources/job.yaml b/samples/multi-cluster-cli-gitops/resources/job.yaml new file mode 100644 index 0000000..4d476cc --- /dev/null +++ b/samples/multi-cluster-cli-gitops/resources/job.yaml @@ -0,0 +1,36 @@ +# Sample PreSync job to perform the manual dataplane recovery before a replica set sync +--- +apiVersion: batch/v1 +kind: Job +metadata: + generateName: multicluster-cli-recover- + annotations: + argocd.argoproj.io/hook: PreSync + argocd.argoproj.io/hook-delete-policy: HookSucceeded +spec: + template: + spec: + containers: + - name: multicluster-cli + image: your-registry/multi-cluster-cli + env: + - name: KUBECONFIG + value: /etc/config/kubeconfig + args: + - "-central-cluster=central.mongokubernetes.com" + - "-member-clusters=cluster1.mongokubernetes.com,cluster2.mongokubernetes.com,cluster4.mongokubernetes.com" + - "-member-cluster-namespace=mongodb" + - "-central-cluster-namespace=mongodb" + - "-operator-name=mongodb-enterprise-operator-multi-cluster" + - "-source-cluster=cluster1.mongokubernetes.com" + volumeMounts: + - mountPath: /etc/config/kubeconfig + name: kube-config-volume + restartPolicy: Never + volumes: + - name: kube-config-volume + secret: + defaultMode: 420 + secretName: mongodb-enterprise-operator-multi-cluster-kubeconfig + + backoffLimit: 2 diff --git a/samples/multi-cluster-cli-gitops/resources/replica-set.yaml b/samples/multi-cluster-cli-gitops/resources/replica-set.yaml new file mode 100644 index 0000000..45306e2 --- /dev/null +++ b/samples/multi-cluster-cli-gitops/resources/replica-set.yaml @@ -0,0 +1,25 @@ +# sample mongodb-multi replicaset yaml +--- +apiVersion: mongodb.com/v1 +kind: MongoDBMulti +metadata: + name: multi-replica-set +spec: + version: 4.4.0-ent + type: ReplicaSet + persistent: false + duplicateServiceObjects: false + credentials: my-credentials + opsManager: + configMapRef: + name: my-project + clusterSpecList: + # provide spec per cluster + clusterSpecs: + # cluster names where you want to deploy the replicaset + - clusterName: cluster1.mongokubernetes.com + members: 2 + - clusterName: cluster2.mongokubernetes.com + members: 1 + - clusterName: cluster4.mongokubernetes.com + members: 2