服务器角色 | 主机名 | IP地址/VIP | 软件 |
Nginx Master | NK1 | 20.0.20.101/20.0.20.100 | Nginx/Keepalived |
Nginx Backup | NK2 | 20.0.20.102/20.0.20.100 | Nginx/Keepalived |
Web Server | NK3 | 20.0.20.103 | Tomcat |
Web Server | NK4 | 20.0.20.104 | Toncat |
#关闭selinux和firewall
#软件版本为:apache-tomcat-7.0.85.tar.gz jdk-8u151-linux-x64.tar.gz
二、安装配置Tomcat
1.安装JDK并配置环境变量
2.部署Tomcat
[root@NK03~]#tarzxfapache-tomcat-7.0.85.tar.gz[root@NK03~]#mvapache-tomcat-7.0.85/usr/local/tomcat
3.配置server.xml
[root@NK03~]#cat/usr/local/tomcat/conf/server.xml<?xmlversion='1.0'encoding='utf-8'?><Serverport="8005"shutdown="SHUTDOWN"><ListenerclassName="org.apache.catalina.startup.VersionLoggerListener"/><ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/><ListenerclassName="org.apache.catalina.core.JasperListener"/><ListenerclassName="org.apache.catalina.core.JreMemoryLeakPreventionListener"/><ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/><ListenerclassName="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/><GlobalNamingResources><Resourcename="UserDatabase"auth="Container"type="org.apache.catalina.UserDatabase"description="Userdatabasethatcanbeupdatedandsaved"factory="org.apache.catalina.users.MemoryUserDatabaseFactory"pathname="conf/tomcat-users.xml"/></GlobalNamingResources><Servicename="Catalina"><Connectorport="8080"protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443"/><Connectorport="8009"protocol="AJP/1.3"redirectPort="8443"/><Enginename="Catalina"defaultHost="localhost"jvmRoute="tomcat1">#NK4为tomcat2<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="8"><ManagerclassName="org.apache.catalina.ha.session.DeltaManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"mapSendOptions="6"/><ChannelclassName="org.apache.catalina.tribes.group.GroupChannel"><MembershipclassName="org.apache.catalina.tribes.membership.McastService"address="228.0.0.4"port="45564"frequency="500"dropTime="3000"/><ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"address="20.0.20.103"#填写本机的IP地址port="4000"autoBind="100"selectorTimeout="5000"maxThreads="6"/><SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter"><TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/></Sender><InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/><InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/><InterceptorclassName="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/></Channel><ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"filter=""/><DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"watchDir="/tmp/war-listen/"watchEnabled="false"/><ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/></Cluster><RealmclassName="org.apache.catalina.realm.LockOutRealm"><RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"resourceName="UserDatabase"/></Realm><Hostname="localhost"appBase="webapps"unpackWARs="true"autoDeploy="true"><ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="logs"prefix="localhost_access_log."suffix=".txt"pattern="%h%l%u%t"%r"%s%b"/></Host></Engine></Service></Server>
4.在web.xml里增加<distributable/>
[root@NK03~]#vim/usr/local/tomcat/conf/web.xml<?xmlversion="1.0"encoding="ISO-8859-1"?><web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><distributable/>............<welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
5.建立测试页面
[root@NK03~]#mkdir/usr/local/tomcat/webapps/test[root@NK03~]#cat/usr/local/tomcat/webapps/test/index.jsp<%@pagelanguage="java"%><html><body><h1><fontcolor="red">Sessionservicedbytomcat</font></h1><tablealigh="center"border="1"><tr><td>SessionID</td><td><%=session.getId()%>-----NK3</td><%session.setAttribute("abc","abc");%></tr><tr><td>Createdon</td><td><%=session.getCreationTime()%></td></tr></table></body><html>
6.复制web.xml到测试文件夹
[root@NK03~]#mkdir/usr/local/tomcat/webapps/test/WEB-INF[root@NK03~]#cp/usr/local/tomcat/conf/web.xml/usr/local/tomcat/webapps/test/WEB-INF/
7.启动tomcat
[root@NK03~]#/usr/local/tomcat/bin/startup.shUsingCATALINA_BASE:/usr/local/tomcatUsingCATALINA_HOME:/usr/local/tomcatUsingCATALINA_TMPDIR:/usr/local/tomcat/tempUsingJRE_HOME:/usr/local/jdkUsingCLASSPATH:/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcatstarted.
#以上操作在NK4上同步执行,除了配置文件server.xml稍有区别,其他都一样
8.浏览测试页面
三、配置Nginx
1.获取Nginx yum源并安装
[root@NK01~]#rpm-Uvhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[root@NK01~]#yuminstall-ynginx
2.修改配置文件(NK1和NK2相同配置)
[root@NK01~]#cat/etc/nginx/nginx.confusernginx;worker_processes1;error_log/var/log/nginx/error.logwarn;pid/var/run/nginx.pid;events{worker_connections1024;}http{include/etc/nginx/mime.types;default_typeapplication/octet-stream;log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"';access_log/var/log/nginx/access.logmain;sendfileon;tcp_nopushon;tcp_nodelayon;keepalive_timeout65;server_tokensoff;gzipon;client_body_buffer_size512k;proxy_connect_timeout5;proxy_send_timeout60;proxy_read_timeout5;proxy_buffer_size16k;proxy_buffers464k;proxy_busy_buffers_size128k;proxy_temp_file_write_size128k;upstreamtest{server20.0.20.103:8080weight=10;server20.0.20.104:8080weight=10;}server{listen80;server_name20.0.20.101;charsetutf-8;location/{proxy_passhttp://test;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerHost$host;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Proto$scheme;}}include/etc/nginx/conf.d/*.conf;}
3.启动nginx
[root@NK01~]#systemctlstartnginx[root@NK01~]#systemctlenablenginx
4.测试
四、配置keepalived
1.安装keepalived
[root@NK01~]#yum-yinstallkeepalived
2.编辑nginx检测脚本
[root@NK01~]#cat/usr/local/keepalived/chknginx.sh#!/bin/bashcounter=$(ps-Cnginx--no-heading|wc-l)if["${counter}"="0"];thensystemctlrestartnginxsleep2counter=$(ps-Cnginx--no-heading|wc-l)if["${counter}"="0"];thensystemctlstopkeepalivedfifi
[root@NK01~]#chmod+x/usr/local/keepalived/chknginx.sh
3.修改配置文件
[root@NK01~]#cat/etc/keepalived/keepalived.conf!ConfigurationFileforkeepalivedglobal_defs{notification_email{}smtp_connect_timeout30router_idLVS_DEVEL}vrrp_scriptcheck_nginx{script"/usr/local/keepalived/chknginx.sh"#nginx检测脚本interval3weight-2}vrrp_instanceVI_1{stateMASTER#NK2为BACKUPinterfaceens192virtual_router_id151priority100#NK2为99advert_int1authentication{auth_typePASSauth_pass2222}track_script{check_nginx#检测脚本名}virtual_ipaddress{20.0.20.100}}
4.在网卡上添加一个IP地址
#NK2和NK1配置类似
5.启动keepalived
[root@NK01~]#systemctlstartkeepalived[root@NK01~]#systemctlenablekeepalived
6.查看IP是否绑定
[root@NK01~]#ipadd|grep"inet20"inet20.0.20.101/16brd20.0.255.255scopeglobalens192inet20.0.20.100/32scopeglobalens192inet20.0.20.100/16brd20.0.255.255scopeglobalsecondaryens192[root@NK02~]#ipadd|grep"inet20"inet20.0.20.102/16brd20.0.255.255scopeglobalens192inet20.0.20.100/16brd20.0.255.255scopeglobalsecondaryens192
7.使用虚拟IP浏览测试页面
四、故障测试
1.keepalived测试
终止掉master后切换到了backup
2.nginx测试
在NK1上终止nginx后,会通过脚本自动启动nginx
3.tomcat测试
终止NK3上的tomcat
Nginx+Keeplived+Tomcat搭建高可用/负载均衡的web服务器集群
原文地址:http://blog.51cto.com/lullaby/2094663