haproxy、nginx以及httpd负载均衡tomcat主机,session保持问题
**实验环境**CentOS 7
haproxy(nginx):172.16.61.4(node4)
httpd+tomcat:172.16.61.7(node7)
172.16.61.8(node8)
http://s3.运维网.com/wyfs02/M01/7D/34/wKiom1biSr7xF0_-AACaR_ESkoo427.jpg
一、使用nginx做负载均衡
**配置tomcat以及httpd**
#在node8节点添加jvmRoute标记,便于直观查看负载均衡效果(node7节点为Tomcat7)
# cat proxy.conf #httpd配置(两台tomcat要相同)
ServerName tc1.tz.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
Require all granted
ProxyPass / ajp://localhost:8009/
ProxyPa***everse / ajp:://localhost:8009/
Require all granted
# cat index.jsp #在tomcat的虚拟主机中定义主页内容
Tomcat7
node8.tz.com
Session ID
Created on
**配置nginx**
upstream tcsrvs { #在nginx配置文件中定义upsteam模块,默认采用轮询的调度算法
server node8:80; #基于主机名调度,本地hosts文件要解析后端hostname
server node7:80;
}
location / { #在server中定义location进行反向代理
proxy_set_header Host $http_host; #将客户端请求的host代理至后端
proxy_pass http://tcsrvs/;
} 配置本地hosts文件,解析172.16.61.4 node8.tz.com地址
http://s3.运维网.com/wyfs02/M00/7D/35/wKiom1biV1jRWQdrAAA9sbYVxC0897.png
http://s3.运维网.com/wyfs02/M00/7D/34/wKioL1biV97i47hwAAA-RcShikk605.png
实现了轮询的效果
**haproxy负载均衡tomcat**
# sed 's@^#.*\+\|^[[:space:]]\+#.*\+@@' haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
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
frontendmain *:80
default_backend tomcat
listen statistics
bind *:9090
stats enable
statshide-version
stats uri /haproxyadmin?stats
stats realm"HAproxy\ Statistics"
stats auth admin:tianzhuang
stats adminif TRUE
backend tomcat
balance roundrobin
server tomcat1 172.16.61.7:80 check
servertomcat2 172.16.61.8:80 check **httpd负载均衡配置**
BalancerMember http://172.16.61.7:80 loadfactor=1
BalancerMember http://172.16.61.8:80 loadfactor=1
ProxySet lbmethod=bytraffic
ServerNamelocalhost
ProxyRequests Off
ProxyViaOn
ProxyPass / balancer://tccluster/
ProxyPa***everse / balancer://tccluster/
ProxyPreserveHost On
Require all granted
Require all granted
**保持session会话**
[*] nginx负载均衡session保持问题:
upstream tcsrvs {
server node8:80;
server node7:80;
ip_hash; #基于客户端地址进行hash,将来自同一ip的请求发至同一个server
# hash $request_uri;#基于请求的uri hash,应用于后端的缓存server;
# hash $remote_addr;#基于远程客户端地址hash,相当于ip_hash;
}
[*] haproxy负载均衡session保持问题:
backend tomcat
balance roundrobin
cookieTOMCAT insert nocache #在后端主机的响应报文中插入cookie
server tomcat1 172.16.61.7:80 check cookie tomcat1
server tomcat2 172.16.61.8:80 check cookie tomcat2
[*] httpd负载均衡session保持问题:
# cat proxy.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED #增加头部的session信息,以route为关键字
BalancerMember http://172.16.61.7:80 loadfactor=1 route=Tomcat7#route为Tomcat7,后端tomcat server也需要加入该route信息
BalancerMember http://172.16.61.8:80 loadfactor=1 route=Tomcat8
ProxySet lbmethod=bytraffic
ProxySet stickysession=ROUTEID
ServerNamelocalhost
ProxyRequests Off
ProxyViaOn
ProxyPass / balancer://tccluster/
ProxyPa***everse / balancer://tccluster/
ProxyPreserveHost On
Require all granted
Require all granted
页:
[1]