LVS:实现real server的负载均衡
Keepalived:实现两节点的高可用,并监控Real server,屏蔽故障节点,将出现故障的节点从策略中移除
实现高可用的两节点:
Server4:172.25.254.4
Server5:172.25.254.5
Vip:172.25.254.116
实现负载均衡的两台Real server
Server2:172.25.254.2
Server3:172.25.254.3
在server4和server5安装keepalived和ipvsadm
设定serevr4使主节点配置如下:
ip addr
ip addr add 172.25.254.116/24 dev eth0
ipsvadm -L ##查看是否有策略,如果有的话用-C清除
ipvsadm -A -t 172.25.254.116:80 -s rr
ipvsadm -a -t 172.25.254.116:80 -r 172.25.254.2:80 -g
ipvsadm -a -t 172.25.254.116:80 -r 172.25.254.3:80 -g
Ipvsadm -l ##查看策略
/etc/init.d/ipvsadm save ##保存策略
/etc/init.d/httpd start
在server4和server5进行相同的配置如下:
cd keepalived-1.2.20/
./configure --prefix=/usr/local/keepalived ##注意解决依赖性的问题
make && make install
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived /etc/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin
chmod +x /etc/init.d/keepalived ##增加执行权限
vim /etc/keepalived/keepalived.conf
scp /usr/local/keepalived server5:/etc/keepalived
/etc/init.d/keepalived ##开启服务
Ipvstables -L
在server2和server3上进行相同配置如下:
安装arptables_jf
ip addr
ip addr add 172.25.254.116/24 dev eth0 ##在两个服务器端设添加vip
/etc/init.d/httpd start
arptables -A IN -d 172.25.254.116 -j DROP
arptables -A OUT -s 172.25.24.116 -j mangle --mangle-ip-s 172.25.254.2(在server2)
arptables -A OUT -s 172.25.24.116 -j mangle --mangle-ip-s 172.25.254.3(在server3)
/etc/init.d/arptables_jf save ##保存添加的arp策略
测试:
1. 高可用测试:停止 master 上的 keepalived 服务,看 backup 是否接管。
2. 负载均衡测试:访问 http://172.25.254.116,看到页面在两个 realserver 上切换表示成功!
3. 故障切换测试:任意关闭 realserver 上的 httpd 服务,Keepalived 监控模块是否能及时发现,然后屏蔽故障节点,同时将服务转移到正常节点来执行。