美文网首页
Rancher系列文章-Rancher v2.6使用脚本实现导入

Rancher系列文章-Rancher v2.6使用脚本实现导入

作者: 东风微鸣 | 来源:发表于2023-03-27 10:16 被阅读0次

概述

最近在玩 Rancher, 先从最基本的功能玩起, 目前有几个已经搭建好的 K8S 集群, 需要批量导入, 发现官网已经有批量导入的文档了. 根据 Rancher v2.6 进行验证微调后总结经验.

1. Rancher UI 获取创建集群参数

  1. 访问Rancher_URL/v3/clusters/,单击右上角“Create”,创建导入集群:

    Rancher API 创建导入集群
  2. 在参数填写页面中,修改以下参数:

    • dockerRootDir 默认为/var/lib/docker,如果 dockerroot 路径有修改,需要修改此配置路径;
    • enableClusterAlerting(可选) 根据需要选择是否默认开启集群告警;
    • enableClusterMonitoring(可选) 根据需要选择是否默认开启集群监控;
    • name(必填) 设置集群名称,名称具有唯一性,不能与现有集群名称相同;
  3. 配置好参数后单击Show Request

  4. 在弹出的窗口中,复制API RequestHTTP 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. 创建集群

  1. 保存以上代码为脚本文件,最后执行脚本。

    ./rancher_import_cluster.sh <your-cluster-name>
    
  2. 脚本执行完成后,集群状态如下所示,其状态为Provisioning;

    导入后状态

3. 创建注册命令

这一步可能不需要, 创建集群时就会自动生成 clusterregistrationtokens

这里又生成了一遍, 会导致有多条 clusterregistrationtokens

4. 获取主机注册命令

复制并保存以下内容为脚本文件,修改前三行api_urltokencluster_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 种命令:

  1. nodeCommand: 直接通过 docker 来执行的;
  2. command: 通过kubectl 来执行的;
  3. 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 机器, 并执行命令纳入脚本.

系列文章

📚️参考文档

三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.

相关文章

  • linux安装和使用Rancher

    linux安装和使用Rancher Rancher介绍请看如下博客 arm架构安装Rancher并导入k8s集群解...

  • Rancher 1.6 调用API 进行 stacks upgr

    Rancher提供了api给我们调用,从而实现不用通过访问Rancher UI 或使用 Rancher CLI 来...

  • Rancher入门

    Rancher是什么 Rancher是一个开源的企业级容器管理平台。通过Rancher,企业再也不必自己使用一系列...

  • Rancher概览

    Rancher文档 Rancher是一个开源的企业级容器管理平台。通过Rancher,企业再也不必自己使用一系列的...

  • rancher使用(2)

    Rancher是什么 Rancher是一个开源的企业级容器管理平台。通过Rancher,企业再也不必自己使用一系列...

  • Rancher 用户安装 Rainbond

    本文适用于正在使用 Rancher 或对 Rancher 有所了解的用户 Rancher,Kubernetes 生...

  • 快速安装docker

    使用rancher提供的shell脚本能快速安装docker。 #!/bin/bash DOCKER_VERSIO...

  • rancher配置负载均衡

    前面的文章中已经讲了怎么通过gitlab+rancher实现devops自动化部署,今天继续讲怎么在rancher...

  • 远程发布/部署

    远程使用Shell脚本去发布(镜像/rsync直传) Docker远程发布/Rancher集群管理/Kuberne...

  • rancher教程目录

    rancher教程一(安装)rancher教程二(nfs)rancher教程三(jenkins)rancher教程...

网友评论

      本文标题:Rancher系列文章-Rancher v2.6使用脚本实现导入

      本文链接:https://www.haomeiwen.com/subject/wrxzrdtx.html