多云搭建 K3S 集群

107

双十一薅了几个云厂商的羊毛,一开始搭建了k3s的单机版,后面就想着能不能搭建一个k3s集群,然后参考了这位大佬的文章,就试着用 WireGuard 来进行组网。它轻量、便捷、高效,而且数据全程加密传输,是依托公网组建虚拟局域网的优秀选择。

环境介绍

服务器介绍

云厂商 公网IP地址 内网IP地址 虚拟网络IP地址 操作系统 内核版本
腾讯云1 42.xx.xx.12 10.0.16.8 192.168.1.1 CentOS Linux release 7.9.2009 (Core) 5.15.2-1
腾讯云2 122.xx.xxx.111 10.0.0.6 192.168.1.2 CentOS Linux release 7.9.2009 (Core) 5.15.2-1
阿里云 122.xx.xx.155 172.17.0.3 192.168.1.3 CentOS Linux release 7.9.2009 (Core) 5.15.2-1

搭建前准备

在搭建跨云的 k3s 集群前,我们需要把 WireGuard 安装好,WireGuard 对内核是有要求的,所以内核已经要升级到 5.15.2-1.el7.elrepo.x86_64

在所有节点开启 IP 地址转发

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf

所有节点开启修改主机名称

# 腾讯云1执行
hostnamectl  set-hostname k3s-master
# 腾讯云2执行
hostnamectl  set-hostname k3s-node1
# 阿里云执行
hostnamectl  set-hostname k3s-node2

升级内核

几个服务器默认的内核都是 3.10 的,安装WireGuard 需要吧内核升级到比较高的版本。

升级内核前

先升级软件包(非必要)

yum update -y

添加 iptables 规则,允许本机的 NAT 转换:

所有节点都要执行

iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.1.1/24 -o eth0 -j MASQUERADE

wg0:为你定义的虚拟网卡

192.168.1.1: 为你的虚拟IP地址段

eth0:为你的物理网卡

升级内核

所有节点都要执行

方法1方法2方法3

方法1:

直接下载RPM包进行安装。

如果你想安装其他内核,你也可以在 这里下载

wget https://pan.cnsre.cn/d/Package/Linux/kernel/kernel-ml-5.15.2-1.el7.elrepo.x86_64.rpm
rpm -ivh kernel-ml-5.15.2-1.el7.elrepo.x86_64.rpm 

方法 2:

利用包管理工具更新

# 载入公钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 升级安装 elrepo
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-5.el7.elrepo.noarch.rpm
# 载入 elrepo-kernel 元数据
yum --disablerepo=\* --enablerepo=elrepo-kernel repolist
# 安装最新版本的内核
yum --disablerepo=\* --enablerepo=elrepo-kernel install  kernel-ml.x86_64  -y
# 删除旧版本工具包
yum remove kernel-tools-libs.x86_64 kernel-tools.x86_64  -y

方法3:

通过源码包编译安装.

这种方式可定制性强,但也比较复杂,有需要的可自行查找资料安装,下面只给出各系统版本内核源码包的下载地址

通过源码包编译安装.

这种方式可定制性强,但也比较复杂,有需要的可自行查找资料安装,下面只给出各系统版本内核源码包的下载地址 : https://www.kernel.org/pub/linux/kernel/

修改默认内核版本

# 查看当前实际启动顺序
grub2-editenv list
# 查看内核插入顺序
grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2
# 设置默认启动
grub2-set-default 'CentOS Linux (5.15.2-1.el7.elrepo.x86_64) 7 (Core)'
# 重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
# 重启服务器
reboot
# 验证当前内核版本
uname -r 

内核版本一定要是比较高的,不然启动WireGuard会有如下报错。

