您好,登錄后才能下訂單哦!
利用Nginx反向代理 Tomcat 多節點
實驗說明:通過兩臺nginx反代理和 keepalived實現雙機熱備并成功訪問后端商城項目數據。
實驗所需
兩臺nginx keepalived 漂移地址 192.168.30.100
Nginx 192.168.30.34
192.168.30.36
Tomcat 192.168.30.31
192.168.30.32
Mysql 192.168.30.35
事先已經安裝完成nginx和mysql
實驗達成結果 通過nginx漂移地址訪問tomcat搭建的商城項目并登陸,主服務器模擬宕機,實現備份機上線
商城文件 點擊鏈接 提取碼: 97f7 里面有商城構架和一個數據庫表
以下是各服務詳細配置
mysql
[root@localhost ~]# mysql -u root -p #登陸mysql
mysql> create database slsaledb; #創建名為slsaledb 的數據庫
mysql> GRANT all ON slsaledb.* TO 'testuser'@'%' IDENTIFIED BY 'admin123'; #授權testuser 使用密碼admin123
mysql> flush privileges; #刷新
上傳商城數據庫文件
[root@localhost ~]# mysql -u root -p <slsaledb-2014-4-10.sql #上傳數據表
Enter password: #輸入數據管理員庫密碼
接下來配置tomcat
Tomcat所需文件
[root@lin3031 ~]# tar xf apache-tomcat-8.5.23.tar.gz #解壓tomcat
[root@lin3031 ~]# tar xf jdk-8u144-linux-x64.tar.gz #解壓java
[root@lin3031 ~]# cp -a jdk1.8.0_144/ /usr/local/java #復制Java解壓文件至/usr/local/java
[root@lin3031 ~]# vi /etc/profile #增加環境變量
在最后插入下四行
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
刷新環境變量
[root@lin3031 ~]# source /etc/profile
查看是否生效
[root@lin3031 ~]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
[root@lin3031 ~]# cp -a apache-tomcat-8.5.23 /usr/local/tomcat8 #將解壓后的tomcat復制到/usr/local/tomcat8
[root@lin3031 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup #優化路徑
[root@lin3031 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown #優化路徑
[root@lin3031 ~]# tomcatup #啟動tomcat
[root@lin3031 ~]# netstat -anpt | grep 8080 #過濾8080端口
tcp6 0 0 :::8080 :::* LISTEN 1325/java
另一臺tomcat安裝操作相同
商城文件 點擊鏈接 提取碼: 97f7 里面有商城構架和一個數據庫表
需要復制商城文件到tomcat
[root@lin3031 ~]# tar xf SLSaleSystem.tar.gz
[root@lin3031 ~]# cp -a SLSaleSystem /usr/local/tomcat8/webapps/
Tomcat主conf文件中添加 <Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context>
[root@lin3031 ~]# vim /usr/local/tomcat8/conf/server.xml
將tomcat連接后方數據庫
[root@lin3031 ~]# vim /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes/jdbc.properties
修改數據庫地址和授權的賬號密碼
第二臺tomcat操作如上相同
下面來測試
啟動tomcat
[root@lin3031 ~]# tomcatdown
訪問 192.168.30.31:8080 賬號 admin 密碼 123456
可以看到已經跳出了登陸選項
當然,用戶是不可以知曉后方服務器的,需要提供一個前端來訪問,下面的操作是配置兩個nginx
Nginx配置
第一臺 192.168.30.36
[root@lin3036 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream center_pool { #默認輪詢
ip_hash; #保持連接
server 192.168.30.31:8080; #兩臺tomcat服務器
server 192.168.30.32:8080;
}
server {
listen 80;
server_name lvs01 192.168.30.36;
location / {
proxy_pass http://center_pool; #代理tomcat
}
}
}
使用nginx –t 檢查語法
[root@lin3036 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
另一臺nginx 192.168.30.34
[root@lin3034 ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream center_pool { #默認輪詢
ip_hash; #保持連接
server 192.168.30.31:8080; #兩臺tomcat服務器
server 192.168.30.32:8080;
}
server {
listen 80;
server_name lvs02 192.168.30.34;
location / {
proxy_pass http://center_pool; #代理tomcat
}
}
}
使用nginx –t 檢查語法
[root@lin3034 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
檢查后沒有問題就重啟nginx
systemctl restart nginx
驗證
使用keepalived來保持兩臺nginx實現熱備份 并用192.168.30.100訪問商城
下面進行安裝
yum -y install popt-devel \
kernel-devel \
openssl-devel
tar xvf keepalived-1.4.2.tar.gz
cd keepalived-1.4.2
[root@lin3034 keepalived-1.4.2]# ./configure --prefix=/
[root@lin3034 keepalived-1.4.2]# make && make install
[root@lin3034 keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@lin3034 keepalived-1.4.2]# systemctl enable keepalived
兩臺安裝方式相同
下面進行配置文件修改
第一臺
[root@lin3036 keepalived-1.4.2]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
route_id NGINX-01 #服務器名稱
}
vrrp_script nginx {
script "/opt/nginx.sh" #keepalived 狀態檢查配置文件路徑
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER #作為主服務器
interface ens33
virtual_router_id 51
priority 150 #優先級設為150
advert_int 1
authentication {
auth_type PASS #認證方式
auth_pass 1111 #認證密碼
}
track_script {
nginx
}
virtual_ipaddress {
192.168.30.100 #兩臺nginx共同虛擬的IP地址(漂移地址)
}
}
該腳本方便同時將nginx和keepalived服務器同時啟動
[root@lin3036 keepalived-1.4.2]# vi /opt/nginx.sh
#!/bin/bash
#Filename:nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
/etc/init.d/nginx start
else
/etc/init.d/nginx stop
fi
[root@lin3036 keepalived-1.4.2]# chmod +x /opt/nginx.sh
[root@lin3036 keepalived-1.4.2]# systemctl start keepalived
[root@lin3036 keepalived-1.4.2]# ip addr
由于第二臺沒有配置,所以默認192.168.30.100 在主服務器上
第二臺
[root@lin3034 keepalived-1.4.2]# vi /etc/keepalived/keepalived.conf
global_defs {
route_id NGINX-02
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state BACKUP #作為從服務器
interface ens33
virtual_router_id 51
priority 100 #和主相差50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.30.100
}
根據漂移地址是否存在而啟動nginx服務。
[root@lin3034 keepalived-1.4.2]# vi /opt/nginx.sh
#!/bin/bash
#Filename:nginx.sh
A=$(ip addr | grep 192.168.80.100/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
/etc/init.d/nginx start
else
/etc/init.d/nginx stop
fi
chmod +x /opt/nginx.sh
systemctl start keepalived
模擬主服務器故障
[root@lin3036 keepalived-1.4.2]# systemctl stop keepalived
[root@lin3036 keepalived-1.4.2]# systemctl stop nginx
[root@lin3036 keepalived-1.4.2]# ip addr
192.168.30.100 已經不在主服務器上了
而是到了從服務器
[root@lin3034 keepalived-1.4.2]# ip addr
訪問192.168.30.100 賬號 admin 密碼 123456
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。