多云组网部署 k3s 集群的另一种选择Tailscale

本文最后更新于 92 天前, 如有失效请评论区留言.

随着最近帕鲁流行,各大云服务商都推出了类似活动,作为买鸡大户,选择又买了几台。趁着春节决定好好折腾一下。

前因

根据k3s官网文档,我本来是基于wireguard进行全球组网的,后面经过测试,如果某个节点因为大陆反向墙了或者主动屏蔽了海外流量的话,会导致这个节点流量会异常增加(数据来源自哪吒监控), 所有想探索一下有没有其他方式。

在k3s v1.27.3v1.26.6及更高版本中,支持Tailscale 集成,以便节点使用 Tailscale VPN 服务在节点之间构建网格。

在这之前先简单介绍我之前怎么部署的

嵌入式 k3s 多云解决方案

K3s 使用 wireguard 为集群流量建立 VPN 网格。每个节点都必须有一个唯一的 IP,通过该 IP 可以访问它们, 集群 (CNI) 流量将使用 wireguard 隧道。

  • 控制节点配置参数
# SERVER_EXTERNAL_IP 这个通常是节点的公网ip
--node-external-ip=<SERVER_EXTERNAL_IP> --flannel-backend=wireguard-native --flannel-external-ip
  • 计算节点配置参数
# AGENT_EXTERNAL_IP 这个通常是节点的公网ip
--node-external-ip=<AGENT_EXTERNAL_IP>

改造

安装tailscale

curl -fsSL https://tailscale.com/install.sh | sh

我的节点基本都已经安装了tailscale,这步可以跳过。如果你安装比较慢可以考虑试试我的镜像源,但不一定好使哈。

tailscale镜像使用帮助

已经登录的,或者已经配置了route都没关系,不用管。

申请tailscale密钥

在Tailscale控制台生成一个身份验证密钥 $AUTH-KEY,该密钥可对集群中的所有节点重复使用Settings > Keys

申请URL

注意点:

  • 勾选Reusable, 开启Key重复使用
  • 记好Key, 仅可见一次,key有效期不用管,注册后可以通过节点的属性设置有效期

开启自动允许路由(可选)

使用tailscale组网的话,每个节点会自动发布自己节点的路由

配置路径Access controls URL

// Example/default ACLs for unrestricted connections.
{
	"autoApprovers": {
		"routes": {
			"10.42.0.0/16": ["ysicing@github"],
		},
	},
	// Declare static groups of users. Use autogroups for all users or users with a specific role.
	// "groups": {
	//  	"group:example": ["alice@example.com", "bob@example.com"],
	// },

节选如上, 具体podCIDR根据你的情况确定,后面的用户也根据你实际情况来写,由于我是用github登录的,我的账号信息就是ysicing@github

改造

编辑所有的/etc/systemd/system/k3s.service, 由于我是手搓的配置,文件名都一样.如果你通过脚本安装的可能是k3s.service/k3s-agent.service

注释掉所有--flannel-external-ip,--flannel-backend wireguard-native,--node-external-ip参数

新增--vpn-auth="name=tailscale,joinKey=tskey-auth-..." \ 参数,joinKey的值就是对应的上面步骤申请的密钥

完成上述步骤重启即可

systemctl daemon-reload
systemctl restart k3s

观察生效

kubectl get nodes -o wide

所有节点的INTERNAL-IP都是100打头开始的就行。

问题QA

节点INTERNAL-IP不变

  • 首先检查有没有开启ipv6,没有的话开启支持ipv6

如果不出意外编辑 /etc/sysctl.conf,把1改成0,编辑结果如下

net.ipv6.conf.all.disable_ipv6 = 0
net.ipv6.conf.default.disable_ipv6 = 0
net.ipv6.conf.lo.disable_ipv6 = 0

生效sysctl -p

然后重启k3s看看, 如果还不变的话,可能需要看看k3s日志

如果日志如下, 提示没法识别ipv6

Feb 07 22:23:02 node1 k3s[1448143]: E0207 22:23:02.346550 1448143 kubelet_node_status.go:701] "Failed to set some node status fields" err="failed to validate secondaryNodeIP: node IP: \"fd7a:115c:a1e0:ab12:4843:cd96:6278:6544\" not found in the host's network interfaces" node="node1"

需要给k3s新增启动参数

--kubelet-arg "node-ip=0.0.0.0" \

然后重载并重启k3s再观察。

调整优势

  • 可以支持NAT机器加入组网中
  • 节点如果双栈支持,避免ipv4被打,集群节点失联

Chapters

Sponsor

Like this article? $1 reward

Comments

According to the relevant laws and regulations of the People's Republic of China, the comment function of the current website has been disabled. If you need to comment, please visit ysicing.me, but the comments still need to be reviewed by AI.