From 370dbba05c6baae7cfb7d89927ab28c2ce0a3d80 Mon Sep 17 00:00:00 2001 From: Yang Le Date: Thu, 2 Nov 2023 09:34:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=96=20add=20solutions=20of=20multiple?= =?UTF-8?q?=20hubs=20(#304)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Yang Le --- solutions/multiple-hubs-hosted/README.md | 91 ++++++++++++++++++++ solutions/multiple-hubs-hosted/setup-hub1.sh | 26 ++++++ solutions/multiple-hubs-hosted/setup-hub2.sh | 32 +++++++ solutions/multiple-hubs/README.md | 76 ++++++++++++++++ solutions/multiple-hubs/setup-hub1.sh | 26 ++++++ solutions/multiple-hubs/setup-hub2.sh | 28 ++++++ 6 files changed, 279 insertions(+) create mode 100644 solutions/multiple-hubs-hosted/README.md create mode 100755 solutions/multiple-hubs-hosted/setup-hub1.sh create mode 100755 solutions/multiple-hubs-hosted/setup-hub2.sh create mode 100644 solutions/multiple-hubs/README.md create mode 100755 solutions/multiple-hubs/setup-hub1.sh create mode 100755 solutions/multiple-hubs/setup-hub2.sh diff --git a/solutions/multiple-hubs-hosted/README.md b/solutions/multiple-hubs-hosted/README.md new file mode 100644 index 000000000..350a3f8df --- /dev/null +++ b/solutions/multiple-hubs-hosted/README.md @@ -0,0 +1,91 @@ +# Manage a cluster with multiple hubs 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. + +## Prerequisite + +- [kind](https://kind.sigs.k8s.io) must be installed on your local machine. The Kubernetes version must be >= 1.19, see [kind user guide](https://kind.sigs.k8s.io/docs/user/quick-start/#creating-a-cluster) for more details. + +- Download and install [clusteradm](https://github.com/open-cluster-management-io/clusteradm/releases). For Linux OS, run the following commands: + + ``` + wget -qO- https://github.com/open-cluster-management-io/clusteradm/releases/latest/download/clusteradm_linux_amd64.tar.gz | sudo tar -xvz -C /usr/local/bin/ + + sudo chmod +x /usr/local/bin/clusteradm + ``` + +## Setup the first hub cluster + +Run `./setup-hub1.sh`. It creates two kind clusters: +- `hub1`. It is initialized as a OCM hub cluster. +- `cluster1`. It joins the `hub1` as a managed cluster in the default mode.
+ +Once it is done, you can then list the managed clusters on `hub1` with command below. +```bash +kubectl get managedclusters +NAME HUB ACCEPTED MANAGED CLUSTER URLS JOINED AVAILABLE AGE +cluster1 true https://cluster1-control-plane:6443 29s +``` +The managed cluster `cluster1` joins `hub1` in the default mode and its Klusterlet resource is created on the managed cluster. +```bash +kubectl get klusterlet +NAME AGE +klusterlet 1m +``` +And the OCM agent of `hub1` is installed under namespace `open-cluster-management-agent` on the managed cluster. Another namespace `open-cluster-management-agent-addon` is created too for add-ons which might be installed in future for this managed cluster. +```bash +kubectl get ns +NAME STATUS AGE +default Active 7m34s +kube-node-lease Active 7m34s +kube-public Active 7m34s +kube-system Active 7m34s +local-path-storage Active 7m30s +open-cluster-management Active 7m27s +open-cluster-management-agent Active 7m27s +open-cluster-management-agent-addon Active 7m11s +``` +## Setup the second hub cluster +Run `./setup-hub2.sh`. It creates two more Kind clusters: +- `hub2`. It is initialized as the second OCM hub cluster. +- `hosting`. It is used as the hosting cluster on which the OCM agent of `hub2` is running. + +The script also joins the previously created Kind cluster `cluster1` to `hub2` as a managed cluster in the hosted mode. Once it is done, you can list the managed clusters on `hub2` with command below. +```bash +kubectl get managedclusters +NAME HUB ACCEPTED MANAGED CLUSTER URLS JOINED AVAILABLE AGE +cluster1 true https://cluster1-control-plane:6443 True True 38s +``` +Since the managed cluster `cluster1` joins in the hosted mode, its Klusterlet resource is created on the hosting cluster instead of the managed cluster. +```bash +kubectl get klusterlet +NAME AGE +klusterlet-hosted-cuz6ia 53s +``` +The OCM agent of `hub2` is installed under namespace `klusterlet-hosted-cuz6ia` on the hosting cluster as well. +```bash +kubectl get ns +NAME STATUS AGE +default Active 77s +klusterlet-hosted-cuz6ia Active 57s +kube-node-lease Active 77s +kube-public Active 77s +kube-system Active 77s +local-path-storage Active 72s +open-cluster-management Active 57s +``` +Some namespaces, like `open-cluster-management-klusterlet-hosted-cuz6ia` and `open-cluster-management-klusterlet-hosted-cuz6ia-addon`, are created on the managed cluster `cluster1` which are required by the OCM agent of `hub2` running in the hosted mode. +```bash +kubectl get ns +NAME STATUS AGE +default Active 4m25s +kube-node-lease Active 4m25s +kube-public Active 4m25s +kube-system Active 4m25s +local-path-storage Active 4m21s +open-cluster-management Active 4m12s +open-cluster-management-agent Active 4m12s +open-cluster-management-agent-addon Active 4m3s +open-cluster-management-klusterlet-hosted-cuz6ia Active 78s +open-cluster-management-klusterlet-hosted-cuz6ia-addon Active 78s +``` \ No newline at end of file diff --git a/solutions/multiple-hubs-hosted/setup-hub1.sh b/solutions/multiple-hubs-hosted/setup-hub1.sh new file mode 100755 index 000000000..fff7c432d --- /dev/null +++ b/solutions/multiple-hubs-hosted/setup-hub1.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e + +hub=${HUB_NAME:-hub1} +cluster=${CLUSTER_NAME:-cluster1} + +hubctx="kind-${hub}" +clusterctx="kind-${cluster}" + +kind create cluster --name "${hub}" +kind create cluster --name "${cluster}" + +kubectl config use ${hubctx} +echo "Initialize the ocm hub cluster" +joincmd=$(clusteradm init --use-bootstrap-token | grep clusteradm) +kubectl wait --for=condition=HubRegistrationDegraded=false clustermanager cluster-manager --timeout=60s + +kubectl config use ${clusterctx} +echo "Join ${cluster} to ${hub}" +$(echo ${joincmd} --singleton --force-internal-endpoint-lookup --wait | sed "s//$cluster/g") + +kubectl config use ${hubctx} +echo "Accept join of ${cluster} on ${hub}" +clusteradm accept --clusters ${cluster} --wait + +kubectl get managedclusters diff --git a/solutions/multiple-hubs-hosted/setup-hub2.sh b/solutions/multiple-hubs-hosted/setup-hub2.sh new file mode 100755 index 000000000..e1df9250d --- /dev/null +++ b/solutions/multiple-hubs-hosted/setup-hub2.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e + +hub=${HUB_NAME:-hub2} +hosting=${HOSTING_CLUSTER_NAME:-hosting} +cluster=${CLUSTER_NAME:-cluster1} + +hubctx="kind-${hub}" +hostingctx="kind-${hosting}" +clusterctx="kind-${cluster}" + +kind create cluster --name "${hub}" +kind create cluster --name "${hosting}" + +kubectl config use ${hubctx} +echo "Initialize the ocm hub cluster" +joincmd=$(clusteradm init --use-bootstrap-token | grep clusteradm) +kubectl wait --for=condition=HubRegistrationDegraded=false clustermanager cluster-manager --timeout=60s + +kubectl config use ${clusterctx} +kubectl config view --flatten --minify | sed "s/127.0.0.1:[0-9]\{5\}/${cluster}-control-plane:6443/" > kubeconfig.${cluster} + +kubectl config use ${hostingctx} +echo "Join ${cluster} to ${hub}" +$(echo ${joincmd} --singleton --force-internal-endpoint-lookup --mode hosted --managed-cluster-kubeconfig kubeconfig.${cluster} --wait | sed "s//$cluster/g") +rm kubeconfig.${cluster} + +kubectl config use ${hubctx} +echo "Accept join of ${cluster} on ${hub}" +clusteradm accept --clusters ${cluster} --wait + +kubectl get managedclusters diff --git a/solutions/multiple-hubs/README.md b/solutions/multiple-hubs/README.md new file mode 100644 index 000000000..5449f8153 --- /dev/null +++ b/solutions/multiple-hubs/README.md @@ -0,0 +1,76 @@ +# Manage a cluster with multiple hubs + +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 thoes two hubs are all running in the default mode on the managed cluster under different namespaces. + +## Prerequisite + +- [kind](https://kind.sigs.k8s.io) must be installed on your local machine. The Kubernetes version must be >= 1.19, see [kind user guide](https://kind.sigs.k8s.io/docs/user/quick-start/#creating-a-cluster) for more details. + +- Download and install [clusteradm](https://github.com/open-cluster-management-io/clusteradm/releases). For Linux OS, run the following commands: + + ``` + wget -qO- https://github.com/open-cluster-management-io/clusteradm/releases/latest/download/clusteradm_linux_amd64.tar.gz | sudo tar -xvz -C /usr/local/bin/ + + sudo chmod +x /usr/local/bin/clusteradm + ``` + +## Setup the first hub cluster + +Run `./setup-hub1.sh`. It creates two Kind clusters: +- `hub1`. It is initialized as a OCM hub cluster. +- `cluster1`. It joins the `hub1` as a managed cluster.
+ +Once it is done, you can then list the managed clusters on `hub1` with command below. +```bash +kubectl get managedclusters +NAME HUB ACCEPTED MANAGED CLUSTER URLS JOINED AVAILABLE AGE +cluster1 true https://cluster1-control-plane:6443 29s +``` +The managed cluster `cluster1` joins `hub1` in the default mode and its Klusterlet resource is created on the managed cluster. +```bash +kubectl get klusterlet +NAME AGE +klusterlet 58s +``` +And the OCM agent of `hub1` is installed under namespace `open-cluster-management-agent` on the managed cluster. Another namespace `open-cluster-management-agent-addon` is created too for add-ons which might be installed in future for this managed cluster. +```bash +kubectl get ns +NAME STATUS AGE +default Active 7m34s +kube-node-lease Active 7m34s +kube-public Active 7m34s +kube-system Active 7m34s +local-path-storage Active 7m30s +open-cluster-management Active 7m27s +open-cluster-management-agent Active 7m27s +open-cluster-management-agent-addon Active 7m11s +``` +## Setup the second hub cluster +Run `./setup-hub2.sh` to create one more Kind cluster `hub2` and initialize it as the second OCM hub cluster. The script also joins the previously created Kind cluster `cluster1` to `hub2` as a managed cluster as well. Once it is done, you can list the managed clusters on `hub2` with command below. +```bash +kubectl get managedclusters +NAME HUB ACCEPTED MANAGED CLUSTER URLS JOINED AVAILABLE AGE +cluster1 true https://cluster1-control-plane:6443 True True 30s +``` +The managed cluster `cluster1` joins `hub2` in the default mode and its Klusterlet resource `klusterlet-hub2` is created on the managed cluster as well. +```bash +kubectl get klusterlet +NAME AGE +klusterlet 16m +klusterlet-hub2 1m +``` +And the OCM agent of `hub2` is installed under `open-cluster-management-agent-hub2` on the managed cluster. +```bash +kubectl get ns +NAME STATUS AGE +default Active 19m +kube-node-lease Active 19m +kube-public Active 19m +kube-system Active 19m +local-path-storage Active 18m +open-cluster-management Active 18m +open-cluster-management-agent Active 18m +open-cluster-management-agent-addon Active 18m +open-cluster-management-agent-hub2 Active 105s +open-cluster-management-agent-hub2-addon Active 105s +``` diff --git a/solutions/multiple-hubs/setup-hub1.sh b/solutions/multiple-hubs/setup-hub1.sh new file mode 100755 index 000000000..fff7c432d --- /dev/null +++ b/solutions/multiple-hubs/setup-hub1.sh @@ -0,0 +1,26 @@ +#!/bin/bash +set -e + +hub=${HUB_NAME:-hub1} +cluster=${CLUSTER_NAME:-cluster1} + +hubctx="kind-${hub}" +clusterctx="kind-${cluster}" + +kind create cluster --name "${hub}" +kind create cluster --name "${cluster}" + +kubectl config use ${hubctx} +echo "Initialize the ocm hub cluster" +joincmd=$(clusteradm init --use-bootstrap-token | grep clusteradm) +kubectl wait --for=condition=HubRegistrationDegraded=false clustermanager cluster-manager --timeout=60s + +kubectl config use ${clusterctx} +echo "Join ${cluster} to ${hub}" +$(echo ${joincmd} --singleton --force-internal-endpoint-lookup --wait | sed "s//$cluster/g") + +kubectl config use ${hubctx} +echo "Accept join of ${cluster} on ${hub}" +clusteradm accept --clusters ${cluster} --wait + +kubectl get managedclusters diff --git a/solutions/multiple-hubs/setup-hub2.sh b/solutions/multiple-hubs/setup-hub2.sh new file mode 100755 index 000000000..0ae0bbdbe --- /dev/null +++ b/solutions/multiple-hubs/setup-hub2.sh @@ -0,0 +1,28 @@ +#!/bin/bash +set -e + +hub=${HUB_NAME:-hub2} +cluster=${CLUSTER_NAME:-cluster1} + +hubctx="kind-${hub}" +clusterctx="kind-${cluster}" + +kind create cluster --name "${hub}" + +kubectl config use ${hubctx} +echo "Initialize the ocm hub cluster" +joincmd=$(clusteradm init --use-bootstrap-token | grep clusteradm) +kubectl wait --for=condition=HubRegistrationDegraded=false clustermanager cluster-manager --timeout=60s + +kubectl config use ${clusterctx} +echo "Join ${cluster} to ${hub}" +$(echo ${joincmd} --singleton --force-internal-endpoint-lookup --dry-run --output-file import.yaml | sed "s//$cluster/g") +kubectl create ns "open-cluster-management-agent-$hub" +cat import.yaml | sed "s/open-cluster-management-agent/open-cluster-management-agent-$hub/g" | sed "s/name: klusterlet/name: klusterlet-$hub/g" | kubectl apply -f - +rm import.yaml + +kubectl config use ${hubctx} +echo "Accept join of ${cluster} on ${hub}" +clusteradm accept --clusters ${cluster} --wait + +kubectl get managedclusters