家居企业网站建设教程,长春怎么做网站,wordpress 空间 域名,竞价排名点击器一、HAproxy
1、负载均衡类型#xff1a;
(1) 无负载均衡#xff1a;
没有负载均衡#xff0c;用户直接连接到 Web 服务器。当许多用户同时访问服务器时#xff0c;可能无法连接。 (2) 四层负载均衡#xff1a;
用户访问负载均衡器#xff0c;负载均衡器将用户的请求…一、HAproxy
1、负载均衡类型
(1) 无负载均衡
没有负载均衡用户直接连接到 Web 服务器。当许多用户同时访问服务器时可能无法连接。 (2) 四层负载均衡
用户访问负载均衡器负载均衡器将用户的请求平衡转发给后端服务器。 (3) 七层负载均衡
7层负载均衡是更复杂的负载均衡方法使用第7层允许负载均衡器根据用户请求的内容将请求转发到不同的后端服务器。 2、HAproxy 亲缘性
在 HAProxy 中会话亲缘性是一种负载均衡策略它确保来自同一客户端的请求总是被路由到同一后端服务器维护会话状态的一致。
保持会话亲缘性的方式
● 用户 ip 识别
HAproxy 将客户端的IP地址计算出一个哈希值然后根据哈希值选择一个后端服务器。
● cookie 识别
在客户端的第一个请求中HAproxy 可以在响应中添加一个特定的 Cookie并在后续请求中使用该 Cookie 来识别客户端会话。
● session 识别
在 HAproxy 中可以将后端服务器生成的会话状态和后端服务器标识存储在HAProxy的表格stick-table中在客户端请求时可以查询该表格以维护会话亲缘性。
3、示例
(1) 环境
HAproxy192.168.198.131
web1192.168.198.132
web2192.168.198.133 域名解析vim /etc/hosts (2) web 配置
yum install -y httpd
echo web111 /var/www/html/index.html
echo web222 /var/www/html/index.html
(3) 配置 haproxy
yum install -y epel-release
yum install -y haproxy
vim /etc/haproxy/haproxy.cfg
globallog 127.0.0.1 local3 infomaxconn 4096user beangroup beandaemonnbproc 1pidfile /run/haproxy.piddefaultslog globalmode httpmaxconn 2048retries 3option redispatchtimeout connect 5000timeout client 50000timeout server 50000option abortonclosestats uri /admin?statsstats realm Private landsstats auth admin:123stats hide-versionfrontend http-inbind 0.0.0.0:80mode httplog globaloption httplogoption httpcloseacl html url_reg -i \.html$use_backend html-server if htmldefault_backend html-serverbackend html-servermode httpbalance roundrobinoption httpchk GET /index.htmlcookie SERVERID insert indirect nocacheserver html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5 global 设置全局配置参数通常与操作系统相关:
● log 127.0.0.1 local3 info配置日志记录的指令
● maxconn 4096: 每个后端服务器的最大连接数优先级较低
● daemon: haproxy以守护进程的方式运行确保haproxy在后台持续运行
● nbproc 1 pidfile /run/haproxy.pid haproxy 进程数和进程 id 存储位置 defaults配置默认参数这些参数可以被用到 frontendbackendListen 组件中。
● log global日志配置按全局配置中进行
● mode http haproxy 工作模式七层 http四层 tcp
● maxconn 2048最大连接数优先级比 global 高
● retries 3haproxy 尝试连接后端服务器的重试次数3次连接失败就认为服务不可用用户请求将不会被发往此后端服务器
● option redispatch当 haproxy 在连接到后端服务器失败时请求将分配给其他可用的后端服务器
● timeout connect 5000 timeout client 50000 timeout server 50000
timeout connect重传计时器haproxy将客户端请求转发给后端服务器所等待的超时时长若超时则再次进行转发
timeout clienthaproxy 作为客户端和后端服务器之间空闲连接的超时时间
timeout serverhaproxy 作为服务端和用户之间空闲连接的超时时间
● option abortonclose当服务器负载过高时haproxy 会结束挂起的请求释放资源提高性能
● stats uri /admin?stats
设置统计页面的URI路径在URL中输入/admin?stats时就可以进入haproxy 的统计页面
● stats realm Private lands stats auth admin:123
统计页面认证时的提示内容 设置用户名和密码
● stats hide-version隐藏了haproxy的版本信息以提高安全性 frontend http-in: 前端部分开始配置。
● bind 0.0.0.0:80: 前端监听器的绑定地址和端口haproxy 监听所有可用的网络接口0.0.0.0上的80端口
● option httplog这个选项开启了HTTP请求的详细日志记录
● option httpclosehaproxy 在每个HTTP事务结束后关闭与客户端的连接
● acl html url_reg -i \.html$ use_backend html-server if html
创建了一个名为 html 的ACL使用正则表达式 -i \.html$ 来匹配以 .html 结尾的URL若匹配中ACL则使用名为 html-server 的后端服务器来处理请求
● default_backend html-server
如果请求不匹配任何ACL条件则 html-server 后端服务器来处理这些请求。 backend html-server后端服务集群的配置。
● balance roundrobin使用的负载均衡算法为 roundrobin (rr)
● option httpchk GET /index.html
定义了健康检查的方式haproxy 使用HTTP GET 请求来检查后端服务器的健康状态。请求 /index.html 页面如果后端服务器返回预期的响应它将被标记为up否则将被标记为down
● cookie SERVERID insert indirect nocache
将用户访问所到达的后端服务器的 id 插入到 cookie 中保持用户与服务器的会话
● server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
后端服务器的名称地址和端口weight 1权重为1cookie 3cookie SERVERID
check inter 2000 rise 2 fall 5每2秒进行一次健康检查rise 2 表示连续2次成功的健康检查标记服务器为 up fall 5 表示连续5次失败的健康检查标记服务器为 down。
(4) 测试结果
systemctl start haproxy
客户机测试 登录 haproxy 统计页面