讲讲LVS两种常见的模式NAT和DR
NAT需要开启内核IP转发,流量穿透机器,至少要双网卡,机器压力比较大,流量越大,后端机器越多,对前端的硬件要求要越高,且容易成为瓶颈;
DR由mac地址欺骗的办法实现。需要做后端服务器的arp屏蔽工作,由于是半连接,流量不经过机器,可以支持很大范围的后端机器,对硬件要求相对也不那么高。
这里只讲DR模式
LVS+keepalived负载均衡兼高可用集群实际上只需要配置keepalived即可,
环境CentOS 6.3
两台机器主机名分别为keepalived01 和 keepalived02
VIP 192.168.1.200
RIP01 192.168.1.21
RIP02 192.168.1.22
安装
yum install keepalived --enablerepo=epel
直接上完整的配置文件,断开的地方是需要注意的地方。
! Configuration File for keepalived
global_defs {
notification_email {
purplegrape4@gmail.com
}
notification_email_from keepalived01@test.org
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id keepalived01
}
vrrp_instance VI_1 {
state MASTER
#主服务器是MASTER,从服务器是SLAVE
interface eth0
virtual_router_id 51
priority 120
#优先级,不论数值是多少,MASTER的要比SLAVE的高,最好多隔一点,比如一个120,一个70
smtp_alert
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200 label eth0:1
VIP所在地,很多人没有配置 lable ,尽管没什么关系,但是通过命令ifconfig 看不到VIP,用lable后就可以看到了。
(其实即使没有配label ,还可以用ip addr命令看到VIP)
}
}
virtual_server 192.168.1.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.21 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.1.22 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
关于健康检查,上文采用的是tcp端口检测,可能导致超时502,lvs误以为后端不通,可以根据需要采用更可靠的url检测,格式如下:
real_server 192.168.1.20 80 { weight 100 HTTP_GET { url { path /lvstest.html status_code 200 } connect_timeout 10 nb_get_retry 3 delay_before_retry 3 } }
最后,为了邮件支持,我们安装postfix并开机启动keepalived
yum install postfix /etc/init.d/postfix start chkconfig postfix on /etc/init.d/keepalived start chkconfig keepalived on
后端的机器需要屏蔽arp,以CentOS 6为例(没有在其他平台测试)
直接写了个脚本,在不同的节点执行前修改下RIP即可。
#!/usr/bin/env bash # author = Purple_Grape set -e #-------------------------------------------------------- VIP=192.168.1.200 RIP=192.168.1.21 yum install arptables_jf -y #block arp request from VIP arptables -Z arptables -F arptables -A IN -d $VIP -j DROP arptables -A OUT -s $VIP -j mangle --mangle-ip-s $RIP /etc/init.d/arptables_jf save chkconfig arptables_jf --level 2345 on # add VIP echo "ifconfig eth0:1 $VIP up" >> /etc/rc.local reboot #the end