薛坤keeplived+lvs负载均衡部署

时间 : 16-11-29 栏目 : linux课程 作者 : 老薛 评论 : 1 点击 : 1,190 次

一、网站负载均衡拓朴图

薛坤lvs keepalived负载均衡

IP信息列表:

名称

IP

LVS-DR-Master

10.0.0.248

LVS-DR-BACKUP

10.0.0.25

LVS-DR-VIP

10.0.0.8

WEB1-Realserver

10.0.0.2

WEB2-Realserver

10.0.0.6

GateWay

10.0.0.254

二、安装LVSKeepalvied软件包

1.下载相关软件包

#cd /usr/local/src

#wget
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

#wget
http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

2. 安装LVSKeepalived

#uname -r

2.6.18-53.el5PAE

#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/
/usr/src/linux

 

#tar
zxvf ipvsadm-1.24.tar.gz

#cd
ipvsadm-1.24

#make
&& make install

#find
/ -name ipvsadm #
查看ipvsadm的位置

 

#tar
zxvf keepalived-1.1.15.tar.gz

#cd
keepalived-1.1.15

#./configure
&& make && make install

#find
/ -name keepalived #
查看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/

#service
keepalived start|stop #
做成系统启动服务方便管理.

 

三、配置LVS实现负载均衡

   1.LVS-DR, 配置LVS脚本实现负载均衡

 #vi /usr/local/keep/lvs-dr.sh

 #!/bin/sh

GW=10.0.0.254

##############website
director vip.

SNS_VIP=10.0.0.8

SNS_RIP1=10.0.0.2

SNS_RIP2=10.0.0.6

#logger $0 called
with $1

 case "$1" in

  start)

   #############set vip ###############

#   /sbin/ipvsadm --set 30 5 60

   /sbin/ifconfig eth0:0 $SNS_VIP broadcast
$SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up

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

#   /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3

   /sbin/ipvsadm -A -t $SNS_VIP:80 -s rr

   /sbin/ipvsadm -a -t $SNS_VIP:80 -r
$SNS_RIP1:80 -g -w 1

   /sbin/ipvsadm -a -t $SNS_VIP:80 -r
$SNS_RIP2:80 -g -w 1

   touch /var/lock/subsys/ipvsadm >/dev/null
2>&1

   echo "ipvsadm
starting--------------------------[OK]"

   ;;

   stop)

   /sbin/ipvsadm -C

   ifconfig eth0:0 down

   route del
$SNS_VIP

   rm -rf /var/lock/subsys/ipvsadm
>/dev/null 2>&1

   echo "ipvsadm stoped"

   ;;

   status)

   if [ ! -e /var/lock/subsys/ipvsadm ];then

   echo "ipvsadm stoped---------------"

   exit 1

        else

            echo "ipvsadm Runing
------------[OK]"

   fi

   ;;

   *)

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

   exit 1

   esac

   exit 0

 

2. 配置Realserver脚本.

  #vi /usr/local/keep/realserver.sh

 #!/bin/sh

SNS_VIP=10.0.0.8

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

  3.启动lvs-dr脚本和realserver脚本,DR上可以查看LVS当前状态

#ipvsadm

四、利用Keepalvied实现负载均衡和和高可用性

  1.配置在主负载均衡服务器上配置keepalived.conf

  #vi /etc/keepalived/keepalived.conf

  !
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 127.0.0.1

#   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

    state MASTER            #备份服务器上将MASTER改为BACKUP

    interface eth0

    virtual_router_id 51

    priority 100               # 备份服务上将100改为99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        10.0.0.8

    }

}

 

virtual_server 10.0.0.8
80 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

#    nat_mask 255.255.255.0

    persistence_timeout 50

    protocol TCP

 

    real_server 10.0.0.2 80 {

        weight 1

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

    real_server 10.0.0.6 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

    }

}

2.
BACKUP
服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.

3. #/etc/init.d/keepalived
start
启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡

4. 查看lvs服务是否正常

[root@lvskeep keepalived-1.1.15]#
ipvsadm

IP Virtual Server version 1.2.1
(size=4096)

Prot LocalAddress:Port Scheduler Flags

  ->
RemoteAddress:Port           Forward
Weight ActiveConn InActConn

TCP  10.0.0.8:http
wrr persistent 50

  -> 10.0.0.6:http                Route   3      0          0        

  -> 10.0.0.2:http                Route   1      0          0

#tail
–f /var/log/message
监听日志,查看状态。

开机启动脚本:

/usr/local/sbin/keepalived
-D -f /etc/keepalived/keepalived.conf

cat
/dev/null >/var/log/messages
清空原有日志

