虚拟机列表如下:
主机名 | 网卡 | IP 地址 | 角色 | 软件 | 操作系统 |
---|---|---|---|---|---|
lb-55 | enp0s8 | 192.168.56.55 | 负载均衡器 MASTER | keepalived/v2.0.19 | Ubuntu 20.04.6 LTS |
lb-56 | enp0s8 | 192.168.56.56 | 负载均衡器 BACKUP | keepalived/v2.0.19 | Ubuntu 20.04.6 LTS |
lb-57 | enp0s8 | 192.168.56.57 | REAL SERVER | Nginx/1.18.0、OpenSSL/1.1.1f | Ubuntu 20.04.6 LTS |
lb-58 | enp0s8 | 192.168.56.58 | REAL SERVER | Nginx/1.18.0、OpenSSL/1.1.1f | Ubuntu 20.04.6 LTS |
虚拟 IP 为:192.168.56.59。
如果使用 Vagrant 管理虚拟机,那么可以使用下面的配置文件:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
vms = Array(55..58)
vms.each do |seq|
config.vm.define :"lb-#{seq}" do |vagrant|
vagrant.vm.hostname = "lb-#{seq}"
vagrant.vm.network "private_network", ip: "192.168.56.#{seq}"
vagrant.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--name", "lb-#{seq}", "--nested-hw-virt", "on"]
vb.gui = false
vb.memory = "2048"
vb.cpus = "2"
end
end
end
end
在两台 REAL SERVER 上执行如下脚本(按需修改 VIP 和 IFACE):
xxxxxxxxxx
VIP=192.168.56.59
IFACE=enp0s8
ifconfig=/usr/sbin/ifconfig
if [ ! -x $ifconfig ] ; then
apt install -y net-tools
fi
echo 1 > /proc/sys/net/ipv4/conf/${IFACE}/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/${IFACE}/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
$ifconfig lo:1 $VIP netmask 255.255.255.255
也可以将上述脚本设置为开机执行,这样每次开机都将设置内核参数和 VIP:
x# /etc/systemd/system/real-server-init.service
[Unit]
Description=Real Server initialization
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/bash /root/real_server.sh
[Install]
WantedBy=multi-user.target
x
$ sudo systemctl daemon-reload
$ sudo systemctl enable real-server-init
在两台 REAL SERVER 上安装及启动 Nginx:
$ sudo apt install -y nginx
$ nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: ...
$ sudo systemctl enable nginx
$ sudo systemctl restart nginx
$ curl http://127.0.0.1:80/
在两台负载均衡器上安装 keepalived:
$ sudo apt install -y keepalived net-tools
$ keepalived -v
Keepalived v2.0.19 (10/19,2019)
...
在负载均衡器 MASTER 上创建配置文件 /etc/keepalived/keepalived.conf:
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
# vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER # 状态
interface enp0s8 # 网络接口
virtual_router_id 51
priority 100 # 优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 16851ceeeb1a11ed8f04855085a05c12
}
virtual_ipaddress {
192.168.56.59/24 brd 192.168.56.255 dev enp0s8 label enp0s8:1 # VIP
}
}
# ipvsadm -A -t 192.168.56.59:80 -s rr
virtual_server 192.168.56.59 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 60 # 会话保持时间,单位是秒
protocol TCP
# ipvsadm -a -t 192.168.56.59:80 -r 192.168.56.57
real_server 192.168.56.57 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.56.58 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
在负载均衡器 BACKUP 上创建配置文件 /etc/keepalived/keepalived.conf,其与 MASTER 上的配置文件的区别是:
...
vrrp_instance VI_1 {
state BACKUP # 状态
priority 90 # 优先级
...
}
...
重启两台负载均衡器上的 keepalived:
xxxxxxxxxx
$ sudo systemctl enable keepalived
$ sudo systemctl daemon-reload
$ sudo systemctl restart keepalived
$ sudo systemctl status keepalived
在任意一台负载均衡器上执行:
x
# 查看 Virtual Server
$ sudo ipvsadm -L -n -t 192.168.56.59:80
虚拟机列表如下:
主机名 | IP 地址 | 角色 | 版本 |
---|---|---|---|
k8s-60 | 192.168.56.60 | Master | v1.23.1 |
k8s-61 | 192.168.56.61 | Worker | v1.23.1 |
如果使用 Vagrant 管理虚拟机,那么可以使用下面的配置文件:
x
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
vms = Array(60..61)
vms.each do |seq|
config.vm.define :"k8s-#{seq}" do |vagrant|
vagrant.vm.hostname = "k8s-#{seq}"
vagrant.vm.network "private_network", ip: "192.168.56.#{seq}"
vagrant.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--name", "k8s-#{seq}", "--nested-hw-virt", "on"]
vb.gui = false
vb.memory = "6144"
vb.cpus = "6"
end
end
end
end
Kubernetes 集群的搭建过程请参考 http://timd.cn/k8s/creating-test-cluster/。