0
点赞
收藏
分享

微信扫一扫

Nginx实现后端多台Tomcat的负载均衡调度

1、环境和架构

image.png

IP 主机名[root@Nginx ~]#vim /etc/hosts 服务 软件
10.0.0.8 Nginx.stars.org 反向代理调度 Nginx
10.0.0.100 Rs1.stars.org tomcat1 JDK8、Tomcat9
10.0.0.102 Rs2.stars.org tomcat2 JDK8、Tomcat9
#需要在10.0.0.8的Nginx主机上实现DNS域名解析,我们这里就直接改hosts文件来实现了
[root@Nginx ~]#vim /etc/hosts
10.0.0.8 www.stars.org
10.0.0.100 web1.stars.org
10.0.0.102 web2.stars.org

2、后端两台tomcat服务主机配置

2.1、RS1服务器配置

root@ubuntu:~# hostnamectl set-hostname Rs1.stars.org
root@ubuntu:~# exit
root@Rs1:~# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@Rs1:~# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.62/bin/apache-tomcat-9.0.62.tar.gz
root@Rs1:~# ls
apache-tomcat-9.0.62.tar.gz  install_tomcat.sh  jdk-8u291-linux-x64.tar.gz
root@Rs1:~# cat install_tomcat.sh 
#!/bin/bash

DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-9.0.62.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$2" && $MOVE_TO_COL
    echo -n "["
    if [ $1 = "success" -o $1 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $1 = "failure" -o $1 = "1"  ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo                                                                                                                              
}

