环境背景:2台Directir Server,2台Reral Server(也是web服务器)

IP分配

DS1:192.168.1.110

DS2:192.168.1.114

RS1:192.168.1.102

RS2:192.168.1.106

VIP:192.168.1.99


2台Directir Server都需要安装ipvsadm,是用来增加一个虚拟ip服务的工具

最简单的安装方法

[root@DS1 lvs]# yum install ipvsadm

在DS1,DS2上都安装上。


开启2台Directir Server的路由机制,DS1,DS2都需要执行。

vi /sbin/sysctl.conf 修改以下内容
net.ipv4.ip_forward = 1

修改完后执行

[root@DS1 lvs]#  sysctl -p

若报以下错误

error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key
error: "net.bridge.bridge-nf-call-iptables" is an unknown key
error: "net.bridge.bridge-nf-call-arptables" is an unknown key

解决方法为

modprobe bridge
lsmod|grep bridge



开启2台Directir Server的虚拟ip服务,DS1,DS2都需要执行。


[root@DS1 lvs]# cat /sbin/lvsdr.sh
#!/bin/bash
VIP=192.168.1.99
RIP1=192.168.1.102
RIP2=192.168.1.106
gw=192.168.1.1
/etc/rc.d/init.d/functions
case "$1" in
start)
       echo "start LVS of DirectorServer"
       #Set the Virtual IP Address
       /sbin/ifconfig eth1:0 $VIP broadcast $VIP netmask 255.255.255.255 up
       /sbin/route add -host $VIP dev eth0:1
       #Clear IPVS Table
       /sbin/ipvsadm -C
       #Set Lvs
       /sbin/ipvsadm -A -t $VIP:80 -s rr
       /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
       /sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g
       #Run Lvs
       /sbin/ipvsadm
;;
stop)
echo "Close LVS Directorserver"
/sbin/ifconfig eth1:0 down
/sbin/ipvsadm -C
;;
*)
echo "Usage0{start|stop}"
exit 1
esac

分配执行权限

[root@DS1 lvs]# chmod 755 /sbin/lvsdr.sh

启动虚拟ip服务脚本

[root@DS1 lvs]# /sbin/lvsdr.sh start

执行完后,验证是否正常

查看ifconfig是否有ifcfg-eth1:0 (有就对了)

查看route -n 路由表是否多了eth1:0路由(有就对了)



配置2台Reral Server(也是web服务器) 的VIP服务脚本,RS1,RS2都需要配置

[root@RS1 sbin]# cat realdr.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
VIP=192.168.1.99
. /etc/rc.d/init.d/functions
case "$1" in
start)
       ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $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
       sysctl -p
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $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
       sysctl -p
       echo "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
exit 0

配置可执行权限

chmod 755 /sbin/realdr.sh

启动脚本

/sbin/realdr.sh start



配置2台Directir Server的keepalive服务,DS1,DS2都需要配置。

DS1的配置文件

[root@DS1 keepalived]# cat 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 192.168.1.240
   #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
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.99
    }
}
virtual_server 192.168.1.99 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
   # nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
real_server 192.168.1.102 80 {
        weight 3
        TCP_CHECK {
      connect_port    80
      connect_timeout 3
    }
}
real_server 192.168.1.106 80 {
        weight 3
        TCP_CHECK {
      connect_port    80
      connect_timeout 3
    }
}
}

DS2的配置文件

[root@DS2 keepalived]# cat 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 192.168.1.240
   #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
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.99
    }
}
virtual_server 192.168.1.99 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
   # nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
real_server 192.168.1.102 80 {
        weight 3
        TCP_CHECK {
      connect_port    80
      connect_timeout 3
    }
}
real_server 192.168.1.106 80 {
        weight 3
        TCP_CHECK {
      connect_port    80
      connect_timeout 3
    }
}
}


启动2台DS的keepalive服务

[root@DS2 keepalived]# /etc/rc.d/init.d/keepalived start



关键配置到场结束

可在两台DS机器上查看连接情况

[root@DS2 keepalived]# 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.99:80 rr
  -> 192.168.1.106:80             Route   1      0          0       
  -> 192.168.1.102:80             Route   1      0          0       
[root@DS2 keepalived]#



最重要一个点说明:当出现错误时,一定要查看日志信息,所有的内容都会在日志中显示出来

查看路径

[root@DS2 keepalived]# cat /var/log/messages |more


这里可以显示出来lvs的运行情况.


最后可在两台RS的机器上搭建apache服务,这样可以直接在页面验证出来。

175811653.png

175813122.png