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 ...
随机推荐
- XSS跨站脚本攻击(Cross Site Scripting)
XSS是跨站脚本攻击(Cross Site Scripting),不写为CSS是为了避免和层叠样式表(Cascading Style Sheets)的缩写混淆,所以将跨站脚本攻击写为XSS. 攻击者可 ...
- IIS 7.0、IIS 7.5 和 IIS 8.0 中的 HTTP 状态代码
https://support.microsoft.com/zh-cn/help/943891/the-http-status-code-in-iis-7-0--iis-7-5--and-iis-8- ...
- oracle函数及相关问题
show user 查看当前用户select * from tab: 查看当前数据库中的表desc 表名 : 查看表结构 between and 包含开头结尾 函数: months_between(a ...
- 【逆向】x64dbg设置条件断点 比较内存字符串是否相等
前言 在OD中可以设置条件断点,通过表达式对字符串数据进行比较,比如在CreateFile打开某个特定文件的时候让调试器中断.但是在x32dbg.x64dbg中因为表达式只支持整数,不支持字符串和其它 ...
- iOS学习十二之选择器控件UIPickerView
UIPickerView是一个简易的列表控件,用于提供有限个数的选项供用户选择. 它是通过代理和数据源的方法对其进行设置和数据源填充的,这种控件的设计模式也是代理模式的应用之一. 添加下面的代码即可实 ...
- uniapp里面设置onlaunch以后在加载页面调接口
main.js 里面 Vue.prototype.$onLaunched = new Promise(resolve => { Vue.prototype.$isResolve = resolv ...
- @Async异步注解的使用
@Async 简介 使用spring快速开启异步执行服务的注解 应用场景 同步:同步就是整个处理过程顺序执行,当各个过程都执行完毕,并返回结果. 异步: 异步调用则是只是发送了调用的指令,调用者无需等 ...
- Shell脚本基本命令4
使用join连接字段 1.$ cat >sales 创建salse文件 #业务员数据 注释说明 #业务员量 joe 100 jane 200 herman 150 chris 300 2.$ ...
- IntelliJ IDEA 最新激活破解教程,永久激活,亲测有效
使用ja-netfilter激活Jetbrains系列软件 注意:无限试用脚本已经失效.本教程适合2021.3.*之上的高版本,使用ja-netfilter插件进行激活操作,永久有效 激活步骤: 第一 ...
- jsp第三个作业
main.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf ...