群集架構(gòu)篇?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫

首先準(zhǔn)備好兩臺nginx,兩臺tomcat,一臺MySQL數(shù)據(jù)庫,如下
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

創(chuàng)新互聯(lián)公司是專業(yè)的普定網(wǎng)站建設(shè)公司,普定接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行普定網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

=====================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 #創(chuàng)建子配置文件夾
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服務(wù)器處理
}
}

vi pool.conf

upstream center_pool {
server 192.168.20.41:8080;
server 192.168.20.42:8080;
ip_hash; #穩(wěn)定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
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
----------------------部署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 #狀態(tài)是master
interface ens33
virtual_router_id 51
priority 150 #優(yōu)先級為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同時開啟關(guān)閉的腳本

#!/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
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
由此可以看出nginx可以隨著keepalived一起開啟關(guān)閉

======================192.168.20.40(lvs02)===========================
主配置文件和主服務(wù)器一樣
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 #狀態(tài)為backup
interface ens33
virtual_router_id 51
priority 100 #優(yōu)先級為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

--------------------------測試驗(yàn)證---------------------------------
從服務(wù)器(192.168.20.40) ip addr show dev ens33
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
再在主服務(wù)器上實(shí)現(xiàn)故障(192.168.20.39) systemctl stop keepalived
killall -9 nginx
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫

在回到從服務(wù)器上(192.168.20.40) ip addr show dev ens33 (發(fā)現(xiàn)漂流地址過來了)
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
============================數(shù)據(jù)庫===================================
mysql -u root -p
create database slsaledb; #創(chuàng)建數(shù)據(jù)庫
GRANT all ON slsaledb.* TO 'testuser'@'%' IDENTIFIED BY 'admin123';
#賦予權(quán)限
flush privileges; #刷新權(quán)限
quit
mysql -u root -p <slsaledb-2014-4-10.sql #將文件導(dǎo)入數(shù)據(jù)庫中
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫

=============================tomcat================================
兩臺tomcat操作完全一樣
vi /usr/local/tomcat8/conf/server.xml
<Context path="" docBase="SLSaleSystem" reloadable="true" debug="0"></Context> #大概148行左右
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
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
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
===========================最終測試=================================
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
群集架構(gòu)篇 ?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫

分享題目:群集架構(gòu)篇?——nginx反向代理+keepalived雙機(jī)熱備+tomcat服務(wù)器池+后端數(shù)據(jù)庫
瀏覽路徑:http://bm7419.com/article4/isghoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)Google、網(wǎng)站改版建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)