# Nginx+Tomcat搭建高可用
服务器名称 预装软件 IP地址Nginx服务器 Nginx1 192.168.6.131Nginx服务器 Nginx2 192.168.6.132# ==================================================================node1 node2
yum -y install gcc gcc-c++ autoconf# http://www.pcre.org/# ==================================================================安装 Pcrescp -r ~/pcre2-10.32.tar.gz node2:~/tar zxf ~/pcre2-10.32.tar.gzcd pcre2-10.32./configure --prefix=/usr/local/pcre && make && make installrm -rf ~/pcre2-10.32.tar.gzrm -rf ~/pcre2-10.32# https://www.openssl.org/source/# ==================================================================安装 openssl# 在rabbitmq安装过就不用再执行了# scp -r ~/openssl-1.1.0g.tar.gz node2:~/# tar -zvxf ~/openssl-1.1.0g.tar.gz# cd openssl-1.1.0g# ./config --prefix=/usr/local/openssl && make && make install# rm -rf ~/openssl-1.1.0g.tar.gz# rm -rf ~/openssl-1.1.0g# http://www.zlib.net/# ==================================================================安装 zlibscp -r ~/zlib-1.2.11.tar.gz node2:~/tar -zxvf ~/zlib-1.2.11.tar.gzcd zlib-1.2.11./configure --prefix=/usr/local/zlib && make && make installrm -rf ~/zlib-1.2.11.tar.gzrm -rf ~/zlib-1.2.11# http://nginx.org/download/# ==================================================================安装 nginxscp -r ~/nginx-1.14.0.tar.gz node2:~/tar -zxvf ~/nginx-1.14.0.tar.gzcd nginx-1.14.0./configure --prefix=/usr/local/nginx && make && make installrm -rf ~/nginx-1.14.0.tar.gzrm -rf ~/nginx-1.14.0# 环境变量vi /etc/profile# 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加export NGINX_HOME=/usr/local/nginxexport PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$NGINX_HOME/sbin# 使环境变量生效source /etc/profile# 查看配置结果echo $NGINX_HOME# 启动$NGINX_HOME/sbin/nginx -c $NGINX_HOME/conf/nginx.conf# 查看Nginx的进程号ps -ef|grep nginx# 三种关闭方式 将***替换为具体的进程号# 从容停止kill -QUIT ***# 快速停止kill -TERM ***# kill -INT ***# 强制停止pkill -9 nginx
# ==================================================================node1
# http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.31/bin/# ==================================================================安装 tomcattar -zxvf ~/apache-tomcat-8.5.31.tar.gz -C /usr/localmv /usr/local/apache-tomcat-8.5.31 /usr/local/tomcatrm -rf ~/apache-tomcat-8.5.31.tar.gz
# 环境变量
# ==================================================================node1 node2 node3vi /etc/profile# 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加export JAVA_HOME=/usr/java/jdk1.8.0_111export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.12export HADOOP_HOME=/usr/local/hadoop-2.7.6export MYSQL_HOME=/usr/local/mysqlexport HBASE_HOME=/usr/local/hbase-1.2.4export HIVE_HOME=/usr/local/hive-2.1.1export SCALA_HOME=/usr/local/scala-2.12.4export KAFKA_HOME=/usr/local/kafka_2.12-0.10.2.1export FLUME_HOME=/usr/local/flume-1.8.0export SPARK_HOME=/usr/local/spark-2.3.0export STORM_HOME=/usr/local/storm-1.1.0export ERLANG_HOME=/usr/local/erlangexport RABBITMQ_HOME=/usr/local/rabbitmq_server-3.7.5export MONGODB_HOME=/usr/local/mongodb-3.4.5export REDIS_HOME=/usr/local/redis-4.0.10export RUBY_HOME=/usr/local/ruby-2.5.1export NGINX_HOME=/usr/local/nginxexport CATALINA_BASE=/usr/local/tomcatexport CATALINA_HOME=/usr/local/tomcatexport TOMCAT_HOME=/usr/local/tomcatexport PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$ZOOKEEPER_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$MYSQL_HOME/bin:$HBASE_HOME/bin:$HIVE_HOME/bin:$SCALA_HOME/bin:$KAFKA_HOME/bin:$FLUME_HOME/bin:$SPARK_HOME/bin:$SPARK_HOME/sbin:$STORM_HOME/bin:$ERLANG_HOME/bin:$RABBITMQ_HOME/ebin:$RABBITMQ_HOME/sbin:$MONGODB_HOME/bin:$REDIS_HOME/bin:$RUBY_HOME/bin:$NGINX_HOME/sbin:$CATALINA_HOME/binexport CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport HADOOP_INSTALL=$HADOOP_HOMEexport HADOOP_MAPRED_HOME=$HADOOP_HOMEexport HADOOP_COMMON_HOME=$HADOOP_HOMEexport HADOOP_HDFS_HOME=$HADOOP_HOMEexport YARN_HOME=$HADOOP_HOMEexport HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
# ==================================================================node1
# 使环境变量生效source /etc/profile# 查看配置结果echo $CATALINA_BASEecho $CATALINA_HOMEecho $TOMCAT_HOMEscp -r $CATALINA_HOME node2:/usr/local/scp -r $CATALINA_HOME node3:/usr/local/
# ==================================================================node2 node3
# 使环境变量生效source /etc/profile# 查看配置结果echo $CATALINA_BASEecho $CATALINA_HOMEecho $TOMCAT_HOME
# ==================================================================node1 node2 node3
# 启动tomcat$CATALINA_HOME/bin/startup.sh# 停止tomcat$CATALINA_HOME/bin/shutdown.sh# 远程查看# http://node1:8080# http://node2:8080# http://node3:8080
# Nginx+Tomcat实现Web服务器的负载均衡
服务器名称 预装软件 IP地址
Nginx服务器 Nginx1 192.168.6.131Nginx服务器 Nginx2 192.168.6.132Web服务器1 tomcat 192.168.6.131Web服务器2 tomcat 192.168.6.132Web服务器3 tomcat 192.168.6.133# ==================================================================node1
mkdir $CATALINA_HOME/webapps/test# 编写测试静态页vi /usr/local/tomcat/webapps/test/login.html您正在访问:node1
login.html
# ==================================================================node2
mkdir /usr/local/tomcat/webapps/test# 编写测试静态页vi /usr/local/tomcat/webapps/test/login.html您正在访问:node2
login.html
# ==================================================================node3
mkdir /usr/local/tomcat/webapps/test # 编写测试静态页vi /usr/local/tomcat/webapps/test/login.html您正在访问:node3
login.html
# http://node1:8080/test/login.html
# http://node2:8080/test/login.html# http://node3:8080/test/login.html# ==================================================================node1
cp $NGINX_HOME/conf/nginx.conf $NGINX_HOME/conf/nginx.conf.bakvi $NGINX_HOME/conf/nginx.confuser nobody;# 工作进程个数,一般跟服务器cpu核数相等,或者核数的两倍worker_processes 2;# 单个进程最大连接数events{ worker_connections 1024; }http{ keepalive_timeout 65; gzip on; # 服务器集群 upstream tomcatcluster{ # 集群有几台服务器即可配置几台,weight表示权重,权重越大被访问到的几率越大 # 启动好Tomcat服务器 server node1:8080 weight=1 max_fails=2 fail_timeout=30s; server node2:8080 weight=1 max_fails=2 fail_timeout=30s; server node3:8080 weight=1 max_fails=2 fail_timeout=30s; } #nginx基本配置 server{ listen 8088; #端口号 server_name node1; #服务名 location /{ #将访问请求转向至服务器集群, tomcatcluster 和上面 upstream tomcatcluster 对应 proxy_pass http://tomcatcluster; # 真实的客户端IP proxy_set_header X-Real-IP $remote_addr; # 请求头中Host信息 proxy_set_header Host $host; # 代理路由信息,此处取IP有安全隐患 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 真实的用户访问协议 proxy_set_header X-Forwarded-Proto $scheme; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
# ==================================================================node2
cp $NGINX_HOME/conf/nginx.conf $NGINX_HOME/conf/nginx.conf.bakvi $NGINX_HOME/conf/nginx.confuser nobody;# 工作进程个数,一般跟服务器cpu核数相等,或者核数的两倍worker_processes 2;# 单个进程最大连接数events{ worker_connections 1024; }http{ keepalive_timeout 65; gzip on; # 服务器集群 upstream tomcatcluster{ # 集群有几台服务器即可配置几台,weight表示权重,权重越大被访问到的几率越大 # 启动好Tomcat服务器 server node1:8080 weight=1 max_fails=2 fail_timeout=30s; server node2:8080 weight=1 max_fails=2 fail_timeout=30s; server node3:8080 weight=1 max_fails=2 fail_timeout=30s; } #nginx基本配置 server{ listen 8088; #端口号 server_name node2; #服务名 location /{ #将访问请求转向至服务器集群, tomcatcluster 和上面 upstream tomcatcluster 对应 proxy_pass http://tomcatcluster; # 真实的客户端IP proxy_set_header X-Real-IP $remote_addr; # 请求头中Host信息 proxy_set_header Host $host; # 代理路由信息,此处取IP有安全隐患 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 真实的用户访问协议 proxy_set_header X-Forwarded-Proto $scheme; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}
# ==================================================================node1 node2
# 查看Nginx的进程号ps -ef|grep nginx# 停止kill -QUIT ***# 启动$NGINX_HOME/sbin/nginx -c $NGINX_HOME/conf/nginx.conf# http://node1:8088/test/login.html# http://node2:8088/test/login.html
# Nginx服务器做负载均衡和动静分离,Web服务器1 Web服务器2 Web服务器3 做集群
# ==================================================================node1 node2 node3mkdir -p $CATALINA_HOME/webapps/test/img
# 上传 testimg.jpg
# ==================================================================node1cp ~/testimg.jpg $CATALINA_HOME/webapps/test/img/# 编写测试静态页vi $CATALINA_HOME/webapps/test/index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Nginx动静分离测试 您正在访问:node1
scp -r ~/testimg.jpg node2:$CATALINA_HOME/webapps/test/img/scp -r ~/testimg.jpg node3:$CATALINA_HOME/webapps/test/img/# ==================================================================node2# 编写测试动态页vi $CATALINA_HOME/webapps/test/index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Nginx动静分离测试 您正在访问:node2
# ==================================================================node3# 编写测试动态页vi /usr/local/tomcat/webapps/test/index.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Nginx动静分离测试 您正在访问:node3
# http://node1:8080/test/index.jsp# http://node2:8080/test/index.jsp# http://node3:8080/test/index.jsp# http://node1:8088/test/index.jsp# http://node2:8088/test/index.jsp
# ==================================================================node1 node2
mkdir -p /data/webapps# 加入下面的配置vi $NGINX_HOME/conf/nginx.confhttp{ server{ charset utf-8; #设置编码为utf-8; #静态文件交给nginx处理 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /data/webapps; expires 30d; } #静态文件交给nginx处理 location ~ .*\.(js|css)?$ { root /data/webapps; expires 1h; } }}# 查看Nginx的进程号ps -ef|grep nginx# 停止kill -QUIT ***# 启动$NGINX_HOME/sbin/nginx -c $NGINX_HOME/conf/nginx.conf# 测试Nginx动静分离# http://node1:8088/test/index.jsp# http://node2:8088/test/index.jspmkdir -p /data/webapps/test/imgcp -r $CATALINA_HOME/webapps/test/img /data/webapps/test/# 测试Nginx动静分离# http://node1:8088/test/index.jsp# http://node2:8088/test/index.jspshutdown -h now# 快照 Nginx_Tomcat集群
服务器名称 预装软件 IP地址
Nginx服务器 Nginx1+Keepalived(Master) 192.168.6.131Nginx服务器 Nginx2+Keepalived(Backup) 192.168.6.132Web服务器1 tomcat 192.168.6.131Web服务器2 tomcat 192.168.6.132Web服务器3 tomcat 192.168.6.133# Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群
# 添加虚拟IP(VIP)
# ==================================================================node1vi /etc/sysconfig/network-scripts/ifcfg-ens33# BOOTPROTO=dhcpBOOTPROTO=static# ONBOOT=noONBOOT=yesIPADDR=192.168.6.131IPADDR1=192.168.6.141NETMASK=255.255.255.0GATEWAY=192.168.6.2DNS1=192.168.6.2systemctl restart networkip addr# ==================================================================node2vi /etc/sysconfig/network-scripts/ifcfg-ens33# BOOTPROTO=dhcpBOOTPROTO=static# ONBOOT=noONBOOT=yesIPADDR=192.168.6.132IPADDR1=192.168.6.142NETMASK=255.255.255.0GATEWAY=192.168.6.2DNS1=192.168.6.2systemctl restart networkip addr# ==================================================================安装 keepalivedyum install -y psmiscscp -r ~/keepalived-2.0.5.tar.gz node2:~/tar -zxvf ~/keepalived-2.0.5.tar.gzcd ~/keepalived-2.0.5yum -y install libnl libnl-develyum install -y libnfnetlink-devel./configure --prefix=/usr/local/keepalivedmake && make installrm -rf ~/keepalived-2.0.5.tar.gzcd ~# 环境变量# ==================================================================node1 node2vi /etc/profileexport NGINX_HOME=/usr/local/nginxexport CATALINA_BASE=/usr/local/tomcatexport CATALINA_HOME=/usr/local/tomcatexport TOMCAT_HOME=/usr/local/tomcatexport KEEPALIVED_HOME=/usr/local/keepalivedexport PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$NGINX_HOME/sbin:$CATALINA_HOME/bin:$KEEPALIVED_HOME/sbin# 使环境变量生效source /etc/profile# 查看配置结果echo $KEEPALIVED_HOME# ==================================================================node1 node2mkdir /etc/keepalived# cp $KEEPALIVED_HOME/etc/keepalived/keepalived.conf /etc/keepalived/cp ~/keepalived-2.0.5/keepalived/etc/init.d/keepalived /etc/init.d/cp $KEEPALIVED_HOME/etc/sysconfig/keepalived /etc/sysconfig/ln -s $KEEPALIVED_HOME/sbin/keepalived /usr/sbin/# ln -s $KEEPALIVED_HOME/sbin/keepalived /sbin/# 设置 keepalived 服务开机启动chkconfig keepalived on# 创建并赋予权限touch $KEEPALIVED_HOME/check_nginx.sh && chmod a+x $KEEPALIVED_HOME/check_nginx.shvi $KEEPALIVED_HOME/check_nginx.sh#!/bin/bashCOUNT=$(ps -C nginx --no-header |wc -l)echo $COUNT# 判断Nginx 是否都挂掉了if [ $COUNT -eq 0 ]then # 如果挂掉了,就启动nginx /usr/local/nginx/sbin/nginx echo "重启nginx" # 等5秒钟后,再次查看是否启动成功 sleep 5 # 如果nginx没有启动起来,就直接干掉keepalived COUNT=$(ps -C nginx --no-header |wc -l) if [ $COUNT -eq 0 ] then echo "干掉keepalived" # 如果killall命令不能使用,就需要安装psmisc工具了 # yum install -y psmisc killall keepalived fifi# ==================================================================node1vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { # 主服务器,从服务器需修改,表示运行keepalived服务器的一个标识 # 标识本节点的字条串,通常为hostname router_id node1}# 监控服务.NGINX mysql等vrrp_script chk_nginx { script "/usr/local/keepalived/check_nginx.sh" # 每2s检查一次 interval 2 # 每次检查-20 weight -20}vrrp_instance VI_1 { # 主从设置 MASTER/BACKUP state MASTER # 绑定VIP的网络接口,通过ifconfig查看自己的网络接口 interface ens33 # 虚拟路由的ID号,两个节点设置必须一样,可选IP最后一段使用,相同的VRID为一个组,他将决定多播的MAC地址 virtual_router_id 51 # 节点优先级,值范围0~254,MASTER要比BACKUP高优先级 priority 100 # 设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,两个节点必须设置一样,默认为1秒 advert_int 1 authentication { auth_type PASS # 设定授权密码,密码相同的为一个集群 auth_pass 1111 } # nginx存活状态检测脚本 track_script { chk_nginx } # 虚拟ip地址 virtual_ipaddress { 192.168.6.150 }}# ==================================================================node2vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { # 主服务器,从服务器需修改,表示运行keepalived服务器的一个标识 # 标识本节点的字条串,通常为hostname router_id node1}# 监控服务.NGINX mysql等vrrp_script chk_nginx { script "/usr/local/keepalived/check_nginx.sh" # 每2s检查一次 interval 2 # 每次检查-20 weight -20}vrrp_instance VI_1 { # 主从设置 MASTER/BACKUP state BACKUP # 绑定VIP的网络接口,通过ifconfig查看自己的网络接口 interface ens33 # 虚拟路由的ID号,两个节点设置必须一样,可选IP最后一段使用,相同的VRID为一个组,他将决定多播的MAC地址 virtual_router_id 51 # 节点优先级,值范围0~254,MASTER要比BACKUP高优先级 priority 99 # 设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,两个节点必须设置一样,默认为1秒 advert_int 1 authentication { auth_type PASS # 设定授权密码,密码相同的为一个集群 auth_pass 1111 } # nginx存活状态检测脚本 track_script { chk_nginx } # 虚拟ip地址 virtual_ipaddress { 192.168.6.150 }}# ==================================================================node1 node2# 启动服务$NGINX_HOME/sbin/nginx -c $NGINX_HOME/conf/nginx.conf$CATALINA_HOME/bin/startup.shservice keepalived start# 停止服务service keepalived stop# 重启服务service keepalived restartps -ef | grep keepalived# 测试# http://node1:8088/test/index.jsp# http://192.168.6.150:8088/test/index.jspshutdown -h now# 快照 Nginx_Keepalived_Tomcat
# ==================================================================参考
vi $NGINX_HOME/sbin/nginx.confuser nobody;# 工作进程个数,一般跟服务器cpu核数相等,或者核数的两倍worker_processes 2;# 单个进程最大连接数events{ worker_connections 1024; }server{ listen 8088; #端口号 server_name node1; #服务名}http{ # 设置默认类型为二进制流 default_type application/octet-stream; server_names_hash_bucket_size 128; #指定来自客户端请求头的headerbuffer大小,设置为32KB client_header_buffer_size 32k; #指定客户端请求中较大的消息头的缓存最大数量和大小,这里是4个32KB large_client_header_buffers 4 32k; #上传文件大小 client_max_body_size 356m; #nginx的HttpLog模块指定,指定nginx日志的输出格式,输出格式为access log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access日志存在未知 access_log /var/log/nginx/access.log access; #开启高效模式文件传输模式,将tcp_nopush和tcp_nodelay两个指另设置为on,用于防止网络阻塞。 sendfile on; tcp_nopush on; tcp_nodelay on; #设置客户端连接保持活动的超时时间 keepalive_timeout 65; server_tokens off; #客户端请求主体读取缓存 client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_send_timeout 60; proxy_read_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; #开启gzip gzip on; #允许压缩的最小字节数 gzip_min_length 1k; #4个单位为16k的内存作为压缩结果流缓存 gzip_buffers 4 16k; #设置识别HTTP协议版本,默认是1.1 gzip_http_version 1.1; #gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU gzip_comp_level 2; #压缩的类型 gzip_types text/plain application/x-javascript text/css application/xml; #让前端的缓存服务器混村经过的gzip压缩的页面 gzip_vary on; # 服务器集群 upstream tomcatcluster{ # 集群有几台服务器即可配置几台,weight表示权重,权重越大被访问到的几率越大 # 启动好Tomcat服务器 server node1:8080 weight=1; server node2:8080 weight=1; server node3:8080 weight=1; } server{ listen 8088; server_name node1; charset utf-8; #设置编码为utf-8; #location / { # root html; # index index.html index.htm; #} #location ~ .*\.(jsp|do|action)$ location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_pass http://tomcatcluster; # 真实的客户端IP proxy_set_header X-Real-IP $remote_addr; # 请求头中Host信息 proxy_set_header Host $host; # 代理路由信息,此处取IP有安全隐患 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 真实的用户访问协议 proxy_set_header X-Forwarded-Proto $scheme; } #静态文件交给nginx处理 location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { root /data/webapps; expires 30d; } #静态文件交给nginx处理 location ~ .*\.(js|css)?$ { root /data/webapps; expires 1h; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}