什么是双机热备
双机热备,也叫高可用服务,主要解决单点故障问题。
高可用常用软件keepalive
keepalive在主备机器上都必须安装,yum, 主要的任务是通过vrrp虚拟路由协议来进行心跳检测。
安装软件
当前的环境
ip | 角色 |
---|---|
192.168.10.174 | 主 |
192.168.10.153 | 备 |
安装步骤
- 安装软件
yum -y install keepalived
- 修改配置文件
在master主机上,修改如下,主要是看优先级不能错,和state不能错
virutal_router_id在同一个组高可用下需要相同,且局域网内部需要唯一。
[root@db01 keepalived]# cat keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.30
}
}
在slave 机器上,如下
[root@db01 keepalived]# cat keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.30
}
}
测试
主机上和备份机上 启动keepakive
systemctl start keepalived
可以看到主机上获取了VIP.
[root@master keepalived]# systemctl start keepalived
[root@master keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever
inet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:e0:98:b0 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.174/24 brd 192.168.10.255 scope global noprefixroute dynamic eth0 valid_lft 631065sec preferred_lft 631065sec inet 192.168.10.30/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::7ef4:cacc:9f94:b9ca/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::7164:8429:bba:c759/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::40fd:5b2:4af7:b811/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
- 把主机上 的keepalive停掉,模拟宕机。
systemctl stop keepalived
可以看到备份机器获取了IP。
[root@db01 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:c5:e3:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.153/24 brd 192.168.10.255 scope global noprefixroute dynamic eth0
valid_lft 619941sec preferred_lft 619941sec
inet 192.168.10.30/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::cfeb:25dd:a383:2d45/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::c142:2d8d:c705:21e0/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::9224:32eb:8e7b:df11/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
自动做nginx生存测试或者完备测试进行漂移
- 首先主机上创建检查nginx是否能够工作的脚本,如果不能工作,就直接关闭keepalived服务,让备份机器获取到VIP。
cat <<EOF >/opt/checknginx.sh
#!/bin/bash
running=` curl -I localhost 2>/dev/null | wc -l `
if [ $running -eq 0 ] ; then
exit 1
else
exit 0
fi
EOF
- 修改主机上的keepalived配置文件,使得能够让keepalive自动执行脚本。
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
!定义脚本名称,执行指定的脚本。
!interval 3表示 3秒钟执行一次。
vrrp_script nginx {
script "/opt/checknginx.sh"
interval 3
weight -30
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 251
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.30
}
! 这里要增加检查的脚本名称
track_script {
nginx
}
}
测试
步骤一
启动ngnix,启动2个keeplive
步骤二
关闭主机上的nginx服务,会发现VIP会漂移到备份机器。
作者:admin 创建时间:2025-06-06 11:15
最后编辑:admin 更新时间:2025-06-07 09:49
最后编辑:admin 更新时间:2025-06-07 09:49