1.系统环境说明

OS: centos 6.2

Server A:  

内网:192.168.1.101      [em2]

外网:219.234.89.101     [em1]

Server B:

内网: 192.168.1.102     [em2]

外网:219.234.87.102     [em1]

VIP: 公网:219.234.87.100


应用场景: 搭建一个简单的服务器高可用,目的是为了能实现一台服务器宕机,另一台服务器立即响应对外提供服务(要求两台服务器上部署相同的服务并且服务都已开启);


2.keepalived下载与安装

[root@test2 tools]# wgethttp://www.keepalived.org/software/keepalived-1.2.7.tar.gz

[root@test2 tools]#yum -y install openssl opensll-devel poptpopt-devel libnl libnl-devel kernel-devel

[root@test2 tools]# tar -zxvf keepalived-1.2.7.tar.gz

[root@test2 tools]#cd keepalived-1.2.7

[root@test2 keepalived-1.2.7]# mkdir /usr/local/keepalived

[root@test2 keepalived-1.2.7]# ./configure--prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-358.6.2.el6.x86_64

[[email protected]]# make;make install


3.keepalived配置

[root@test2 keepalived-1.2.7]# cd /usr/local/app/keepalived/etc

[root@test2 etc]# vim keepalived.conf


3.1Server A keepalived.conf:

! Configuration File for keepalived


global_defs {

 notification_email {

      [email protected]

  }

 notification_email_from [email protected]

  smtp_server 192.168.1.101

 smtp_connect_timeout 30

  router_idLVS_DEVEL

}


vrrp_instance VI_1 {

   state BACKUP

   interface em1

  virtual_router_id 51

   mcast_src_ip 219.234.87.102

   priority 100

   advert_int 1

   authentication{

       auth_typePASS

       auth_pass gy2012

   }

   virtual_ipaddress{

       219.234.87.100

   }

}


3.2 Server B keepalived.conf:

! Configuration File for keepalived


global_defs {

 notification_email {

[email protected]

  }

 notification_email_from [email protected]

  smtp_server 192.168.1.102

 smtp_connect_timeout 30

  router_idLVS_DEVEL

}

#vrrp: 虚拟路由器冗余协议是一种选择协议,它可以把一个虚拟路由器的责任动态分配到局域网上的 VRRP 路由器中的一台(http://baike.baidu.com/view/876125.htm?fromId=51194)

vrrp_instanceVI_1 {

   state MASTER

   interface em1

  virtual_router_id 51

   mcast_src_ip 219.234.87.101

   priority 100

   advert_int 1

   authentication{

       auth_typePASS

       auth_pass gy2012

   }

  virtual_ipaddress {

       219.234.87.100

   }

}

4.启动和关闭

4.1 启动

[root@test2 etc]#/usr/local/app/keepalived/sbin/keepalived-f /usr/local/app/keepalived/etc/keepalived.conf –D


4.2 关闭

                  [root@test2 etc]# pkill keepalived

5.  测试,出现如下信息表示成功:

C:\Users\Administrator>ping xxxx.com


正在 Ping taoying.com [219.234.87.100] 具有 32 字节的数据:

来自 219.234.87.100 的回复: 字节=32 时间=6ms TTL=47

来自 219.234.87.100 的回复: 字节=32 时间=5ms TTL=48

来自 219.234.87.100 的回复: 字节=32 时间=7ms TTL=47