测试:   

日志文件/var/log/messages

1. LVS-DR-Master启动, LVS-DR-BACKUP关机

  LVS-DR-Master的日志:

  [root@lvskeep
keepalived-1.1.15]# cat /var/log/messages

Sep 24 22:45:59 lvskeep Keepalived: Starting Keepalived v1.1.15
(09/24,2010)

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Using
MII-BMSR NIC polling thread...

Sep 24 22:45:59 lvskeep Keepalived: Starting Healthcheck child
process, pid=11472

Sep 24 22:45:59 lvskeep Keepalived: Starting VRRP child
process, pid=11473

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Netlink
reflector reports IP 10.0.0.248 added

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Registering
Kernel netlink reflector

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Registering
Kernel netlink command channel

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Configuration
is using : 11909 Bytes

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Activating
healtchecker for service [10.0.0.2:80]

Sep 24 22:45:59 lvskeep Keepalived_healthcheckers: Activating
healtchecker for service [10.0.0.6:80]

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Using MII-BMSR NIC
polling thread...

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Netlink reflector
reports IP 10.0.0.248 added

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Registering Kernel
netlink reflector

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Registering Kernel
netlink command channel

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Registering gratutious
ARP shared channel

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 24 22:45:59 lvskeep Keepalived_vrrp: Configuration is using
: 36296 Bytes

Sep 24 22:46:00 lvskeep Keepalived_vrrp: VRRP sockpool:
[ifindex(2), proto(112), fd(8,9)]

Sep 24 22:46:01 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Sep 24 22:46:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Sep 24 22:46:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Sep 24 22:46:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.0.0.8

Sep 24 22:46:02 lvskeep Keepalived_vrrp: Netlink reflector
reports IP 10.0.0.8 added

Sep 24 22:46:02 lvskeep Keepalived_healthcheckers: Netlink
reflector reports IP 10.0.0.8 added

Sep 24 22:46:02 lvskeep avahi-daemon[2187]: Registering new
address record for 10.0.0.8 on eth0.

Sep 24 22:46:07 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.0.0.8

2. LVS-DR-Master开机, LVS-DR-BACKUP启动 LVS-DR-BACKUP日志

  LVS-DR-BACKUP的日志:

  [root@xuekun
keepalived]# cat /var/log/messages

Sep 23 22:55:51 xuekun Keepalived: Starting Keepalived v1.1.15
(09/23,2010)

Sep 23 22:55:51 xuekun Keepalived_healthcheckers: Using
MII-BMSR NIC polling thread...

Sep 23 22:55:51 xuekun Keepalived_healthcheckers: Registering
Kernel netlink reflector

Sep 23 22:55:51 xuekun Keepalived_healthcheckers: Registering
Kernel netlink command channel

Sep 23 22:55:51 xuekun Keepalived_healthcheckers: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 23 22:55:51 xuekun Keepalived_healthcheckers: Configuration
is using : 8641 Bytes

Sep 23 22:55:51 xuekun Keepalived: Starting Healthcheck child
process, pid=6245

Sep 23 22:55:51 xuekun Keepalived_vrrp: Using MII-BMSR NIC
polling thread...

Sep 23 22:55:51 xuekun Keepalived_vrrp: Registering Kernel
netlink reflector

Sep 23 22:55:51 xuekun Keepalived_vrrp: Registering Kernel
netlink command channel

Sep 23 22:55:51 xuekun Keepalived_vrrp: Registering gratutious
ARP shared channel

Sep 23 22:55:51 xuekun Keepalived_vrrp: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 23 22:55:51 xuekun Keepalived_vrrp: Configuration is using
: 36204 Bytes

Sep 23 22:55:51 xuekun Keepalived: Starting VRRP child process,
pid=6246

Sep 23 22:55:51 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Sep 23 22:55:51 xuekun Keepalived_vrrp: VRRP sockpool:
[ifindex(2), proto(112), fd(7,8)]

3. LVS-DR-Master关机, LVS-DR-BACKUP开机 LVS-DR-BACKUP日志

  LVS-DR-BACKUP的日志:

  Sep 23 22:58:17 xuekun
Keepalived: Starting Keepalived v1.1.15
(09/23,2010)

Sep 23 22:58:17 xuekun Keepalived_healthcheckers: Using
MII-BMSR NIC polling thread...

Sep 23 22:58:17 xuekun Keepalived_healthcheckers: Registering
Kernel netlink reflector

Sep 23 22:58:17 xuekun Keepalived_healthcheckers: Registering
Kernel netlink command channel

