负载均衡的综合部署练习(LVS-DR模式+keepalived+Nginx七层反向代理+Tomcat多实例)

3 篇文章 1 订阅
2 篇文章 0 订阅

 内容预知

 1.实验设计

 实验所满足的需求

 实验集群的组件预定

2.实验操作步骤  

实验环境准备步骤 (所有虚拟机)

 步骤一:配置主备DR调度器

 主调度器配置(192.168.73.105)

备DR调度器:(192.168.73.106) 

步骤二:两个Nginx节点服务器的安装配置 

Nginx01节点服务器(192.168.73.107)

Nginx02节点服务器(192.168.73.109) 

步骤三:安装tomcat服务,进行相关配置 

配置 多实例tomcat服务器(192.168.73.109)

 配置单实例tomcat服务器(192.168.73.110)

 3.实验测试

测试一:直接访问VIP,进行刷新,观察是否出现负载均衡现象 

测试二:访问VIP/index.jsp  ,并且进行刷新,观察tomcat是否进行负载均衡(同时证明动静分离)

测试三 :关闭主DR调度器,清空历史记录,再次访问VIP观察是否仍然正常运转 

测试四: 重启主DR 调度器,清空历史记录,观察主调器是否重新占用VIP,服务是否正常


 1.实验设计

 实验所满足的需求

满足某公司,想搭建一套高可用的负载均衡DR模式的集群,同时该集群收到用户访问请求时能够自主判断用户发送的请求是动态资源还是静态,依次划分进行动静分离:Nginx处理静态资源,Tomcat处理动态资源

 实验集群的组件预定

 一台主DR调度器:192.168.73.105       VIP:192.168.73.66

一台备DR调度器:192.168.73.106        VIP:192.168.73.66

Nginx节点服务器01: 192.168.73.107   VIP:192.168.73.66

Nginx节点服务器02: 192.168.73.108    VIP:192.168.73.66

Tomcat多实例节点服务器:   Tomcat01:192.168.73.109:8080      Tomcat02: 192.168.73.109:8081

Tomcat单例节点服务器:  Tomcat03:192.168.73.110:8080

客户访问测试机:192.168.73.111

实验部署简图:

2.实验操作步骤  

 本次实验的操作中用到的服务均为yum本地源服务或则源码编译安装。为了实验的流畅性,建议所有虚拟机搭建本地源来完成实验

实验环境准备步骤 (所有虚拟机)

#关闭防火墙和selinux,防止其对实验干扰
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled


#搭建本地yum源
mount /dev/sr0  /mnt
cd  /etc/yum.repos.d
mkdir ./repo.bak
mv *.repo   ./repo.bak/
vim local.repo
[local]
name=local
baseurl=file:///mnt
gpgcheck=0
enabled=1

yum clean  all && yum makecache

 

 步骤一:配置主备DR调度器

 主调度器配置(192.168.73.105)

 (1)修改keepalived的主配置参数

yum -y install ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs

cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
vim keepalived.conf
......
global_defs {						#定义全局参数
--10行--修改,邮件服务指向本地
	smtp_server 127.0.0.1
--12行--修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02
	router_id LVS_01
--14行--注释掉,取消严格遵守VRRP协议功能,否则VIP无法被连接
	#vrrp_strict
}
 
vrrp_instance VI_1 {				#定义VRRP热备实例参数
--20行--修改,指定热备状态,主为MASTER,备为BACKUP
    state MASTER
--21行--修改,指定承载vip地址的物理接口
    interface ens33
--22行--修改,指定虚拟路由器的ID号,每个热备组保持一致	
    virtual_router_id 10
	#nopreempt		#如果设置非抢占模式,两个节点state必须为BACKUP,并加上配置 nopreempt
--23行--修改,指定优先级,数值越大优先级越高,这里设置主为100,备为90
    priority 100
    advert_int 1					#通告间隔秒数(心跳频率)
    authentication {				#定义认证信息,每个热备组保持一致
		auth_type PASS				#认证类型
--27行--修改,指定验证密码(可以自定义),主备服务器保持一致
        auth_pass 1111   
    }
    virtual_ipaddress {				#指定群集vip地址
        192.168.73.66
    }
}
--36行--修改,指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.73.66 80 {
    delay_loop 6					#健康检查的间隔时间(秒)
    lb_algo rr						#指定调度算法,轮询(rr)
--39行--修改,指定群集工作模式,直接路由(DR)
    lb_kind DR
    persistence_timeout 0			#连接保持时间(秒)
    protocol TCP					#应用服务采用的是 TCP协议
--43行--修改,指定第一个Web节点的地址、端口
    real_server 192.168.73.107 80 {
        weight 1					#节点的权重
--45行--删除,添加以下健康检查方式		
        TCP_CHECK {
			connect_port 80			#添加检查的目标端口
			connect_timeout 3		#添加连接超时(秒)
			nb_get_retry 3			#添加重试次数
			delay_before_retry 3	#添加重试间隔
		}
	}
 
	real_server 192.168.73.108 80 {		#添加第二个 Web节点的地址、端口
        weight 1
        TCP_CHECK {
			connect_port 80
			connect_timeout 3
			nb_get_retry 3
			delay_before_retry 3
		}
	}
##删除后面多余的配置##
}
 
 
systemctl start keepalived
ip addr						#查看虚拟网卡vip

 

 (2)启动ipvsadm服务

ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
 
