群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

发布时间:2020-06-09 15:52:22 作者:星乄海
来源:网络 阅读:672

首先准备好两台nginx,两台tomcat,一台mysql数据库,如下
NginxIP地址:192.168.20.39(lvs01)
192.168.20.40(lvs02)
漂移地址:192.168.20.66
TomcatIP地址:192.168.20.41(TM01)
192.168.20.42(TM02)
MysqlIP地址:192.168.20.50

=====================192.168.20.39(lvs01)=======================
vi /usr/local/nginx/conf/nginx.conf

在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"';

在gzip on下增加
include /usr/local/nginx/conf/conf.d/*.conf; #指向子配置文件

cd /usr/local/nginx/conf/
mkdir conf.d #创建子配置文件夹
cd conf.d/

vi lvs01.conf

server {
listen 80;
server_name lvs01 192.168.20.39;
index index.html index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://center_pool; #将所有文件给tomcat服务器处理
}
}

vi pool.conf

upstream center_pool {
server 192.168.20.41:8080;
server 192.168.20.42:8080;
ip_hash; #稳定ip会话
}

启动脚本
vi /etc/init.d/nginx

#!/bin/bash
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
chmod +x /etc/init.d/nginx
chkconfig --add nginx

nginx -t #检查是否报错
service nginx start
netstat -anpt | grep 80
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
----------------------部署keepalived----------------------------
yum -y install \
popt-devel \
kernel-devel \
openssl-devel

tar xvf keepalived-1.4.2.tar.gz
cd keepalived-1.4.2
./configure --prefix=/
make && make install
cp keepalived/etc/init.d/keepalived /etc/init.d/
systemctl enable keepalived
cd /etc/keepalived/
vi keepalived.conf

! Configuration File for keepalived
global_defs {
route_id NGINX-01
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state MASTER #状态是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.20.66 #漂移地址ip
}
}

vi /opt/nginx.sh #将nginx和keepalived同时开启关闭的脚本

#!/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

chmod +x /opt/nginx.sh
systemctl start keepalived

---------------------------测试---------------------------------
systemctl stop keepalived
killall -9 nginx (yum install psmisc -y)
netstat -anpt | grep 80

systemctl start keepalived
netstat -anpt | grep 80
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
由此可以看出nginx可以随着keepalived一起开启关闭

======================192.168.20.40(lvs02)===========================
主配置文件和主服务器一样
vi /usr/local/nginx/conf/nginx.conf

在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"';

在gzip on下增加
include /usr/local/nginx/conf/conf.d/*.conf; #指向子配置文件

cd /usr/local/nginx/conf/
mkdir conf.d
cd conf.d/
vi lvs02.conf

server {
listen 80;
server_name lvs01 192.168.20.40;
index index.html index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Client-IP $remote_addr;
proxy_set_header X-For $proxy_add_x_forwarded_for;
proxy_pass http://center_pool;
}
}

vi pool.conf

upstream center_pool {
server 192.168.20.41:8080;
server 192.168.20.42:8080;
ip_hash;
}

vi /etc/init.d/nginx

#!/bin/bash

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
chmod +x /etc/init.d/nginx
chkconfig --add nginx
nginx -t
service nginx start
netstat -anpt | grep 80

------------------------部署keepalived------------------------
yum -y install popt-devel \
kernel-devel \
openssl-devel

tar xvf keepalived-1.4.2.tar.gz
cd keepalived-1.4.2
./configure --prefix=/
make && make install
cp keepalived/etc/init.d/keepalived /etc/init.d/
systemctl enable keepalived
cd /etc/keepalived/
vi keepalived.conf

! Configuration File for keepalived
global_defs {
route_id NGINX-02
}
vrrp_script nginx {
script "/opt/nginx.sh"
interval 2
weight -10
}
vrrp_instance VI_1 {
state BACKUP #状态为backup
interface ens33
virtual_router_id 51
priority 100 #优先级为100,由此看出是备
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.20.66
}
}

vi /opt/nginx.sh

#!/bin/bash
#Filename:nginx.sh
A=$(ip addr | grep 192.168.20.66/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

--------------------------测试验证---------------------------------
从服务器(192.168.20.40) ip addr show dev ens33
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
再在主服务器上实现故障(192.168.20.39) systemctl stop keepalived
killall -9 nginx
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

在回到从服务器上(192.168.20.40) ip addr show dev ens33 (发现漂流地址过来了)
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
============================数据库===================================
mysql -u root -p
create database slsaledb; #创建数据库
GRANT all ON slsaledb.* TO 'testuser'@'%' IDENTIFIED BY 'admin123';
#赋予权限
flush privileges; #刷新权限
quit
mysql -u root -p <slsaledb-2014-4-10.sql #将文件导入数据库中
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

=============================tomcat================================
两台tomcat操作完全一样
vi /usr/local/tomcat8/conf/server.xml
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> #大概148行左右
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
tar xf SLSaleSystem.tar.gz -C /usr/local/tomcat8/webapps/
cd /usr/local/tomcat8/webapps/SLSaleSystem/WEB-INF/classes
vi jdbc.properties
修改ip uname password
url=jdbc\:mysql\://192.168.20.50\:3306/slsaledb?useUnicode\=true&characterEncoding\=UTF-8
uname=testuser
password=admin123
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
===========================最终测试=================================
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库
群集架构篇  ——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库

推荐阅读:
  1. 大型网站架构演变
  2. LNMT群集基于Redis实现Session共享

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx 反代理 keepalived

上一篇:链路聚合

下一篇:安装php模块eaccelerator 缓存加速模块报错

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》