作者:姚姚
链接:https://www.zhihu.com/question/39595620/answer/126026530
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
目前有两种方法可以实现,这两种方法都需要机器有两块网卡,例 eth0为内网,eth1为公网ip,两台机器的eth1上分别绑定公网ip都要能正常使用。
例:
A机器ip为: 192.168.10.10
B机器ip为: 192.168.10.11
公网ip为: 123.123.123.123 网关:123.123.123.1 掩码:255.255.255.240
第一种:
1,先设置A机器与B机器的eth1的网卡配置配置文件,不要设置公网ip信息
$vim /etc/sysconfig/network-scripts/ifcfg-eth1
-
DEVICE=eth1 -
TYPE=Ethernet -
ONBOOT=yes -
HWADDR=52:54:00:97:d2:2b -
NM_CONTROLLED="no" -
USERCTL=no
然后重启网卡
2,配置A机器keepalived.conf(B机器请修改MASTER为BACKUP,priority的值小于100,其他不变)
安装yum install -y keepalived
打开文件
vim /etc/keepalived/keepalived.conf
-
global_defs { -
router_id ID_1 -
} -
vrrp_script checkscript -
{ -
script "/etc/keepalived/check.sh" -
interval 3 -
weight -20 -
} -
vrrp_instance ID { -
state MASTER #设置为主服务器,备服务器设置为BACKUP -
interface eth0 #监测网络接口 -
nopreempt #设置非抢占模式 -
virtual_router_id 111 #主、备必须一样 -
priority 100 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高) -
advert_int 1 #VRRP Multicast广播周期秒数 -
authentication { -
auth_type PASS #VRRP认证方式,主备必须一致 -
auth_pass 1111 #(密码) -
} -
track_script -
{ -
checkscript -
} -
virtual_ipaddress { -
123.123.123.123/28 dev eth1 #设置 在 eth1网卡上绑定 123.123.123.123 掩码为 240的公网ip作为HA虚拟地址 -
} -
virtual_routes { -
default via 123.123.123.1 # 设置默认网关为 123.123.123.1 -
} -
}
上图中需要解释的配置如下:
virtual_ipaddress {
123.123.123.123/28 dev eth1
} ##设置 在 eth1网卡上绑定 123.123.123.123 掩码为 240的公网ip
virtual_routes {
default via 123.123.123.1
} ## 设置默认网关为 123.123.123.1
检查脚本内容如下:
-
#!/bin/bash -
count = `ps aux | grep -v grep | grep haproxy | wc -l` -
if [ $count > 0 ]; then -
exit 0 -
else -
exit 1 -
fi
3,两边分别启动keepalived,并查看A主机的eth1网卡是否绑定公网ip 123.123.123.123。停止A主机的keepalived,查看B主机是否正常绑定公网ip。
keepalived -D -f /etc/keepalived/keepalived.conf
查看log消息:
tail -f /var/log/messages
启动主节点A后的日志为:会广播ARP消息
[html] view plain copy
- [[email protected] ~]# tail -f /var/log/messages
- Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes
- Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]
- Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
- Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
- Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
- Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100
- Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 192.168.8.100 added
- Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.8.100 added
- Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 192.168.8.100 on eth0.
- Sep 20 01:45:36 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100
通过ip a 命令可以看到192.168.8.100/24绑定到了eth0上
[html] view plain copy
- [[email protected] bin]# ip a
- 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
- link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
- inet 127.0.0.1/8 scope host lo
- inet6 ::1/128 scope host
- valid_lft forever preferred_lft forever
- 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
- link/ether 00:0c:29:50:2d:9d brd ff:ff:ff:ff:ff:ff
- inet 192.168.8.4/24 brd 192.168.8.255 scope global eth0
- inet 192.168.8.100/24 scope global secondary eth0
- inet6 fe80::20c:29ff:fe50:2d9d/64 scope link
- valid_lft forever preferred_lft forever
启动备用节点B后的日志为:
[html] view plain copy
- Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: Configuration is using : 34262 Bytes
- Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
- Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]
- Sep 20 01:47:31 hadoopsrv Keepalived: Starting VRRP child process, pid=20567
第二种:(这种较麻烦,需要借助脚本)
1,先设置A机器与B机器的eth1的网卡配置配置文件,配置好公网的ip信息,不设置公网的网关
&amp;lt;img src="https://pic3.zhimg.com/19c97b837a38763a9e0cb03aa8b9ae2e_b.png" data-rawwidth="715" data-rawheight="130" class="origin_image zh-lightbox-thumb" width="715" data-original="https://pic3.zhimg.com/19c97b837a38763a9e0cb03aa8b9ae2e_r.png"&amp;gt;
2,分别停止A机器与B机器的eth1网卡(ifdown eth1)
3,配置A机器的keepalived.conf配置文件(B机器请修改MASTER为BACKUP,priority的值小于100,其他不变)
&amp;lt;img src="https://pic3.zhimg.com/d19dfc7d611d9f58264db63bc0a73bca_b.png" data-rawwidth="596" data-rawheight="409" class="origin_image zh-lightbox-thumb" width="596" data-original="https://pic3.zhimg.com/d19dfc7d611d9f58264db63bc0a73bca_r.png"&amp;gt;
4,配置/etc/keepalived/scripts/master.sh脚本,并赋予可执行权限,判断公网ip是否在本机,如不在,执行启动eth1操作,并添加默认网关
&amp;lt;img src="https://pic4.zhimg.com/687eefc1ffa29897117ff793534b5ceb_b.png" data-rawwidth="615" data-rawheight="146" class="origin_image zh-lightbox-thumb" width="615" data-original="https://pic4.zhimg.com/687eefc1ffa29897117ff793534b5ceb_r.png"&amp;gt;
5,配置/etc/keepalived/scripts/slave.sh脚本,并赋予可执行权限,执行停止 eth1操作
&amp;lt;img src="https://pic1.zhimg.com/3d631ff1aa987dc8ec1e264d5a9f0004_b.png" data-rawwidth="645" data-rawheight="72" class="origin_image zh-lightbox-thumb" width="645" data-original="https://pic1.zhimg.com/3d631ff1aa987dc8ec1e264d5a9f0004_r.png"&amp;gt;
6,两边分别启动keepalived,并查看A主机的eth1网卡有没有启动,是否绑定公网ip 123.123.123.123。停止A主机的keepalived,查看B主机的网卡有没有启动,是否正常绑定公网ip。
https://blog.csdn.net/mergerly/article/details/73292748