echo "1" > /proc/sys/net/ipv4/ip_forward   --允许转发


ipvsadm  -A -t 192.168.1.20:80 -s wlc    --为LVS添加算法,WLC为最小负载


ipvsadm -a -t 192.168.1.20:80 -r 192.168.1.9:80 -g   --添加客户端


ipvsadm -a -t 192.168.1.20:80 -r 192.168.1.69:80 -g


[root@master ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.1.20:80 wlc

 -> 192.168.1.9:80               Route   1      0          0        

 -> 192.168.1.69:80              Route   1      0          0  

此时Activeconn和inActconn都为0,因为无客户端的请求


【安装LVS】

1、【主从都要执行】

yum -y install ipvsadm




ipvsadm -C 清除lvs上所有的规则


2、【keepalived】  主从都要配置

[root@CentOS-LVS_MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz


[root@CentOS-LVS_MASTER ~]# tar zxvf keepalived-1.2.4.tar.gz


[root@CentOS-LVS_MASTER ~]# cd keepalived-1.2.4


[root@CentOS-LVS_MASTER keepalived-1.2.4]# ./configure && make && make install


######### 将keepalived做成启动服务,方便管理##########


[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/


[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/


[root@CentOS-LVS_MASTER ~]# mkdir /etc/keepalived/


[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/


[root@CentOS-LVS_MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/


[root@CentOS-LVS_MASTER ~]# service keepalived start | stop


2、开启路由转发


[root@CentOS-LVS_MASTER ~]# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

[root@CentOS-LVS_MASTER ~]# sysctl -p


3、配置keepalived

 ! Configuration File for keepalived

global_defs {

  notification_email {

        [email protected]

  }

  notification_email_from [email protected]

  smtp_server 127.0.0.1

  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.20

   }

}

virtual_server 192.168.1.20 80 {

   delay_loop 6

   lb_algo wlc

   lb_kind DR

#    persistence_timeout 60

   protocol TCP

   real_server 192.168.1.69 80 {

       weight 3

       TCP_CHECK {

       connect_timeout 10

       nb_get_retry 3

       delay_before_retry 3

       connect_port 80

        }

   }

   real_server 192.168.1.9 80 {

       weight 3

       TCP_CHECK {

       connect_timeout 10

       nb_get_retry 3

       delay_before_retry 3

       connect_port 80

       }

    }

}


##从的话,只要修改2个地方就行了

一个是state 改为BACKUP

一个是priority 改为比100小就行了

然后再开启keepalived服务


在查看ipvsadm -ln就会出现配置文件中的服务器列表了



查看主从上是否有vip这个ip

LVS+keepalived_第1张图片



3、【配置后端服务器】---让后端web能与vip通信

vim 1.sh

#!/bin/bash

#description : start realserver

VIP=192.168.1.20

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

case "$1" in

start)

echo " start LVS of REALServer"

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

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

;;

stop)

/sbin/ifconfig lo:0 down

echo "close LVS Directorserver"

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 "Usage: $0 {start|stop}"

exit 1

esac


然后执行该脚本  sh 1.sh start


最后查看web上的ip信息,看看是否有vip

153314351.jpg


【检测】

1、查看主上面的日志信息

155150836.jpg


然后停了主上面的keepalived服务,查看从上面的日志信息:

155343851.jpg


然后停了后端其中一台WEB,看看lvs会不会把他给剔除掉

Removing service [10.8.0.61]:3306 from VS [10.8.0.85]:3306
61就不会出现在队列中