双机高可用方法目前分为两种:
1)双机主从模式:即前端使用两台服务器,一台主服务器和一台热备服务器,正常情况下,主服务器绑定一个公网虚拟IP,提供负载均衡服务,热备服务器处于空闲状态;当主服务器发生故障时,热备服务器接管主服务器的公网虚拟IP,提供负载均衡服务;但是热备服务器在主机器不出现故障的时候,永远处于浪费状态,对于服务器不多的网站,该方案不经济实惠。
2)双机主主模式:即前端使用两台负载均衡服务器,互为主备,且都处于活动状态,同时各自绑定一个公网虚拟IP,提供负载均衡服务;当其中一台发生故障时,另一台接管发生故障服务器的公网虚拟IP(这时由非故障机器一台负担所有的请求)。这种方案,经济实惠,非常适合于当前架构环境。
1.主从模式(非抢占式)
lb01
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 192.168.1.201
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_web.sh"
interval 3
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 # 根据实际网卡更改
virtual_router_id 251
priority 100
advert_int 1
mcast_src_ip 192.168.1.201
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.200
}
}
cat /etc/keepalived/check_web.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
service nginx start
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/bin/systemctl stop keepalived.service
fi
fi
lb02
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 192.168.1.202
script_user root
enable_script_security
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 # 根据实际网卡更改
virtual_router_id 251
mcast_src_ip 192.168.1.202
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 11111111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.200
}
}