手动加载 CentOS7 box 文件

配置 Vagrantfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Vagrant.configure("2") do |config|
(1..3).each do |i|
config.vm.define "k3s-node#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos/7"

# 设置虚拟机的主机名
node.vm.hostname="k3s-node#{i}"

# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.56.#{99+i}", netmask: "255.255.255.0"

# 设置主机与虚拟机的共享目录
# node.vm.synced_folder "~/Documents/vagrant/share", "/home/vagrant/share"

# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "k3s-node#{i}"
# 设置虚拟机的内存大小
v.memory = 4096
# 设置虚拟机的CPU个数
v.cpus = 4
end
end
end
end

先修改全局设定中的默认电脑位置

进入到三个虚拟机,开启 SSH 的访问权限

1
2
3
4
5
6
7
8
# vagrant ssh k3s-node1
sudo -s # 密码为 vagrant

vi /etc/ssh/sshd_config

# 修改以下配置
PermitRootLogin yes
PasswordAuthentication yes
1
service sshd restart

如果不需要配置网卡/网络,跳过下一步执行 reboot 重启即可

配置网络/网卡

  • shutdown 关机操作
  • 选择三个节点,然后执行 "管理" -> "全局设定" -> "网络",添加一个 NAT 网络
  • 分别修改每台设备的网络类型,网卡 1 为 NAT(三个节点都使用同一个 NAT 网络即可),网卡 2 为仅主机交互(VirtualBox 默认会带一个 192.168.56.1 网段的主机网络),并操作每个节点刷新 MAC 地址
  • 网卡 1 用于集群交互,网卡 2 用于宿主交互

设置 Linux 环境(三个节点都执行)

设置主机名

三台主机必须都要有唯一的 hostname:

1
2
# 使用如下命令进行配置
hostnamectl set-hostname <value>

关闭防火墙

1
2
systemctl stop firewalld
systemctl disable firewalld

关闭 SELinux

1
2
3
# Linux 默认的安全策略
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

关闭 swap

1
2
3
4
5
6
# 临时关闭
swapoff -a
# 永久关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 验证,swap 必须为 0
free -h

添加主机名与 IP 对应关系

查看主机名:

1
hostname

如果主机名不正确,可以通过 hostnamectl set-hostname <newhostname> 命令来进行修改。

1
2
3
4
5
cat >> /etc/hosts << EOF
10.0.2.17 k3s-node1
10.0.2.16 k3s-node2
10.0.2.15 k3s-node3
EOF

更新 yum 源

1
yum update -y

配置 sysctl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 将桥接的 IPv4 流量传递到 iptables 的链,禁用 swap
cat > /etc/sysctl.d/k3s.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
# 关闭 swap 虚拟内存
vm.swappiness = 0
# 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
vm.overcommit_memory = 1
# 内存不足时,启动 OOM killer
vm.panic_on_oom = 0
# 允许非特权用户使用端口从 0 开始,k8s 1.22 以上版本支持此参数,且内核支持版本需要大于 4.4
net.ipv4.ip_unprivileged_port_start=0
# 禁用 IPv6
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1
EOF
1
2
# 应用当前规则
sysctl -p /etc/sysctl.d/k3s.conf

参数说明

net.ipv4.ip_forward

这个参数是控制 Linux 系统的 IP 转发功能的。IP 转发(IP Forwarding)功能允许 Linux 系统拦截发往其他目标地址的 IP 数据包,并将它们转发到对应的网络接口。

  • net.ipv4.ip_forward=0 表示关闭 IP 转发功能。这通常是默认值
  • net.ipv4.ip_forward=1 则表示开启 IP 转发功能

开启 IP 转发功能后,Linux 系统就可以作为一个 Router,在两个网络接口之间进行 IP 数据的转发。

举个例子,如果一台主机同时连接了两个网络,eth0 接内网,eth1 接外网。此时如果开启 IP 转发,它就可以在内网和外网之间进行路由功能,实现内网到外网的访问。

简单来说,net.ipv4.ip_forward=1 就是开启了 Linux 作为网络 Router 的功能,使其可以在不同网络间进行 IP 数据包的转发。这对构建 Linux 路由器很重要。

net.bridge.bridge-nf-call-ip6tablesnet.bridge.bridge-nf-call-iptables

这两个参数用于控制 Linux bridge 对 IPv6 和 IPv4 数据包的过滤。

