群集架构篇——nginx反向代理+keepalived双机热备+tomcat服务器池+后端数据库一、系统环境
1、2台CentOS 7作为nginx服务器绑定同一块虚拟网卡:vnet1 二、项目简介
1、拓扑图 三、项目部署部署nginx服务器1、安装nginx服务器(另外一台相同操作) yum install -y \ //搭建安装工具和编辑环境 gcc \ gcc-c++ \ make \ openssl-devel \ zlib-devel \ pcre-devel useradd -s /sbin/nologin -M nginx //添加帐号 tar vxf nginx-1.13.9.tar.gz //解压nginx源码包 cd nginx-1.13.9 ./configure \ //检查环境 --prefix=/usr/local/nginx \ //指定安装路径 --user=nginx \ --group=nginx make && make install //编译并且安装 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //nginx命令做软连接方便使用 vi /etc/init.d/nginx //制作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 chmod +x /etc/init.d/nginx //更改启动脚本的权限 chkconfig --add nginx //将脚本添加到systemctl环境中 systemctl start nginx //启动nginx服务器
2、验证两台nginx服务器搭建成功 部署keepalived双机热备1、nginx主服务器中安装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/ //将keepalived添加开机进程中 systemctl enable keepalived //设置开机自启 2、修改keepalived配置文件 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.80.90 //两台nginx服务器共同的虚拟IP也称漂移地址 } } 3、添加检查keepalived状态的配置文件 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 //开启服务
4、验证漂移地址vip是否生成 ! Configuration File for keepalived global_defs { route_id NGINX-02 //服务器名称为02 } vrrp_script nginx { script "/opt/nginx.sh" //keepalived状态检查配置文件的路径 interval 2 weight -10 //检查失败对应优先级减少的值 } vrrp_instance VI_1 { state BACKUP //状态为从服务器 interface ens33 virtual_router_id 51 priority 100 //优先级设为100与主相差50 advert_int 1 authentication { auth_type PASS //认证方式 auth_pass 1111 //认证密码 } track_script { nginx } virtual_ipaddress { 192.168.80.90 //两台nginx服务器共同的虚拟IP也称漂移地址 } }
(2)nginx.sh脚本中nginx服务的启动不是根据keepalived的启动而启动,而是根据 vi /opt/nginx.sh #!/bin/bash #Filename:nginx.sh A=$(ip addr | grep 192.168.80.90/32 | grep -v grep | wc -l) if [ $A -gt 0 ]; then /etc/init.d/nginx start else /etc/init.d/nginx stop fi
6、验证nginx从服务器配置是否生效 部署tomcat
说明: tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk cp -r jdk1.8.0_144/ /usr/local/java //将解压好的jdk下的文件复制到java目录下 vi /etc/profile //添加jdk系统变量 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 source /etc/profile //刷新下文件 java -version //查看版本成功说明环境搭建成功
tar xvf apache-tomcat-8.5.23.tar.gz //解压软件包 cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //将解压好包复制到tomcat8目录下 //将tomcat启动命令软链接到系统命令的目录下重命名为tomcatup方便使用 ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown tomcatup //启动tomcat
3、验证两台tomcat服务器是否成功访问(另一台tomcat部署操作上同) 部署nginx反代理
1、配置nginx服务器 upstream tomcat_pool { server 192.168.80.120:8080 weight=1; server 192.168.80.130:8080 weight=1; }
nginx -t systemctl restart nginx
部署后端数据库1、搭建安装环境 yum -y install \ gcc \ gcc-c++ \ make \ ncurses \ ncurses-devel \ bison \ Cmake
2、添加数据库用户 tar xf mysql-boost-5.7.20.tar.gz –C /opt/ //解压源码包 cmake \ //检查安装配置环境 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 make && make install //编译安装 chown -R mysql.mysql /usr/local/mysql/ //将数据库安装目录的使用权限给用户mysql vi /etc/my.cnf //将主配置文件清空添加以下内容 [client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES chown mysql:mysql /etc/my.cnf //更改主配置文件的权限 echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile echo 'export PATH' >> /etc/profile //添加系统环境变量 source /etc/profile //刷新立即生效 cd /usr/local/mysql/ //进入安装目录初始化数据库 bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ //制作数据库系统启动命令 systemctl daemon-reload //刷新识别启动脚本 systemctl start mysqld //启动数据库 netstat -anpt | grep 3306 //查看端口是否在监听状态
systemctl enable mysqld //开机自启 mysqladmin -u root -p password "abc123" //添加数据库用户 mysql -u root -p //进入数据库
部署商城项目
1、将商城项目上传到两台tomcat服务器中(两台配置一样以其中一台为例) mysql -u root –p //进入数据库 create database slsaledb; //为商城项目创建数据库 GRANT all ON slsaledb.* TO 'testuser'@'%' IDENTIFIED BY 'admin123'; //授权testuser 用户连接slsaledb数据库 flush privileges; mysql -u root -p < slsaledb-2014-4-10.sql //商城项目对应的数据库文件上传到slsaledb数据库中
4、更改商城项目中的数据库对接配置文件 tomcatdown tomcatup
访问192.168.80.120:8080 systemctl stop keepalived killall -9 nginx netstat -anput | grep nginx
四、总结本文只做了简单的架构,现实中还需在nginx服务器和tomcat服务器中做各种优化,以便让客户感受最佳的访问效果。 (责任编辑:IT) |