背景
当业务访问量达到一定规模需引入负载均衡支持系统的水平扩展,以集群方式来应对访问量的增加。负载均衡有四层负载、七层负载:四层负载是基于OSI模型前四层通过请求转发实现负载,七层负载是基于OSI模型七层通过请求代理实现负载。LVS是Linux 四层负载的标准方案:基于集群技术在提供相同服务的业务服务器(real server)之前架设转发服务器(Director),由它作为网络请求唯一入口,根据负载算法将请求转发至业务服务器,整个集群看起来像是一台高性能的虚拟服务器。nginx是应用很广的七层负载,基于应用层(http相关信息)信息采用upstream方式进行负载均衡选取业务服务器,通过反向代理发起新请求获取响应信息,然后返回给请求客户端
相关概念
- 集群
- LVS(Linux Virtual Server)
- IPVS
- ipvsadm
- DR/NAT
- ARP
- Keepalived
- VRRP
搭建
四层负载网络拓扑(LVS_DR)
- 路由器通过端口映射(NAT)将目标IP、PORT调整为DS的VIP、PORT
- DR模式基于数据链路层负载均衡,DS、RS、路由器必须在同一网段
- RS、DS端口需保持一致
- DS从RS列表中选取一台并将请求的MAC地址调整为RS的MAC地址
- RS直接响应客户端请求
四层负载配置
LVS通过Keepalived配置最为简便,keepalived是LVS的管理组件,通过VRRP协议实现DS服务的主从备份避免单点故障,通过心跳机制监听RS服务状态实现故障机器自动排除。
- DS配置(keepalived.conf):
global_defs{
lvs_id LVS_VS_1
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state MASTER
interface wlp4s0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.90
}
}
virtual_server 192.168.0.90 8000 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 600
protocol TCP
real_server 192.168.0.110 8000 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}
real_server 192.168.0.111 8000 {
weight 100
TCP_CHECK {
connect_timeout 3
}
}
}
- RS配置(lvs_real.sh)
#!/bin/bash
# chkconfig: 345 20 80
# description: lvs_real ....
VIP=192.168.0.90
# Start the service
start() {
echo " start LVS of REALServer"
/sbin/ifconfig lo0 alias VIP netmask 255.255.255.255 -arp 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
}
stop() {
/sbin/ifconfig lo0 alias down
echo "close LVS REALServer"
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
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
;;
restart|reload|condrestart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
七层负载网络拓扑(LVS_DR+Nginx)
- nginx 基于反向代理,发起新请求从业务服务器获取响应内容
- nginx 可基于应用层信息提供更精细的负载配置
- nginx 通过加载特定功能模块,提供附加功能,如:安全检验、日志记录
七层负载配置
- nginx服务配置(nginx_proxy.conf)
http {
......
......
upstream django {
server 192.168.0.10:8088;
server 192.168.0.9:8088;
server 192.168.0.11:8088;
}
server {
listen 8000;
server_name dream.ticp.net; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / {
proxy_pass django;
include /opt/local/etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
......
......
}
网友评论