群集架构篇

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

目录
第一部分 环境准备
第二部分 部署调度器—搭建Nginx+Keepalived(双机热备)
第三部分 部署服务器池—搭建Tomcat
第四部分 搭建Mysql数据库
第五部分 案例应用

第一部分 环境准备

一:Nginx+keepalived服务器两台(调度器,双机热备)
系统:Linux—CentOS7.4
IP地址:192.168.40.15
192.168.40.16
软件需求:nginx安装包(nginx-1.13.9.tar.gz)
Keepalived安装包(keepalived-1.4.2.tar.gz)

二:tomcat服务器两台(服务器池)
系统:Linux—CentOS7.4
IP地址:192.168.40.18(TM01)
192.168.40.19(TM02)
软件需求:java环境jdk包(jdk-8u144-linux-x64.tar.gz)
tomcat安装包(apache-tomcat-8.5.23.tar.gz)
SL会员商城项目软件包(SLSaleSystem.tar.gz)

三:Mysql服务器一台
系统:Linux-CentOS7.4
IP地址:192.168.40.30
软件需求:mysql安装包(mysql-boost-5.7.20.tar.gz)
SL会员商场数据库文件(slsaledb-2014-4-10.sql)

四:客户端一台,以本机为例,测试验证用
IP地址:192.168.40.12
//Linux系统信息

群集架构篇

server_tokens off;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;

keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;

}
保存退出
//以下编辑子配置文件
[root@localhost nginx-1.13.9]# cd /usr/local/nginx/conf/
[root@localhost conf]# mkdir conf.d
[root@localhost conf]# cd conf.d/
[root@localhostconf.d]# vi lvs01.conf //新建子配置文件
server {
listen 80;
server_name localhost 192.168.40.15; #服务器名称与IP地址
index index.html index.jsp;
root /usr/local/nginx/html;
access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
location ~ ..jsp$ {
index index.jsp;
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;
}
location ~ .
.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
proxy_pass http://center_pool;
}
location ~ .*.(js|css){
expires 1h;
proxy_pass http://center_pool;

}
[root@localhost conf.d]# vi pool.conf //创建服务器池
upstream center_pool { #默认轮询
server 192.168.40.18:8080;
server 192.168.40.19:8080;
}
//制作启动脚本
[root@localhost conf.d]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 35 99 20
# description: Nginx Service Control Script**

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/
[root@localhost conf.d]# chmod +x /etc/init.d/nginx //增加执行权限
[root@localhost conf.d]# chkconfig –add nginx //加入系统管理服务
[root@localhost conf.d]# service nginx start //启动nginx服务
[root@localhost conf.d]# netstat -anpt | grep 80

————-部署keepalived———–
[root@localhost conf.d]#
yum -y install
popt-devel
kernel-devel
openssl-devel
[root@localhost ~]# tar xvf keepalived-1.4.2.tar.gz
[root@localhost ~]# cd keepalived-1.4.2
[root@localhost keepalived-1.4.2]# ./configure –prefix=/ //配置
[root@localhost keepalived-1.4.2]# make && make install //编译与安装
[root@localhost keepalived-1.4.2]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@localhost keepalived-1.4.2]# systemctl enable keepalived //设置开机自启
//以下编辑keepalived配置文件
[root@localhost keepalived-1.4.2]# cd /etc/keepalived/
[root@localhost 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
interface ens33
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
nginx
}
virtual_ipaddress {
192.168.40.100
}
}
//注意,主备的优先级配置的相差50

//判断keepalived进程是否存在,在就启动nginx不在就关闭
[root@localhost keepalived]# 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@localhost keepalived]# chmod +x /opt/nginx.sh
[root@localhost keepalived]# systemctl start keepalived
[root@localhost ~]# ip addr show dev ens33 //查看漂移地址是否生成

群集架构篇

—————测试验证————
[root@localhost ]# systemctl stop keepalived //关闭keepalived服务
[root@localhost ]# systemctl status keepalived //查看其状态
[root@localhost ]# ip addr show dev ens33 //查看漂移地址是否还在

群集架构篇

//验证成功

第二步:配置从服务器(192.168.40.16)

——-安装nginx服务——-
[root@lvs01 ~]# yum install -y //安装编译工具及插件

gcc
gcc-c++
make
openssl-devel
zlib-devel
pcre-devel
[root@localhost ~]# useradd -s /sbin/nologin -M nginx //添加帐号
[root@localhost ~]# tar vxf nginx-1.13.9.tar.gz //解压nginx安装包
[root@localhost ~]# cd nginx-1.13.9
[root@localhost ~ nginx-1.13.9]# ./configure //个性化配置
–user=nginx
–group=nginx
–with-file-aio
–with-http_flv_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_gzip_static_module
–with-http_realip_module
[root@localhost nginx-1.13.9]# make //编译
[root@localhost nginx-1.13.9]# make install //安装
//以下编译nginx主配置文件
[root@localhost nginx-1.13.9]# 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 {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

server_tokens off;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;

keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;

来源:weixin_34122810

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2018年10月23日
下一篇 2018年10月23日

相关推荐