Rancher Article Series - Rancher v2.6 Importing Clusters Using Scripts
This article was last updated on: July 24, 2024 am
overview
Recently playing Rancher, starting with the most basic functions, there are currently several K8S clusters that have been built, which need to be imported in batches and discoveredThe official website already has documents imported in batchesSummarize the experience after verifying and fine-tuning according to Rancher v2.6.
1. Rancher UI gets the parameters for creating a cluster
visitRancher_URL/v3/clusters/, click Create in the upper-right corner to create an import cluster:
On the Parameters page, modify the following parameters:
dockerRootDir The default is/var/lib/docker, if the dockerroot path is modified, this configuration path needs to be modified;
enableClusterAlerting(Optional) Select whether to enable cluster alarms by default.
enableClusterMonitoring(Optional) Select whether to enable cluster monitoring by default.
name(Required) Set the cluster name, which is unique and cannot be the same as an existing cluster name;
After configuring the parameters, click ClickShow Request;
In the pop-up window, copyAPI RequestmiddleHTTP Request:target{}, which is the API parameter of the created cluster;
if [ $? -eq 0 ]; then cluster_ID=$( curl -s -k -H "Authorization: Bearer ${api_token}"$api_url/v3/clusters | jq -r ".data[] | select(.name == \"$cluster_name\") | .id" ) # insecureCommand curl -s -k -H "Authorization: Bearer ${api_token}"$api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].insecureCommand echo"Please execute the above command in the imported cluster to complete the process." else echo"Import cluster in rancher failed" fi
1
./rancher_import_cluster.sh <your-cluster-name>
After execution, a command will be output, and the following command will be executed on the imported cluster:
1 2 3 4 5 6 7 8 9 10
# curl --insecure -sfL https://rancher-demo.example.com/v3/import/lzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxqm6v4lp576c6mg_c-vwv5l.yaml | kubectl apply -f - clusterrole.rbac.authorization.k8s.io/proxy-clusterrole-kubeapiserver created clusterrolebinding.rbac.authorization.k8s.io/proxy-role-binding-kubernetes-master created namespace/cattle-system created serviceaccount/cattle created clusterrolebinding.rbac.authorization.k8s.io/cattle-admin-binding created secret/cattle-credentials-ec53bfa created clusterrole.rbac.authorization.k8s.io/cattle-admin created deployment.apps/cattle-cluster-agent created service/cattle-cluster-agent created
The import is successful.
🎉🎉🎉
📝TODO:
Later, log in to the master machine of the corresponding cluster and execute the command into the script.