实验环境:
192.168.3.151 nginx负载均衡服务器
192.168.3.152 tomcat实例1
192.168.3.153 tomcat实例2
【部署tomcat服务器】
1.下载tomcat源码包和jdk包,两台tomcat同步进行
//可自行根据版本需求下载
wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz
//jdk包可在Oracle官网下载
https://www.oracle.com/java/technologies/javase-jre8-downloads.html
2.解压tomcat和jdk包,两台tomcat同步进行
tar xf apache-tomcat-9.0.36.tar.gz -C /usr/local
tar xf jdk-8u151-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
//创建软链接
ln -s apache-tomcat-9.0.36 tomcat
ln -s jdk1.8.0_151 java
//设置java环境变量
vim /etc/profile
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
//重新读取环境变量
source /etc/profile
//查看java版本
java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
3.修改tomcat网站页面便于区分是否进行分发
vim /usr/local/tomcat/webapps/ROOT/index.jsp
//tomcat1
<html>
<title> tomcat1</title>
<body bgcolor="blue">
<h1> tomcat1-8080</h1>
</body>
</html>
//tomcat2
<html>
<title> tomcat2</title>
<body bgcolor="red">
<h1> tomcat2-8080 </h1>
</body>
</html>
4.启动tomcat
//绝对路径启动
/usr/local/tomcat/bin/startup.sh
【部署负载均衡服务器】
1.通过ngnx官网下载nginx的yum仓库
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
2.安装nginx
yum -y install nginx
3.修改nginx配置
vim /etc/nginx/nginx.conf
//在http下方添加负载均衡配置,tomcat可跟进需求修改,同步修改location内的tomcat,名称禁用_下划线,会报错
upstream tomcat {
server 192.168.3.152:8080;
server 192.168.3.153:8080;
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
proxy_pass http://tomcat;
}
}
4.启动nginx
systemctl start nginx
systemctl enble nginx
【测试负载均衡】
访问192.168.3.151,刷新页面检测是否分发页面