#LVS+keepalived快速搭建测试环境
#LVS+keepalived快速搭建测试环境
#centos6 X64
# LVS 负载均衡模式:DR(直接路由) 192.168.18.31 master(LVS)
192.168.18.32 slave(LVS)
192.168.18.18 VIP
192.168.18.11 web1
192.168.18.12 web2 ##################################
#手动配置测试 ###LVS 安装配置
yum install ipvsadm -y
rpm -ql ipvsadm
# ipvsadm
# lsmod|grep ip_vs
#添加VIP
/sbin/ifconfig eth1: 192.168.18.18/ up
#ifconfig eth1: down #测试完后停止VIP
ipvsadm -C #清空
#添加虚拟服务
ipvsadm -A -t 192.168.18.18: -s wrr -p
#添加后端服务
ipvsadm -a -t 192.168.18.18: -r 192.168.18.11: -g -w
ipvsadm -a -t 192.168.18.18: -r 192.168.18.12: -g -w
#删除ipvsadm -d -t 192.168.18.18: -r 192.168.18.11:
ipvsadm -L -n #查看列表
########## ##################################
#后端WEB配置
# #安装Apache或nginx,启动服务
# yum install httpd -y
# echo “this is rs1” > /var/www/html/index.html
# service httpd restart
vim lvs-rs.sh
#!/bin/bash
# 配置VIP、配置ARP抑制
VIP=192.168.18.18
. /etc/rc.d/init.d/functions case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo: $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null >&
;;
stop)
/sbin/ifconfig lo: down
echo "close LVS Directorserver"
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 {start|stop}"
exit
esac
exit #给脚本加权限,并执行
chmod +x lvs-rs.sh
chmod /etc/rc.d/init.d/functions
./lvs-rs.sh start ##################################
#LVS+keepalived 配置
yum install keepalived -y
#rpm -ql keepalived
#分别安装配置keepalived
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
/etc/init.d/keepalived restart
/etc/init.d/keepalived stop #关闭、开启LVS_MASTER测试
ip add #查看VIP
#配置如下: #####keepalived配置
! Configuration File forkeepalived
global_defs {
# notification_email {
# test@sina.com #故障接受联系人
# }
# notification_email_from admin@test.com #故障发送人
# smtp_server 127.0.0.1 #本机发送邮件
# smtp_connect_timeout
router_id LVS_MASTER
#router_id LVS_BACKUP #LVS_BACKUP
}
vrrp_instance VI_1 {
state MASTER
#state BACKUP
interface eth1
virtual_router_id #虚拟路由标识,主从相同
priority
#priority #BACKUP
advert_int
authentication {
auth_type PASS
auth_pass #主从认证密码必须一致
}
virtual_ipaddress { #Web虚拟IP(VTP)
192.168.18.18/
}
}
###real_server###
virtual_server 192.168.18.18 { #定义虚拟IP和端口
delay_loop #检查真实服务器时间,单位秒
lb_algo rr #设置负载调度算法,rr为轮训
lb_kind DR #设置LVS负载均衡DR模式
persistence_timeout #同一IP的连接30秒内被分配到同一台真实服务器
protocol TCP #使用TCP协议检查realserver状态
real_server 192.168.18.11 { #第一个web服务器
weight #节点权重值
TCP_CHECK { #健康检查方式
connect_timeout #连接超时
nb_get_retry #重试次数
delay_before_retry #重试间隔/S
}
}
real_server 192.168.18.12 { #第二个web服务器
weight
TCP_CHECK {
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
#####keepalived配置 ##
curl http://192.168.18.18 >/tmp/null #http访问测试
tail - /var/log/nginx/access.log #查看nginx日志
tail - /etc/httpd/logs/access_log #查看Apache日志