Cluster architecture - Nginx load balancing and Tomcat cluster

1: Load balancing technology

1. Introduction of load balancing technology

Load balancing technology is a concept that evenly distributes the use of resources.

Load balancing: distribute traffic and requests to different servers. Distribute the flow evenly (ideal state)

effect:

Server disaster recovery traffic distribution

Main functions:

① The traffic distribution request reduces the single case pressure on average

Other functions:

② Hide the real back-end services securely

③ Shield illegal requests (layer 7 load balancing)

2. Load balancing classification

-

1) Layer 2 load balancing (mac)

According to the OSI model, the load is divided into two layers. Generally, the virtual MAC address is used. The external requests for the virtual MAC address are received by load balancing, and then the actual MAC address response of the back-end is allocated

**2) Three layer load balancing (ip)**

Generally, the virtual IP address mode is adopted. After the external requests for virtual IP addresses are received by load balancing, the actual IP address response of the back end is allocated

3) Four layer load balancing (tcp) is the load balancing of network transportation

On the basis of three-tier load balancing, the request is received with ip+port and then forwarded to the corresponding machine

4) Seven layer load balancing (http) intelligent load balancing

According to the virtual url or IP, the host receives the request and then turns to the corresponding processing server (reverse proxy)

3. Common implementation methods

Implementation classification:

① Software level, high cost performance and strong controllability

② The hardware level has good performance and the price ranges from tens of thousands to hundreds of thousands

Hardware implementation mode:

F5 BIG-IP layer 4 and layer 7

Software implementation mode:

OSI layering Implementation mode
Seventh floor Nginx,HAProxy
four layers LVS, HAProxy, Nginx (after version 1.9)

Comparison between the fourth floor and the seventh floor:

II Experimental analysis of Nginx load balancing

  • Usually, a tomcat site can not be used in the production environment alone because it may have a single point of failure and can not cope with too many complex and diverse requests from customers. Therefore, we need a more reliable solution to improve the web site architecture
  • Nginx is an http server software with superior performance. It can support the response of up to 50000 concurrent connections, has strong static resource processing ability, runs stably, and consumes very low system resources such as memory and CPU. At present, many large websites use nginx server as the reverse proxy and load balancer of back-end website programs to improve the load concurrency of the whole site

III Network topology and experimental environment

type IP address system software package
Nginx server 192.168.100.140 centos7 nginx-1.12.2.tar.gz
Tomcat server 1 192.168.100.150 centos7 apache-tomcat-8.5.16.tar.gz;jdk-8u91-linux-x64.tar.gz
Tomcat server 2 192.168.100.160 centos7 apache-tomcat-8.5.16.tar.gz;jdk-8u91-linux-x64.tar.gz
Client 192.168.100.10 Windows10

