From eb076b4cb09cebb538c055aed8d8d67917875657 Mon Sep 17 00:00:00 2001 From: xuezhaojun Date: Sat, 21 Sep 2024 12:58:11 +0800 Subject: [PATCH] Add multiplehubs solution and rename multiple-agents-hosted to multiple-agents Signed-off-by: xuezhaojun --- .../README.md | 4 +- .../setup-hub1.sh | 0 .../setup-hub2.sh | 0 solutions/multiple-agents/README.md | 2 +- solutions/multiplehubs/README.md | 59 +++++++++++++++++++ solutions/multiplehubs/setup.sh | 40 +++++++++++++ 6 files changed, 102 insertions(+), 3 deletions(-) rename solutions/{multiple-hubs-hosted => multiple-agents-hosted}/README.md (95%) rename solutions/{multiple-hubs-hosted => multiple-agents-hosted}/setup-hub1.sh (100%) rename solutions/{multiple-hubs-hosted => multiple-agents-hosted}/setup-hub2.sh (100%) create mode 100644 solutions/multiplehubs/README.md create mode 100755 solutions/multiplehubs/setup.sh diff --git a/solutions/multiple-hubs-hosted/README.md b/solutions/multiple-agents-hosted/README.md similarity index 95% rename from solutions/multiple-hubs-hosted/README.md rename to solutions/multiple-agents-hosted/README.md index 5c9246536..87d7ea0da 100644 --- a/solutions/multiple-hubs-hosted/README.md +++ b/solutions/multiple-agents-hosted/README.md @@ -1,6 +1,6 @@ -# Manage a cluster with multiple hubs in hosted mode +# Manage a cluster with multiple agents in hosted mode -The scripts provided in this doc help you to setup an Open Cluster Management (OCM) environment with Kind clusters where a single cluster is managed by two hubs. The agents of the second hub runs in the hosted mode on a hostng cluster. +The scripts provided in this doc help you to setup an Open Cluster Management (OCM) environment with Kind clusters where a single cluster is managed by two hubs via two agents installed on the managed cluster. The agents of the second hub runs in the hosted mode on a hosting cluster. ## Prerequisite diff --git a/solutions/multiple-hubs-hosted/setup-hub1.sh b/solutions/multiple-agents-hosted/setup-hub1.sh similarity index 100% rename from solutions/multiple-hubs-hosted/setup-hub1.sh rename to solutions/multiple-agents-hosted/setup-hub1.sh diff --git a/solutions/multiple-hubs-hosted/setup-hub2.sh b/solutions/multiple-agents-hosted/setup-hub2.sh similarity index 100% rename from solutions/multiple-hubs-hosted/setup-hub2.sh rename to solutions/multiple-agents-hosted/setup-hub2.sh diff --git a/solutions/multiple-agents/README.md b/solutions/multiple-agents/README.md index 984a565ae..95f34efdf 100644 --- a/solutions/multiple-agents/README.md +++ b/solutions/multiple-agents/README.md @@ -1,6 +1,6 @@ # Manage a spoke installed with multiple agents connected to different hubs -The scripts provided in this document help you set up an Open Cluster Management (OCM) environment with Kind clusters, where a single cluster is managed by two hubs. The agents of the two hubs are all running in the default mode on the managed cluster under different namespaces. +The scripts provided in this document help you set up an Open Cluster Management (OCM) environment with Kind clusters, where a single cluster is managed by two hubs via two agents installed on the managed cluster. The agents of the two hubs are all running in the default mode on the managed cluster under different namespaces. ## Prerequisite diff --git a/solutions/multiplehubs/README.md b/solutions/multiplehubs/README.md new file mode 100644 index 000000000..dec46a2d8 --- /dev/null +++ b/solutions/multiplehubs/README.md @@ -0,0 +1,59 @@ +# Multiplehubs + +In this demo, we are going to use 3 clusters: `hub1`, `hub2` and `agent` to show the MultipleHubs feature. + +We will enable MultipleHubs feature on the managedcluster, the agent will first be registered to hub1. + +Then we will trigger the managedcluster to switch to hub2 by manually by setting the ManagedCluster `hubAcceptsClient` to false on hub1. + +After that, we will observe that the managedcluster will automatically register to hub2. + +Next, We set the ManagedCluster `hubAcceptsClient` to true on hub1 and delete the hub2, we will observe that the managedcluster will automatically register back to hub1 and be available. + +## Prerequisites + +Run the following command to install the prerequisites: + +```bash +sh setup.sh +``` + +## Switch agent to hub2 + +Run the following command to switch the managedcluster to hub2, first set the ManagedCluster `hubAcceptsClient` to false on hub1, then watch the managedcluster to be unaccepted from hub1: + +```bash +kubectl --context kind-hub1 patch managedcluster agent --type=merge -p '{"spec":{"hubAcceptsClient":false}}' +kubectl --context kind-hub1 get managedcluster -w +``` + +Wait for the managedcluster to be `Available` on hub2: + +```bash +kubectl --context kind-hub2 get managedcluster -w +``` + +## Switch agent to hub1 + +Set the ManagedCluster `hubAcceptsClient` back to true on hub1: +```bash +kubectl --context kind-hub1 patch managedcluster agent --type=merge -p '{"spec":{"hubAcceptsClient":true}}' +``` + +It takes about 5 minutes for the managedcluster to be updated to `Unknown` on hub1: + +```bash +kubectl --context kind-hub1 get managedcluster -w +``` + +Delete the hub2: + +```bash +kind delete cluster --name hub2 +``` + +After 5 minutes, the managedcluster to be switched back to hub1 and be `Available`: + +```bash +kubectl --context kind-hub1 get managedcluster -w +``` diff --git a/solutions/multiplehubs/setup.sh b/solutions/multiplehubs/setup.sh new file mode 100755 index 000000000..3cd35a155 --- /dev/null +++ b/solutions/multiplehubs/setup.sh @@ -0,0 +1,40 @@ +# In this demo, we are going to use 3 clusters: hub1, hub2 and agent. +# +# We will enable MultipleHubs feature on agent, the agent will first be registered to hub1. +# Then we manually reject the registration from agent on hub1 by setting the managed cluster `hubAcceptsClient` to false. +# After that, we will observe that the agent will automatically register to hub2. +# Next, We set the managed cluster `hubAcceptsClient` to true on hub1 and delete the hub2, we will observe that the agent will automatically register back to hub1. + +kind create cluster --name hub1 +kind create cluster --name hub2 +kind create cluster --name agent + +# Init the hub1 +clusteradm init --wait --bundle-version=latest --context kind-hub1 +joinhub1=$(clusteradm get token --context kind-hub1 | grep clusteradm) + +# Join the agent into the hub1 +$(echo ${joinhub1} --bundle-version=latest --force-internal-endpoint-lookup --wait --context kind-agent | sed "s//agent/g") +clusteradm accept --context kind-hub1 --clusters agent --wait + +# Init the hub2 +clusteradm init --wait --bundle-version=latest --context kind-hub2 + +# Prepare the hub1, hub2 bootstrap kubeconfigs +kind get kubeconfig --name hub1 --internal > hub1.kubeconfig +kind get kubeconfig --name hub2 --internal > hub2.kubeconfig + +# Save hub1, hub2 bootstrap kubeconfigs as secrets in the open-cluster-management-agent ns +kubectl --context kind-agent create secret generic hub1-kubeconfig -n open-cluster-management-agent --from-file=kubeconfig=hub1.kubeconfig +kubectl --context kind-agent create secret generic hub2-kubeconfig -n open-cluster-management-agent --from-file=kubeconfig=hub2.kubeconfig + +# Enable AutoApproval on hub1, hub2 +# The username is kubernetes-admin, which is the default admin user in kind clusters +kubectl --context kind-hub1 patch clustermanager cluster-manager --type=merge -p '{"spec":{"registrationConfiguration":{"featureGates":[ +{"feature": "ManagedClusterAutoApproval", "mode": "Enable"}], "autoApproveUsers":["kubernetes-admin"]}}}' +kubectl --context kind-hub2 patch clustermanager cluster-manager --type=merge -p '{"spec":{"registrationConfiguration":{"featureGates":[ +{"feature": "ManagedClusterAutoApproval", "mode": "Enable"}], "autoApproveUsers":["kubernetes-admin"]}}}' + +# Enable MultipleHubs feature on agent +kubectl --context kind-agent patch klusterlet klusterlet --type=merge -p '{"spec":{"registrationConfiguration":{"featureGates":[ +{"feature": "MultipleHubs", "mode": "Enable"}], "bootstrapKubeConfigs":{"type":"LocalSecrets", "localSecretsConfig":{"hubConnectionTimeoutSeconds":180,"kubeConfigSecrets":[{"name":"hub1-kubeconfig"},{"name":"hub2-kubeconfig"}]}}}}}'