負載均衡Tomcat

本文章內容:

1. Nginx負載均衡Tomcat

2. Apache負載均衡Tomcat
    2.1 byrequests
    2.2 bybusiness
    2.3 bytraffic

 

環境:

1. Proxy主機 (前端)

[root@proxy ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@proxy ~]# ip a | egrep "(ens33|ens37)"
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.169.254/24 brd 192.168.169.255 scope global noprefixroute ens33  #對內IP
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.0.21/24 brd 192.168.0.255 scope global noprefixroute dynamic ens37  #對外IP

2. Tomcat-1主機 (後端)

[root@tomcat-1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@tomcat-1 ~]# ip a | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.169.131/24 brd 192.168.169.255 scope global noprefixroute ens33  #內部IP

3. Tomcat-2主機 (後端)

[root@tomcat-2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@tomcat-2 ~]# ip a | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.169.132/24 brd 192.168.169.255 scope global noprefixroute ens33  #內部IP

 

1. Nginx負載均衡Tomcat

1.1 安裝、設定Tomcat-1主機

1.1.1 安裝Tomcat

提示:使用yum安裝Tomcat會自動安裝上OpenJDK,但是不會安裝OpenJDK的devel包。
[root@tomcat-1 ~]# yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps -y
[root@tomcat-1 ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

1.1.2 設定Tomcat-1主機應用 (有意設定Tomcat-1主機與Tomcat-2主機應用不一樣,以便驗證負載均衡是否成功)

[root@tomcat-1 ~]# mkdir -pv /var/lib/tomcat/webapps/test/{WEB-INF,META-INF,lib,classes}
mkdir: created directory ‘/var/lib/tomcat/webapps/test’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/WEB-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/META-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/lib’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/classes’
[root@tomcat-1 ~]# vim /var/lib/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
    <html>
        <head><title>tomcat-1</title></head>
        <body>
            <h1><font color="red">tomcat-1.lab.com</font></h1>
            <table align="centre" border="1">
                <tr>
                    <td>Session ID</td>
                    <% session.setAttribute("lab.com","lab.com");%>
                    <td><%= session.getId() %></td>
                </tr>
                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
        </body>
    </html>

1.1.3 啟動Tomcat服務並設定開機自啟動

[root@tomcat-1 ~]# systemctl start tomcat.service
[root@tomcat-1 ~]# systemctl enable tomcat.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
[root@tomcat-1 ~]# ss -tnl
State       Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN      0      100                                              [::]:8009                                                         [::]:*                  
LISTEN      0      100                                              [::]:8080                                                         [::]:*                  
LISTEN      0      1                                  [::ffff:127.0.0.1]:8005                                                         [::]:* 
...

1.1.4 訪問"192.168.169.131:8080/test" (因為它要編譯非常消耗記憶體,所以第一次訪問會比較慢)

1.2 安裝、設定Tomcat-2主機

1.2.1 安裝Tomcat

提示:使用yum安裝Tomcat會自動安裝上OpenJDK,但是不會安裝OpenJDK的devel包。
[root@tomcat-2 ~]# yum install tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps -y
[root@tomcat-2 ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

1.2.2 設定Tomcat-2主機應用 (有意設定Tomcat-1主機與Tomcat-2主機應用不一樣,以便驗證負載均衡是否成功)

[root@tomcat-2 ~]# mkdir -pv /var/lib/tomcat/webapps/test/{WEB-INF,META-INF,lib,classes}
mkdir: created directory ‘/var/lib/tomcat/webapps/test’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/WEB-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/META-INF’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/lib’
mkdir: created directory ‘/var/lib/tomcat/webapps/test/classes’
[root@tomcat-2 ~]# vim /var/lib/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
    <html>
        <head><title>tomcat-2</title></head>
        <body>
            <h1><font color="green">tomcat-2.lab.com</font></h1>
            <table align="centre" border="1">
                <tr>
                    <td>Session ID</td>
                    <% session.setAttribute("lab.com","lab.com");%>
                    <td><%= session.getId() %></td>
                </tr>
                <tr>
                    <td>Created on</td>
                    <td><%= session.getCreationTime() %></td>
                </tr>
            </table>
        </body>
    </html>

1.2.3 啟動Tomcat服務並設定開機自啟動

[root@tomcat-2 ~]# systemctl start tomcat.service
[root@tomcat-2 ~]# systemctl enable tomcat.service
Created symlink from /etc/systemd/system/multi-user.target.wants/tomcat.service to /usr/lib/systemd/system/tomcat.service.
[root@tomcat-2 ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      1                                  [::ffff:127.0.0.1]:8005                                                         [::]:*                  
LISTEN     0      100                                              [::]:8009                                                         [::]:*                  
LISTEN     0      100                                              [::]:8080                                                         [::]:*                  
...

1.2.4 訪問"192.168.169.132:8080/test" (因為它要編譯非常消耗記憶體,所以第一次訪問會比較慢)

1.3 安裝、設定Proxy主機

1.3.1 安裝Nginx

提示:如果顯示沒有nginx包可用,則先安裝epel-release包。
[root@proxy ~]# yum install nginx -y

1.3.2 設定訪問www.ilinux.io虛擬主機的80 port會負載均衡至後端192.168.169.131:8080、192.168.169.132:8080主機

1.3.2.1 設定nginx.conf

[root@proxy ~]# vim /etc/nginx/nginx.conf
 33     upstream tcsrvs {  #定義後端主機組
 34         server 192.168.169.131:8080;
 35         server 192.168.169.132:8080;
 36     }

1.3.2.2 設定www-ilinux-io_http_tomcat.conf

[root@proxy ~]# vim /etc/nginx/conf.d/www-ilinux-io_http_tomcat.conf
server {
    listen 80;
    server_name www.ilinux.io;

    location / {
        proxy_pass http://tcsrvs;
    }
}
[root@proxy ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

1.3.3 啟動Nginx服務並設定開機自啟動

[root@proxy ~]# systemctl start nginx.service
[root@proxy ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@proxy ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      128                                                 *:80                                                              *:*                  
LISTEN     0      128                                              [::]:80                                                           [::]:*                  
...

1.3.4 訪問多次"http://www.ilinux.io/test"

1.3.4.1 使用瀏覽器訪問

如果頁面沒有變那可能是瀏覽器緩存問題,可以換個瀏覽器試一試。

1.3.4.2 使用curl訪問

[root@client ~]# for i in {1..4};do curl -s www.ilinux.io/test/ | grep -i tomcat*;done
        <head><title>tomcat-1</title></head>
            <h1><font color="red">tomcat-1.lab.com</font></h1>
        <head><title>tomcat-2</title></head>
            <h1><font color="green">tomcat-2.lab.com</font></h1>
        <head><title>tomcat-1</title></head>
            <h1><font color="red">tomcat-1.lab.com</font></h1>
        <head><title>tomcat-2</title></head>
            <h1><font color="green">tomcat-2.lab.com</font></h1>

 

2. Apache負載均衡Tomcat

首先:

1. Tomcat-1、Tomcat-2主機同1.1、1.2來安裝、設定。

2. Proxy主機安裝Apache,執行yum install httpd -y。

2.1 byrequests

根據請求來調度,相當於輪詢(RR,Round-Robin Scheduling),支持權重做加權輪詢(WRR,Weighted Round-Robin Scheduling)。

2.1.1 Proxy主機檢查是否有裝載proxy_module、proxy_http_module、proxy_balancer_module、lbmethod_byrequests_module模塊(proxy_module模塊主要是實現反代功能的,proxy_http_module模塊是實現HTTP協議反代功能,proxy_balancer_module模塊是實現負載均衡功能,lbmethod_byrequests_module是實現負載均衡功能的方法)

[root@proxy ~]# httpd -M
 proxy_module (shared)
 lbmethod_byrequests_module (shared)
 proxy_balancer_module (shared)
 proxy_http_module (shared)
...

2.1.2 Proxy主機設定www-ilinux-io_http_tomcat.conf

[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_http_tomcat.conf
<Proxy balancer://tcsrvs>  #設定後端主機組
    BalancerMember http://192.168.169.131:8080
    BalancerMember http://192.168.169.132:8080
    ProxySet lbmethod=byrequests  #設定負載均衡的方法,默認是byrequests,所以這一行不寫也是默認
</Proxy>

<VirtualHost *:80>
    ServerName www.ilinux.io
    ProxyRequests off
    ProxyPreserveHost on
    ProxyVia on

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / balancer://tcsrvs/  #反代至後端主機組
    ProxyPassReverse / balancer://tcsrvs/

    <Location />
        Require all granted
    </Location>
</VirtualHost>
[root@proxy ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5098:67ae:9678:b052. Set the 'ServerName' directive globally to suppress this message
Syntax OK

2.1.3 Proxy主機啟動Apache服務並設定開機自啟動

[root@proxy ~]# systemctl start httpd.service
[root@proxy ~]# systemctl enable httpd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@proxy ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      128                                              [::]:80                                                           [::]:*    
...

2.1.4 訪問多次"http://www.ilinux.io/test"

2.1.4.1 使用瀏覽器訪問

2.1.4.2 使用curl訪問

[root@client ~]# for i in {1..4};do curl -s www.ilinux.io/test/ | grep -i tomcat*;done
        <head><title>tomcat-1</title></head>
            <h1><font color="red">tomcat-1.lab.com</font></h1>
        <head><title>tomcat-2</title></head>
            <h1><font color="green">tomcat-2.lab.com</font></h1>
        <head><title>tomcat-1</title></head>
            <h1><font color="red">tomcat-1.lab.com</font></h1>
        <head><title>tomcat-2</title></head>
            <h1><font color="green">tomcat-2.lab.com</font></h1>

2.2 bybusyness

誰比較閒誰就接收請求,這會依賴(WLC,Weighted Least-Connection Scheduling)算法。

2.2.1 Proxy主機檢查是否有裝載proxy_module、proxy_http_module、proxy_balancer_module、lbmethod_bybusyness_module模塊(proxy_module模塊主要是實現反代功能的,proxy_http_module模塊是實現HTTP協議反代功能,proxy_balancer_module模塊是實現負載均衡功能,lbmethod_bybusyness_module是實現負載均衡功能的方法)

[root@proxy ~]# httpd -M
 proxy_module (shared)
 lbmethod_bybusyness_module (shared)
 proxy_balancer_module (shared)
 proxy_http_module (shared)
...

2.2.2 Proxy主機設定www-ilinux-io_http_tomcat.conf

[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_http_tomcat.conf
<Proxy balancer://tcsrvs>  #設定後端主機組
    BalancerMember http://192.168.169.131:8080
    BalancerMember http://192.168.169.132:8080
    ProxySet lbmethod=bybusyness  #設定負載均衡的方法
</Proxy>

<VirtualHost *:80>
    ServerName www.ilinux.io
    ProxyRequests off
    ProxyPreserveHost on
    ProxyVia on

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / balancer://tcsrvs/  #反代至後端主機組
    ProxyPassReverse / balancer://tcsrvs/

    <Location />
        Require all granted
    </Location>
</VirtualHost>
[root@proxy ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5098:67ae:9678:b052. Set the 'ServerName' directive globally to suppress this message
Syntax OK

2.2.3 Proxy主機啟動Apache服務並設定開機自啟動

[root@proxy ~]# systemctl start httpd.service
[root@proxy ~]# systemctl enable httpd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@proxy ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      128                                              [::]:80                                                           [::]:*    
...

2.2.4 訪問多次"http://www.ilinux.io/test"

訪問方式同2.1.4。

2.3 bytraffic

根據後端服務器現在的流量來檢查誰的流量少,帶寬佔用少的就給誰負載。

2.3.1 Proxy主機檢查是否有裝載proxy_module、proxy_http_module、proxy_balancer_module、lbmethod_bytraffic_module模塊(proxy_module模塊主要是實現反代功能的,proxy_http_module模塊是實現HTTP協議反代功能,proxy_balancer_module模塊是實現負載均衡功能,lbmethod_bytraffic_module是實現負載均衡功能的方法)

[root@proxy ~]# httpd -M
 proxy_module (shared)
 lbmethod_bytraffic_module (shared)
 proxy_balancer_module (shared)
 proxy_http_module (shared)
...

2.3.2 Proxy主機設定www-ilinux-io_http_tomcat.conf

[root@proxy ~]# vim /etc/httpd/conf.d/www-ilinux-io_http_tomcat.conf
<Proxy balancer://tcsrvs>  #設定後端主機組
    BalancerMember http://192.168.169.131:8080
    BalancerMember http://192.168.169.132:8080
    ProxySet lbmethod=bytraffic  #設定負載均衡的方法
</Proxy>

<VirtualHost *:80>
    ServerName www.ilinux.io
    ProxyRequests off
    ProxyPreserveHost on
    ProxyVia on

    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / balancer://tcsrvs/  #反代至後端主機組
    ProxyPassReverse / balancer://tcsrvs/

    <Location />
        Require all granted
    </Location>
</VirtualHost>
[root@proxy ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5098:67ae:9678:b052. Set the 'ServerName' directive globally to suppress this message
Syntax OK

2.3.3 Proxy主機啟動Apache服務並設定開機自啟動

[root@proxy ~]# systemctl start httpd.service
[root@proxy ~]# systemctl enable httpd.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@proxy ~]# ss -tnl
State      Recv-Q Send-Q                                  Local Address:Port                                                 Peer Address:Port              
LISTEN     0      128                                              [::]:80                                                           [::]:*    
...

2.3.4 訪問多次"http://www.ilinux.io/test"

訪問方式同2.1.4。