Rancher系列文章-Rancher v2.6使用脚本实现导入集群
概述
最近在玩 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 机器, 并执行命令纳入脚本.
系列文章
️参考文档
三人行, 必有我师; 知识共享, 天下为公. 本文由东风微鸣技术博客 EWhisper.cn 编写.
Rancher系列文章-Rancher v2.6使用脚本实现导入集群的更多相关文章
- shell脚本一键同步集群时间
shell脚本一键同步集群时间 弋嘤捕大 椿澄辄 ψ壤 茇徜燕 ㄢ交涔沔 阚龇棚绍 テ趼蜱棣 灵打了个寒颤也没有去甩脱愣是拖着 喇吉辔 秋北酏崖 琮淄脸酷 茇呶剑 莲夤罱 陕遇骸淫 ...
- 02使用java脚本向Pxc集群写入数据
使用java脚本向Pxc集群写入数据 批量写入pxc集群程序 导入mysql驱动包 # 批量插入数据的java脚本 package pxc_demo; import java.sql.Connecti ...
- Shell脚本实现---Swarm集群部署实例(Swarm Cluster)
Shell脚本实现---Swarm集群部署实例(Swarm Cluster) 一.机器环境(均是centos7.8) IP hostname 角色 192.168.10.200 manager-swa ...
- 【Kubernetes 系列四】Kubernetes 实战:管理 Hello World 集群
目录 1. 创建集群 1.1. 安装 kubectl 1.1.1. 安装 kubectl 到 Linux 1.1.2. 安装 kubectl 到 macOS 1.1.3. 安装 kubectl 到 W ...
- shell脚本启动所有集群节点
#profile变量追加到.bashrc中 cat /etc/profile >> ~/.bashrc #start-all-cluster.sh 启动脚本 #!/bin/bash ec ...
- shell脚本监控k8s集群job状态,若出现error通过触发阿里云的进程监控报警
#!/bin/bash while [ 1 ] do job_error_no=`kubectl get pod -n weifeng |grep -i "job"|grep -c ...
- 从0到1使用Kubernetes系列(三):使用Ansible安装Kubernetes集群
前两期的文章介绍了Kubernetes基本概念和架构,用Kubeadm+Ansible搭建Kubernetes集群所需要的工具及其作用.本篇介绍怎么使用Ansible安装Kubernetes集群. 启 ...
- MySQL集群系列1:2台机器搭建双主集群
先配置静态IP 2台机器mysql密码一样,最好在同一局域网内,最好在mysql刚安装时就配置好,后面有数据了不好同步. 本文实现了2台机器mysql数据同步成功: 配置my.cnf 先关闭防火墙 s ...
- Rancher 2.2.2 - HA 部署高可用k8s集群
对于生产环境,需以高可用的配置安装 Rancher,确保用户始终可以访问 Rancher Server.当安装在Kubernetes集群中时,Rancher将与集群的 etcd 集成,并利用Kuber ...
- 近万字案例:Rancher + VMware PKS实现全球数百站点K8S集群管理
Sovereign Systems是一家成立于2007年的技术咨询公司,帮助客户将传统数据中心技术和应用程序转换为更高效的.基于云的技术平台,以更好地应对业务挑战.曾连续3年提名CRN,并且在2012 ...
随机推荐
- C++ 函数参数与按值传递
C++ 函数参数与按值传递 C++ 通常安值传递参数,这意味着将数值参数传递给函数,而后者将其赋给一个新的变量. double volume = cube(side); 其中,side 是一个变量. ...
- Restful Fast Request 添加前置脚本,实现不同环境免设置token 直接请求
idea安装Restful Fast Request插件后,进行如下设置,并打开 项目全局参数 对话框 进入前置脚本 tab 编写如下groovy脚本代码(插件脚本语言默认支持groovy,该语言被称 ...
- Mac设置自动连接蓝牙设备
1.安装blueutil管理软件 通过 brew install blueutil 2.通过命令: blueutil --paired 查看当前连接过的蓝牙设备的地址. 3.获取blueutil软件的 ...
- ios底部安全距离
一.使用背景 苹果官方推荐:使用env(),constant()来适配,env()和constant(),是IOS11新增特性,用于设定安全区域与边界的距离 safe-area-inset-left: ...
- ajax的简单应用
jsp文件: <%@ page import="java.text.SimpleDateFormat" %><%@ page import="java. ...
- jetson TX2 + opencv3.4 + python3 + 双目 +人脸检测
淘宝看到一款很便宜的双目,150元,就买了.想着用它学习一下opencv,好换个工作.当然,也想着能否用它做一些好玩的,比如三维重建之类高大上的东西.先用便宜的入个门,等以后眼界高了再看是不是买那些更 ...
- STL练习-排列2
Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0<=数字<=9 ...
- APIView使用
1 CBV源码分析 # 基于类的视图--->讲过了 -必须要继承它:from django.views import View # drf的东西,都是写cbv,必须要继承View##### ...
- pycharm2019.3.1版本需要的JetBrains Runtime 11不支持windows 32位系统。
提示信息显示安装pycharm2019.3.1版本需要的JetBrains Runtime 11不支持windows 32位系统. 2.更换pycharm社区版的安装版本 百度找到解决办法,参考文章& ...
- python 编程找出矩阵中的幸运数字:说明,在一个给定的M*N的矩阵(矩阵中的取值0-1024,且各不相同),如果某一个元素的值在同一行中最小,并且在同一列中元素最大,那么该数字为幸运数字。
假设给定矩阵如下: matrix=[[10,36,52], [33,24,88], [66,76,99]] 那么输出结果应为66(同时满足条件) 代码如下: arr=[[10,36,52], [33, ...