1. 程式人生 > >Nginx+Keeplived+Tomcat搭建高可用/負載均衡的web服務器集群

Nginx+Keeplived+Tomcat搭建高可用/負載均衡的web服務器集群

tomcat keepalived nginx

一、集群規劃

服務器角色主機名IP地址/VIP軟件
Nginx MasterNK120.0.20.101/20.0.20.100Nginx/Keepalived
Nginx BackupNK220.0.20.102/20.0.20.100Nginx/Keepalived
Web ServerNK320.0.20.103Tomcat
Web ServerNK420.0.20.104Toncat

#關閉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 &quot;%r&quot; %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服務器集群