作用:负载、高可用、经济(相比硬件负载)、扩展收缩方便
基于tcp/udp四层协议负载
LVS的三种转发模式:
LVS-NAT:网络地址转换 Network address translation
LVS-DR:直接路由 Direct routing
LVS-TUN:IP隧道 IP tunneling
LVS的常用调度算法:
WRR:Weighted round-robin:加权轮循-按照性能分配,为其计算出权重
WLC:Weighted least connection:加权最少连接-overhead/weight,考虑到real serverd的性能,默认集群算法
-------------------------------------
准备:
关闭防火墙:
service iptables stop
setenforce 0
chkconfig iptables off
chkconfig selinux off
网卡配置:
/bin/cat > /etc/sysconfig/network-scripts/ifcfg-eth2<< EOF
DEVICE=eth2
IPADDR=192.168.2.30
NETMASK=255.255.255.0
GATEWAY=192.168.2.2
ONBOOT=yes
EOF
service network restart
主机名、dns配置:
/bin/cat > /etc/sysconfig/network << EOF
NETWORKING=yes
HOSTNAME=drbd_02
EOF
echo "nameserver 114.114.114.114" > /etc/resolv.conf
网络yum源配置:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum makecache
------------------------------------------
LVS负载服务器配置:
利用网络yum安装lvs软件包:
yum -y install keepalived
/etc/init.d/keepalived restart
ps -ef |grep keepalived
编辑主配置文件:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
edisonchou@hotmail.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
smtp_connection_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth4
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass zhong
}
virtual_ipaddress {
192.168.2.25
}
}
virtual_server 192.168.2.25 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
#
real_server 192.168.2.20 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
#
real_server 192.168.2.21 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
#
}
----------------------------------------------
real server应用服务器配置:
vi /etc/init.d/realserver
/etc/init.d/realserver restart
#--------------
SNS_VIP=192.168.2.25
/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