IV Specific operation steps and verification

  • Tomcat01 configuration

    1.decompression jdk Compressed package
    systemctl stop firewalld
    setenforce 0 
    tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
    
    2.to configure Java environment variable
    vim /etc/profile
    'add to'
    export JAVA_HOME=/usr/local/jdk1.8.0_91
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH
    
    3.load JAVA environment variable
    source /etc/profile
    
    4.decompression Tomcat software package
    tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local
    
    5.Modify the directory name for easy operation
    cd /usr/local
    mv apache-tomcat-8.5.16/ tomcat
    6.Create soft links to optimize service control
    ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
    ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
    
    7.establish web Site, edit site content,Add test page
    mkdir -pv /web/webapp1
    vim /web/webapp1/index.jsp
     
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
     <head>
      <title>JSP test1 page</title>
     </head>
     <body>
      <% out.println("Welcome Tomcat 01");%>
     </body>
    </html>
    
    8.modify Tomcat of server.xml File, define a virtual host, and point the website file path to the established/web/webapp1,stay hos Segment increase context paragraph
    [root@tomcat01 ~]# vim /usr/local/tomcat/conf/server.xml
    //Line 148
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
            <Context docBase="/web/webapp1" path="" reloadable="false">
            </Context>
     //Note: docbase: document reference directory of web application
    reloadable Set whether the monitoring "class" changes
    path=""Set default class
    
    9.Turn off the security function and turn on the service
    startup.sh 
    systemctl stop firewalld
    setenforce 0
    
  • Tomcat02 configuration is basically the same as that of 01, index The output Welcome Tomcat 02 defined on the JSP homepage is different here for differentiation

    1.decompression jdk Compressed package
    tar xzvf jdk-8u231-linux-x64.tar.gz -C /usr/local
    
    2.to configure JAVA environment variable
    vim /etc/profile  //Append to end
    export JAVA_HOME=/usr/local/jdk1.8.0_231
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH
    
    3.load JAVA environment variable
    source /etc/profile
    
    4.decompression tomcat software package
    tar xzvf apache-tomcat-8.5.50.tar.gz -C /usr/local
    
    5.The directory name is easy to modify and operate
    cd /usr/local
    mv apache-tomcat-8.5.50/ tomcat/
    
    6.Create soft links to optimize service control
    ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
    ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
    
    7.establish web Site, edit site content
    mkdir -pv /web/webapp1
    vim /web/webapp1/index.jsp
     
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
     <head>
      <title>JSP test1 page</title>
     </head>
     <body>
      <% out.println("Welcome Tomcat 02");%>
     </body>
    </html>
    ~
    
    8.modify Tomcat of server.xml File, define a virtual host, and point the website file path to the established/web/webapp1,stay hos Segment increase context paragraph
    [root@tomcat02 ~]# vim /usr/local/tomcat/conf/server.xml
          <Host name="localhost"  appBase="webapps"
                unpackWARs="true" autoDeploy="true">
            <Context docBase="/web/webapp1" path="" reloadable="false">
            </Context>
     
    9.Turn off the security function and turn on the service
    shutdown.sh
    startup.sh 
    systemctl stop firewalld
    setenforce 0
    
  • Configuration of Nginx server

    1.Unzip package
    tar xzvf nginx-1.12.2.tar.gz -C /opt
    
    2.Download relevant software packages
    yum install pcre-devel zlib-devel gcc gcc-c++ make -y
    
    3.establish nginx User
    useradd -M -s /sbin/nologin nginx
    
    4.Compile and install nginx
    cd /opt/nginx-1.12.2/
    ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx --group=nginx \
    --with-http_stub_status_module \    //Enable status statistics
    --with-http_gzip_static_module \    //Enable gzip static compression
    --with-http_flv_module     //Enable the flv module to provide time-based offset files for seeking memory usage
     
    make && make install
     
    5.edit nginx Master profile for
    vim /usr/local/nginx/conf/nginx.conf
     #keepalive_timeout  0;
        keepalive_timeout  65;
        #gzip  on;
        upstream tomcat_server {              //Add the upstream function and configure the tomcat server pool name customization, but ensure that it is consistent with the following call name
            server 192.168.100.150:8080 weight=1;   //Weight stands for weight
            server 192.168.100.160:8080 weight=2;
            }
     
        server {
            listen       80;
            server_name  localhost;
            #charset koi8-r;
            #access_log  logs/host.access.log  main;
     
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://tomcat_server;    // Add the reverse proxy to the set Tomcat server pool. The name of the call here should be consistent with the function name above
            }
    
    		
    6.Optimize service control mode
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
     
    7.Check the syntax for correct configuration
    [root@nginx ~]# nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    [root@nginx ~]# 
     
    8.Turn off the security function and turn it on nginx service
    systemctl stop firewalld
    setenforce 0
    nginx  				//Enable nginx service
    netstat -ntap | grep nginx
    killall -1 nginx 	//Restart nginx service
    
    9. Page Test
     Access proxy server 192.168.100.140   Refresh multiple times and view weighted polling
    

Tags: Load Balance Nginx Tomcat

Posted by davidcriniti on Thu, 19 May 2022 18:09:29 +0300