K3s 多云搭建集群
检查内核版本
uname -r
需要安装版本高于 5.6 的内核
# 添加 PPA:
sudo add-apt-repository ppa:canonical-kernel-team/unstable
sudo apt update
# 安装新内核
sudo apt install linux-generic-lts-<版本号>
如果你不能升级内核,你可以尝试安装 WireGuard 的 DKMS(动态内核模块支持)版本,这将为你的当前内核版本编译和安装 WireGuard 模块。
1、安装必要的软件包:
sudo apt install software-properties-common
1、添加 WireGuard PPA:
sudo add-apt-repository ppa:wireguard/wireguard
2、更新软件包列表并安装 WireGuard:
sudo apt update
sudo apt install wireguard-dkms wireguard-tools
这将为你的现有内核版本编译并安装 WireGuard 模块。
配置 IP
在所有节点开启 IP 地址转发
sudo echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sudo echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf
# 让配置生效
sudo sysctl -p /etc/sysctl.conf
配置防火墙
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
安装 WireGuard
WireGuard 是一种现代化、快速和安全的 VPN 技术。用于把这几个不同云下的服务器连接起来(同个内网环境)
sudo apt install wireguard
在服务器上:
# 统一放 在这里方便查找
mkdir wireguard && cd wireguard
wg genkey | tee privatekey | wg pubkey > publickey
保护你的私钥不被他人访问。
创建并编辑 WireGuard 配置文件:
sudo vim /etc/wireguard/wg0.conf
在文件中添加以下内容,替换 <Server's Private Key> 和 <Client's Public Key>:
[Interface]
Address = 192.168.2.1/24
PrivateKey = <Server's Private Key>
ListenPort = 51820
[Peer]
PublicKey = <Client's Public Key>
EndPoint = 122.xx.xxx.111:5418
AllowedIPs = 192.168.2.2/32
[Peer]
PublicKey = <Client's Public Key>
EndPoint = 122.xx.xxx.111:5418
AllowedIPs = 192.168.2.3/32
[Peer]
PublicKey = <Client's Public Key>
EndPoint = 122.xx.xxx.111:5418
AllowedIPs = 192.168.2.4/32
客户端也是一样的配置,只不过修改了 IP 地址和公钥。
配置说明:
- Interface: 小节是属于腾讯云1(也就是本机)的配置.
- Address: 是分配给 腾讯云1 虚拟IP,
- ListenPort: 是主机之间通讯使用的端口,是 UDP 协议的。
- Peer: 是属于需要通信的 腾讯云2 、阿里云 的信息,有多少需要通信的主机,就添加多少个 Peer 小节。
- EndPoint: 是 腾讯云2 、阿里云的公网IP与 WireGuard 监听的 UDP 端口,这个 IP 不一定是公网
- AllowedIPs: 是指本机发起连接的哪些IP应该将流量转发到这个节点去,比如我们给主机B分配了内网IP
192.168.2.2,那么在主机A上发送到192.168.2.2的数据包,都应该转发到这个 EndPoint 上,它其实起的是一个过滤作用。而且多个 Peer 时,这里配置的IP地址不能有冲突。
启动 WireGuard:
sudo systemctl start wg-quick@wg0
# 并设置为开机启动:
sudo systemctl enable wg-quick@wg0
systemctl status wg-quick@wg0
注意:如果启动失败了,可能是 K3s 已经配置了一个 wg,要和它端口区分开来(默认都是 51820)
# 检查配置
sudo wg show