作者:SRE运维博客

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

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

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


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

环境介绍

服务器介绍

云厂商公网IP地址内网IP地址虚构网络IP地址操作系统内核版本
腾讯云142.xx.xx.1210.0.16.8192.168.1.1CentOS Linux release 7.9.2009 (Core)5.15.2-1
腾讯云2122.xx.xxx.11110.0.0.6192.168.1.2CentOS Linux release 7.9.2009 (Core)5.15.2-1
阿里云122.xx.xx.155172.17.0.3192.168.1.3CentOS 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.confecho "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.confsysctl -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 ACCEPTiptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPTiptables -A FORWARD -i wg0 -o wg0 -m conntrack --ctstate NEW -j ACCEPTiptables -t nat -A POSTROUTING -s 192.168.1.1/24 -o eth0 -j MASQUERADE

留神:

wg0:为你定义的虚构网卡

192.168.1.1: 为你的虚构IP地址段

eth0:为你的物理网卡

降级内核

所有节点都要执行

办法1:

间接下载RPM包进行装置。

如果你想装置其余内核,你也能够在 这里下载

wget  http://ftp.sjtu.edu.cn/sites/elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-5.15.2-1.el7.elrepo.x86_64.rpmrpm -ivh kernel-ml-5.15.2-1.el7.elrepo.x86_64.rpm 

办法 2:

利用包管理工具更新

# 载入公钥rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org# 降级装置 elreporpm -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:

通过源码包编译装置.

这种形式可定制性强,但也比较复杂,有须要的可自行查找材料装置,上面只给出各零碎版本内核源码包的下载地址

批改默认内核版本

# 查看以后理论启动程序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 wireguardRTNETLINK answers: Operation not supportedUnable to access interface: Protocol not supported[#] ip link delete dev wg0Cannot 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.rpmyum 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 publickeyEMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=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.1ListenPort = 5418[Peer]PublicKey = 腾讯云2 publickeyEndPoint = 122.xx.xxx.111:5418AllowedIPs = 192.168.1.2/32[Peer]PublicKey = 阿里云publickeyEndPoint = 122.xx.xx.155:5418AllowedIPs = 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 publickeyEMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=# node1 节点[root@k3s-node1 ~]# cat privatekey publickeyQGdNkzpnIkuvUU+00C6XYxxxxxxxxxK0D82qJVc=3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=# node2 节点[root@k3s-node2 ~]# cat privatekey publickeyWOOObkWINkW/hqaAME9r+xxxxxxxxxm+r2Q=0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=

各个节点配置文件如下

# master 节点cat /etc/wireguard/wg0.conf[Interface]PrivateKey = EMWcI01iqM4zkb7xfbaaxxxxxxxxDo2GJUA=Address = 192.168.1.1ListenPort = 5418[Peer]PublicKey = 3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=EndPoint = 122.xx.xxx.111:5418AllowedIPs = 192.168.1.2/32[Peer]PublicKey = 0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=EndPoint = 122.xx.xx.155:5418AllowedIPs = 192.168.1.3/32
# node1 节点cat /etc/wireguard/wg0.conf[Interface]PrivateKey = QGdNkzpnIkuvUU+00C6XYxxxxxxxxxK0D82qJVc=Address = 192.168.1.2ListenPort = 5418[Peer]PublicKey = 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=EndPoint = 42.xx.xx.12:5418AllowedIPs = 192.168.1.1/32[Peer]PublicKey = 0f0dn60+tBUfYgzw7rIihKbqxxxxxxxxa6Wo=EndPoint = 122.xx.xx.155:5418AllowedIPs = 192.168.1.3/32
# node2 节点cat /etc/wireguard/wg0.conf[Interface]PrivateKey = WOOObkWINkW/hqaAME9r+xxxxxxxxxm+r2Q=Address = 192.168.1.3ListenPort = 5418[Peer]PublicKey = 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=EndPoint = 42.xx.xx.12:5418AllowedIPs = 192.168.1.1/32[Peer]PublicKey = 3izpVbZgPhlM+S5szOogTDTxxxxxxxxxuKuDGn4=EndPoint = 122.xx.xx.155:5418AllowedIPs = 192.168.1.2/32

启动 WireGuard

配置文件写好后,应用 wg-quick 工具来创立虚构网卡,

wg-quick up wg0

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

腾讯云2阿里云 的网卡设施都装置配置好后,就能应用 wg 命令来察看组网状况了

[root@k3s-master ~]# wginterface: wg0  public key: 0ay8WfGOIHndWklSIVBqrsp5LDWxxxxxxxxxxxxxxQ=  private key: (hidden)  listening port: 5418peer: 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 sentpeer: 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

能够看到列出了对等联网的节点信息,还有通信测量数据。而后能够通过 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.xx.xx.12 为节点设置内部IP,阿里云VPC的外网IP并未间接绑定到虚拟机网卡上,所以我要设置这个参数,防止k3s组件在设置loadbalance时,将内网IP当作公网IP应用。
  • --advertise-address 42.xx.xx.12 用于设置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

-A 参数用于查看所有命名空间,如果容器都处于 running 状态,那么装置就胜利了,接下来要能够增加被控节点。

Agent 装置

有了上述装置主控的教训,装置work节点更加简略,参数须要肯定的调整

腾讯云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=K10720eda8a278bdc7b9b6d787c9676a92119bb2cf95048d6a3cd85f15717edfbe5::server:e98b986e8202885cb54da1b7e701f67e sh -s - --node-external-ip 122.xx.xxx.111 --node-ip 192.168.1.2 --flannel-iface wg0

阿里云执行

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=K10720eda8a278bdc7b9b6d787c9676a92119bb2cf95048d6a3cd85f15717edfbe5::server:e98b986e8202885cb54da1b7e701f67e sh -s - --node-external-ip 122.xx.xx.155 --node-ip 192.168.1.3 --flannel-iface wg0

参数不用过多解释

  • K3S_Token 依据文档阐明,去 /var/lib/rancher/k3s/server/node-token 获取即可。
  • K3S_URL 须要设置主控的通信地址端口,端口默认是6443,IP地址就是虚构网域的IP,这样流量就会通过WireGuard加密传输。

另外两个参数也不用多说,与主控一样的逻辑。执行后稍等一会,装置胜利后,照例查看服务运行状态

systemctl status k3s-agent

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

都装置好当前 在master节点查看。

kubectl get nodes -o wide 

至此 多云 K3S 集群曾经搭建结束。


作者:SRE运维博客

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

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

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