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

文章介绍了如何使用Rancherv2.6的API和bash脚本来批量导入已有的K8S集群。首先,通过RancherUI获取集群创建参数,然后创建一个bash脚本执行POST请求以创建集群。集群创建后,获取注册命令,特别是使用`insecureCommand`以完成集群的注册。最后,执行在导入集群上的命令以完成整个过程。
摘要由CSDN通过智能技术生成

最近在玩 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

即可导入成功.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Rancher是一个开源的容器管理平台,它提供了丰富的功能来简化和管理容器的部署和管理过程。其中一个重要的功能是警报驱动程序(Alerting Drivers),它可以帮助用户及时了解集群和容器的健康状况,并及时采取相应的措施。 Rancher支持多种警报驱动程序,其中之一是sachet配置。Sachet是一个基于Web的错误日志和异常信息聚合工具,它可以将来自不同源的警报信息集中展示,帮助用户更好地监控和分析系统中的问题。 要使用rancher-alerting-drivers-sachet配置,首先需要在Rancher中启用警报驱动程序插件。然后,您需要从Rancher的应用商店中安装rancher-alerting-drivers-sachet插件。安装完成后,您可以在Rancher的Alerting配置页面中配置Sachet。 在Sachet的配置中,您需要提供Sachet的API地址、API密钥等信息。这些信息将用于与Sachet服务器建立连接,将Rancher中的警报数据传输到Sachet中。您还可以选择配置警报的级别、触发条件等。 一旦配置完成,当Rancher中的警报触发时,警报信息将被发送到Sachet中,并在Sachet的控制台中显示出来。您可以通过Sachet的界面查看警报信息、分析问题,并采取相应的措施。 总的来说,rancher-alerting-drivers-sachet配置是在Rancher中启用和配置Sachet作为警报驱动程序的一种方式。它可以帮助用户更好地监控和管理容器集群,及时发现和解决问题,提高系统的稳定性和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

系统免驱动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值