nginx+tomcat部署均衡+虚拟IP配置

一、配置java环境

更新数据源

sudo apt update

安装java

sudo apt install openjdk-java-8-jdk

查看是否安装成功

java -version

二、Tomcat配置

​ 直接访问tomcat官网(http://tomcat.apache.org/),下载需要的版本,我这里选择apache-tomcat-8.5.90版本

操作如下:

# 安装需求的tomcat
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.90/bin/apache-tomcat-8.5.90.tar.gz
# 解压
tar -zxvf apache-tomcat-8.5.90.tar.gz

# 重命名
mv apache-tomcat-8.5.90 tomcat1
# 复制
cp tomecat1 tomcat2

修改tomcat的端口号,将tomcat1的http端口设置为8081,tomcat2的端口设为8082,操作如下:

Tomcat1的修改

<-- port="8005" 改为 port="9005" -->
<Server port="8005" shutdown="SHUTDOWN">
    ...
<-- port="8080" 改为 port="8081" redirectPort="8443" 改为 redirectPort="9443" -->
<Connector port="8080" protocol="HTTP/1.1"
              connectionTimeout="20000"
              redirectPort="8443"
              maxParameterCount="1000"
              />
<-- port="8009" 改为 port="9009" -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443">

Tomcat2的修改

<-- port="8005" 改为 port="10005" -->
<Server port="8005" shutdown="SHUTDOWN">
    ...
<-- port="8080" 改为 port="8082" redirectPort="8443" 改为 redirectPort="10443" -->
<Connector port="8080" protocol="HTTP/1.1"
              connectionTimeout="20000"
              redirectPort="8443"
              maxParameterCount="1000"
              />
...
<-- port="8009" 改为 port="10009" -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443">
  • 设置Tomcat服务开机启动

    systemctl enable tomcat
    

三、虚拟ip、keepalived实现VIP和Nginx的可高用

  1. 安装keepalived
apt install keepalived
  1. 配置keepalived

    ifconfig 查看网卡名称和ip

    配置文件 sudo vim /etc/keepalived/keepalived.conf

    vrr_script check_nginx {
            script "/etc/keepalived/check_nginx.sh"
            interval 2
            weight -5
    }
    
    vrrp_instance VI_1 {
       state MASTER
       interface ens33 #ifconfig确定
       virtual_router_id 51 #路由器标识,MASTER和BACKUP必须是一致的
       priority 100 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来
       advert_int 1
       authentication {
           auth_type PASS
           auth_pass 123456
       }
       virtual_ipaddress {
           192.168.94.12 # 虚拟ip
       }
       track_script {
            check_nginx;
       }
    }
    
    

四、Nginx实现部署均衡

进入打开文件/etx/nginx/nginx.conf文件,进行部署均衡

user www-data;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream tomcat-server {
         server 192.168.94.12:8081 weight=1;
         server 192.168.94.12:8082 weight=2;
    }

    server {
         listen       80;
         server_name  192.168.94.12:8080;

         location / {
         root   html;
         index  index.html index.htm;

         proxy_pass  http://tomcat-server;
         }
    }
}


启动服务

# 启动tomcat1 和 tomcat2
tomcat1/bin/startup.sh
tomcat2/bin/startup.sh

# 重新启动nginx
cd /sbin/
./nginx -s stop
./nginx

可以进行同时访问

当我把tomcat2关闭之后,tomcat1仍可以访问

posted @ 2023-07-23 13:15  菠菜好不好吃  阅读(127)  评论(0编辑  收藏  举报