Linux bridge 是一种网络桥接技术,可以将两个或多个网络接口桥接在一起,组成一个广播域,实现网络互连。而数据包过滤是通过 iptables/ip6tables 实现的。bridge-nf-call 就是控制是否调用 iptables/ip6tables 对通过 bridge 的数据包进行过滤。

  • net.bridge.bridge-nf-call-ip6tables=1 表示 bridge 调用 ip6tables 对通过的 IPv6 数据包进行过滤
  • net.bridge.bridge-nf-call-iptables=1 表示 bridge 调用 iptables 对通过的 IPv4 数据包进行过滤

通常这两个参数需要设置为 1,否则 bridge 将绕过 iptables/ip6tables,导致过滤规则不生效。

举例来说,在 OpenStack 中,Linux bridge 被广泛用于连接虚拟机网卡。为了实现网络安全,需要通过 iptables 过滤虚拟机之间的网络访问。此时就需要开启 bridge 对 IPv4 数据包的 iptables 过滤。

总结:

  • ip_forward 是 IP 转发,桥接不同网段
  • bridge-nf-call 是 bridge 调用 iptables/ip6tables 进行数据包过滤

二者互相配合,可以实现连接且安全的网络互通。

如果使用 IPVS 模式(可选项)

当 kube-proxy 使用 IPVS 模式时执行:

使用 lsmod 命令可以查看当前已支持的 LVS 方式。

开启 IPVS 支持

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
yum -y install ipvsadm ipset

# 临时生效
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- ip_vs_lc
modprobe -- ip_vs_wlc
modprobe -- nf_conntrack_ipv4

# 永久生效
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- ip_vs_lc
modprobe -- ip_vs_wlc
modprobe -- nf_conntrack_ipv4
EOF

使用 lsmod | grep ip_vs 检查 mod 是否生效:

1
2
3
4
5
6
7
8
9
[root@k3s-master1 ~]# lsmod | grep ip_vs
ip_vs_wlc 12519 0
ip_vs_lc 12516 19
ip_vs_sh 12688 0
ip_vs_wrr 12697 0
ip_vs_rr 12600 0
ip_vs 145458 29 ip_vs_lc,ip_vs_rr,ip_vs_sh,ip_vs_wlc,ip_vs_wrr
nf_conntrack 139264 10 ip_vs,nf_nat,nf_nat_ipv4,nf_nat_ipv6,xt_conntrack,nf_nat_masquerade_ipv4,nf_nat_masquerade_ipv6,nf_conntrack_netlink,nf_conntrack_ipv4,nf_conntrack_ipv6
libcrc32c 12644 4 xfs,ip_vs,nf_nat,nf_conntrack

待 Kubernetes 安装完之后,可以使用 ipvsadm -Ln 检查 service 是否应用到 IPVS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.56.100:34552 lc
-> 10.42.0.7:8443 Masq 1 0 0
TCP 192.168.56.100:35222 lc
-> 10.42.0.7:8000 Masq 1 0 0
TCP 10.0.2.17:80 lc
-> 10.42.0.7:8000 Masq 1 0 0
TCP 10.0.2.17:443 lc
-> 10.42.0.7:8443 Masq 1 0 0
TCP 10.0.2.17:34552 lc
-> 10.42.0.7:8443 Masq 1 0 0
TCP 10.0.2.17:35222 lc
-> 10.42.0.7:8000 Masq 1 0 0
TCP 10.42.0.0:34552 lc
-> 10.42.0.7:8443 Masq 1 0 0
TCP 10.42.0.0:35222 lc
-> 10.42.0.7:8000 Masq 1 0 0
TCP 10.42.0.1:34552 lc
-> 10.42.0.7:8443 Masq 1 0 0
TCP 10.42.0.1:35222 lc
-> 10.42.0.7:8000 Masq 1 0 0
TCP 10.43.0.1:443 lc
-> 10.0.2.17:6443 Masq 1 27 0

应用规则

手动加载所有的配置文件,执行:

1
sysctl --system

--system 参数会从所有下列配置文件中加载系统设置:

1
2
3
4
5
6
/run/sysctl.d/*.conf
/etc/sysctl.d/*.conf
/usr/local/lib/sysctl.d/*.conf
/usr/lib/sysctl.d/*.conf
/lib/sysctl.d/*.conf
/etc/sysctl.conf

单独指定配置文件加载,执行:

1
sysctl -p filename.conf

CentOS 安装 ntpdate 并同步时间

安装 ntp

1
yum install -y ntp

与一个已知的时间服务器同步

1
2
# time.nist.gov 是一个时间服务器
ntpdate time.nist.gov

删除本地时间并设置时区为上海

1
2
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

下一篇进入 K3s 高可用安装篇