install_jdk () {
if !  [  -f "$DIR/$JDK_FILE" ];then
    color 1 "$JDK_FILE 文件不存在" 
    exit; 
elif [ -d $JDK_DIR/jdk ];then
    color 1  "JDK 已经安装" 
    exit
else 
    [ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE  -C $JDK_DIR
cd  $JDK_DIR && ln -s jdk1.8.* jdk 

cat >  /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib/
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
.  /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" || { color 1  "JDK 安装失败" ; exit; }

}

install_tomcat () {
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
    color 1 "$TOMCAT_FILE 文件不存在" 
    exit; 
elif [ -d $TOMCAT_DIR/tomcat ];then
    color 1 "TOMCAT 已经安装" 
    exit
else 
    [ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd  $TOMCAT_DIR && ln -s apache-tomcat-*/  tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat

cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF

chown -R tomcat.tomcat $TOMCAT_DIR/tomcat/

cat > /lib/systemd/system/tomcat.service  <<EOF
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target 

[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service &> /dev/null
systemctl is-active tomcat.service &> /dev/null && color 0 "TOMCAT 安装完成" || { color 1 "TOMCAT 安装失败" ; exit; }
}

install_jdk 
install_tomcat
root@Rs1:~# bash -n install_tomcat.sh 
root@Rs1:~# bash install_tomcat.sh
..................
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
JDK 安装完成                                               [  OK  ]
TOMCAT 安装完成                                            [  OK  ]
#这个也要在另外一台主机上跑一下安装tomcat服务

修改配置文件server.xml

root@Rs1:~# vim /usr/local/tomcat/conf/server.xml
#找到下面内容修改一下
<Engine name="Catalina" defaultHost="web1.stars.org">
    <Host name="web1.stars.org" appBase="/data/webapps" autoDeploy="true" >
    </Host>
</Engine>
root@Rs1:~# mkdir -pv /data/webapps/ROOT
root@Rs1:~# vim /data/webapps1/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
</head>
<body>
 Tomcat Website 
<div>On  <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>
root@Rs1:~# chown -R tomcat.tomcat /data/webapps/
root@Rs1:~# systemctl restart tomcat.service 
root@Rs1:~# systemctl status tomcat.service 
● tomcat.service - Tomcat
   Loaded: loaded (/lib/systemd/system/tomcat.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-05-07 15:33:24 CST; 6s ago
  Process: 21162 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 21188 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 21196 (java)
    Tasks: 29 (limit: 2284)
   CGroup: /system.slice/tomcat.service
           └─21196 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/log

May 07 15:33:24 Rs1.stars.org systemd[1]: Starting Tomcat...
May 07 15:33:24 Rs1.stars.org systemd[1]: Started Tomcat.

2.2、RS2服务器配置

#安装的步骤和上面RS1差不多,这里我就不写了,我就直接安装好修改好文件了
root@ubuntu:~# hostnamectl set-hostname Rs2.stars.org
root@ubuntu:~# exit
root@Rs2:~# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
root@Rs2:~# vim /usr/local/tomcat/conf/server.xml
#找到下面内容修改一下
<Engine name="Catalina" defaultHost="web2.stars.org">
    <Host name="web2.stars.org" appBase="/data/webapps" autoDeploy="true" >
    </Host>
</Engine>
root@Rs2:~# mkdir -pv /data/webapps/ROOT
root@Rs2:~# vim /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
</head>
<body>
 Tomcat Website 
<div>On  <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>
root@Rs2:~# chown -R tomcat.tomcat /data/webapps
root@Rs2:~# systemctl restart tomcat.service
root@Rs2:~# systemctl status tomcat.service
● tomcat.service - Tomcat
   Loaded: loaded (/lib/systemd/system/tomcat.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2022-05-07 15:45:04 CST; 5s ago
  Process: 22057 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 22082 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 22091 (java)
    Tasks: 29 (limit: 2284)
   CGroup: /system.slice/tomcat.service
           └─22091 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/log

May 07 15:45:04 Rs2.stars.org systemd[1]: Starting Tomcat...
May 07 15:45:04 Rs2.stars.org systemd[1]: Started Tomcat.

3、反向代理Nginx配置

[root@Centos8 ~]#hostnamectl set-hostname Nginx.stars.org
[root@Centos8 ~]#exit
[root@Nginx ~]#vim /etc/hosts
10.0.0.8 www.stars.org
10.0.0.100 web1.stars.org
10.0.0.102 web2.stars.org
[root@Nginx ~]#ping -c1 web1.stars.org
PING web1.stars.org (10.0.0.100) 56(84) bytes of data.
64 bytes from web1.stars.org (10.0.0.100): icmp_seq=1 ttl=64 time=0.588 ms

--- web1.stars.org ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.588/0.588/0.588/0.000 ms
[root@Nginx ~]#ping -c1 web2.stars.org
PING web2.stars.org (10.0.0.102) 56(84) bytes of data.
64 bytes from web2.stars.org (10.0.0.102): icmp_seq=1 ttl=64 time=0.609 ms

--- web2.stars.org ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.609/0.609/0.609/0.000 ms
#安装nginx
[root@Nginx ~]#cat install_nginx.sh 
#!/bin/bash

SRC_DIR=/usr/local/src
NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.18.0
TAR=.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu | awk '/^CPU\(s\)/{print $2}'`

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

os_type () {
   awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}

os_version () {
   awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}

check () {
    [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
    cd  ${SRC_DIR}
    if [  -e ${NGINX_FILE}${TAR} ];then
        color "相关文件已准备好" 0
    else
        color '开始下载 nginx 源码包' 0
        wget ${NGINX_URL}${NGINX_FILE}${TAR} 
        [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; } 
    fi
} 

install () {
    color "开始安装 nginx" 0
    if id nginx  &> /dev/null;then
        color "nginx 用户已存在" 1 
    else
        useradd -s /sbin/nologin -r  nginx
        color "创建 nginx 用户" 0 
    fi
    color "开始安装 nginx 依赖包" 0
    if [ `os_type` == "CentOS" -a `os_version` == '8' ] ;then
        yum -y -q install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed 
    elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then
        yum -y -q  install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
    else
        apt update &> /dev/null
        apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE}${TAR}
    NGINX_DIR=`echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||  { color "nginx 编译安装失败,退出!" 1 ;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
LimitNOFILE=100000
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { color "nginx 启动失败,退出!" 1 ; exit; }
    color "nginx 安装完成" 0
}

check
install
[root@Nginx ~]#bash -n install_nginx.sh 
[root@Nginx ~]#bash install_nginx.sh

#配置nginx的配置文件实现反向代理
[root@Nginx ~]#vim /apps/nginx/conf/nginx.conf
#找到下面相关语句修改或者添加一下
http{
    upstream tomcat-server {
        #ip_hash;   #先禁用看看轮询,之后开启开黏性
        #hash #cookie_JSESSIONID;   #先禁用看看轮询,之后开启开黏性
        server web1.stars.org:8080;
        server web2.stars.org:8080;
    }
    server {
        listen       80;
        server_name  www.stars.org;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location ~* \.(jsp|do)$ {
            proxy_pass http://tomcat-server;
        }
    }
}
[root@Nginx ~]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@Nginx ~]#nginx -s reload

4、在浏览器上访问测试

在这里需要在宿主机上修改hosts文件,加上下面这行
10.0.0.8 www.stars.org
使用浏览器访问http://www.stars.org/index.jsp
image.png
image.png
image.png
image.png
从上面的访问结果发现可以看到轮询调度效果了,不过每次刷新后端主机和SessionID都会变化

5、实现 session 黏性

[root@Nginx ~]#vim /apps/nginx/conf/nginx.conf
upstream tomcat-server {
    ip_hash;                                                                                  
    #hash #cookie_JSESSIONID;
    server web1.stars.org:8080;
    server web2.stars.org:8080;
}
#修改完后reload一下nginx
[root@Nginx ~]#nginx -s reload
#用curl访问每次都调度到10.0.0.102主机上,但因为curl每次请求不会自动携带之前获取的cookie,所有SessionID还是每次都在变化
[root@Nginx ~]#curl http://www.stars.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
</head>
<body>
 Tomcat Website 
<div>On  tomcat-server</div>
<div>10.0.0.102:8080</div>
<div>SessionID = <span style="color:blue">1E7491BD5F856131B74B5708BFE4DD77</span></div>
Sat May 07 08:59:03 UTC 2022
</body>
</html>
[root@Nginx ~]#curl http://www.stars.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tomcat test</title>
</head>
<body>
 Tomcat Website 
<div>On  tomcat-server</div>
<div>10.0.0.102:8080</div>
<div>SessionID = <span style="color:blue">78DEBBDA22E2BD7DBA033174CE9860EA</span></div>
Sat May 07 08:59:10 UTC 2022
</body>
</html>

通过浏览器看到的主机是不变的,sessionID也是不变的,是因为浏览器默认就带了session去访问服务器的。
image.png
image.png

6、模拟故障修复后观察效果

关闭Session对应的Tomcat服务(10.0.0.102),再重启启动它,看看Session的变化。

root@Rs2:~# systemctl restart tomcat.service

通过浏览器看到主机不变,但sessionID和上一次变化,但后续刷新不再变化。
image.png

举报

相关推荐

0 条评论