Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1462704
  • 博文数量: 122
  • 博客积分: 340
  • 博客等级: 一等列兵
  • 技术积分: 2967
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-01 11:50
个人简介

斑竹网络专注为中小企业客户提供以管理服务为核心的IT全方位服务 https://www.sysadm.cn

文章分类

全部博文(122)

文章存档

2018年(2)

2017年(1)

2015年(2)

2014年(30)

2013年(81)

2011年(5)

2009年(1)

分类: 系统运维

2013-11-22 13:46:25

lvs_keepalived安装配置

(整理时间:2012-04-10)

(环境CentOS release 5.5  2.6.18-194.el5xen)

一、 拓朴结构

Director1 192.168.1.214   VIP 192.168.1.216  (内核版本:2.6.32-71.el6.i686 #1 SMP

Director2:192.168.1.213  VIP:192.168.1.216 (内核版本:2.6.9-11.ELsmp

RealServer1:192.168.1.213 VIP:192.168.1.216

RealServer2:192.168.1.205  VIP:192.168.1.216

二、 软件安装

Director1上安装

   安装 ipvsadm

rpm -ihv ipvsadm-1.24-10.i386.rpm

下载 keepalived 
# wget 

关闭不必要的服务
vi shutoffservice.sh

#!/bin/bash

/sbin/chkconfig bluetooth off

/sbin/chkconfig firstboot off

/sbin/chkconfig cups off

/sbin/chkconfig ip6tables off

/sbin/chkconfig smartd off

service bluetooth stop

service firstboot stop

service cups stop

service ip6tables stop

service smartd stop

# chmod +x shutoffservice.sh

# ./shutoffservice.sh

安装keepalived

# rpm -ihv kernel-devel-2.6.18-194.el5.i686.rpm

# tar -zxvf keepalived-1.2.2.tar.gz

# ln -s /usr/src/kernels/2.6.18-194.el5-xen-i686/ /usr/src/linux

# ./configure

出现如下信息说明已经配置成跟内核集成了:

Keepalived configuration

------------------------

Keepalived version       : 1.2.2

Compiler                 : gcc

Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd

Extra Lib                : -lpopt -lssl -lcrypto 

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

IPVS use libnl           : No

Use VRRP Framework       : Yes

Use Debug flags          : No

#make 编译报错,换一个版本

# wget 

# tar -zxvf keepalived-1.1.15.tar.gz

# cd keepalived-1.1.15  

# ./configure

# make

# make install

这此编译成功

设置keepalived以服务方式启动

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

配置keepalived

# cd /etc/keepalived/

vi keepalived.conf

修改内容成如下:

! Configuration File for keepalived

global_defs {

   notification_email {

     wayne.wang@weejoy.cn

   }

#   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

mcast_src_ip 192.168.1.214  #主机的IP,当主机故障后恢复,通知备机时,发给备机的IP地址头,备机有了这个头,就能及时将自己切回为备机角色

    authentication {

        auth_type PASS

        auth_pass mko09ijn

    }

    virtual_ipaddress {

        192.168.1.216

    }

}

virtual_server 192.168.1.216 80 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

#    persistence_timeout 50

    protocol TCP

    real_server 192.168.1.213 80 {

         weight 3

         TCP_CHECK {

         connect_timeout 10

         nb_get_retry 3

         delay_before_retry 3

         connect_port 80

         }

    }

    real_server 192.168.1.205 80 {

         weight 3

         TCP_CHECK {

         connect_timeout 10

         nb_get_retry 3

         delay_before_retry 3

         connect_port 80

         }

    }

}

启重keepalived 
/etc/rc.d/init.d/keepalived start 

然后用:

# ip a 命令查看,有类似如下信息,就表明配置成功:
2: eth0:  mtu 1500 qdisc pfifo_fast qlen 10

    link/ether 78:e7:d1:5e:91:b0 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.214/24 brd 192.168.1.255 scope global eth0

    inet 192.168.1.216/32 scope global eth0

    inet6 fe80::7ae7:d1ff:fe5e:91b0/64 scope link 

       valid_lft forever preferred_lft forever
红色斜体部分是vip地址,即系统已经将vip绑定到eth0网卡上成功,同时用以下命令查看:

# ipvsadm –l

P Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.1.216:http wrr

  -> 192.168.1.205:http           Route   3      0          0         

  -> 192.168.1.213:http           Route   3      0          0   

# chkconfig add keepalived   keepalived添加为自启动

# chkconfig level 345 keepalived on  

Director2上安装

关闭不必要的服务

vi shutoffservice.sh

#!/bin/bash

/sbin/chkconfig bluetooth off

/sbin/chkconfig firstboot off

/sbin/chkconfig cups off

/sbin/chkconfig ip6tables off

/sbin/chkconfig smartd off

service bluetooth stop

service firstboot stop

service cups stop

service ip6tables stop

service smartd stop

# chmod +x shutoffservice.sh

# ./shutoffservice.sh

# ln -s /usr/src/kernels/2.6.18-194.el5-xen-i686/ /usr/src/linux

# wget 

# tar -zxvf keepalived-1.1.15.tar.gz

# cd keepalived-1.1.15

# ./configure

# make

# make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

vi keepalived.conf

修改内容成如下:

! Configuration File for keepalived

global_defs {

   notification_email {

     wayne.wang@weejoy.cn

   }

#   notification_email_from Alexandre.Cassen@firewall.loc

#   smtp_server 192.168.200.1

#   smtp_connect_timeout 30

   router_id LVS_DEVEL

! Configuration File for keepalived

global_defs {

   notification_email {

     wayne.wang@weejoy.cn

   }

#   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 BACKUP

    interface eth0

    virtual_router_id 51

    priority 90

advert_int 1

mcast_src_ip 192.168.1.213   #备机地址,当备份故障后重启给主机发通知的IP信息

    authentication {

        auth_type PASS

        auth_pass mko09ijn

    }

    virtual_ipaddress {

        192.168.1.216

    }

}

virtual_server 192.168.1.216 80 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

#    persistence_timeout 50

    protocol TCP

    real_server 192.168.1.213 80 {

         weight 3

         TCP_CHECK {

         connect_timeout 10

         nb_get_retry 3

         delay_before_retry 3

         connect_port 80

         }

    }

    real_server 192.168.1.205 80 {

         weight 3

         TCP_CHECK {

         connect_timeout 10

         nb_get_retry 3

         delay_before_retry 3

         connect_port 80

         }

    }

}

# chkconfig add keepalived   keepalived添加为自启动

# chkconfig level 345 keepalived on

查看是否安装配置成功与主机相同

三、 配置真实服务器上的脚本

分别在两台真实服务器上建立如下脚本:

# vi realserver.sh  

#!/bin/bash

SNS_VIP=192.168.1.216

. /etc/rc.d/init.d/functions

case "$1" in

start)

       ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP

       /sbin/route add -host $SNS_VIP dev lo:0

       echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

       sysctl -p >/dev/null 2>&1

       echo "RealServer Start OK"

       ;;

stop)

       ifconfig lo:0 down

       route del $SNS_VIP >/dev/null 2>&1

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore

       echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce

       echo "RealServer Stoped"

       ;;

*)

       echo "Usage: $0 {start|stop}"

       exit 1

esac

exit 0

 # chmod +x realserver.sh

# vi /etc/rc.d/rc.local   添加以上脚本,以便开机自动运行脚本

/root/shell/realserver.sh start &

四、 测试

分别关掉两台真实服务器中的一台,然后检查服务是否能正常访问;

分别关掉主从Director,检查系统是否能自动切换

阅读(1604) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~