目录
一、理论
1、Haproxy集群
HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,是免费、快速并且可靠的一种解决方案。HAProxy非常适用于并发大(并发达1w以上)web站点,这些站点通常又需要会话保持或七层处理。HAProxy的运行模式使得它可以很简单安全的整合至当前的架构中,同时可以保护web服务器不被暴露到网络上。
2、特点
●可靠性和稳定性非常好,可以与硬件级的F5负载均衡设备相媲美;
●最高可以同时维护40000-50000个并发连接,单位时间内处理的最大请求数为20000 个,最大处理能力可达10Git/s;
●支持多达8种负载均衡算法
●支持Session会话保持,Cookie的引导;
●支持通过获取指定的url来检测后端服务器的状态;
●支持虚机主机功能,从而实现web负载均衡更加灵活;
●支持连接拒绝、全透明代理等独特的功能;
●拥有强大的ACL支持,用于访问控制;
●支持TCP和HTTP协议的负载均衡转发;
●支持客户端的keepalive功能,减少客户端与haproxy的多次三次握手导致资源浪费,让多个请求在一个tcp连接中完成
3、策略
- roundrobin,表示简单的轮询
- static-rr,表示根据权重
- leastconn,表示最少连接者先处理
- source,表示根据请求源IP
- uri,表示根据请求的URI,做cdn需使用
- url_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name
- hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求
- rdp-cookie(name),表示根据cookie(name)来锁定并哈希每一次TCP请求。
4、优势
① Haproxy在负载均衡的速度和并发处理上优于nginx
② Haproxy支持虚拟主机,工作在4,7层
③ Haproxy有会话保持,cookie引导等工作
④ Haproxy支持以url的方式检测后端服务器状态
⑤ Haproxy可以对mysql进行负载均衡
⑥ Haproxy支持很多负载均衡算法:轮询。加权轮询,原地址保持,请求URL,根据cookie
二、搭建web集群
实验环境 准备三台机器 一台Haproxy 两台节点服务器
然后创建目录 把下载好的安装包拖进来解压
然后下载依赖包
yum -y install gcc gcc-c++ make zlib-devel.x86_64 pcre-devel.x86_64 openssl-devel.x86_64 systemd-devel.x86_64
然后创建用户 查看内核 编译
make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1
编译好后 安装 make install PREFIX=/usr/local/haproxy
然后
然后 vim /etc/init.d/haproxy 把我标记的这行加上 “ ”
可以直接复制粘贴到配置里面
# Baisic config mapping a listening IP:port to another host's IP:port with
# support for HTTP/1 and 2.
global
log 127.0.0.1 local0 info
log 127.0.0.1 local1 warning
maxconn 3000
pidfile /var/run/haproxy.pid
user haproxy
group haproxy
daemon
# nbproc 1
spread-checks 2
defaults
log global
mode http
option http-server-close
option forwardfor
#option httplog
option dontlognull
option redispatch
option abortonclose
maxconn 20000
retries 3
timeout http-request 2s
timeout queue 3s
timeout connect 1s
timeout client 10s
timeout server 2s
timeout http-keep-alive 10s
timeout check 2s
frontend http-xy101
bind 20.0.0.100:80
#maxconn 18000
#acl url_static1 path_beg -i /static /images
acl url_jsp path_end -i .jsp
#use_backend ms1 if url_static1
use_backend tomcat_backend if url_jsp
default_backend nginx_backend
backend nginx_backend
balance roundrobin
option http-keep-alive
option httpchk GET /test.html
server nginx.inst1 20.0.0.101:80 maxconn 10000 check inter 2000 rise 2 fall 3
server nginx.inst2 20.0.0.102:80 maxconn 10000 check inter 2000 rise 2 fall 3
backend tomcat_backend
# balance roundrobin
# option httpchk GET /test.html
server tomcat.inst1 20.0.0.101:8080 maxconn 10000 check inter 2000 rise 2 fall 3
server tomcat.inst2 20.0.0.102:8080 maxconn 10000 check inter 2000 rise 2 fall 3
listen stats
# this is the address and port we'll listen to, the ones to aim the
# load generators at
bind :8000
stats enable
stats refresh 30s
stats uri /stats
stats realm HAProxy\ Stats
stats auth admin:admin123
# create a certificate and uncomment this for SSL
# bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
# Put the server's IP address and port below
server s1 172.31.32.33:8000
然后查看配置是否正确 然后启动它
现在haproxy配好了 接下来去配置节点服务器
两台节点服务器同时初始化一下
两台机器先安装nginx 安装命令 rpm -ivh nginx软件包
然后准备网页
先进入目录下
cd /usr/share/nginx/html/
然后
第一台机器 echo "this is kgc web1" > /usr/local/nginx/html/test.html
第二台机器 echo "this is kgc web2" > /usr/local/nginx/html/test.html
然后两台机器 启动 nginx 启动命令 systemctl start nginx
然后两台机器安装 tomcat 解压命令 tar xf 安装包
然后移动解压后的包 到 /usr/local/tomcat 并改名tomcat
然后启动 tomcat
然后准备网站
先依次进入目录
然后 vim test.jsp 配置内容如下
直接复制
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<body>
<% out.println("动态页面1");%>
</body>
</html>
第一台机器 动态页面1 第二台机器 动态页面2 到时候好区分
此时 集群就创建好了 可以验证一下
实验结束