Tomcat+nginx+Keepalived部署反向代理、负载均衡、高可用
环境说明:
系统:Centos-7
主机:Centos-7 x3
服务器1(192.168.10.102/24)
服务器2(192.168.10.103/24)
服务器3(192.168.10.104/24)
源码安装包路径:/GZ/
安装路径:/usr/loval/
一、Tomcat安装部署:(全部服务器安装)
1、安装Tomcat,安装之前必须先安装Java,先安装java
>1、解压安装java
[root@lzc GZ]# tar zxvf jdk-8u151-linux-x64.tar.gz
>2、配置java
[root@lzc GZ]# mv jdk1.8.0_151/ /usr/local/java
[root@lzc GZ]# vim /etc/profile.d/java.sh
#写入
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
#保存退出
>3、导入环境变量,查看Java版本
[root@lzc GZ]# source/etc/profile.d/java.sh #导入环境变量
[root@lzc GZ]# java -version #查看JAVA版本
2、下载安装Tomcat(每台服务器配置Tomcat8080、Tomcat8081)
安装路径:/usr/local/Tomcat/
├── Tomcat8080
├── Tomcat8081
>1、下载解压Tomcat
[root@lzc GZ]# tarzxvf apache-tomcat-9.0.2.tar.gz
>2、Tomcat安装路径
[root@lzc GZ]# mv apache-tomcat-9.0.2 /usr/local/Tomcat/Tomcat8080
[root@lzc GZ]# mv apache-tomcat-9.0.2 /usr/local/Tomcat/Tomcat8081
>3、修改Tomcat端口号:(修改前先备份 cpserver.xml server.xml.bak)
/usr/local/Tomcat/Tomcat8081/conf
vim server.xml(把8005修改8006)
(8080修改8081)
#保存退出
>4、启动Tomcat
[root@lzc /]#/usr/local/Tomcat/Tomcat8080/bin/startup.sh
Using CATALINA_BASE: /usr/local/Tomcat/Tomcat8080
Using CATALINA_HOME: /usr/local/Tomcat/Tomcat8080
Using CATALINA_TMPDIR: /usr/local/Tomcat/Tomcat8080/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/Tomcat/Tomcat8080/bin/bootstrap.jar:/usr/local/Tomcat/Tomcat8080/bin/tomcat-juli.jar
Tomcat started.
[root@lzc /]#/usr/local/Tomcat/Tomcat8081/bin/startup.sh
Using CATALINA_BASE: /usr/local/Tomcat/Tomcat8081
Using CATALINA_HOME: /usr/local/Tomcat/Tomcat8081
Using CATALINA_TMPDIR: /usr/local/Tomcat/Tomcat8081/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/Tomcat/Tomcat8081/bin/bootstrap.jar:/usr/local/Tomcat/Tomcat8081/bin/tomcat-juli.jar
Tomcat started.
>5、查看Tomcat端口
[root@lzc /]# netstat -ntpl | egrep ":8080|8081"
tcp6 0 0 :::8080 :::* LISTEN 16907/java
tcp6 0 0 :::8081 :::* LISTEN 17430/java
>6、修改web页面为对应的服务器和端口号
路径:/usr/local/Tomcat/Tomcat8080/webapps/ROOT
[root@lzc ROOT]# pwd
/usr/local/Tomcat/Tomcat8080/webapps/ROOT
[root@lzc ROOT]# more index.jsp
<%@ page session="false"pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<center><H1> 服务器:192.168.10.102<br> Tomcat:8080</H1></center>
<hr>
[root@lzc ROOT]# pwd #复制index.jsp到Tomcat8081
cp index.jsp /usr/local/Tomcat/Tomcat8081/webapps/ROOT/
/usr/local/Tomcat/Tomcat8081/webapps/ROOT
[root@lzc ROOT]# sed -i "s/8080/8081/g"index.jsp
[root@lzc ROOT]# more index.jsp
<%@ pagesession="false" pageEncoding="UTF-8"contentType="text/html; charset=UTF-8" %>
<center><H1>服务器:192.168.10.102<br> Tomcat:8081</H1></center>
<hr>
>8、扩展内容Tomcat:
启动Tomcat:
[root@lzc]# /usr/local/Tomcat/Tomcat8080/bin/startup.sh
[root@lzc]# /usr/local/Tomcat/Tomcat8081/bin/startup.sh
关闭Tomcat:
[root@lzc]# /usr/local/Tomcat/Tomcat8080/bin/shutdown.sh
[root@lzc]# /usr/local/Tomcat/Tomcat8081/bin/shutdown.sh
或者
[root@lzc]# ps -ef | grep Tomcat8080 |grep -v grep | awk '{print $2}' | xargs kill -9
shell脚本进程守护:
创建脚本:
[root@lzc shell]# more keeptomcat.sh
#!/bin/bash
#脚本功能每一分钟检测Tomcat进程是否存在
#如Tomcat进程不存在自动重启Tomcat
#Tomcat8080
netstat -ntpl | grep ":8080"
if [ $? -ne 0 ]
then
ps-ef | grep Tomcat8080 | grep -v grep | awk '{print $2}' | xargs kill -9
sleep3
/usr/local/Tomcat/Tomcat8080/bin/startup.sh
fi
#Tomcat8081
netstat -ntpl | grep ":8081"
if [ $? -ne 0 ]
then
ps-ef | grep Tomcat8081 | grep -v grep | awk '{print $2}'
sleep5
/usr/local/Tomcat/Tomcat8081/bin/startup.sh
fi
添加到定时任务:
[root@lzc shell]# crontab -l
*/1 * * * * /usr/bin/sh/shell/keeptomcat.sh
2、Nginx安装部署:
1、 安装pcre
注:安装这个pcre库是为了让nginx支持HTTPRewrite模块
创建一个专用的软件工具目录(实际生产环境中一定要养成好的规范习惯)
下载pcre软件(Centos 6版本下载)
[root@lzc GZ]# wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
编译安装
[root@lzc GZ]# tar -xvfpcre-8.38.tar.gz
[root@lzc GZ]# mv pcre-8.38 /usr/local/
[root@lzc GZ]# cd /usr/local/pcre-8.38
[root@lzc pcre-8.38]# ./configure
[root@lzc pcre-8.38]# make && make install
2、安装配置Nginx
>1、源码编译安装nginx
[root@lzc]# useradd nginx -s/sbin/nologin -M
[root@lzc GZ]# wgethttp://nginx.org/download/nginx-1.10.1.tar.gz
[root@lzc GZ]# tar zxfnginx-1.10.1.tar.gz
[root@lzc GZ]# cd nginx-1.14.2/
[root@lzc GZ]# ./configure --user=nginx--group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/usr/local/pcre-8.38--without-http_gzip_module
[root@lzc GZ]# make && makeinstall
>2、配置启动nginx
[root@lzcGZ]# cd /usr/local/nginx/
[root@lzcnginx]# tree
├── conf #nginx配置文件
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html #web项目代码
│ ├── 50x.html
│ └── index.html
├── logs #nginx日志
└── sbin #nginx启动文件
└── nginx
[root@lzc nginx]# ./sbin/nginx #启动nginx
[root@lzc nginx]# ps -ef | grep nginx #查看nginx进程
root 37276 1 0 15:20 ? 00:00:00 nginx: master process./sbin/ngin
nginx 37277 37276 0 15:20 ? 00:00:00 nginx: worker process
root 37327 15908 0 15:22 pts/0 00:00:00 grep --color=auto nginx
[root@lzcnginx]# netstat -ntpl | grep ":80" #查看nginx端口号
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 37276/nginx: master
>3、配置nginx反向代理
[root@lzc conf]# vim/usr/local/nginx/conf/nginx.conf #添加以下内容
#keepalive_timeout 0;
keepalive_timeout 65;
###############################################
upstream tomcat.lzc.com { #定义主机池
server 192.168.10.102:8080weight=1;#按权重的方式进行轮询、数值越大
server 192.168.10.102:8081weight=1;
server 192.168.10.103:8080weight=1;
server 192.168.10.103:8081weight=1;
server 192.168.10.104:8080weight=1;
server 192.168.10.104:8081weight=1;
}
###############################################
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.jsp index.html index.htm;
}
#保存退出
[root@lzc nginx]# ps -ef | grep nginx |grep -v grep | awk '{print $2}' | xargs kill -9 #杀死nginx进程
[root@lzc nginx]# ./sbin/nginx #启动nginx
Web页面正常访问到Tomcat项目
>4、扩展内容
关闭nginx:
[root@lzc nginx]# ps -ef | grep nginx |grep -v grep | awk '{print $2}' | xars kill -9
启动nginx:
[root@lzc /]#/usr/local/nginx/sbin/nginx
shell脚本进程守护:创建脚本
[root@lzcshell]# more keepNginx.sh
#!/bin/bash
#脚本功能检测nginx进程是否存在
#如nginx进程不存在自动重启nginx
netstat -ntpl | grep ":80" |grep nginx
if [ $? -ne 0 ]
then
ps-ef | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9
sleep3
cd/
/usr/local/nginx/sbin/nginx
fi
添加到定时任务:每分钟执行一次
[root@lzcshell]# crontab -l
*/1* * * * /usr/bin/sh/shell/keeptomcat.sh &>/dev/null2>&1
*/1 * * * * /usr/bin/sh /shell/keepNginx.sh &>/dev/null 2>&1
3、Keepalived安装部署:
1、环境配置
2、yum安装keepablived
>1、环境说明:
>2、yum安装配置
[root@lzc /]# yum install openssl-develpopt* -y
[root@lzc /]# yum install -y keepalived
修改配置文件(主)
[root@lzc /]# tree /etc/keepalived/
/etc/keepalived/
├── keepalived.conf
[root@lzc /]# vim/etc/keepalived/keepalived.conf
修改配置文件(备)
[root@lzc /]# tree /etc/keepalived/
/etc/keepalived/
├── keepalived.conf
[root@lzc /]# vim/etc/keepalived/keepalived.conf
#保存退出
>3、启动keepalived
[root@lzc /]# systemctlenable keepalived.service #加入启动项
[root@lzc /]# systemctlstart keepalived.service #启动keepalived
[root@lzc /]# systemctl restartkeepalived.service#重启keepalived
[root@lzc /]# systemctl stopkeepalived.service #停止keepalived
>4、验证和测试
#正常ping
模拟故障手动stop服务器1(192.168.10.102)的keepalived服务
[root@lzc /]# systemctl stopkeepalived
成功切换到服务器3(192.168.10.104)
由于服务器2服务器3优先级都是99,两者同等的优先级。