LVS+Keepalived搭建负载均衡服务器

LVS+Keepalived搭建负载均衡服务器

 

IP信息列表:

LVS-DR-Master                192.168.0.14

LVS-DR-BACKUP              192.168.0.16

Realserver-1                   192.168.0.11

Realserver-2                   192.168.0.30

VIP192.168.0.110

GATEWAY=192.168.0.1

服务器IP配置信息:

192.168.0.14    cat /etc/sysconfig/network-scripts/ifcfg-eth0

192.168.0.16    cat /etc/sysconfig/network-scripts/ifcfg-eth0

192.168.0.11    cat /etc/sysconfig/network-scripts/ifcfg-eth0

192.168.0.30    cat /etc/sysconfig/network-scripts/ifcfg-eth0

 

一、安装lvs

分别在LVS-DR-Master和LVS-DR-BACKUP上安装lvs

http://www.linuxvirtualserver.org/官网下载lvs ipvsadm-1.26.tar.gz

1.将压缩包上传到/usr/local/soft/下

2.解压tar包

3.cd ipvsadm-1.26

4.lvs的安装依赖gcc、yum install -y libnl* popt*

5.make && make install

二、安装keepalived

分别在LVS-DR-Master和LVS-DR-BACKUP上安装keepalived

http://www.keepalived.org/官网下载keepalived  1.2.17

1.将压缩包上传到/usr/local/soft/下

2.解压tar包

3.cd keepalived-1.2.17

4.keepalived的安装依赖yum install -y openssl openssl-devel

5../configure 

6.make && make install

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

8.cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

9.mkdir /etc/keepalived

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

11.cp /usr/local/sbin/keepalived /usr/sbin/

12.keepalived加入系统启动项chkconfig --level 0123456 keepalived on

13.service keepalived start|stop

三、开启路由转发

vi /etc/sysctl.conf

修改下面的值,从0修改为1:

net.ipv4.ip_forward = 1

刷新系统变量,使系统文件变更马上生效

sysctl -p

DR和REALSERVER服务器的配置文件如下:

 

四、DR配置master和backup

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

     [email protected]#email通知接收的邮箱

#     [email protected]

#     [email protected]

   }

   notification_email_from [email protected]#email通知发起的邮箱

   smtp_server 127.0.0.1

#   smtp_connect_timeout 30

   router_id LVS_DEVEL#设置lvs的id,在一个网络内应该是唯一的

}

 

vrrp_sync_group hot {           #设置vrrp组

group {

        loadbalance

}

}

 

vrrp_instance loadbalance {

    state MASTER#备份服务器上将MASTER改为BACKUP设置lvs的状态,包括MASTER和BACKUP两种,必须大写

    interface eth0#设置对外服务的接口

    lvs_sync_daemon_inteface eth0   #设置lvs监听的接口

    virtual_router_id 51#设置虚拟路由表示

    priority 100#备份服务上将100改为99设置优先级,数值越大,优先级越高

    advert_int 5#设置同步时间间隔

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {#设置lvs vip

        192.168.0.110

#        192.168.200.17

#        192.168.200.18

    }

}

 

virtual_server 192.168.0.110 80 {

    delay_loop 6#健康检查时间间隔每隔x秒查询realserver状态

    lb_algo wlc#负载均衡调度算法

    lb_kind DR#负载均衡转发规则

    #nat_mask 255.255.255.0

    persistence_timeout 60#(同一IP的连接60秒内被分配到同一台realserver)

    protocol TCP#(用TCP协议检查realserver状态)

 

    real_server 192.168.0.11 80 {

        weight 3#(权重)

        TCP_CHECK {

        connect_timeout 10       #(10秒无响应超时)

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

 

    real_server 192.168.0.30 80 {

        weight 3#(权重)

        TCP_CHECK {

        connect_timeout 10       #(10秒无响应超时)

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

}

五、RealServer配置文件

vi /usr/local/sbin/realserver.sh

#!/bin/bash

# description: Config realserver lo and apply noarp

 

SNS_VIP=192.168.0.110

 

/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

六、RealServer配置文件赋给执行权限

chmod 755 /etc/rc.d/init.d/functions

chmod 755 /usr/local/sbin/realserver.sh

/usr/local/sbin/realserver.sh start

添加自启动  echo “/usr/local/sbin/realserver.sh start” >> /etc/rc.local

启动时如果报错:

/bin/bash^M: bad interpreter: 没有那个文件或目录

vi /usr/local/sbin/realserver.sh

:set ff?

若果出现fileformat=dos那么说明文件格式是windows的

:set fileformat=unix

:wq

七、日志位置  /var/log/message

八、开放端口  vi /etc/sysconfig/iptables

增加一行

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

保存  重启 service iptables restart

相关推荐