Ubuntu 上ipvsadm负载均衡(DR模式,以22端口为例)
说明:3台服务器,ip分别为:10.0.1.2 10.0.1.3 10.0.1.4 虚拟ip:10.0.1.10
ipvsadm服务器(10.0.1.2)
$ sudo apt-get install ipvsadm
$ sudo ifconfig eth0:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
$ sudo ip a (查看设置是否生效)
$ sudo route add -host 10.0.1.10 dev eth0:0
$ sudo route -n (查看设置是否生效)
$ sudo ipvsadm -A -t 10.0.1.10:22 -s rr (注rr为轮询模式)
$ sudo ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.3:22 -g
$ sudo ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.4:22 -g
$ sudo ipvsadm --save (保存ipvsadm设置)
$ sudo ipvsadm -l (查看ipvsadm设置)
$ sudo ipvsadm -C (清除ipvsadm设置)
ipvsadm客户端(10.0.1.3)
$ sudo ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
$ sudo ip a (查看设置是否生效)
$ sudo route add -host 10.0.1.10 dev lo:0
$ sudo route -n (查看设置是否生效)
$ sudo echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
$ sudo echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
$ sudo echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
$ sudo echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
ipvsadm客户端(10.0.1.4)
$ sudo ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
$ sudo ip a (查看设置是否生效)
$ sudo route add -host 10.0.1.10 dev lo:0
$ sudo route -n (查看设置是否生效)
$ sudo echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
$ sudo echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
$ sudo echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
$ sudo echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
验证:
putty或ssh 10.0.1.10 (第一次登录的是10.0.1.3,第二次登录的是10.0.1.4)
Ubuntu 上ipvsadm负载均衡