From 153c388f81c8895f0b125556dc966186f7762320 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Tue, 6 Sep 2022 15:24:35 +0800 Subject: [PATCH 1/2] Add cluster-api solution Signed-off-by: Jian Qiu --- solutions/cluster-api/README.md | 44 ++++++++++++++++++++++++++++++ solutions/cluster-api/capi-join.sh | 27 ++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 solutions/cluster-api/README.md create mode 100755 solutions/cluster-api/capi-join.sh diff --git a/solutions/cluster-api/README.md b/solutions/cluster-api/README.md new file mode 100644 index 000000000..970e6faa3 --- /dev/null +++ b/solutions/cluster-api/README.md @@ -0,0 +1,44 @@ +# Work with cluster-api + +[Cluster API](https://cluster-api.sigs.k8s.io/) is a Kubernetes sub-project focused on providing declarative APIs and +tooling to simplify provisioning, upgrading, and operating multiple Kubernetes clusters. This doc is a guideline on how +to use `cluster-api` and `open-cluster-management` project together + +## Prerequisite +Download and install [clusteradm](https://github.com/open-cluster-management-io/clusteradm/releases). For Linux OS, run the following commands: + +```shell +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 +``` + +Follow the instruction [here](https://cluster-api.sigs.k8s.io/user/quick-start.html) to install the `clusterctl` + +## Initialize the cluster api management plane and create cluster + +Before initilize the management plane, some feature gates should be enabled: + +```shell +export CLUSTER_TOPOLOGY=true +export EXP_CLUSTER_RESOURCE_SET=true +``` + +Next initiate the cluster api management plane by following [this](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialize-the-management-cluster) + +Then you can create a cluster on any cloud using cluster-api. Suppose the name of the cluster created is `capi-cluster` + +## Initialize the ocm hub and register the cluster created by cluster api + +ocm hub can run in the same cluster of cluster api management plane + +``` +clusteradm init --use-bootstrap-token +``` + +then run + +``` +./capi-join.sh capi-cluster +``` + diff --git a/solutions/cluster-api/capi-join.sh b/solutions/cluster-api/capi-join.sh new file mode 100755 index 000000000..9f2c10a21 --- /dev/null +++ b/solutions/cluster-api/capi-join.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +cd $(dirname ${BASH_SOURCE}) + +set -e + +echo "get bootstrap token from ocm hub cluster" +joincmd=$(clusteradm get token --use-bootstrap-token | grep clusteradm) + +echo "Join $1 to hub" +$(echo ${joincmd} --dry-run --output-file join.yaml | sed "s//$1/g") +kubectl create secret generic import-secret-$1 --from-file=join.yaml --type=addons.cluster.x-k8s.io/resource-set + +cat << EOF | kubectl apply -f - +apiVersion: addons.cluster.x-k8s.io/v1alpha3 +kind: ClusterResourceSet +metadata: + name: import-$1 +spec: + strategy: "ApplyOnce" + clusterSelector: + matchLabels: + cluster.x-k8s.io/cluster-name: $1 + resources: + - name: import-secret + kind: Secret +EOF \ No newline at end of file From 281891bef757635242817b680ddfdb6fb42d7483 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Wed, 7 Sep 2022 11:34:20 +0800 Subject: [PATCH 2/2] Resolve comments Signed-off-by: Jian Qiu --- solutions/cluster-api/README.md | 25 +++++++++++-------- .../{capi-join.sh => register-join.sh} | 10 +++++--- 2 files changed, 21 insertions(+), 14 deletions(-) rename solutions/cluster-api/{capi-join.sh => register-join.sh} (82%) diff --git a/solutions/cluster-api/README.md b/solutions/cluster-api/README.md index 970e6faa3..ac540151c 100644 --- a/solutions/cluster-api/README.md +++ b/solutions/cluster-api/README.md @@ -1,8 +1,8 @@ -# Work with cluster-api +# Working with the Cluster API project [Cluster API](https://cluster-api.sigs.k8s.io/) is a Kubernetes sub-project focused on providing declarative APIs and tooling to simplify provisioning, upgrading, and operating multiple Kubernetes clusters. This doc is a guideline on how -to use `cluster-api` and `open-cluster-management` project together +to use the Cluster API project and the [Open Cluster Management (OCM)](https://open-cluster-management.io/) project together. ## Prerequisite Download and install [clusteradm](https://github.com/open-cluster-management-io/clusteradm/releases). For Linux OS, run the following commands: @@ -13,9 +13,9 @@ wget -qO- https://github.com/open-cluster-management-io/clusteradm/releases/late sudo chmod +x /usr/local/bin/clusteradm ``` -Follow the instruction [here](https://cluster-api.sigs.k8s.io/user/quick-start.html) to install the `clusterctl` +Follow the instruction [here](https://cluster-api.sigs.k8s.io/user/quick-start.html) to install the `clusterctl`. -## Initialize the cluster api management plane and create cluster +## Initialize the Cluster API management plane and create a cluster Before initilize the management plane, some feature gates should be enabled: @@ -24,21 +24,26 @@ export CLUSTER_TOPOLOGY=true export EXP_CLUSTER_RESOURCE_SET=true ``` -Next initiate the cluster api management plane by following [this](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialize-the-management-cluster) +Next initiate the Cluster API management plane by following [this](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialize-the-management-cluster) -Then you can create a cluster on any cloud using cluster-api. Suppose the name of the cluster created is `capi-cluster` +Now create a cluster on any cloud provider by following the instruction [here](https://cluster-api.sigs.k8s.io/user/quick-start.html#create-your-first-workload-cluster). -## Initialize the ocm hub and register the cluster created by cluster api +## Initialize the OCM multicluster control plane hub cluster and register the newly created cluster -ocm hub can run in the same cluster of cluster api management plane +The OCM multicluster control plane hub can be run in the same cluster as the Cluster API management plane. Use the following command to initialize the hub cluster: ``` clusteradm init --use-bootstrap-token ``` -then run +Register the newly created cluster by running the following command: ``` -./capi-join.sh capi-cluster +./register-join.sh # ./register-join.sh capi-cluster ``` +The registration process might take a while. After it's done, you can run the following command to verify the newly created cluster has successfully joined the hub cluster: + +``` +kubectl get managedcluster +``` diff --git a/solutions/cluster-api/capi-join.sh b/solutions/cluster-api/register-join.sh similarity index 82% rename from solutions/cluster-api/capi-join.sh rename to solutions/cluster-api/register-join.sh index 9f2c10a21..000483e9b 100755 --- a/solutions/cluster-api/capi-join.sh +++ b/solutions/cluster-api/register-join.sh @@ -4,24 +4,26 @@ cd $(dirname ${BASH_SOURCE}) set -e +CLUSTER_NAME=$1 + echo "get bootstrap token from ocm hub cluster" joincmd=$(clusteradm get token --use-bootstrap-token | grep clusteradm) echo "Join $1 to hub" -$(echo ${joincmd} --dry-run --output-file join.yaml | sed "s//$1/g") +$(echo ${joincmd} --dry-run --output-file join.yaml | sed "s//$CLUSTER_NAME/g") kubectl create secret generic import-secret-$1 --from-file=join.yaml --type=addons.cluster.x-k8s.io/resource-set cat << EOF | kubectl apply -f - apiVersion: addons.cluster.x-k8s.io/v1alpha3 kind: ClusterResourceSet metadata: - name: import-$1 + name: import-$CLUSTER_NAME spec: strategy: "ApplyOnce" clusterSelector: matchLabels: - cluster.x-k8s.io/cluster-name: $1 + cluster.x-k8s.io/cluster-name: $CLUSTER_NAME resources: - name: import-secret kind: Secret -EOF \ No newline at end of file +EOF