DR模式 LVS负载均衡群集
DR 服务器:192.168.110.10
nginx 服务器1:192.168.110.50
nginx 服务器2:192.168.110.60
vip:192.168.110.88
tomcat 服务器1:192.168.110.70
tomcat 服务器2:192.168.110.80
nfs 服务器:192.168.110.20
部署共享存储
nfs 服务器:192.168.110.20
关闭防火墙和核心防护
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
下载,开启nfs服务
yum install nfs-utils rpcbind -y
systemctl start rpcbind.service
systemctl start nfs.service
systemctl enable nfs.service
systemctl enable rpcbind.service
创建共享文件夹和测试用的静态网页文件
mkdir /opt/sunny1 /opt/sunny2
chmod 777 /opt/sunny1 /opt/sunny2
echo '静态网页1' > /opt/sunny1/index.html
echo '静态网页2' > /opt/sunny2/index.html
编辑nfs配置文件
vim /etc/exports
/usr/share *(ro,sync)
/opt/sunny1 192.168.110.20/24(rw,sync)
/optsunny2 192.168.110.20/24(rw,sync)
发布共享
exportfs -rv
查看共享
showmount -e
配置 tomcat 服务器
tomcat 服务器1:192.168.110.70
tomcat 服务器2:192.168.110.80
关闭防火墙和核心防护
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
安装tomcat
#查看是否安装java(没有安装则安装)
java -version
#安装java
yum install -y java
#设置JDK环境变量
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64
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.d/java.sh
如何查找java的安装目录
[root@tomcat2 yum.repos.d]# whereis java
java: /usr/bin/java /usr/lib/java /etc/java /usr/share/java /usr/share/man/man1/java.1.gz
[root@tomcat2 yum.repos.d]# ll /usr/bin/java
lrwxrwxrwx. 1 root root 22 10月 8 14:13 /usr/bin/java -> /etc/alternatives/java
[root@tomcat2 yum.repos.d]# ll /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 10月 8 14:13 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/jre/bin/java
tomcat 安装
#上传 tomcat 软件包
#解压安装
tar xf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
#启动 tomcat
/usr/local/tomcat/bin/startup.sh
#查看是否启动成功
ss -nltp | grep java
tomcat 安装完成
配置 tomcat 多实例
#创建tomcat1,2
cd
tar xf apache-tomcat-9.0.16.tar.gz
cp -a 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
#修改 tomcat1,2 中的 server.xml 文件,端口不重复。tomcat1:8006 8081 8010;tomcat2:8007 8082 8011。
vi /usr/local/tomcat/tomcat1/conf/server.xml
vi /usr/local/tomcat/tomcat2/conf/server.xml
#修改各 tomcat 实例中的 startup.sh 和 shutdown.sh 文件,添加 tomcat 环境变量
vim /usr/local/tomcat/tomcat1/bin/startup.sh
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat1/bin/shutdown.sh
export CATALINA_BASE=$CATALINA_BASE1
export CATALINA_HOME=$CATALINA_HOME1
export TOMCAT_HOME=$TOMCAT_HOME1
vim /usr/local/tomcat/tomcat2/bin/startup.sh
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
vim /usr/local/tomcat/tomcat2/bin/shutdown.sh
export CATALINA_BASE=$CATALINA_BASE2
export CATALINA_HOME=$CATALINA_HOME2
export TOMCAT_HOME=$TOMCAT_HOME2
#启动各 tomcat 中的 /bin/startup.sh
/usr/local/tomcat/tomcat1/bin/startup.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
#查看是否开启
ss -natp | grep java
创建测试用的动态网页
vi /usr/local/tomcat/tomcat1/webapps/ROOT/test.jsp
动态页面1
vi /usr/local/tomcat/tomcat2/webapps/ROOT/test.jsp
动态页面2
#测试
curl 192.168.110.70:8081/test.jsp
curl 192.168.110.70:8082/test.jsp
配置 nginx 服务器
nginx 服务器1:192.168.110.50
nginx 服务器2:192.168.110.60
关闭防火墙和核心防护
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
配置
配置虚拟 IP 地址
vip:192.168.110.88
虚拟 IP 仅用作发送 Web响应数据包的源地址,并不需要监听客户机的访问请求(由调度器监听并分发)。
因此使用虚接口 lo∶0 来承载 VIP 地址,并为本机添加一条路由记录,将访问 VIP 的数据限制在本地,以避免通信紊乱。
#创建一个虚接口
cd /etc/sysconfig/network-scripts/
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.110.88
NETMASK=255.255.255.255
#开启虚接口lo:0并查看状态
ifup lo:0
ifconfig lo:0
#添加路由
route add -host 192.168.110.88 dev lo:0
调整内核的 ARP 响应参数以阻止更新 VIP 的 MAC 地址,避免发生冲突
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1 #系统只响应目的IP为本地IP的ARP请求
net.ipv4.conf.lo.arp_announce = 2 #系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
#加载配置
sysctl -p
安装nginx
yum安装
nginx 服务器1:192.168.110.50
#下载
yum install -y nginx
#开启
nginx
软件包安装
nginx 服务器2:192.168.110.60
#上传软件包
#解压软件包
tar xf nginx-1.26.2.tar.gz
#安装依赖环境
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#配置
cd nginx-1.26.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
#编译安装
make -j 2 && make install
#让系统识别nginx的操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#开启
nginx
挂载共享文件夹
下载nfs
yum -y install nfs-utils rpcbind
systemctl start rpcbind
systemctl start httpd
挂载
#查看共享文件夹
showmount -e 192.168.110.20
#挂载
#nginx 服务器1
mount.nfs 192.168.110.20:/opt/sunny1 /usr/share/nginx/html
#nginx 服务器2
mount.nfs 192.168.110.20:/opt/sunny2 /usr/local/nginx/html
#查看挂载状态
df -Th
测试静态网页
curl 192.168.110.50
curl 192.168.110.60
配置nginx七层反向代理
vim /usr/local/nginx/conf/nginx.conf
#http块中
upstream tomcat_web {
server 192.168.110.70:8081 weight=1;
server 192.168.110.70:8082 weight=1;
server 192.168.110.80:8081 weight=1;
server 192.168.110.80:8082 weight=1;
}
#server块中
location ~ .*\.jsp$ {
proxy_pass http://tomcat_web;
proxy_set_header HOST $host;
proxy_set_header X_Real_IP $remote_addr;
proxy_set_header X_Forwarded_For $proxy_add_x_forwarded_for;
}
测试
curl 192.168.110.60/test.jsp
curl 192.168.110.50/test.jsp
配置负载调度器
DR 服务器:192.168.110.10
关闭防火墙和核心防护
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
#加载IP虚拟服务器(IPVS)模块
modprobe ip_vs
#检查IPVS的状态
cat /proc/net/ip_vs
#安装IPVS管理工具
yum -y install ipvsadm
配置虚拟 IP 地址
vip:192.168.110.88
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.110.88
NETMASK=255.255.255.255
ifup ens33:0
ifconfig ens33:0
调整 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
配置负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -C
ipvsadm -A -t 192.168.110.88:80 -s rr
ipvsadm -a -t 192.168.110.88:80 -r 192.168.110.50:80 -g
ipvsadm -a -t 192.168.110.88:80 -r 192.168.110.60:80 -g
ipvsadm
ipvsadm -ln
访问测试
curl 192.168.110.88/test.jsp