Sep 23 22:58:17 xuekun Keepalived_healthcheckers: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 23 22:58:17 xuekun Keepalived_healthcheckers: Configuration
is using : 8641 Bytes

Sep 23 22:58:17 xuekun Keepalived: Starting Healthcheck child
process, pid=6299

Sep 23 22:58:17 xuekun Keepalived_vrrp: Using MII-BMSR NIC
polling thread...

Sep 23 22:58:17 xuekun Keepalived_vrrp: Registering Kernel
netlink reflector

Sep 23 22:58:17 xuekun Keepalived_vrrp: Registering Kernel
netlink command channel

Sep 23 22:58:17 xuekun Keepalived_vrrp: Registering gratutious
ARP shared channel

Sep 23 22:58:17 xuekun Keepalived_vrrp: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 23 22:58:17 xuekun Keepalived_vrrp: Configuration is using
: 36204 Bytes

Sep 23 22:58:18 xuekun Keepalived: Starting VRRP child process,
pid=6300

Sep 23 22:58:18 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Sep 23 22:58:18 xuekun Keepalived_vrrp: VRRP sockpool:
[ifindex(2), proto(112), fd(7,8)]

Sep 23 22:58:21 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Sep 23 22:58:22 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Sep 23 22:58:22 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Sep 23 22:58:22 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.0.0.8

Sep 23 22:58:22 xuekun avahi-daemon[2163]: Registering new
address record for 10.0.0.8 on eth0.

4. LVS-DR-Master启动, LVS-DR-BACKUP开机, LVS-DR-MasterLVS-DR-BACKUP日志

  LVS-DR-Master的日志:

  [root@lvskeep
keepalived-1.1.15]# cat /var/log/messages

Sep 24 22:57:01 lvskeep Keepalived: Starting Keepalived v1.1.15
(09/24,2010)

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Using
MII-BMSR NIC polling thread...

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Netlink
reflector reports IP 10.0.0.248 added

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Registering
Kernel netlink reflector

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Registering
Kernel netlink command channel

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers:
Configuration is using : 11909 Bytes

Sep 24 22:57:01 lvskeep Keepalived: Starting Healthcheck child
process, pid=11519

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Using MII-BMSR NIC
polling thread...

Sep 24 22:57:01 lvskeep Keepalived: Starting VRRP child
process, pid=11520

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Activating
healtchecker for service [10.0.0.2:80]

Sep 24 22:57:01 lvskeep Keepalived_healthcheckers: Activating
healtchecker for service [10.0.0.6:80]

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Netlink reflector
reports IP 10.0.0.248 added

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Registering Kernel
netlink reflector

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Registering Kernel
netlink command channel

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Registering gratutious
ARP shared channel

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Opening file
'/etc/keepalived/keepalived.conf'.

Sep 24 22:57:01 lvskeep Keepalived_vrrp: Configuration is using
: 36296 Bytes

Sep 24 22:57:01 lvskeep Keepalived_vrrp: VRRP sockpool:
[ifindex(2), proto(112), fd(8,9)]

Sep 24 22:57:01 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Transition to MASTER STATE

Sep 24 22:57:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Entering MASTER STATE

Sep 24 22:57:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
setting protocol VIPs.

Sep 24 22:57:02 lvskeep Keepalived_vrrp: VRRP_Instance(VI_1)
Sending gratuitous ARPs on eth0 for 10.0.0.8

Sep 24 22:57:02 lvskeep Keepalived_vrrp: Netlink reflector
reports IP 10.0.0.8 added

Sep 24 22:57:02 lvskeep Keepalived_healthcheckers: Netlink
reflector reports IP 10.0.0.8 added

Sep 24 22:57:02 lvskeep avahi-daemon[2187]: Registering new
address record for 10.0.0.8 on eth

LVS-DR-BACKUP的日志:

[root@xuekun
keepalived]# cat /var/log/messages

Sep 23 23:03:12 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Received higher prio advert

Sep 23 23:03:12 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
Entering BACKUP STATE

Sep 23 23:03:12 xuekun Keepalived_vrrp: VRRP_Instance(VI_1)
removing protocol VIPs.

Sep
23 23:03:12 xuekun avahi-daemon[2163]: Withdrawing address record for 10.0.0.8
on eth0.

本文标签

除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/operationsc/2071.html

薛坤keeplived+lvs负载均衡部署:目前有1 条留言

  1. admin
    沙发
    admin
    Post: 2016-11-29 下午3:37

    如有按此文档部署的过程中,可以联系博主。QQ:812711277

发表评论

1 + 9 = ?


博主微信号,很高兴为您提供帮助

随便看看

为您推荐

1