概述
最近在玩 Rancher, 先從最基本的功能玩起, 目前有幾個已經搭建好的 K8S 集群, 需要批量匯入, 發現官網已經有批量匯入的檔案了. 根據 Rancher v2.6 進行驗證微調后總結經驗.
1. Rancher UI 獲取創建集群引數
-
訪問
Rancher_URL/v3/clusters/,單擊右上角“Create”,創建匯入集群:
-
在引數填寫頁面中,修改以下引數:
dockerRootDir默認為/var/lib/docker,如果 dockerroot 路徑有修改,需要修改此配置路徑;enableClusterAlerting(可選) 根據需要選擇是否默認開啟集群告警;enableClusterMonitoring(可選) 根據需要選擇是否默認開啟集群監控;name(必填) 設定集群名稱,名稱具有唯一性,不能與現有集群名稱相同;
-
配置好引數后單擊
Show Request; -
在彈出的視窗中,復制
API Request中HTTP Request:的{}中的內容,此內容即為創建的集群的 API 引數;
#!/bin/bash
api_url='https://rancher-demo.example.com'
api_token='token-dbkgj:7pqf5rrjmlxxxxxxxxxxxxxxxxxxxxxxxtrnfljwtxh'
cluster_name=$1
create_cluster_data()
{
cat <<EOF
{
"agentEnvVars": [ ],
"aksConfig": null,
"aliyunEngineConfig": null,
"amazonElasticContainerServiceConfig": null,
"answers": null,
"azureKubernetesServiceConfig": null,
"clusterTemplateRevisionId": "",
"defaultClusterRoleForProjectMembers": "",
"defaultPodSecurityPolicyTemplateId": "",
"dockerRootDir": "/var/lib/docker",
"eksConfig": null,
"enableClusterAlerting": false,
"enableClusterMonitoring": false,
"gkeConfig": null,
"googleKubernetesEngineConfig": null,
"huaweiEngineConfig": null,
"k3sConfig": null,
"localClusterAuthEndpoint": null,
"name": "$cluster_name",
"rancherKubernetesEngineConfig": null,
"rke2Config": null,
"scheduledClusterScan": null,
"windowsPreferedCluster": false
}
EOF
}
curl -k -X POST \
-H "Authorization: Bearer ${api_token}" \
-H "Content-Type: application/json" \
-d "$(create_cluster_data)" $api_url/v3/clusters
2. 創建集群
-
保存以上代碼為腳本檔案,最后執行腳本,
./rancher_import_cluster.sh <your-cluster-name> -
腳本執行完成后,集群狀態如下所示,其狀態為
Provisioning;
3. 創建注冊命令
這一步可能不需要, 創建集群時就會自動生成 clusterregistrationtokens
這里又生成了一遍, 會導致有多條 clusterregistrationtokens
4. 獲取主機注冊命令
復制并保存以下內容為腳本檔案,修改前三行api_url、token、cluster_name,然后執行腳本,
#!/bin/bash
api_url='https://rancher-demo.example.com'
api_token='token-dbkgj:7pqf5rrjmlbgtssssssssssssssssssssssssssssnfljwtxh'
cluster_name=$1
cluster_ID=$( curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters | jq -r ".data[] | select(.name == \"$cluster_name\") | .id" )
# nodeCommand
#curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].nodeCommand
# command
#curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].command
# insecureCommand
curl -s -k -H "Authorization: Bearer ${api_token}" $api_url/v3/clusters/${cluster_ID}/clusterregistrationtokens | jq -r .data[].insecureCommand
??Notes:
這里看需要, 有 3 種命令:
nodeCommand: 直接通過 docker 來執行的;command: 通過kubectl來執行的;insecureCommand: 私有 CA 證書, 通過curl結合kubectl來執行的.這里我使用了第三種
AllInOne
#!/bin/bash
api_url='https://rancher-demo.example.com'
api_token='token-dbkgj:7pqf5rrjxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxljwtxh'
cluster_name=$1
create_cluster_data()
{
cat <<EOF
{
"agentEnvVars": [ ],
"aksConfig": null,
"aliyunEngineConfig": null,
"amazonElasticContainerServiceConfig": null,
"answers": null,
"azureKubernetesServiceConfig": null,
"clusterTemplateRevisionId": "",
"defaultClusterRoleForProjectMembers": "",
"defaultPodSecurityPolicyTemplateId": "",
"dockerRootDir": "/var/lib/docker",
"eksConfig": null,
"enableClusterAlerting": false,
"enableClusterMonitoring": false,
"gkeConfig": null,
"googleKubernetesEngineConfig": null,
"huaweiEngineConfig": null,
"k3sConfig": null,
"localClusterAuthEndpoint": null,
"name": "$cluster_name",
"rancherKubernetesEngineConfig": null,
"rke2Config": null,
"scheduledClusterScan": null,
"windowsPreferedCluster": false
}
EOF
}
curl -k -X POST \
-H "Authorization: Bearer ${api_token}" \
-H "Content-Type: application/json" \
-d "$(create_cluster_data)" $api_url/v3/clusters >/dev/null
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
./rancher_import_cluster.sh <your-cluster-name>
執行后會輸出一條命令, 在被匯入集群上執行如下命令:
# 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
即可匯入成功.
??????
??TODO:
后面再把登錄到對應集群的 master 機器, 并執行命令納入腳本.
系列文章
- Rancher 系列文章
???參考檔案
- 使用腳本創建匯入集群 | Rancher檔案
三人行, 必有我師; 知識共享, 天下為公. 本文由東風微鳴技術博客 EWhisper.cn 撰寫.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/548414.html
標籤:其他
