作者:姚姚
链接: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 3weight -20
}
vrrp_instance ID {  state MASTER    #设置为主服务器,备服务器设置为BACKUPinterface 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 ]; thenexit 0
elseexit 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
  1. [root@srv4 ~]# 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. [root@srv4 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;

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;

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;
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;
6,两边分别启动keepalived,并查看A主机的eth1网卡有没有启动,是否绑定公网ip 123.123.123.123。停止A主机的keepalived,查看B主机的网卡有没有启动,是否正常绑定公网ip。

keepalived的vip设置为公网IP相关推荐

  1. 【云计算】弹性公网IP

    theme: condensed-night-purple 小知识,大挑战!本文正在参与"程序员必备小知识"创作活动. 在云计算领域中,对于云虚拟服务器,裸金属服务器,虚拟IP(V ...

  2. 获取电脑出口公网IP地址

    钉钉推送时,根据需求可能需要设置出口公网IP地址段,下面是获取开发者出口公网IP地址的方式: 1.返回公网IP curl ifconfig.me 2.返回IP及运营商信息 curl cip.cc

  3. Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)...

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...

  4. h3c GR5200路由器上如何设置公网ip可以访问

    以下是配置说明,请参考: 页面向导:高级设置→地址转换→虚拟服务器 本页面为您提供如下主要功能 输入客户端访问虚拟服务器所使用的端口 取值范围:1-65535,端口范围必须从小到大,推荐设置10000 ...

  5. RouterOS 5.22固定公网IP共享上网设置

    说明: RouterOS版本:RouterOS 5.22破解版 网卡1(wan):接外网 IP:58.2xx.xxx.43 子网掩码:255.255.255.0 网关:58.2xx.xxx.41 DN ...

  6. 技术专题:几个子网通过一个公网IP上网的WAYOS设置方法

    最近有发现部分地方电信给用户的都是一个公网IP,然后几个子网,有一些人不清楚这是怎么一回事? 也不知道如何设置?我现在把这个告诉大家! 如:电信给的公网IP:125.92.233.48/255.255 ...

  7. 免费内网穿透工具(网络通),无需公网IP,无需设置路由器

    介绍一款免费的简单易用工具 网络通 这款工具永久免费内网端口映射,内网穿透软件,可轻松访问连接内网. 不需设置路由器,不需公网ip,不需固定ip,不需动态域名,用户可免费添加多个映射,适合在公司,小区 ...

  8. 优酷路由宝设置虚拟服务器,优酷路由宝添加“黑科技”,ROOT后加脚本,自动获取公网IP...

    文章背景: 网友是江苏省中国移动的宽带用户,移动宽带上网需要拨号,而且有时候拨号获得的是公网IP,有时候获取到的是内网IP,向我求助帮忙,编写个脚本,让路由器自动判断.获取到内网IP就自动重启,直到获 ...

  9. 个人家庭宽带如何使用和设置公网IP

    强烈推荐用电信的宽带,因为个人的宽带目前就电信支持提供公网IP(但是这个IP会随着路由器重启而变动) 1.打电话到电信客户申请公网IP使用 2.联系维修人员(就是给你装宽带的那位)将光猫改成桥接模式( ...

最新文章

  1. HDU 5606 tree 并查集
  2. Redis Cluster 介绍与搭建
  3. python学习框架图-Python学习—框架篇之初识Django
  4. linux命令冒号加叹号,Linux中的叹号命令
  5. 第 8 章 容器网络 - 051 - 在 overlay 中运行容器
  6. MapReduce实例(数据去重)
  7. 获取Dataset前几条数据的两种方法
  8. linux逻辑卷管理
  9. netty cpu 占用率 高_Netty 是如何支撑高性能网络通信的?
  10. html填满剩余空间,html – 标题,两侧填充剩余空间
  11. 深入理解计算机系统 相关课程,深入理解计算机系统
  12. 刚刚做完的一个屏幕截图程序,分享一下
  13. linux命令ps aux|grep xxx
  14. 微软Exchange Server 2010 SP1下载
  15. 守望先锋中的netcode_如何跟踪守望先锋中的化妆品和事件物品
  16. IE浏览器无法更改缓存,
  17. 5. 工业大数据典型应用
  18. java 实现写出倒立三角形的几种方法
  19. 图片转excel的网站
  20. vscode提示:“An SSH installation couldn‘t be found”

热门文章

  1. 阿里巴巴北京总部鸟瞰图曝光 2024年投入使用
  2. GIT合并分支到Master
  3. Linux下的cut选取命令详解
  4. 为Django添加图片验证码
  5. Python基础(if判断语句/逻辑运算符)
  6. Style样式设置器
  7. speedoffice表格中如何插入图片
  8. 四、SOCKET 协议
  9. Facebook如何击溃Myspace,Yahoo!和Google?(转载)
  10. 【数据结构】——单链表超详细介绍(独家介绍,小白必看!!!)