hadoop生态搭建(3节点)-15.Nginx_Keepalived_Tomcat配置
# Nginx+Tomcat搭建高可用
服务器名称 预装软件 IP地址
Nginx服务器 Nginx1 192.168.6.131
Nginx服务器 Nginx2 192.168.6.132
# ==================================================================node1 node2
yum -y install gcc gcc-c++ autoconf # http://www.pcre.org/ # ==================================================================安装 Pcre scp -r ~/pcre2-10.32.tar.gz node2:~/ tar zxf ~/pcre2-10.32.tar.gz cd pcre2-10.32 ./configure --prefix=/usr/local/pcre && make && make install rm -rf ~/pcre2-10.32.tar.gz rm -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/ # ==================================================================安装 zlib scp -r ~/zlib-1.2.11.tar.gz node2:~/ tar -zxvf ~/zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure --prefix=/usr/local/zlib && make && make install rm -rf ~/zlib-1.2.11.tar.gz rm -rf ~/zlib-1.2.11 # http://nginx.org/download/ # ==================================================================安装 nginx scp -r ~/nginx-1.14.0.tar.gz node2:~/ tar -zxvf ~/nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --prefix=/usr/local/nginx && make && make install rm -rf ~/nginx-1.14.0.tar.gz rm -rf ~/nginx-1.14.0 # 环境变量 vi /etc/profile # 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加 export NGINX_HOME=/usr/local/nginx export 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/ # ==================================================================安装 tomcat tar -zxvf ~/apache-tomcat-8.5.31.tar.gz -C /usr/local mv /usr/local/apache-tomcat-8.5.31 /usr/local/tomcat rm -rf ~/apache-tomcat-8.5.31.tar.gz
# 环境变量
# ==================================================================node1 node2 node3
vi /etc/profile # 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加 export JAVA_HOME=/usr/java/jdk1.8.0_111 export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.12 export HADOOP_HOME=/usr/local/hadoop-2.7.6 export MYSQL_HOME=/usr/local/mysql export HBASE_HOME=/usr/local/hbase-1.2.4 export HIVE_HOME=/usr/local/hive-2.1.1 export SCALA_HOME=/usr/local/scala-2.12.4 export KAFKA_HOME=/usr/local/kafka_2.12-0.10.2.1 export FLUME_HOME=/usr/local/flume-1.8.0 export SPARK_HOME=/usr/local/spark-2.3.0 export STORM_HOME=/usr/local/storm-1.1.0 export ERLANG_HOME=/usr/local/erlang export RABBITMQ_HOME=/usr/local/rabbitmq_server-3.7.5 export MONGODB_HOME=/usr/local/mongodb-3.4.5 export REDIS_HOME=/usr/local/redis-4.0.10 export RUBY_HOME=/usr/local/ruby-2.5.1 export NGINX_HOME=/usr/local/nginx export CATALINA_BASE=/usr/local/tomcat export CATALINA_HOME=/usr/local/tomcat export TOMCAT_HOME=/usr/local/tomcat export 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/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export HADOOP_INSTALL=$HADOOP_HOME export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
# ==================================================================node1
# 使环境变量生效 source /etc/profile # 查看配置结果 echo $CATALINA_BASE echo $CATALINA_HOME echo $TOMCAT_HOME scp -r $CATALINA_HOME node2:/usr/local/ scp -r $CATALINA_HOME node3:/usr/local/
# ==================================================================node2 node3
# 使环境变量生效 source /etc/profile # 查看配置结果 echo $CATALINA_BASE echo $CATALINA_HOME echo $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.131
Nginx服务器 Nginx2 192.168.6.132
Web服务器1 tomcat 192.168.6.131
Web服务器2 tomcat 192.168.6.132
Web服务器3 tomcat 192.168.6.133
# ==================================================================node1
mkdir $CATALINA_HOME/webapps/test # 编写测试静态页 vi /usr/local/tomcat/webapps/test/login.html <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> </head> <body> <h1>您正在访问:node1</h1> <h1>login.html</h1> </body> </html>
# ==================================================================node2
mkdir /usr/local/tomcat/webapps/test # 编写测试静态页 vi /usr/local/tomcat/webapps/test/login.html <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> </head> <body> <h1>您正在访问:node2</h1> <h1>login.html</h1> </body> </html>
# ==================================================================node3
mkdir /usr/local/tomcat/webapps/test # 编写测试静态页 vi /usr/local/tomcat/webapps/test/login.html <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> </head> <body> <h1>您正在访问:node3</h1> <h1>login.html</h1> </body> </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.bak vi $NGINX_HOME/conf/nginx.conf user 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.bak vi $NGINX_HOME/conf/nginx.conf user 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 node3
mkdir -p $CATALINA_HOME/webapps/test/img
# 上传 testimg.jpg
# ==================================================================node1 cp ~/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"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Nginx动静分离测试</title> </head> <body> <h1>您正在访问:node1</h1> <img src="/test/img/testimg.jpg" alt="测试" /> </body> </html> 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"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Nginx动静分离测试</title> </head> <body> <h1>您正在访问:node2</h1> <img src="/test/img/testimg.jpg" alt="测试" /> </body> </html> # ==================================================================node3 # 编写测试动态页 vi /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Nginx动静分离测试</title> </head> <body> <h1>您正在访问:node3</h1> <img src="/test/img/testimg.jpg" alt="测试" /> </body> </html> # 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.conf http{ 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.jsp mkdir -p /data/webapps/test/img cp -r $CATALINA_HOME/webapps/test/img /data/webapps/test/ # 测试Nginx动静分离 # http://node1:8088/test/index.jsp # http://node2:8088/test/index.jsp shutdown -h now # 快照 Nginx_Tomcat集群
服务器名称 预装软件 IP地址
Nginx服务器 Nginx1+Keepalived(Master) 192.168.6.131
Nginx服务器 Nginx2+Keepalived(Backup) 192.168.6.132
Web服务器1 tomcat 192.168.6.131
Web服务器2 tomcat 192.168.6.132
Web服务器3 tomcat 192.168.6.133
# Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群
# 添加虚拟IP(VIP)
# ==================================================================node1 vi /etc/sysconfig/network-scripts/ifcfg-ens33 # BOOTPROTO=dhcp BOOTPROTO=static # ONBOOT=no ONBOOT=yes IPADDR=192.168.6.131 IPADDR1=192.168.6.141 NETMASK=255.255.255.0 GATEWAY=192.168.6.2 DNS1=192.168.6.2 systemctl restart network ip addr # ==================================================================node2 vi /etc/sysconfig/network-scripts/ifcfg-ens33 # BOOTPROTO=dhcp BOOTPROTO=static # ONBOOT=no ONBOOT=yes IPADDR=192.168.6.132 IPADDR1=192.168.6.142 NETMASK=255.255.255.0 GATEWAY=192.168.6.2 DNS1=192.168.6.2 systemctl restart network ip addr # ==================================================================安装 keepalived yum install -y psmisc scp -r ~/keepalived-2.0.5.tar.gz node2:~/ tar -zxvf ~/keepalived-2.0.5.tar.gz cd ~/keepalived-2.0.5 yum -y install libnl libnl-devel yum install -y libnfnetlink-devel ./configure --prefix=/usr/local/keepalived make && make install rm -rf ~/keepalived-2.0.5.tar.gz cd ~ # 环境变量 # ==================================================================node1 node2 vi /etc/profile export NGINX_HOME=/usr/local/nginx export CATALINA_BASE=/usr/local/tomcat export CATALINA_HOME=/usr/local/tomcat export TOMCAT_HOME=/usr/local/tomcat export KEEPALIVED_HOME=/usr/local/keepalived export 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 node2 mkdir /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.sh vi $KEEPALIVED_HOME/check_nginx.sh #!/bin/bash COUNT=$(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 fi fi # ==================================================================node1 vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_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 } } # ==================================================================node2 vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_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.sh service keepalived start # 停止服务 service keepalived stop # 重启服务 service keepalived restart ps -ef | grep keepalived # 测试 # http://node1:8088/test/index.jsp # http://192.168.6.150:8088/test/index.jsp shutdown -h now # 快照 Nginx_Keepalived_Tomcat
# ==================================================================参考
vi $NGINX_HOME/sbin/nginx.conf user 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; } } }