前言
访问大型服务器网站由于网络流量过大,比如双十一的访问,网络流量访问太大导致所有的流量都集中到一台服务器上,这个时候可以通过负载均衡来把流量分担到不同的服务器上。负载平衡主要是通过最小化响应时间、优化资源的使用率和提高多服务器配置的性能来平衡服务器上的负载。那么如何来实现这个负载均衡呢?nginx以及HAProxy等都可以进行负载均衡,nginx支持七层负载均衡,然而HAProxy支持四层负载以及七层负载均衡等更多功能。该文主要讲解HAProxy负载,那么啥是HAProxy呢?
HAProxy或高可用性代理是一种开源的TCP和HTTP负载平衡器和代理服务器软件。HAProxy由Willy Tarreau用C语言编写,它支持SSL,压缩,保持活动状态,自定义日志格式和标头重写。HAProxy 是一款快速轻量级的代理服务器和负载平衡器,内存占用小,CPU 使用率低。它被Github,StackOverflow,Reddit,Tumblr,Twitter等大型网站使用。在过去几年中,它已成为最流行的软件负载平衡器和代理服务器。
下面通过一些例子来讲解负载均衡。
一,更新系统
对centos系统的软件进行一些更新或者升级,从而在进行安装的时候可以很快的找到安装包。
sudo yum install epel-release -ysudo yum update -y
二,安装HAXProxy
由于HAXProxy在centos仓库中默认就有HAProxy的安装包,所以可以直接通过yum命令来执行就完成安装。
sudo yum install haproxy -y
安装完成之后检查是否安装成功。
yum info haproxy
安装完成为了能够让服务在启动时生效,配置HAProxy服务在重启或者启动时运行。
为了能够通过外网能够访问HAProxy,配置HAProxy服务的防火墙。
firewall-cmd --permanent --zone=public --add-service=httpfirewall-cmd --permanent --zone=public --add-port=8181/tcpfirewall-cmd --reload
最后通过以下命令启动HAProxy服务。
systemctl start haproxy && systemctl status haproxy
三,安装nginx
为了能够演示负载均衡,安装俩个nginx服务器,从而能够通过负载均衡能够负载访问两台不同的nginx服务器。
yum install nginx
接下来检查完没有配置nginx是否安装成功。
yum info nginx
修改nginx服务器中的index.html,从而能够区分是哪一个服务器。
echo "web1">/usr/share/nginx/html/index.htm
修改完成之后,接下来启动nginx服务器。另外一台服务器的nginx也是相同的操作。
四,配置HAProxy
HAProxy均衡有可以实现4层负载均衡以及七层负载均衡。
第 4 层负载均衡器最常用于简单数据包负载均衡。这种类型的负载平衡在传输级别运行(并且不检查文件的内容),因此通过第 4 层负载平衡器的所有流量都根据请求的网络信息进行管理。应用程序端口或 TCP 协议将是此类信息的一个例子。数据可以快速移动,因为在发送或接收时不会对其进行检查或加密。下面通过一个例子来配置4层负载均衡。
cp haproxy.cfg haproxy.cfgorgvi haproxy.cfg
[root@localhost haproxy]# vi haproxy.cfg#---------------------------------------------------------------------# Example configuration for a possible web application. See the# full configuration options online.## http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------# common defaults that all the 'listen' and 'backend' sections will# use if not designated in their block#---------------------------------------------------------------------defaults mode http log global option httplog option dontlognull option http-server-close# option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000#---------------------------------------------------------------------# main frontend which proxys to the backends#---------------------------------------------------------------------#frontend main *:5000# acl url_static path_beg -i /static /images# acl url_static path_end -i .jpg .gif .png .css .js## use_backend static if url_static# default_backend app#---------------------------------------------------------------------# static backend for serving up images, stylesheets and such#---------------------------------------------------------------------#backend static# balance roundrobin# server static 127.0.0.1:4331 check#---------------------------------------------------------------------# round robin balancing between the various backends#---------------------------------------------------------------------#backend app# balance roundrobin# server app1 127.0.0.1:5001 check# server app2 127.0.0.1:5002 check# server app3 127.0.0.1:5003 check# server app4 127.0.0.1:5004 checklisten http_web 192.168.175.130:8082 mode http balance roundrobin # Load Balancing algorithm# option httpchk option forwardfor server server1 192.168.175.129:8082 weight 1 maxconn 512 check server server2 192.168.175.130:80 weight 1 maxconn 512 check
上图中的192.168.175.129.8082 以及192.168.175.130 是两台nginx服务器对应的不同端口。下面来验证一下,访问192.168.175.130:8082会轮询返回不同服务器内容。
七层负载均衡
七层负载均衡是更高级的负载均衡,它可以检查正在发送或请求的内容,这与第 4 层不同。这种类型的检查在高级应用程序层称为七层负载平衡。用户可以在您的网站上打开会话并请求特定类型的内容(如图片或视频)或下订单。第 7 层将基于该用户的请求类型将流量路由到高度优化的后端服务器,这些服务器存储请求的图像或视频。第七层负载均衡可以操作的应用层很多报文,不止通过端口来进行负载。
下面通过一个配置来验证七层负载均衡配置。
[root@localhost haproxy]# vi haproxy.cfg#---------------------------------------------------------------------# Example configuration for a possible web application. See the# full configuration options online.## http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------# common defaults that all the 'listen' and 'backend' sections will# use if not designated in their block#---------------------------------------------------------------------defaults mode http log global option httplog option dontlognull option http-server-close# option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000#---------------------------------------------------------------------# main frontend which proxys to the backends#---------------------------------------------------------------------#frontend main *:5000# acl url_static path_beg -i /static /images /javascript # acl url_static path_end -i .jpg .gif .png .css .js## use_backend static if url_static# default_backend app#---------------------------------------------------------------------# static backend for serving up images, stylesheets and such#---------------------------------------------------------------------#backend static# balance roundrobin# server static 127.0.0.1:4331 check#---------------------------------------------------------------------# round robin balancing between the various backends#---------------------------------------------------------------------#backend app# balance roundrobin# server app1 127.0.0.1:5001 check# server app2 127.0.0.1:5002 check# server app3 127.0.0.1:5003 check# server app4 127.0.0.1:5004 checklisten monitor *:8080 mode http option httplog stats uri / stats auth admin:admin stats admin if TRUE stats enable stats refresh default_backend backendfrontend web bind *:8083 option http-server-close option forwardfor default_backend backendbackend backend balance roundrobin server server1 192.168.175.129:8082 check server server2 192.168.175.130:80 check
最后,来看一个7层负载均衡示例,通过cookie的值来进行负载均衡。
[root@localhost haproxy]# vi haproxy.cfg#---------------------------------------------------------------------# Example configuration for a possible web application. See the# full configuration options online.## http://haproxy.1wt.eu/download/1.4/doc/configuration.txt##---------------------------------------------------------------------#---------------------------------------------------------------------# Global settings#---------------------------------------------------------------------global # to have these messages end up in /var/log/haproxy.log you will # need to: # # 1) configure syslog to accept network log events. This is done # by adding the '-r' option to the SYSLOGD_OPTIONS in # /etc/sysconfig/syslog # # 2) configure local2 events to go to the /var/log/haproxy.log # file. A line like the following can be added to # /etc/sysconfig/syslog # # local2.* /var/log/haproxy.log # log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats#---------------------------------------------------------------------# common defaults that all the 'listen' and 'backend' sections will# use if not designated in their block#---------------------------------------------------------------------defaults mode http log global option httplog option dontlognull option http-server-close# option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000#---------------------------------------------------------------------# main frontend which proxys to the backends#---------------------------------------------------------------------#frontend main *:5000# acl url_static path_beg -i /static /images# acl url_static path_end -i .jpg .gif .png .css .js## use_backend static if url_static# default_backend app#---------------------------------------------------------------------# static backend for serving up images, stylesheets and such#---------------------------------------------------------------------#backend static# balance roundrobin# server static 127.0.0.1:4331 check#---------------------------------------------------------------------# round robin balancing between the various backends#---------------------------------------------------------------------#backend app# balance roundrobin# server app1 127.0.0.1:5001 check# server app2 127.0.0.1:5002 check# server app3 127.0.0.1:5003 check# server app4 127.0.0.1:5004 checklisten monitor *:8080 mode http option httplog stats uri / stats auth admin:admin stats admin if TRUE stats enable stats refresh default_backend backendfrontend web bind *:8083 option http-server-close option forwardfor acl host_bac hdr(host) -i 192.168.175.130 acl web1 cook(gray_flag) -i 1 acl web2 cook(gray_flag) -i 2 use_backend backend if host_bac || web1 use_backend backend1 if host_bac || web2 default_backend backendbackend backend balance hdr(X-Forwarded-For) server server1 192.168.175.129:8082 cookie server1 check inter 4000 rise 3 fall 3 #server server2 192.168.175.130:80 cookie server2 check inter 4000 rise 3 fall 3 cookie back-1 insert indirect nocache maxidle 10m maxlife 4hbackend backend1 balance hdr(X-Forwarded-For) server server2 192.168.175.130:80 cookie server2 check inter 4000 rise 3 fall 3 cookie back-2 insert indirect nocache maxidle 10m maxlife 4h
通过上图可以看出,根据不同的cookie值可以进行不同的服务器负载。