作者:姚姚
链接: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

 
  1. DEVICE=eth1

  2. TYPE=Ethernet

  3. ONBOOT=yes

  4. HWADDR=52:54:00:97:d2:2b

  5. NM_CONTROLLED="no"

  6. USERCTL=no

然后重启网卡
2,配置A机器keepalived.conf(B机器请修改MASTER为BACKUP,priority的值小于100,其他不变)
安装yum install -y keepalived

打开文件
vim /etc/keepalived/keepalived.conf

 
  1. global_defs {

  2. router_id ID_1

  3. }

  4. vrrp_script checkscript

  5. {

  6. script "/etc/keepalived/check.sh"

  7. interval 3

  8. weight -20

  9. }

  10. vrrp_instance ID {

  11. state MASTER #设置为主服务器,备服务器设置为BACKUP

  12. interface eth0 #监测网络接口

  13. nopreempt #设置非抢占模式

  14. virtual_router_id 111 #主、备必须一样

  15. priority 100 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)

  16. advert_int 1 #VRRP Multicast广播周期秒数

  17. authentication {

  18. auth_type PASS #VRRP认证方式,主备必须一致

  19. auth_pass 1111 #(密码)

  20. }

  21. track_script

  22. {

  23. checkscript

  24. }

  25. virtual_ipaddress {

  26. 123.123.123.123/28 dev eth1 #设置 在 eth1网卡上绑定 123.123.123.123 掩码为 240的公网ip作为HA虚拟地址

  27. }

  28. virtual_routes {

  29. default via 123.123.123.1 # 设置默认网关为 123.123.123.1

  30. }

  31. }

上图中需要解释的配置如下:
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

检查脚本内容如下:

 

 
  1. #!/bin/bash

  2. count = `ps aux | grep -v grep | grep haproxy | wc -l`

  3. if [ $count > 0 ]; then

  4. exit 0

  5. else

  6. exit 1

  7. 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

  1. [[email protected] ~]# tail -f /var/log/messages  
  2. Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes  
  3. Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]  
  4. Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE  
  5. Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE  
  6. Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.  
  7. Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100  
  8. Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 192.168.8.100 added  
  9. Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.8.100 added  
  10. Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 192.168.8.100 on eth0.  
  11. 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

  1. [[email protected] bin]# ip a  
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue   
  3.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
  4.     inet 127.0.0.1/8 scope host lo  
  5.     inet6 ::1/128 scope host   
  6.        valid_lft forever preferred_lft forever  
  7. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000  
  8.     link/ether 00:0c:29:50:2d:9d brd ff:ff:ff:ff:ff:ff  
  9.     inet 192.168.8.4/24 brd 192.168.8.255 scope global eth0  
  10.     inet 192.168.8.100/24 scope global secondary eth0  
  11.     inet6 fe80::20c:29ff:fe50:2d9d/64 scope link   
  12.        valid_lft forever preferred_lft forever  

启动备用节点B后的日志为:
[html] view plain copy

  1. Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: Configuration is using : 34262 Bytes  
  2. Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE  
  3. Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]  
  4. Sep 20 01:47:31 hadoopsrv Keepalived: Starting VRRP child process, pid=20567  


第二种:(这种较麻烦,需要借助脚本)
1,先设置A机器与B机器的eth1的网卡配置配置文件,配置好公网的ip信息,不设置公网的网关
&amp;amp;lt;img src=&quot;https://pic3.zhimg.com/19c97b837a38763a9e0cb03aa8b9ae2e_b.png&quot; data-rawwidth=&quot;715&quot; data-rawheight=&quot;130&quot; class=&quot;origin_image zh-lightbox-thumb&quot; width=&quot;715&quot; data-original=&quot;https://pic3.zhimg.com/19c97b837a38763a9e0cb03aa8b9ae2e_r.png&quot;&amp;amp;gt;keepalived的vip设置为公网IP

2,分别停止A机器与B机器的eth1网卡(ifdown eth1)

3,配置A机器的keepalived.conf配置文件(B机器请修改MASTER为BACKUP,priority的值小于100,其他不变)
&amp;amp;lt;img src=&quot;https://pic3.zhimg.com/d19dfc7d611d9f58264db63bc0a73bca_b.png&quot; data-rawwidth=&quot;596&quot; data-rawheight=&quot;409&quot; class=&quot;origin_image zh-lightbox-thumb&quot; width=&quot;596&quot; data-original=&quot;https://pic3.zhimg.com/d19dfc7d611d9f58264db63bc0a73bca_r.png&quot;&amp;amp;gt;keepalived的vip设置为公网IP

4,配置/etc/keepalived/scripts/master.sh脚本,并赋予可执行权限,判断公网ip是否在本机,如不在,执行启动eth1操作,并添加默认网关
&amp;amp;lt;img src=&quot;https://pic4.zhimg.com/687eefc1ffa29897117ff793534b5ceb_b.png&quot; data-rawwidth=&quot;615&quot; data-rawheight=&quot;146&quot; class=&quot;origin_image zh-lightbox-thumb&quot; width=&quot;615&quot; data-original=&quot;https://pic4.zhimg.com/687eefc1ffa29897117ff793534b5ceb_r.png&quot;&amp;amp;gt;keepalived的vip设置为公网IP
5,配置/etc/keepalived/scripts/slave.sh脚本,并赋予可执行权限,执行停止 eth1操作
&amp;amp;lt;img src=&quot;https://pic1.zhimg.com/3d631ff1aa987dc8ec1e264d5a9f0004_b.png&quot; data-rawwidth=&quot;645&quot; data-rawheight=&quot;72&quot; class=&quot;origin_image zh-lightbox-thumb&quot; width=&quot;645&quot; data-original=&quot;https://pic1.zhimg.com/3d631ff1aa987dc8ec1e264d5a9f0004_r.png&quot;&amp;amp;gt;keepalived的vip设置为公网IP
6,两边分别启动keepalived,并查看A主机的eth1网卡有没有启动,是否绑定公网ip 123.123.123.123。停止A主机的keepalived,查看B主机的网卡有没有启动,是否正常绑定公网ip。

 

https://blog.csdn.net/mergerly/article/details/73292748

相关文章: