keepalived+lvsNAT实现高可用负载整理:
主备都需要开启ip转发功能,不然运行不起来。
vi /etc/sysctl.conf set net.ipv4.ip_forward = 1 之后service network restart重启网络。
MASTER:eth0:192.168.5.190(假设是外网IP) eth1 192.168.0.11(假设内网IP,用于绑定高可用的漂移IP)
BACKUP:192.168.5.200 (假设是外网IP) eth1 192.168.0.13(假设内网IP,用于绑定高可用的漂移IP)
Realserver1:192.168.0.100 网关设置为 keepalived配置的漂移eth1绑定的内网IP,即:192.168.0.10
Realserver2:192.168.0.200 网关设置为 keepalived配置的漂移eth1绑定的内网IP,即:192.168.0.10 realserver设置的ip与网关要与主备的eth1在一个网段互通。
主配置文件如下,备配置文件主要把MASTER改成BACKUP,priority 改小即可。
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group VG1 {
group {
vip_inter
vip_intra
}
}
vrrp_instance vip_inter {
state MASTER
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.199
}
}
vrrp_instance vip_intra {
state MASTER
interface eth1
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.10
}
}
virtual_server 192.168.5.199 80 {
delay_loop 6
lb_algo sh
lb_kind NAT
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.0.100 80 {
weight 1
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
real_server 192.168.0.200 80 {
weight 1
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
启动主备keepalived,之后发现VirtuaLIP192.168.5.199绑定到主服务器的eth0上,VirtuaLIP192.168.0.10绑定到主的eth1上,负载也成功。
当主负载宕机后,那么备份会接管相应的VIP地址并绑定实现高可用。
keepalived启动后相当于自动加载LVS负载分发功能。