目录
前言
负载均衡(Load Balance):
lvs
是工作在四层、仅负责分发请求作用的负载均衡,负载均衡性能最强。Lvs实现负载均衡可结合keepalived进行后端健康检测。
nginx
工作在7层,针对http应用做一些策略配置,可以检测后端端口故障,最大的优点是并发量高,且支持二次开发,商用价值高。
haproxy
是工作在4/7层,提供高可用性和负载均衡。本文将通过配置haproxy的负载均衡、结合pacemaker实现haproxy双机热备、fence介绍haproxy。haproxy具有健康检测
7层负载平衡是更复杂的负载均衡网络流量的方法是使用第7层(应用层)负载均衡。使用第7层允许负载均衡器根据用户请求的内容将请求转发到不同的后端服务器。这种负载平衡模式允许您在同一域和端口下运行多个Web应用程序服务器。
一、haproxy负载均衡
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。
实验环境:
server1为haproxy主机, server2和server3为haproxy后端负载均衡主机
实验步骤:
server1安装 haproxy
[root@server1 ~]# yum install haproxy -y
编写配置文件vim /etc/haproxy/haproxy.cfg
gloabal是全局,defaluts是4/7层,使用7层,defaluts会覆盖global
# 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
stats uri /status #haproxy 监控页面
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main *:80
#acl url_static path_beg -i /static /images /javascript /stylesheets
#acl url_static path_end -i .jpg .gif .png .css .js
#iuse_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such