环境:
VIP 192.168.2.224
LVS 192.168.2.217 centos7
nginx1 192.168.2.231 centos6
nginx2 192.168.2.194 centos6
LVS原理:
一、LVS节点需做的
1、安装ipvsadm,keepalived
yum -y install keepalived ipvsadm
yum install net-tools
2、修改配置keepalived的配置文件
global_defs {
router_id LVS_DEVEL
} vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.2.224
}
} virtual_server 192.168.2.231 {
delay_loop
lb_algo rr
lb_kind NAT
protocol TCP real_server 192.168.2.194 {
weight
SSL_GET {
connect_timeout
nb_get_retry
delay_before_retry
}
} real_server 192.168.2.231 {
weight
SSL_GET {
connect_timeout
nb_get_retry
delay_before_retry
}
}
}
3、启动keepalived
systemctl start keepalived
4、执行下面这个脚本
#!/bin/bash
VIP=192.168.2.224
RIP1=192.168.2.231
RIP2=192.168.2.194
case "$1" in
start)
ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ifconfig eno16777736: $VIP broadcast $VIP netmask 255.255.255.255 broadcast $VIP up
route add -host $VIP dev eno16777736:
ipvsadm -A -t $VIP: -s rr
ipvsadm -a -t $VIP: -r $RIP1: -g
ipvsadm -a -t $VIP: -r $RIP2: -g
echo "ipvsadm starting --------------------[ok]"
;;
stop)
ipvsadm -C
systemctl stop ipvsadm
ifconfig eno16777736: down
route del $VIP
echo "ipvsamd stoped----------------------[ok]"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped---------------"
exit
else
echo "ipvsamd Runing ---------[ok]"
fi
;;
*) echo "Usage: $0 {start|stop|status}"
exit
esac
exit
二、web节点需做的
1、先在两台nginx上把nginx装好,通过本机IP访问能区别两台机器,方便后面的测试,我这的nginx直接用的yum安装
2、分别在两台机器上执行如下脚本
#!/bin/bash
VIP=192.168.2.224 case "$1" in
start)
ifconfig lo: $VIP netmask 255.255.255.255 broadcast $VIP
/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 >&
echo "RealServer Start OK "
;;
stop)
ifconfig lo: down
route del $VIP /dev/null >&
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 "RealServer Stopd"
;;
*)
echo "Usage: $0 {start|stop}"
exit
esac exit
三、测试
这里的测试要在非lvs,nginx机器上测试,也就是与这些无关的机器上测试
注:参考网上各路大哥的,但是还是有很多问题,我这个绝对的可以。