Table of contents
5. Configure NGINX dual-machine hot backup
6. Verify dual-machine hot standby
Preface
LNMT dynamic and static separation refers to the use of dynamic and static separation technology in the LNMT architecture to process and distribute dynamic requests and static requests separately to improve performance and scalability. This architecture uses Nginx as a reverse proxy server, provides static resources directly from the Nginx server, and forwards dynamic requests to the back-end Tomcat application server for processing.
Keepalived is an open source high availability solution that can be used to implement failover and load balancing between servers. It uses Virtual Router Redundancy Protocol (VRRP) to monitor the status of servers and automatically switches to backup servers for high availability.
When LNMT dynamic and static separation architecture is combined with Keepalived, Keepalived is usually deployed on the Nginx server to achieve high availability. Multiple Nginx servers can be deployed and Keepalived used to monitor their status. If the primary Nginx server fails or is unavailable, Keepalived will automatically transfer the IP address to the backup server to ensure service continuity.
This combined architecture provides the benefits of high availability and load balancing. Even if one of the Nginx servers fails, Keepalived will automatically forward requests to other available Nginx servers to ensure service continuity and high performance. At the same time, the LNMT dynamic and static separation architecture reduces the load on the back-end Tomcat server and improves the access speed of static resources by directly providing static resources through Nginx.
1. Experimental topology
This experiment uses nginx combined with Tomcat to achieve dynamic and static separation and load balancing; nfs service is used to share the directory for Tomcat
Configure keepalived on the scheduler NGINX to improve availability
2. Configure tomcat
##两台tomcat服务器先进行如下步骤 :
##解压/建立软连接
tar xf apache-tomcat-8.5.16.tar.gz
mv apache-tomcat-8.5.16 /usr/local/tomcat
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/bin/catdown
ln -s /usr/local/tomcat/bin/startup.sh /usr/bin/catup
###创建挂载目录
/usr/local/tomcat
mkdir webapps1
###修改tomcat的访问目录
vim /usr/local/tomcat/conf/server.xml
###修改host字段插入
<Context path="" docBase="/usr/local/tomcat/webapps1" />
3. nfs configuration
Installation and configuration
##安装nfs服务
yum -y install nfs
##创建共享目录
mkdir /opt/web1
mkdir /opt/web2
##配置nfs的配置文件
vim /etc/exports
##插入
/opt/web1 192.168.115.136/24(rw,sync,no_root_squash)
/opt/web2 192.168.115.140/24(rw,sync,no_root_squash)
##让他生效
exportfs -arv
##启动nfs
systemctl start nfs
Return to the two Tomcats to mount the shared directory
###查看可挂载的目录
showmount -e 192.168.115.131
###挂载到webapps1
mount 192.168.115.131:/opt/web1 /usr/local/tomcat/webapps1
##查看挂载情况
df
###分别书写测试页面
echo tomcat1 > webapps1/index.jsp
echo tomcat2 > webapps1/index.jsp
##启动Tomcat
catup
##查看端口、
netstat -anput |grep 8080
test page
4. Configure NGINX
Install NGINX
###安装
yum -y install epel-release.noarch
yum -y install nginx
##修改配置文件
vim /etc/nignx/nginx.conf
##在http字段插入
upstream tomcat {
server 192.168.115.136:8080;
server 192.168.115.140:8080;
}
##server字段插入
location ~ \.jsp$ {
proxy_pass http://tomcat;
proxy_set_header Host $host;
}
location / {
root /usr/share/nginx/html;
index index.html;
}
##启动NGINX
Access 2 schedulers
Test dynamic and static separation
5. Configure NGINX dual-machine hot backup
Install keepalived
##安装
yum -y install keepalived
##配置
vim /etc/keepalived/keepalived.conf
############################################################
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL ###从的编号要改动一下 LVS_DEVEL1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER ###从改为BACKUP
interface ens33 ###网卡名
virtual_router_id 51
priority 100 ###从的优先级要比主低
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.115.200 ###VIP用来访问的虚拟IP
}
}
virtual_server 192.168.115.200 8080 { ###vip对应的真是IP 在下面
delay_loop 6
lb_algo rr
persistence_timeout 50
protocol TCP
real_server 192.168.115.136 8080 { ###这是Tomcat服务器的IP
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
}
url {
path /testurl2/test.jsp
}
url {
path /testurl3/test.jsp
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.115.140 8080 { ###这是Tomcat服务器的IP
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
}
url {
path /testurl2/test.jsp
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
Start keepalived
systemctl start keepalived
Check the master's IP to indicate that keepalived has taken effect.
VIP page access test
Static pages
dynamic page
6. Verify dual-machine hot standby
Simulate the scheduler main crash init 0, shut it down directly, and use the scheduler for backup access
1. Check the IP of the scheduler. IP drift indicates that keepalived takes effect.
2. Page access test
dynamic page
Normal access, test successful.