[#] ip link add wg0 type wireguard
RTNETLINK answers: Operation not supported
Unable to access interface: Protocol not supported
[#] ip link delete dev wg0
Cannot find device "wg0"

安装 WireGuard

所有节点执行

安装流程非常简单,我这里是直接将 CentOS 内核更新到目前最新的 5.15.2 版本,其中就已经包含了 WireGuard 的内核模块,只需要安装 wireguard-tools 这个 yum 包就行了。

yum install epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum install yum-plugin-elrepo kmod-wireguard wireguard-tools -y

配置 WireGuard

wireguard-tools 包提供了我们所需的工具 wgwg-quick,可以使用它们来分别完成手动部署和自动部署。

先按照官方文档描述的形式,生成腾讯云1用于加密解密的密钥

wg genkey | tee privatekey | wg pubkey > publickey

然后在当前目录下就生成了 privatekeypublickey 两个文件

密钥是配置到本机的,而公钥是配置到其它机器里的。

cat privatekey publickey
EMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=
0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=

现在我们需要与上述主机对等联网的 腾讯云2 阿里云 ,其公网IP(这边需要填写的是能与主机通信的IP)是 122.xx.xxx.111122.xx.xx.155

我们首先依照上面的流程安装 WireGuard 并生成好 腾讯云2 阿里云 的密钥。

然后编写 腾讯云1 完整的配置文件,以供 wg-quick 使用,在主机A的 /etc/wireguard/wg0.conf 中写入

[Interface]
PrivateKey = EMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=
Address = 192.168.1.1
ListenPort = 5418

[Peer]
PublicKey = 腾讯云2 publickey
EndPoint = 122.xx.xxx.111:5418
AllowedIPs = 192.168.1.2/32

[Peer]
PublicKey = 阿里云publickey
EndPoint = 122.xx.xx.155:5418
AllowedIPs = 192.168.1.3/32

配置说明

Interface: 小节是属于腾讯云1(也就是本机)的配置.

Address: 是分配给 腾讯云1 虚拟IP,

ListenPort: 是主机之间通讯使用的端口,是 UDP 协议的。

Peer: 是属于需要通信的 腾讯云2阿里云的信息,有多少需要通信的主机,就添加多少个 Peer 小节。

EndPoint: 是 腾讯云2阿里云的公网IP与 WireGuard 监听的 UDP 端口,这个 IP 不一定是公网,

如果你的机器通过内网也能通信,直接用内网 IP 也可以,当然要注意这个IP需要所有加入局域网的主机都能通信才行。

AllowedIPs: 是指本机发起连接的哪些IP应该将流量转发到这个节点去,比如我们给主机B分配了内网IP 192.168.1.2,那么在主机A上发送到 192.168.1.2 的数据包,都应该转发到这个 EndPoint 上,它其实起的是一个过滤作用。而且多个 Peer 时,这里配置的IP地址不能有冲突。

各个节点生产的 privatekeypublickey 分别如下

# master 节点
[root@k3s-master ~]# cat privatekey publickey
EMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=
0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=
# node1 节点
[root@k3s-node1 ~]# cat privatekey publickey
QGdNkzpnIkuvUU+00C6XYxxxxxxxxxK0D82qJVc=
3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=
# node2 节点
[root@k3s-node2 ~]# cat privatekey publickey
WOOObkWINkW/hqaAME9r+xxxxxxxxxm+r2Q=
0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=

各节点配置

Master配置

# master 节点
cat /etc/wireguard/wg0.conf
[Interface]
PrivateKey = EMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=
Address = 192.168.1.1
ListenPort = 5418

[Peer]
PublicKey = 3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=
EndPoint = 122.xx.xxx.111:5418
AllowedIPs = 192.168.1.2/32

[Peer]
PublicKey = 0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=
EndPoint = 122.xx.xx.155:5418
AllowedIPs = 192.168.1.3/32

Node1配置

# node1 节点
cat /etc/wireguard/wg0.conf
[Interface]
PrivateKey = QGdNkzpnIkuvUU+00C6XYxxxxxxxxxK0D82qJVc=
Address = 192.168.1.2
ListenPort = 5418

[Peer]
PublicKey = 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=
EndPoint = 42.xxx.xx.16:5418
AllowedIPs = 192.168.1.1/32

[Peer]
PublicKey = 0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=
EndPoint = 122.xx.xx.155:5418
AllowedIPs = 192.168.1.3/32

Node2配置

# node2 节点
cat /etc/wireguard/wg0.conf
[Interface]
PrivateKey = WOOObkWINkW/hqaAME9r+xxxxxxxxxm+r2Q=
Address = 192.168.1.3
ListenPort = 5418

[Peer]
PublicKey = 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=
EndPoint = 42.xxx.xx.16:5418
AllowedIPs = 192.168.1.1/32

[Peer]
PublicKey = 3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=
EndPoint = 122.xx.xx.155:5418
AllowedIPs = 192.168.1.2/32

启动 WireGuard

配置文件写好后,使用 wg-quick 工具来创建虚拟网卡,

wg-quick up wg0

上面命令中的 wg0 对应的是 /etc/wireguard/wg0.conf 这个配置文件,其自动创建的网卡设备,名字就是 wg0,这对应关系自不必多言。

腾讯云2阿里云的网卡设备都安装配置好后,就能使用 wg 命令来观察组网情况了

[root@k3s-master ~]# wg
interface: wg0
  public key: 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=
  private key: (hidden)
  listening port: 5418

peer: 0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=
  endpoint: 122.xx.xx.155:5418
  allowed ips: 192.168.1.3/32
  latest handshake: 3 minutes, 3 seconds ago
  transfer: 35.40 KiB received, 47.46 KiB sent

peer: 3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=
  endpoint: 122.xx.xxx.111:5418
  allowed ips: 192.168.1.2/32
  latest handshake: 5 minutes, 6 seconds ago
  transfer: 24.84 KiB received, 35.21 KiB sent

sre运维|Linux运维|自动化运维|多云搭建K3S集群|公网搭建K3S集群|跨云搭建K3S集群

sre运维|Linux运维|自动化运维|多云搭建K3S集群|公网搭建K3S集群|跨云搭建K3S集群

可以看到列出了对等联网的节点信息,还有通信测量数据。然后可以通过 ping 其他主机的虚拟IP或者 ssh 其他主机的IP地址,来检查网络通信是否正常。

自动化

系统重启后,WireGuard 创建的网卡设备就会丢失,有自动化的脚本

systemctl enable wg-quick@wg0

使用上述命令生成systemd守护脚本,开机会自动运行up指令。

配置热重载

wg-quick并未提供重载相关的指令,但是提供了 strip 指令,可以将 conf 文件转换为 wg 指令可以识别的格式。

wg syncconf wg0 <(wg-quick strip wg0)

即可实现热重载。

完成 WireGuard 的安装配置以后,我们就可以接下来安装 k3s 的集群了。

安装 K3S 集群

master节点安装

curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh  -s -  --node-external-ip 42.xx.xx.12--advertise-address 42.xx.xx.12--node-ip 192.168.1.1 --flannel-iface wg0

参数说明:

  • --node-external-ip 42.xxx.xx.16 为节点设置外部IP,阿里云VPC的外网IP并未直接绑定到虚拟机网卡上,所以我要设置这个参数,避免k3s组件在设置loadbalance时,将内网IP当作公网IP使用。
  • --advertise-address 42.xxx.xx.16 用于设置kubectl工具以及子节点进行通讯使用的地址,可以是IP,也可以是域名,在创建apiserver证书时会将此设置到有效域中。
  • --node-ip 10.20.30.1 如果不设置这个参数,那么第一张网卡设备上的IP就会被选中,所以这个IP常是内网IP。但我自行组建了虚拟局域网,所以需要指定虚拟局域网的IP(也就是WireGuard的IP)。
  • --flannel-iface wg0 wg0是WireGuard创建的网卡设备,我需要使用虚拟局域网来进行节点间的通信,所以这里需要指定为wg0。

另外就是,由于WireGuard的所有流量都是加密传输的,通过它来进行节点间的通信,就已经能够保证通信安全,也就没有必要改用其它的CNI驱动,使用默认的就可以了。

在主节点执行上述命令后,一分钟不到就可以看到脚本提示安装完成。通过命令查看下主控端的运行情况

systemctl status k3s

如果运行正常,那么就看看容器的运行状态是否正常

kubectl get pods -A

sre运维|Linux运维|自动化运维|多云搭建K3S集群|公网搭建K3S集群|跨云搭建K3S集群

-A 参数用于查看所有命名空间,如果容器都处于 running 状态,那么安装就成功了,接下来要可以添加被控节点。

Agent 安装

有了上述安装主控的经验,安装work节点更加简单,参数需要一定的调整

腾讯云2执行阿里云执行

腾讯云2执行

curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=https://192.168.1.1:6443 K3S_TOKEN=K107xxxxxxxxxxxxxxxx2cf95048d6a3cd85f15717edfbe5::server:xxxxxxxxxxxxxxxxxxxx4da1b7e701f67e sh -s - --node-external-ip 122.xx.xxx.111 --node-ip 192.168.1.2 --flannel-iface wg0

参数不必过多解释

  • K3S_Tokencat /var/lib/rancher/k3s/server/node-token 获取即可。
  • K3S_URL 需要设置master的通信地址端口,端口默认是6443,IP地址就是虚拟网域的IP,这样流量就会通过WireGuard加密传输。
  • node-external-ip 为节点公网地址
  • node-ip 节点虚拟IP地址
    执行完稍等一会,安装成功后,查看服务运行状态。
systemctl status k3s-agent

如果有报错就根据报错查找解决方案。

都安装好以后 在master节点检查。

kubectl get nodes -o wide

sre运维|Linux运维|自动化运维|多云搭建K3S集群|公网搭建K3S集群|跨云搭建K3S集群

至此 多云 K3S 集群已经搭建完毕。


作者:SRE运维博客

博客地址: https://www.cnsre.cn/

文章地址:https://www.cnsre.cn/posts/211119132529/

相关话题:https://www.cnsre.cn/tags/k3s/


Source https://www.cnsre.cn/posts/211119132529/ https://zhuanlan.zhihu.com/p/445353413
Comments