ipvsadm -ln

 (3)添加内核参数

vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
 
sysctl -p

备DR调度器:(192.168.73.106) 

相关服务的安装: 

yum -y install ipvsadm keepalived
modprobe ip_vs
cat /proc/net/ip_vs

(1)由主调度器将keepalived配置 文件通过scp传输给备DR调度器: 

cp -a keepalived.conf    keepalived.conf.bak1
scp `pwd`/keepalived.conf.bak1 root@192.168.73.106:`pwd`

(2)进行原配置备份,并且用scp传输的文件覆盖原文件 :

cd /etc/keepalived/
cp -a keepalived.conf keepalived.conf.bak
cp -a keepalived.conf.bak1    keepalived.conf

(3)进行配置修改调整:

#########全局配置中定义LVS编号为02#########
 
   router_id LVS_02
 
######虚拟网卡######
vrrp_instance VI_1 {
    state BAKUP   #修改状态为备份
.................
    priority 90   #优先级设置要比主DR调度器低
 
   
    }
 
 

 

(4)启动ipvsadm服务

ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
 
ipvsadm -ln

(5)添加内核参数 

vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
 
sysctl -p

步骤二:两个Nginx节点服务器的安装配置 

Nginx服务源码编译安装 : 

Nginx网站服务详解(第一部分:编译安装及系统服务添加)_站在这别动,我去给你买橘子的博客-CSDN博客https://blog.csdn.net/qq_62462797/article/details/127072140?spm=1001.2014.3001.5501

Nginx01节点服务器(192.168.73.107)

(1)设置回环虚拟网卡(VIP),添加静态路由  

#配置回环的虚拟网卡,设置VIP承载
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.73.66
NETMASK=255.255.255.255
 
service network restart 或 systemctl restart network
ifup lo:0
ifconfig lo:0
 
 
#添加静态路由
route add -host 192.168.73.66 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.73.109:8080 weight=1;
		server 192.168.73.109:8081 weight=1;
		server 192.168.73.110: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;
		}
......
	}
......
}

 

Nginx02节点服务器(192.168.73.109) 

(1)设置回环虚拟网卡(VIP),添加静态路由   

#配置回环的虚拟网卡,设置VIP承载
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.73.66
NETMASK=255.255.255.255
 
service network restart 或 systemctl restart network
ifup lo:0
ifconfig lo:0
 
 
#添加静态路由
route add -host 192.168.73.66 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)编写Nginx02的静态资源测试网页

vim /usr/local/nginx/html/index.html
<html>
<body>
<h1> this is Nginx02 static test !</h1>
</body>
</html>

(4)进行Nginx02负载均衡和动静分离配置 

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	#gzip on;
	
	upstream tomcat_server {
		server 192.168.73.109:8080 weight=1;
		server 192.168.73.109:8081 weight=1;
		server 192.168.73.110: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.73.109)

 (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,进行刷新,观察是否出现负载均衡现象 

 

测试二:访问VIP/index.jsp  ,并且进行刷新,观察tomcat是否进行负载均衡(同时证明动静分离)

测试三 :关闭主DR调度器,清空历史记录,再次访问VIP观察是否仍然正常运转 

测试四: 重启主DR 调度器,清空历史记录,观察主调器是否重新占用VIP,服务是否正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值