基于Nginx和Keepalived双机热备
Keepalived 实现双机热备,消除一台机器单点故障的发生。准备2台机器ip地址分别为192.168.163.34、192.168.163.35。同时这2台机器都设置同一虚拟ip,
这里虚拟ip设置为192.168.163.100。2台机器都需要装上nginx和keepalived。
设置虚拟ip
这里要用到虚拟ip,虚拟ip的设置参考。
安装nginx 并启动
192.168.163.34启动
192.168.163.35启动
安装Keepalived
tar –zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make
make install
keepalived.conf配置(192.168.163.34)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVELA #MASTER节点
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51 #主、备必须一样
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.100 #VRRP HA虚拟地址
}
}
keepalived.conf配置(192.168.163.35)
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVELB #BACKUP节点
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51 #主、备必须一样
priority 99#优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.100 #VRRP HA虚拟地址
}
}
建立软链接并启动keepalived
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
keepalived -D -f /usr/local/keepalived/etc/keepalived/keepalived.conf
测试
测试可见,浏览器中我们通过虚拟ip来访问,如上面所设置的会访问到master 34的机器。34这台机器的优先级高于35。
停用master机器
master机器停用(宕机、nginx死机等)会看到自动切换到35 backup那台机器。即192.168.163.34那台机器访问不了。