一,环境配置
VIP:192.168.222.222
RIP:192.168.222.134
RIP:192.168.222.135
DR_MASTER:192.168.222.128
DR_SLAVE:192.168.222.129
二,DR配置
1,安装软件
yum install epel-release -y
yum install ipvsadm -y
yum install keepalived -y
2,keepalived配置
MASTER上面配置:
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1#运行keepalived的机器的一个标识
}
vrrp_instance VI_1 {
state MASTER
interface eth0#实例绑定的网卡
virtual_router_id 51#VRID 标记 (0-255)主备服务器必须一致
priority 100#优先级
advert_int 1#检查间隔,默认为1秒
authentication {
auth_type PASS#认证方式 PASS
auth_pass 1111#认证密码
}
virtual_ipaddress {
192.168.222.222# VIP 地址
}
}
virtual_server 192.168.222.222 80 {
delay_loop 3#每3秒查询realserver状态
lb_algo wrr
lb_kind DR
# persistence_timeout 50#会话保持时间,就是同一个ip在50秒内会被分到同一个realserver上
protocol TCP#使用TCP 协议来检查
real_server 192.168.222.134 80 {#配置Real Server,需要指定真实IP与其端口号,两者用空格隔开
weight 3
TCP_CHECK {#以什么方式来检查健康状况 HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
connect_timeout 3#连接超时时间
nb_get_retry 3#重试连接次数
delay_before_retry 3#每次重试连接间隔(单位秒)
connect_port 80
}
}
real_server 192.168.222.135 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
SLAVE不同配置如下其它项相同:
#vim /etc/keepalived/keepalived.conf
router_id LVS_DEVEL2
state SLAVE
priority 150
3,启动服务
/etc/init.d/keepalived start
三,realserver配置
1,vim realserver.sh
#!/bin/bash
SNS_VIP=192.168.222.222
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
# /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
2,启动
/etc/init.d/httpd start
./realserver.sh start
附注:以上是lvs+keepalived的配置,以下是不需要keepalived,无高可用单lvs的简单配置,其lvs_dr.sh如下:
#!/bin/bash
#website director vip.
SNS_VIP=192.168.222.222
SNS_RIP1=192.168.222.135
SNS_RIP2=192.168.222.134
# ./etc/rc.d/init.d/functions
case "$1" in
start)
# set squid vip
/sbin/ipvsadm --set 30 5 60
/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 up
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t $SNS_VIP:80 -s rr
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80
touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
echo "ipvsadm started"
;;
stop)
/sbin/ipvsadm -C
/sbin/ipvsadm -Z
ifconfig eth0:0 down
rm -f /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 OK"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
四,增加lvs并发连接
调整 ip_vs_conn_tab_bits的方法:
新的IPVS代码,允许调整 ip_vs_conn_bits 的值。而老的IPVS代码则需要通过重新编译来调整。
在发行版里,IPVS通常是以模块的形式编译的。
确认能否调整使用命令 modinfo -p ip_vs(查看 ip_vs 模块的参数),看有没有 conn_tab_bits 参数可用。假如可以用,那么说时可以调整,调整方法是加载时通过设置 conn_tab_bits参数:
在/etc/modprobe.d/目录下添加文件ip_vs.conf,内容为:
options ip_vs conn_tab_bits=20
假如没有 conn_tab_bits 参数可用,则需要重新调整编译选项,重新编译。