服务器角色 | 主机名 | 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 ~]# tar zxf apache-tomcat-7.0.85.tar.gz [root@NK03 ~]# mv apache-tomcat-7.0.85 /usr/local/tomcat
3.配置server.xml
[root@NK03 ~]# cat /usr/local/tomcat/conf/server.xml <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1"> #NK4为tomcat2 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.0.4" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="20.0.20.103" #填写本机的IP地址 port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="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 <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://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 <%@page language="java"%> <html> <body> <h1><font color="red">Session serviced by tomcat</font></h1> <table aligh="center" border="1"> <tr> <td>Session ID</td> <td><%=session.getId() %>-----NK3</td> <% session.setAttribute("abc","abc");%> </tr> <tr> <td>Created on</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.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
#以上操作在NK4上同步执行,除了配置文件server.xml稍有区别,其他都一样
8.浏览测试页面
三、配置Nginx
1.获取Nginx yum源并安装
[root@NK01 ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm [root@NK01 ~]# yum install -y nginx
2.修改配置文件(NK1和NK2相同配置)
[root@NK01 ~]# cat /etc/nginx/nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$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.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; server_tokens off; gzip on; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_send_timeout 60; proxy_read_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; upstream test{ server 20.0.20.103:8080 weight=10; server 20.0.20.104:8080 weight=10; } server { listen 80; server_name 20.0.20.101; charset utf-8; location / { proxy_pass http://test; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } include /etc/nginx/conf.d/*.conf; }
3.启动nginx
[root@NK01 ~]# systemctl start nginx [root@NK01 ~]# systemctl enable nginx
4.测试
四、配置keepalived
1.安装keepalived
[root@NK01 ~]# yum -y install keepalived
2.编辑nginx检测脚本
[root@NK01 ~]# cat /usr/local/keepalived/chknginx.sh #!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl restart nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then systemctl stop keepalived fi fi
[root@NK01 ~]# chmod +x /usr/local/keepalived/chknginx.sh
3.修改配置文件
[root@NK01 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { } smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script check_nginx { script "/usr/local/keepalived/chknginx.sh" #nginx检测脚本 interval 3 weight -2 } vrrp_instance VI_1 { state MASTER #NK2为BACKUP interface ens192 virtual_router_id 151 priority 100 #NK2为99 advert_int 1 authentication { auth_type PASS auth_pass 2222 } track_script { check_nginx #检测脚本名 } virtual_ipaddress { 20.0.20.100 } }
4.在网卡上添加一个IP地址
#NK2和NK1配置类似
5.启动keepalived
[root@NK01 ~]# systemctl start keepalived [root@NK01 ~]# systemctl enable keepalived
6.查看IP是否绑定
[root@NK01 ~]# ip add |grep "inet 20" inet 20.0.20.101/16 brd 20.0.255.255 scope global ens192 inet 20.0.20.100/32 scope global ens192 inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192 [root@NK02 ~]# ip add |grep "inet 20" inet 20.0.20.102/16 brd 20.0.255.255 scope global ens192 inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192
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