Linux配置虚拟IP(VIP) - 猿码设计师

Linux配置虚拟IP(VIP) - 猿码设计师Linux vip config配置;添加虚IP的两种方式:手动ifconfig添加和keepalived添加虚ip。在keepalived中2种模式,分别是master->backup模式和backup->backup模式https://www.yuanmadesign.com/ymdesign/linux-vip

手动添加虚IP
1 ifconfig查看当前活动网卡。如:eth0
2 进行vip添加

ifconfig eth0:0 166.111.69.100 netmask 255.255.255.0 up

3 执行ifconfig查看是否生效
4 测试 ping 166.111.69.100
5 写在/etc/rc.local里进行开机自动设置


第三方工具keepalived增加VIP

1,下载与安装

安装:
tar xf keepalived-1.2.12.tar.gz
cd keepalived-1.2.12
./configure –prefix=/usr/local/keepalived
make &&  make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

2,配置:/etc/keepalived/keepalived.conf

global_defs {

notification_email {

xxx@qq.com

}

notification_email_from dba@dbserver.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id MySQL-HA

}

vrrp_instance VI_1 {

state BACKUP

interface eth1

virtual_router_id 51

priority 150

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.0.88

}

}

  • notification_email : 变更后通知的email
  • notification_email_from : 发送email
  • router_id MySQL-HA: 路由器组id,局域网中需要共享该vip的服务器,该配置要一致。
  • state BACKUP:在keepalived中2种模式,分别是master->backup模式和backup->backup模式
    这两种模式有很大区别。在master->backup模式下,一旦主库宕机,虚拟ip会自动漂移到从库,当主库修复后,keepalived启动后,还会把虚拟ip抢占过来,即使设置了非抢占模式(nopreempt)抢占ip的动作也会发生
    在backup->backup模式下,当主库宕机后虚拟ip会自动漂移到从库上,当原主库恢复和keepalived服务启动后,并不会抢占新主的虚拟ip,即使是优先级高于从库的优先级别,也不会发生抢占。
  • interface eth1: 具体网卡,通过ifconfig确认具体网卡。
  • priority 150: 表示设置的优先级为150
  • nopreempt: 非抢占模式

启动:
> /etc/init.d/keepalived start ; tail -f /var/log/messages’

> 或者 systemctl start keepalived

显示如下表示成功:

Starting keepalived:                                       [  OK  ]
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Registering gratuitous ARP shared channel
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Opening file ‘/etc/keepalived/keepalived.conf’.
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Configuration is using : 62976 Bytes
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Using LinkWatch kernel netlink reflector…
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)]
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.80.138 added
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.0.60 added
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6a9e added
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6aa8 added
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink reflector
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink command channel
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Opening file ‘/etc/keepalived/keepalived.conf’.
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Configuration is using : 7231 Bytes
Apr 20 20:26:18 192 kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP)
Apr 20 20:26:18 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
Apr 20 20:26:18 192 kernel: IPVS: ipvs loaded.
Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Using LinkWatch kernel netlink reflector…

测试:
1:ping命令测试。
2:一台服务器停止keepalived服务进行ip漂移测试。

» 转载保留版权:猿码设计师 » 《Linux配置虚拟IP(VIP)》

» 本文链接地址:Linux配置虚拟IP(VIP) - 猿码设计师

» 如果喜欢可以:关注《猿码设计师》公众号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值