相关知识了解
-
LVS
全称Linux Virtual Server,也就是Linux虚拟服务器,由章文嵩(现就职于于淘宝,正因为如此才出现了后来的fullnat模式)博士发起的Load Balance开源项目。 -
Keepalived
keepalived是Linux下一个轻量级别的高可用解决方案。高可用(High Avalilability,HA),广义上讲,是指整个系统的高可用行,狭义上来说就是主机的冗余和接管。 -
Nginx
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
拓扑图
最少五台服务器(这里为了节省服务器,所以将三个tomcat都放在一个服务器上了,当然实际的情况应该是七台)
vip:192.168.1.100
lvs master 192.168.1.101
lvs slave 192.168.1.102nginx1 192.168.1.201
nginx2 192.168.1.202tomcat 192.168.1.203(三台tomcat,端口不一样)
centos 7.x
安装Nginx 和 Tomcat 并且配置好负载均衡
具体的流程可以参考这篇文章:配置nginx和tomcat负载均衡/反向代理
安装lvs+keepalived
1. lvs master
- 安装ipvsadm、keepalived
yum -y install ipvsadm keepalived
- 开启路由转发
查看是否开启
sysctl -a | grep "ip_forward"
如果显示net.ipv4.ip_forward = 1
则表示已经开启。
永久开启
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
加载,使得配置文件立即生效
sysctl -p
检查
sysctl -a | grep "ip_forward"
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
- 使用keepalived来管理lvs(master)
vim /etc/keepalived/keepalived.conf
global_defs
{
router_id master_210
}
vrrp_instance aiyou {
state MASTER
interface eth0 #对应虚拟机的网卡,使用ifconfig查看
virtual_router_id 100 #这个数值 master和slave必须统一
priority 151 #这个数值决定哪台服务器是master
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.146.200
}
}
virtual_server 192.168.146.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
# 这里的real server是具体的一台nginx服务器
real_server 192.168.146.139 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
# 这里的real server是具体的一台nginx服务器
real_server 192.168.146.140 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
重启keepalived
systemctl restart keepalived
查看状态
systemctl status keepalived
2. lvs slave
- 安装ipvsadm、keepalived
yum -y install ipvsadm keepalived
- 开启路由转发
永久开启
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
加载,使得配置文件立即生效
sysctl -p
检查
sysctl -a | grep "ip_forward"
net.ipv4.ip_forward = 1
net.ipv4.ip_forward_use_pmtu = 0
- 使用keepalived来管理lvs(slave)
vim /etc/keepalived/keepalived.conf
global_defs
{
router_id slave_211
}
vrrp_instance aiyou {
state MASTER
interface eth0
virtual_router_id 100 #这个数值 master和slave必须统一
priority 150 #这个数值决定哪台服务器是master 这里我们比master数值低,所以角色是backup,
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.146.200
}
}
virtual_server 192.168.146.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
# persistence_timeout 50
protocol TCP
# 这里的real server是具体的一台nginx服务器
real_server 192.168.146.139 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
# 这里的real server是具体的一台nginx服务器
real_server 192.168.146.140 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
重启keepalived
systemctl restart keepalived
查看状态
systemctl status keepalived
3. nginx1
- nginx上要跑个脚本
vi /etc/init.d/realserver
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=192.168.1.222 #这里根据需要改成自己的VIP地址
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
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
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
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
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
赋予可执行权限,并启动脚本
chmod 755 /etc/init.d/realserver
service realserver start
- nginx 2(每个nginx步骤和内容一样)
4. 测试
- master上验证
ipvsadm –ln
- salve上验证
ipvsadm –ln
如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~
网友评论