帶你瞭解Nginx+Tomcat負載均衡,動靜分離群集
Nginx+Tomcat負載均衡,動靜分離群集
Tomcat簡介
·最初是由Sun的軟體架構師詹姆斯.鄧肯。戴維森開發
·安裝Tomcat後,安裝路徑下面的目錄和檔案,是使用或者配置Tomcat的重要檔案
Tomcat重要目錄
bin: 存放啟動和關閉Tomcat指令碼
conf :存放Tomcat不同的配置檔案
doc :存放Tomcat文件
lib :存放Tomcat執行需要的庫檔案
logs :存放Tomcat執行時的LOG檔案
src :存放Tomcat的原始碼
webapps: Tomcat的主要Web釋出目錄
work :存放jsp編譯後產生的class檔案
Nginx負載均衡實現原理
1、Nginx實現負載均衡是通過反向代理實現
2、Nginx配置反向代理的主要引數
(1)upstream 服務池名{}
配置後端伺服器池,以提供響應資料
(2)proxy_pass http://服務池名
配置將訪問請求轉發給後端伺服器池的伺服器處理
Nginx動靜分離實現原理
動靜分離原理
服務端接收來自客戶端的請求中,既有靜態資源也有動態資源,靜態資源由Nginx提供服務,動態資源Nginx轉發至後端
Nginx靜態處理優勢
1、Nginx處理靜態頁面的效率遠高於Tomcat的處理能力
2、若Tomcat的請求量為1000次,則Nginx的請求量為6000次
3、Tomcat每秒的吞吐量為0.6M,Nginx的每秒吞吐量為3.6M
4、Nginx處理靜態資源的能力是Tomcat處理的6倍
專案
部署nginx+tomcat負載均衡、動靜分離
環境部署
nginx1:192.168.188.10
nginx2:192.168.188.20
tomcat1:192.168.188.30
tomcat2:192.168.188.40
nfs:192.168.188.50
客戶機:192.168.188.60
注:一定要把防火牆和核心防護關掉
nfs:
[root@nfs ~]#mkdir /web1
[root@nfs ~]#mkdir /web2
[root@nfs ~]#vi /web1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>tomcat 1</head>
<body>
<% out.println("this is tomcat1!!!");%>
<div>動態頁面</div><br/><img src="a.jpg" />
</body>
</html>
[root@nfs ~]#vi /web2/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>tomcat 2</head>
<body>
<% out.println("this is tomcat2!!!");%>
<div>動態頁面</div><br/><img src="b.jpg" />
</body>
</html>
[root@nfs ~]#
[root@nfs web1]#ls //把a.jpg放入web1的目錄中
a.jpg index.jsp
[root@nfs ~]#cd /web2
[root@nfs web2]#ls //把b.jpg放入web2的目錄中
b.jpg index.jsp
[root@nfs ~]#vi /etc/exports
/web1 192.168.188.30(ro)
/web2 192.168.188.40(ro)
[root@nfs ~]#systemctl start nfs
[root@nfs ~]#systemctl restart rpcbind
[root@nfs ~]#showmount -e
Export list for nfs:
/web2 192.168.188.40
/web1 192.168.188.30
tomcat1:
把jdk-8u91-linux-x64.tar.gz放入root目錄下
[root@tomcat1 ~]#tar zxvf jdk-8u91-linux-x64.tar.gz
[root@tomcat1 ~]#mv jdk1.8.0_91/ /usr/local/java
[root@tomcat1 ~]#vi /etc/profile
##末尾新增
export JAVA_HOME=/usr/local/java ##定義變數,設定JAVA根目錄設為全域性變數
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre ##環境變數,在PATH環境變數中新增JAVA根目錄下的bin/子目錄,jre/子目錄
[root@tomcat1 ~]#source /etc/profile
[root@tomcat1 ~]#echo $PATH //輸出環境資訊
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre
[root@tomcat1 ~]#java -version //檢視版本
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@tomcat1 ~]#vi abc.java
[root@tomcat1 ~]#javac abc.java //編寫指令碼
[root@tomcat1 ~]#java abc //執行顯示
hello world!!!
安裝tomcat1
[root@tomcat1 ~]#tar zxvf apache-tomcat-8.5.16.tar.gz
[root@tomcat1 ~]#mv apache-tomcat-8.5.16/ /usr/local/tomcat8
建立站點目錄
[root@tomcat1 ~]#mkdir /webapp1
[root@tomcat1 ~]#cd /webapp1/
[root@tomcat1 webapp1]#ls
a.jpg b.jpg
[root@tomcat1 ~]#mount 192.168.188.50:/web1 /webapp1
[root@tomcat1 ~]#df -Th
[root@tomcat1 ~]#vi /usr/local/tomcat8/conf/server.xml
<Context docBase="/webapp1" path="" reloadable="false">
</Context>
[root@tomcat1 ~]#ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tomcat1 ~]#ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tomcat1 ~]#tomcatup //啟動tomcat
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 ~]#netstat -anpt | grep 8080 //檢視8080是否啟動
tcp6 0 0 :::8080 :::* LISTEN 42877/java
[root@tomcat1 ~]#
客戶機
開啟瀏覽器輸入http://192.168.188.30:8080/
tomcat2
[root@tomcat2 ~]#tar zxvf jdk-8u91-linux-x64.tar.gz
[root@tomcat2 ~]#mv jdk1.8.0_91/ /usr/local/java
[root@tomcat2 ~]#vi /etc/profile
末尾新增
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre
[root@tomcat2 ~]#source /etc/profile
[root@tomcat2 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre
[root@tomcat2 ~]#java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@tomcat2 ~]#vi abc.java
public class abc {
public static void main (String[] args) {
System.out.println("hello world!!!");
}
}
[root@tomcat2 ~]#javac abc.java
[root@tomcat2 ~]#java abc
hello world!!!
[root@tomcat2 ~]#tar zxvf apache-tomcat-8.5.16.tar.gz
[root@tomcat2 ~]#mv apache-tomcat-8.5.16/ /usr/local/tomcat8
[root@tomcat2 ~]#mkdir /webapp2
[root@tomcat2 ~]#cd /webapp2/
[root@tomcat2 webapp2]#ls //新增圖片
a.jpg b.jpg
[root@tomcat2 webapp2]#cd
[root@tomcat2 ~]#mount 192.168.188.50:/web2 /webapp2
[root@tomcat2 ~]#df -Th
[root@tomcat2 ~]#vi /usr/local/tomcat8/conf/server.xml
<Context docBase="/webapp2" path="" reloadable="false">
</Context>
[root@server4 webapp2]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@server4 webapp2]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@server4 webapp2]# tomcatup
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@server4 webapp2]# netstat -anpt | grep 8080
客戶機
nginx1(排程器)
[root@nginx1 ~]#yum -y install gcc gcc-c++ make pcre-devel zlib-devel
pcre-devel:正規表示式庫
zlib-devel:函式庫
[root@nginx1 ~]#useradd -M -s /sbin/nologin nginx //建立不可登入,不帶宿主的使用者
[root@nginx1 ~]#tar zxvf nginx-1.12.2.tar.gz
[root@nginx1 ~]#cd nginx-1.12.2/
[root@nginx1 nginx-1.12.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx //安裝路徑,屬主,屬組設定
[root@nginx1 nginx-1.12.2]#make && make install //編譯安裝
[root@nginx1 nginx-1.12.2]#ln -s /usr/local/nginx/conf/nginx.conf /etc/ //建立設定連結
[root@nginx1 nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ //建立命令連結(nginx命令使用前提)
[root@nginx1 nginx-1.12.2]#cd
[root@nginx1 ~]#vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_server {
server 192.168.188.30:8080 weight=1;
server 192.168.188.40:8080 weight=1;
}
location ~ \.jsp$ {
proxy_pass http://tomcat_server;
}
[root@nginx1 ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx1 ~]#nginx
[root@nginx1 ~]#netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 58978/nginx: master
[root@nginx1 ~]#cd /usr/local/nginx/html/ //在這裡面新增圖片
[root@nginx1 html]#ls
50x.html index.html
[root@nginx1 html]#ls
50x.html a.jpg b.jpg index.html
[root@nginx1 html]#
nginx2(排程器)
[root@nginx2 ~]#yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@nginx2 ~]#useradd -M -s /sbin/nologin nginx //建立不可登入,不帶宿主的使用者
[root@nginx2 ~]#tar zxvf nginx-1.12.2.tar.gz
[root@nginx2 ~]#cd nginx-1.12.2/
[root@nginx2 nginx-1.12.2]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx //安裝路徑,屬主,屬組設定
[root@nginx2 nginx-1.12.2]#make && make install //編譯安裝
[root@nginx2 nginx-1.12.2]#ln -s /usr/local/nginx/conf/nginx.conf /etc/ //建立設定連結
[root@nginx2 nginx-1.12.2]#ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ //建立命令連結(nginx命令使用前提)
[root@nginx2 nginx-1.12.2]#cd
[root@nginx2 ~]#vi /usr/local/nginx/conf/nginx.conf
upstream tomcat_server {
server 192.168.188.30:8080 weight=1;
server 192.168.188.40:8080 weight=1;
}
location ~ \.jsp$ {
proxy_pass http://tomcat_server;
}
[root@nginx2 ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx2 ~]#nginx
[root@nginx2 ~]#netstat -anpt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 43519/nginx: master
[root@nginx2 ~]#cd /usr/local/nginx/html/ //在裡面新增圖片
[root@nginx2 html]#ls
50x.html index.html
[root@nginx2 html]#ls
50x.html a.jpg b.jpg index.html
客戶機
http://192.168.188.10/
http://192.168.188.10/index.jsp
http://192.168.188.20/
http://192.168.188.20/index.jsp
nginx1
安裝keepalive
[root@nginx1 html]#yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@nginx1 html]#tar zxvf keepalived-2.0.13.tar.gz
[root@nginx1 html]#cd keepalived-2.0.13/
[root@nginx1 keepalived-2.0.13]#./configure --prefix=/
[root@nginx1 keepalived-2.0.13]#make && make install
[root@nginx1 keepalived-2.0.13]#cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx1 keepalived-2.0.13]#vi /etc/init.d/keepalived
[root@nginx1 ~]#chmod 755 /etc/init.d
[root@nginx1 ~]#chkconfig --add keepalived
[root@nginx1 ~]#chkconfig --list
注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置資料
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 'systemctl list-unit-files'。
檢視在具體 target 啟用的服務請執行
'systemctl list-dependencies [target]'。
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@nginx1 ~]#
[root@nginx1 ~]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本伺服器的名稱
}
vrrp_script chk_http_port { ## vrrp指令碼
interval 2 ## 檢測間隔時間 單位秒
weight 2
}
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本伺服器的名稱
}
vrrp_script chk_http_port { ## vrrp指令碼
interval 2 ## 檢測間隔時間 單位秒
weight 2
}
vrrp_instance vi_1 { ##例項名稱
state MASTER ###指定keepalived的角色,MASTER為主,BACKUP為備
interface ens33 ##當前進行vrrp通訊的網路介面卡
virtual_router_id 51 ##虛擬路由編號,主從要一致
priority 110 ##優先順序,數值越大,獲取地址優先順序越大
adver_int 1 ##檢查間隔,預設為1s (vrrp組播週期秒數)
authentication { ##認證方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ##呼叫檢測指令碼
}
virtual_ipaddress {
192.168.188.200 ##定義虛擬ip(VIP)
}
}
[root@nginx1 ~]#vi /usr/local/src/nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
[root@nginx1 ~]#chmod +x /usr/local/src/nginx.sh
[root@nginx1 ~]#systemctl restart keepalived.service
[root@nginx1 ~]#ip addr
[root@nginx1 ~]#systemctl status keepalived.service
nginx2
安裝keepalivr
[root@nginx2 html]#yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
[root@nginx2 html]#tar zxvf keepalived-2.0.13.tar.gz
[root@nginx2 html]#cd keepalived-2.0.13/
[root@nginx2 keepalived-2.0.13]#./configure --prefix=/
[root@nginx2 keepalived-2.0.13]#make && make install
[root@nginx2 keepalived-2.0.13]#cp keepalived/etc/init.d/keepalived /etc/init.d
[root@nginx2 keepalived-2.0.13]#vi /etc/init.d/keepalived
修改
# chkconfig: 35 21 79
[root@nginx2 keepalived-2.0.13]#chmod 755 /etc/init.d
[root@nginx2 keepalived-2.0.13]#chkconfig --add keepalived
[root@nginx2 keepalived-2.0.13]#chkconfig --list
注:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置資料
可能被原生 systemd 配置覆蓋。
要列出 systemd 服務,請執行 'systemctl list-unit-files'。
檢視在具體 target 啟用的服務請執行
'systemctl list-dependencies [target]'。
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:開 4:開 5:開 6:關
[root@nginx2 keepalived-2.0.13]#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx2 ##本伺服器的名稱
}
! Configuration File for keepalived
global_defs {
router_id nginx2 ##本伺服器的名稱
}
vrrp_script chk_http_port { ## vrrp指令碼
interval 2 ## 檢測間隔時間 單位秒
weight 2
}
! Configuration File for keepalived
global_defs {
router_id nginx1 ##本伺服器的名稱
}
vrrp_script chk_http_port { ## vrrp指令碼
interval 2 ## 檢測間隔時間 單位秒
weight 2
}
vrrp_instance vi_1 { ##例項名稱
state BACKUP ###指定keepalived的角色,MASTER為主,BACKUP為備
interface ens33 ##當前進行vrrp通訊的網路介面卡
virtual_router_id 51 ##虛擬路由編號,主從要一致
priority 105 ##優先順序,數值越大,獲取地址優先順序越大
adver_int 1 ##檢查間隔,預設為1s (vrrp組播週期秒數)
authentication { ##認證方式
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ##呼叫檢測指令碼
}
virtual_ipaddress {
192.168.188.200 ##定義虛擬ip(VIP)
}
}
[root@nginx2 keepalived-2.0.13]#vi /usr/local/src/nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header | wc -l`
if [ $A -eq 0 ]
then /usr/local/nginx/sbin/nginx
if [ `ps -C nginx --no-header | wc -l` -eq 0 ]
then exit 1
else exit 0
fi
else
exit 0
fi
[root@nginx2 keepalived-2.0.13]#chmod +x /usr/local/src/nginx.sh
[root@nginx2 keepalived-2.0.13]#systemctl restart keepalived.service
[root@nginx2 keepalived-2.0.13]#ip addr
[root@nginx2 keepalived-2.0.13]#systemctl status keepalived.service
客戶機
http://192.168.188.200/
http://192.168.188.200/index.jsp
斷掉nginx1排程器
[root@nginx1 ~]#systemctl stop keepalived.service
[root@nginx1 ~]#ip addr //檢視下hi否有虛擬地址
[root@nginx2 ~]#ip addr //虛擬地址到nginx2排程器上了
相關文章
- Nginx+Tomcat負載均衡,動靜分離群集部署解析NginxTomcat負載
- Nginx—tomcat負載均衡動靜分離群集NginxTomcat負載
- 伺服器群集—Nginx+Tomcat+keepalived負載均衡、動靜分離群集伺服器NginxTomcat負載
- Nginx+Tomcat實現負載均衡、動靜分離叢集部署NginxTomcat負載
- 3分鐘帶你瞭解負載均衡服務負載
- Nginx 動靜分離與負載均衡的實現Nginx負載
- LVS負載均衡群集負載
- Nginx使用篇:實現負載均衡、限流與動靜分離Nginx負載
- Nginx+Tomcat部署負載均衡NginxTomcat負載
- Nginx+Tomcat實現動靜分離NginxTomcat
- Tomcat+Nginx實現動靜分離和負載均衡架構部署TomcatNginx負載架構
- nginx+tomcat實現負載均衡NginxTomcat負載
- LVS負載均衡群集--NAT模式負載模式
- 簡單瞭解負載均衡負載
- LVS負載均衡群集概念、NAT模式LVS負載均衡實戰部署負載模式
- Haproxy搭建 Web 群集實現負載均衡Web負載
- 伺服器群集LVS負載均衡-NAT伺服器負載
- 伺服器群集—LVS負載均衡群集(LVS-NAT部署)伺服器負載
- 負載均衡和動態負載均衡分別是什麼?-VeCloud負載Cloud
- CDN和負載均衡的基本瞭解負載
- Linux環境搭建Nginx+Tomcat負載均衡叢集LinuxNginxTomcat負載
- 《Nginx系列》之青銅入門篇 反向代理 負載均衡 動靜分離就是這麼簡單Nginx負載
- Mycat實現mysql的負載均衡讀寫分離MySql負載
- 使用Redis儲存Nginx+Tomcat負載均衡叢集的SessionRedisNginxTomcat負載Session
- openGauss JDBC客戶端負載均衡與讀寫分離JDBC客戶端負載
- 資料庫的讀寫分離與負載均衡策略資料庫負載
- 負載均衡詳解負載
- MySQL Route負載均衡與讀寫分離Docker環境使用MySql負載Docker
- AutoScaling彈性伸縮附加與分離負載均衡例項負載
- 直流負載的案例,你瞭解多少?負載
- Nginx負載均衡詳解Nginx負載
- 10分鐘帶你瞭解NLA
- docker+mysql叢集+讀寫分離+mycat管理+垂直分庫+負載均衡DockerMySql負載
- gRPC負載均衡(自定義負載均衡策略)RPC負載
- gRPC負載均衡(客戶端負載均衡)RPC負載客戶端
- nginx負載均衡策略你知道多少?Nginx負載
- 負載均衡負載
- 【Tony 老師】基於 Maxscale 實現讀寫分離和負載均衡負載