实验准备:
1、虚拟机172.18.250.77 安装nginx和httpd 实现负载均衡和反向代理
2、虚拟机172.18.250.78 安装tomcat
3、虚拟机172.18.250.79 安装tomcat
架构图:
一、nginx反向代理和负载均衡
1、安装nginx和tomcat
]# rpm -ivh nginx-1.8.0-1.el6.ngx.x86_64.rpm ]# lsapache-tomcat-7.0.55.tar.gz jdk-7u79-linux-x64.rpm ]# tar -xf apache-tomcat-7.0.55.tar.gz -C /usr/local/ //解压二进制编译包]# cd /usr/local/]# ln -s apache-tomcat-7.0.55/ tomcat //软链接]# rpm -ivh jdk-7u79-linux-x64.rpm //安装官方的二进制jdk包系统找java时是根据JAVA_HOME这个系统变量来寻找的,所以导出这个变量:]# vim /etc/profile.d/java.sh/usr/local/tomcatJAVA_HOME=/usr/java/latestPATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME PATH]# vim /etc/profile.d/tomcat.sh //导出tomcat路径到环境变量中CATALINA_HOME=/usr/local/tomcatPATH=$CATALINA_HOME/bin:$PATHexport CATALINA_BASE PATH]# . /etc/profile.d/java.sh ]# . /etc/profile.d/tomcat.sh ]# echo $JAVA_HOME/usr/bin/]# echo $CATALINA_HOME/usr/local/tomcat
2、配置nginx和tomcat,在250.78和250.79上
]# vim /etc/nginx/conf.d/default.conf location / { root /usr/share/nginx/html; proxy_pass http://localhost:8080/ //所有请求nginx80端口的都反代到tomcat8080上 index index.html index.htm; } ]# cd /usr/local/tomcat/webapps/ ]# mkdir myapp //创建测试目录]# cd myapp/]# mkdir classes lib WEB-INF META-INF]# vim index.jsp //提供测试页<%@ page language="java" %><%@ page import="java.util.*" %>JSP Tset Page <% out.println("Hello World from 250.78"); %> ]# nginx //启动tomcat和nginx]# catalina.sh startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usr/java/latestUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcat started.
3、测试反代是否正常
4、配置nginx负载均衡
]# vim /etc/nginx/nginx.conf //在250.77上配置upstream webserver { server 172.18.250.78:80; server 172.18.250.79:80; }]# vim /etc/nginx/conf.d/default.conf location / { root /usr/share/nginx/html; proxy_pass http://webserver/; index index.html index.htm; } ]# nginx ]# curl http://172.18.250.77/myapp/index.jspJSP Tset Page Hello World from 250.78 ]# curl http://172.18.250.77/myapp/index.jsp //在服务器上curl请求,实现了负载均衡JSP Tset Page Hello World from 250.79
4、基于session sticky实现会话保持
]# vim /etc/nginx/nginx.conf upstream webserver { server 172.18.250.78:80; server 172.18.250.79:80; ip_hash; }
源地址hash后,多次刷新,都是反代到相同的一台服务器上,保持了会话
二、httpd的反向代理
]# yum -y install httpd //三台服务器全部装上httpd]# nginx -s stop //停止nginx
配置httpd,实现反向代理:
]# httpd -M //httpd的反代有三种方式 proxy_ajp_module (shared) //ajp协议的反代模块 proxy_balancer_module (shared) //负载均衡模块 proxy_http_module (shared) //http协议的反代模块 还有一种模块是mod_jk,需要手动安装加载使用 1、配置http协议的反代模块]# cd /etc/httpd/conf.d/ ]# vim http_proxy_tomcat.confServerName web.magedu.com ProxyVia On //用于在多级代理中控制代理请求的流向 ProxyRequests Off //关闭正向代理 ProxyPreserveHost On //如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址 //允许反代任何请求 Require all granted ProxyPass / http://localhost:8080/ //反代到指定服务器 ProxyPa***everse / //用于让apache调整HTTP重定向响应报文中的Location、Content-Location及URI标签所对应的URL,在反向代理环境中必须使用此指令避免重定向报文绕过proxy服务器。 Require all granted
测试能否正常反代:
2、配置ajp协议的反代]# vim ajp_proxy_tomcat.confServerName web1.magedu.com ProxyRequests Off ProxyPreserveHost On Require all granted ProxyPass / ajp://localhost:8009/ ProxyPa***everse / ajp://localhost:8009/ Require all granted
测试能否正常反代:
3、根据mod_jk进行反代 //在250.78和250.79上进行]# lstomcat-connectors-1.2.40-src.tar.gz]# tar -xf tomcat-connectors-1.2.40-src.tar.gz]# cd tomcat-connectors-1.2.40-src/native]# ./configure --with-apxs=/usr/bin/apxs //编译httpd模块]# make && make install]# /etc/httpd/conf.d //在250.78上]# vim mod_jk.confLoadModule jk_module modules/mod_jk.so //加载mod_jkJkWorkersFile /etc/httpd/conf.d/workers.properties //jk的配置参数文件JkLogFile logs/mod_jk.log //jk代理日志 JkLogLevel debug //jk的日志级别 JkMount /* TomcatB //反代到哪台主机上JkMount /jk_status StatB //开启jk状态页]# vim workers.propertiesworker.list=TomcatB,StatB //worker的列表worker.TomcatB.host=172.18.250.78 //worker对应的主机worker.TomcatB.port=8009 //worker对应的端口 worker.TomcatB.type=ajp13 //worker对应的tomcat的协议worker.StatB.type=status //状态页250.79上只需要把TomcatB改成TomcatA就行,并改下IP地址至此,一个基于mod_jk模块与后端名为TomcatA的worker通信的配置已经完成,重启httpd服务即可生效。
测试是否正常反代:
三、httpd的负载均衡和session sticky(250.77上)
3.1、基于http协议的负载均衡至tomcat
]# cd /etc/httpd/conf.d/]# vim http_balance_tomcat.confBalancerMember http://172.18.250.78:8080 loadfactor=1 //loadfactor表示权重 BalancerMember http://172.18.250.79:8080 loadfactor=2 ProxySet lbmethod=byrequests //定义调度算法 ServerName tc.magedu.com ProxyRequests Off ProxyPreserveHost On ProxyVia On Require all granted Proxypass / balancer://webserver/ Proxypa***everse / balancer://webserver/ Require all granted 后端两台需要使用http_proxy_tomcat.conf的配置文件
测试能否正常负载均衡:
设置session_sticky:
]# vim http_balance_tomcat.confHeader add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGEDBalancerMember http://172.18.250.78:8080 route=TomcatA BalancerMember http://172.18.250.79:8080 route=TomcatB ProxySet lbmethod=byrequests ProxySet stickysession=ROUTEID //在将某请求调度至某worker后,源于此址的所有后续请求都将直接调度至此worker,实现将用户session与某worker绑定。 ServerName tc.magedu.com ProxyRequests Off ProxyPreserveHost On ProxyVia On Require all granted ProxyPass / balancer://webserver/ ProxyPa***everse / balancer://webserver/ Require all granted
3.2、基于ajp协议负载均衡至tomcat
]# cd /etc/httpd/conf.d/]# vim ajp_balance_tomcat.confBalancerMember ajp://172.18.250.78:8009 loadfactor=1 BalancerMember ajp://172.18.250.79:8009 loadfactor=2 ProxySet lbmethod=byrequests ServerName tc.magedu.com ProxyRequests Off ProxyPreserveHost On ProxyVia On Require all granted Proxypass / balancer://webserver/ Proxypa***everse / balancer://webserver/ Require all granted 后端两台需要使用ajp_proxy_tomcat.conf的配置文件
测试能否正常负载均衡:
基于session_sticky的会话保持
]# vim ajp_balance_tomcat.conf Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGEDBalancerMember ajp://172.18.250.78:8009 route=TomcatA BalancerMember ajp://172.18.250.79:8009 route=TomcatB ProxySet lbmethod=byrequests ProxySet stickysession=ROUTEID ServerName tc.magedu.com ProxyRequests Off ProxyPreserveHost On ProxyVia On Require all granted ProxyPass / balancer://webserver/ ProxyPa***everse / balancer://webserver/ Require all granted
3.3、基于mod_jk来实现负载均衡
]# vim mod_jk.confLoadModule jk_module modules/mod_jk.soJkWorkersFile /etc/httpd/conf.d/workers.propertiesJkLogFile logs/mod_jk.logJkLogLevel debugJkMount /* webserver //改成服务器池名称就行JkMount /jk_status StatA]# vim workers.propertiesworker.list=webserver,StatAworker.TomcatA.host=172.18.250.78worker.TomcatA.port=8009worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.TomcatB.host=172.18.250.79worker.TomcatB.port=8009worker.TomcatB.type=ajp13worker.TomcatB.lbfactor=2worker.webserver.type=lb //类型为负载均衡worker.webserver.balance_workers=TomcatA,TomcatBworker.StatA.type=status
测试能否正常负载均衡:
基于session_sticky的会话保持
]# vim workers.propertiesworker.list=webserver,StatAworker.TomcatA.host=172.18.250.78worker.TomcatA.port=8009worker.TomcatA.type=ajp13worker.TomcatA.lbfactor=1worker.TomcatB.host=172.18.250.79worker.TomcatB.port=8009worker.TomcatB.type=ajp13worker.TomcatB.lbfactor=2worker.webserver.type=lb worker.webserver.balance_workers=TomcatA,TomcatBworker.webserver.sticky_session=1 //开启会话粘性,默认是开启的worker.StatA.type=statusmod_jk除了开启会话粘性外,还需在各个节点tomcat的配置文件server.xml上添加jvmRouet//250.78上 //250.79上