vip: 10.110.200.200
nginx1(keepalived): 10.110.200.201
nignx2(keepalived): 10.110.200.202
tomcat1: 10.110.200.203
tomcat2: 10.110.200.204
关闭selinux,firewalld,iptables(iptables -F)

一、安装好tomcat:

A、安装2台tomcat服务器:

1、安装jdk
yum -y install tomcat -y
yum -y install tomcat-webapps.noarch tomcat-admin-webapps.noarch -y
yum install -y tomcat-docs-webapp.noarch tomcat-javadoc -y

2、创建java超链接 ln -s /usr/local/java1.8 /usr/local/java
3、 导入环境变量
vi /etc/profile
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME

    4、tomcat 开机启动
    在/usr/lib/systemd/system目录下增加tomcat9文件,内容如下:

[Unit]

Description=Tomcat

After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]

Type=oneshot

ExecStart=/usr/local/tomcat/tomcat9/bin/startup.sh

ExecStop=/usr/local/tomcat/tomcat9/bin/shutdown.sh

ExecReload=/bin/kill -s HUP $MAINPID

RemainAfterExit=yes

[Install]

WantedBy=multi-user.target

如何修改tomcat的默认端口
在web.xml文件中,有一段如下:

将port "8080"改成80端口
新版本的tomcat修改 server.xml 的8080

如何修改tomcat的默认路径
cd /var/lib/tomcat/webapps
或者cd /usr/share/tomcat/webapps
mkdir myjsp
在server.xml文件中,有一段如下:
之间加入

如何修改tomcat的默认首页
在web.xml文件中,有一段如下:

index.html
index.htm
index.jsp

之间添加上:
a.jsp

B、安装2台nginx+keepalived服务器:
1、nginx1-2:
yum install nginx keepalived -y

2、nginx1(keepalived):
vim /etc/keepalived/keepalved.conf

             vrrp_script check_nginx

{
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 5
user root

}
vrrp_instance VI_1 {
state MASTER
interface eth0
mcast_src_ip 10.110.200.201
virtual_router_id 51
preempt
priority 120
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
check_nginx
}
virtual_ipaddress {
10.110.200.200
}
}

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
count=0
for (( k=0; k<2; k++ ))
do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\n" http://localhost -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
echo $count
else
count=0
break
echo "break"
fi
done
if [ "$count" != "0" ]; then
systemctl stop keepalived
echo "keepalived is stoped"
exit 1
else
exit 0
fi

  1. nginx2(keepalived):
    vim /etc/keepalived/keepalved.conf

             vrrp_script check_nginx

    {
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -5
    user root
    enable_script_security
    }
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    mcast_src_ip 10.110.200.202
    virtual_router_id 51
    preempt
    priority 120
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }

track_script {
check_nginx
}
virtual_ipaddress {
10.110.200.200
}
}

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
count=0
for (( k=0; k<2; k++ ))
do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\n" http://localhost -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
echo $count
else
count=0
break
echo "break"
fi
done
if [ "$count" != "0" ]; then
systemctl stop keepalived
echo "keepalived is stoped"
exit 1
else
exit 0
fi

C、配置nginx负债均衡让8080转发给80端口:

upstream backend {
ip_hash;
server 10.1.1.1:8000 down;
server 10.1.1.2:8080 weight=10 max_fails=3 fail_timeout=10s;
server 10.1.1.3:6801 weight=20 max_fails=3 fail_timeout=10s;
}

server{
listen 80;
server_name www.test.com;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default;
root html;
index index.jsp;
}
}
以上代码必须放在http{} 内,如果你的nginx.conf调用了子配置文件 www.conf,那 include /etc/nginx/conf.d/web.conf; 必须放在nginx.conf的http{} 内。
**
D、