relha 发表于 2015-12-24 10:59:10

Keepalived 实现双机热备

Keepalived 实现双机热备
[日期:2015-04-15]来源:Linux社区作者:tangyanbo[字体:大 中 小]

  原理
  首先有一个虚拟ip暴露给客户端,虚拟ip对应的mac地址为一台真实服务器,即用户向虚拟ip发送一个请求,该请求会被分发到真实服务器上。
  现在有2台真实服务器,一台master,一台backup,master和backup上都运行着keepalived,当master挂了的时候,backup检测之后,自己成为master,且arp缓存虚拟ip对应的mac地址将变为backup的mac地址,这样请求虚拟ip的报文会被发送到backup
  架构图如下:

  安装
  主机:192.168.1.227
  备机:192.168.1.246
  虚拟ip:192.168.1.99
  安装命令:
  wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure --disable-fwmark --prefix=/usr/local/keepalived
make && make install
  整理:
  mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
  配置:
  vi /etc/keepalived/keepalived.conf
  Master
  
  ! Configuration File for keepalived
  global_defs {
notification_email {
    acassen@firewall.loc
    failover@firewall.loc
    sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
  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.1.99
    }
}
  
  Backup 和master一样,只要修改如下地方:
  
  vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99 # 这里改为99,master优先级是100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      192.168.1.99
    }
}
  
  管理命令:
  
  停止
service keepalived stop
启动
service keepalived start
重启
service keepalived restart
查看状态
service keepalived status
  
  测试
  在本机上查看虚拟ip状态
  ip add

  
  在同一网段的其他机器上查询arp缓存
  arp -a

  这里的1.99对应的mac地址是1.227
  停掉master的keepalived
  service keepalived stop
  在同一网段的其他机器上查询arp缓存
  arp –a

  发现1.99的mac地址已经变为slave的mac地址
  那么主备双机热备就完成了
  总结:本篇文章实现的功能
  1.master主机挂了,或者master 上keepalived挂了之后,可以切换到slave
  2.master的网络不通的时候,可以立即切换到slave,但是如果只是master上的应用出现问题的时候,是不会主动切换的,这个要通过编写脚本来监控应用出问题,这个问题后续再讨论吧。
  CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.iyunv.com/Linux/2013-06/85598.htm
  Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.iyunv.com/Linux/2012-03/55672.htm
  Keepalived+Haproxy配置高可用负载均衡 http://www.iyunv.com/Linux/2012-03/56748.htm
  Haproxy+Keepalived构建高可用负载均衡 http://www.iyunv.com/Linux/2012-03/55880.htm
  CentOS 7 上配置LVS + Keepalived + ipvsadm http://www.iyunv.com/Linux/2014-11/109237.htm
  Keepalived高可用集群搭建 http://www.iyunv.com/Linux/2014-09/106965.htm
  Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里
  本文永久更新链接地址:http://www.iyunv.com/Linux/2015-04/116231.htm
页: [1]
查看完整版本: Keepalived 实现双机热备