1.实验设计
实验所满足的需求
满足某公司,想搭建一套高可用的负载均衡DR模式的集群,同时该集群收到用户访问请求时能够自主判断用户发送的请求是动态资源还是静态,依次划分进行动静分离:Nginx处理静态资源,Tomcat处理动态资源
实验集群的组件预定
一台主DR调度器:192.168.50.20 VIP:192.168.50.50 (实验没用用主备调度器)
Nginx节点服务器01: 192.168.50.22 VIP:192.168.50.50
Nginx节点服务器02: 192.168.50.23 VIP:192.168.50.50
Tomcat多实例节点服务器: Tomcat01:192.168.50.24:8080 Tomcat02: 192.168.73.50.24:8081
Tomcat单例节点服务器: Tomcat03:192.168.50.25:8080
客户访问测试机:192.168.50.26
2.实验操作步骤
本次实验的操作中用到的服务均为yum本地源服务或则源码编译安装。为了实验的流畅性,建议所有虚拟机搭建本地源来完成实验
步骤一:配置DR调度器 192.168.50.20
systemctl stop firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
modprobe ip_vs
cat /proc/net/ip_vs
yum -y install ipvsadm
(1)配置虚拟 IP 地址(VIP:192.168.50.50)VIP
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0 #若隧道模式,复制为ifcfg-tunl0
vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.50.50
NETMASK=255.255.255.255
ifup ens33:0
ifconfig ens33:0
(2)调整 proc 响应参数
#由于 LVS 负载调度器和各节点需要共用 VIP 地址,需要关闭 icmp 的重定向,不充当路由器。
vim /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
(3)配置负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -C
ipvsadm -A -t 192.168.50.50:80 -s rr
ipvsadm -a -t 192.168.50.50:80 -r 192.168.50.22:80 -g #若隧道模式,-g替换为-i
ipvsadm -a -t 192.168.50.50:80 -r 192.168.50.23:80 -g
ipvsadm
ipvsadm -ln #查看节点状态,Route代表 DR模式
步骤二:两个Nginx节点服务器的安装配置
两台虚拟机192.168.50.22 192.168.50.23 直接用脚本源码安装nginx服务
Nginx01节点服务器(192.168.50.22)设置
(1)设置回环虚拟网卡(VIP),添加静态路由
#配置回环的虚拟网卡,设置VIP承载
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.50.50
NETMASK=255.255.255.255
service network restart 或 systemctl restart network
ifup lo:0
ifconfig lo:0
#添加静态路由
route add -host 192.168.50.50 dev lo:0
(2) 设置内核参数
—响应参数以阻止更新 VIP 的 MAC 地址,避免发生冲突(web1和web2的相同设置)
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
(3)编写Nginx01的静态资源测试网页
vim /usr/local/nginx/html/index.html
<html>
<body>
<h1> this is Nginx01 static test !</h1>
</body>
</html
(4)进行Nginx01负载均衡和动静分离配置
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
#gzip on;
upstream tomcat_server {
server 192.168.50.24:8080 weight=1;
server 192.168.50.24:8081 weight=1;
server 192.168.50.25:8080 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root html;
index index.html index.htm;
}
......
}
......
}
步骤三:安装tomcat服务,进行相关配置
配置 多实例tomcat服务器(192.168.50.24)
(1)安装jdk和tomcat,配置多实例服务启动
#jdk环境的配置
tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH
source /etc/profile
#安装tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mkdir /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1
cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
#配置多实例tomcat
vim /etc/profile.d/tomcat.sh
#tomcat1的全局变量设置
export CATALINA_HOME1=/usr/local/tomcat/tomcat1
export CATALINA_BASE1=/usr/local/tomcat/tomcat1
export TOMCAT_HOME1=/usr/local/tomcat/tomcat1
#tomcat2的全局变量设置
export CATALINA_HOME2=/usr/local/tomcat/tomcat2
export CATALINA_BASE2=/usr/local/tomcat/tomcat2
export TOMCAT_HOME2=/usr/local/tomcat/tomcat2
#加载设置好的全局变量
source /etc/profile.d/tomcat.sh
(2) 修改 多实例中tomcat02中的 server.xml 文件
vim /usr/local/tomcat/tomcat2/conf/server.xml
<Server port="8006" shutdown="SHUTDOWN">
#22行,修改Server prot,默认为8005 -> 修改为8006
<Connector port="8081" protocol="HTTP/1.1"
#69行,修改Connector port,HTTP/1.1 默认为8080 -> 修改为8081
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
#116行,修改Connector port AJP/1.3,默认为8009 -> 修改为8010
(3)修改多实例tomcat的开启与关闭配置文件,添加相关的tomcat全局变量
vim /usr/local/tomcat/tomcat1/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
##添加以下内容
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat2/bin/startup.sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
# -----------------------------------------------------------------------------
# Stop script for the CATALINA Server
# -----------------------------------------------------------------------------
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
(4)配置tomcat01的测试页面
mkdir /usr/local/tomcat/tomcat1/webapps/test
vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat01</title>
</head>
<body>
<% out.println("tomcat01 running");%>
</body>
</html>
vim /usr/local/tomcat/tomcat1/conf/server.xml
#删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>
/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
(5)配置tomcat02的测试页面
mkdir /usr/local/tomcat/tomcat2/webapps/test
vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat02</title>
</head>
<body>
<% out.println("tomcat02 running");%>
</body>
</html>
vim /usr/local/tomcat/tomcat2/conf/server.xml
#删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
</Host>
/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
配置单实例tomcat服务器(192.168.73.110)
(1)安装jdk环境和tomcat服务
#jdk环境的配置
tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH
source /etc/profile
java -version
#安装tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
netstat -ntap | grep 8080
(2)配置tomcat03的测试网页
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat03</title> #指定为 test3 页面
</head>
<body>
<% out.println("tomcat03 running ");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
#由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true">
</Context>
</Host>
/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
3.实验测试
测试一:直接访问VIP,进行刷新,观察是否出现负载均衡现象