使用tomcat+nginx+keepalived搭建高可用集群
Keepalived-2.2.7.tar.gz
下载地址:https://www.keepalived.org/download.html
Apache-Tomcat8.5.70.tar.gz
下载地址:https://tomcat.apache.org/download-80.cgi
Nginx-1.22.0.tar.gz
下载地址:http://nginx.org/download
jdk-8u291-linux-x64.tar.gz
下载地址:
https://www.oracle.com/java/technologies/javse/javase8u211-later-archive-downloads.html
yum groupinstall "Development Tools"
yum-y install openssl-devel libnl libnl-devel
四台centos7.9虚拟机(两台安装nginx+keepalived,两台安装tomcat)
nginx+keepalived(主) 192.168.205.150
nginx+keepalived(备) 192.168.205.151
Tomcat(1) 192.168.205.152
Tomcat(2) 192.168.205.153
Nginxweb ui模块nginx-module-vts
下载地址:https://github.com/vozlt/nginx-module-vts/releases/tag/v0.2.1
负载均衡模块nginx-goodies-nginx-sticky
下载地址:
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/
mkdir -p /usr/java/jvm
vim /etc/profile
exportJAVA_HOME=/usr/java/jvm/jdk1.8.0_291
exportJRE_HOME=${JAVA_HOME}/jre
exportCLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
exportPATH=${JAVA_HOME}/bin:$PATH
更新:source/etc/profile
javac -version
mkdir -p /usr/java/tomcat
tar -zxf apache-tomcat-8.5.70.tar.gz -C/usr/java/tomcat
注意:以下操作都在/usr/ava/tomcat/apache-tomcat-8.5.70里执行
mkdir web-template
cp-rp web-template web-8080(配置默认)
cp-rp web-template web-8081(配置里面修改端口号)
(8081配置
原本的8005改为9005
原本的8080改为8081
cd /usr/java/tomcat/apache-tomcat-8.5.70/web-8080/webapps
mkdir testweb
cd testweb
vim /indes.jsp
<%@page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
http://www.w3.org/TR/html4/loose.dtd">
192.168.205.152
vim/etc/tomcat_8080.sh
RETVAL=$?
#tomcat instance directory
export CATALINA_HOME="/usr/java/tomcat/apache-tomcat-8.5.70"
#tomcat installation directory, change to your own
export CATALINA_BASE="$CATALINA_HOME/web-8080"
#Optional
exportJVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m-XX:MaxPermSize=512m"
case"$1" in
start)
if[ -f $CATALINA_HOME/bin/startup.sh ];then
echo$"Start Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if[ -f $CATALINA_HOME/bin/shutdown.sh ];then
echo$"Stop Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo$"Usage:$0 {start|stop}"
exit1
;;
esac
exit$RETVAL
vim /etc/tomcat_8081.sh
RETVAL=$?
#tomcat instance directory
export CATALINA_HOME="/usr/java/tomcat/apache-tomcat-8.5.70"
#tomcat installation directory, change to your own
export CATALINA_BASE="$CATALINA_HOME/web-8081"
#Optional
exportJVM_OPTIONS="-Xms128m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m"
case"$1" in
start)
if[ -f $CATALINA_HOME/bin/startup.sh ];then
echo$"Start Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if[ -f $CATALINA_HOME/bin/shutdown.sh ];then
echo$"Stop Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
;;
*)
echo$"Usage:$0 {start|stop}"
exit1
;;
esac
exit$RETVAL
赋予权限
chmod +x tomcat_8080.sh
chmod +x tomcat_8081.sh
执行
./tomcat_8080.sh start
./tomcat_8081.sh start
如要停止请执行:./tomcat_8080.shstop
192.168.205.152:8080
192.168.205.153:8080
静态页面
192.168.205.152:8080/testweb/index.jsp
192.168.205.153:8080/testweb/index.jsp
yum groupinstall "Development Tools"(安装之前先打下面两条命令不然安不上)
yum groups mark install "DevelopmentTools"
yum groups mark convert "DevelopmentTools"
yum-y install openssl-devel libnl libnl-devel
groupaddnginx && useradd -s /sbin/nologin -g nginx nginx
tar-zxf nginx-1.22.0.tar.gz && cd nginx-1.22.0
Nginxweb ui模块nginx-module-vts
负载均衡模块nginx-goodies-nginx-sticky
这两个模块放在nginx父目录(环境准备里有下载地址)
./configure\
--prefix=/usr/local/nginx\
--user=nginx--group=nginx \
--with-http_stub_status_module\
--with-http_ssl_module\
--with-http_realip_module\
--add-module=../nginx-module-vts\
--add-module=../nginx-sticky-module-ng
执行完
make -j8 && make install
vim /usr/local/nginx/conf/nginx.conf
添加: include/usr/local/nginx/conf/conf.d/*.conf;
vim /usr/local/nginx/conf/conf.d/tomcat.conf
set_real_ip_from 192.168.205.150/32;
set_real_ip_from 192.168.205.151/32;
real_ip_header X-Forwarded-For;
real_ip_recursiveon;
vhost_traffic_status_zone;
upstreamtomcat{
sticky;
server 192.168.205.152:8080;
server 192.168.205.153:8081;
server 192.168.205.152:8080;
server 192.168.205.153:8081;
}
server{
listen 8090;
server_name localhost;
location / {
proxy_pass http://tomcat;
root html;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_formathtml;
}
error_log /var/nginx/logs/error.log error;
access_log /var/nginx/logs/access.log main;
}
/usr/local/nginx/sbin/nginx -t //检测配置语法
/usr/local/nginx/sbin/nginx -s reload //重启nginx
/usr/local/nginx/sbin/nginx -s stop //停止nginx
/usr/local/nginx/sbin/nginx //启动nginx
./configure--prefix=/usr/local/keepalived
make-j8 && make install
mkdir -p /etc/keepalived
二进制启动文件: cp keepalived/etc/init.d/keepalived /etc/init.d/
配置文件: cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
keepalived系统配置文件:cpkeepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
keepalived指令: cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
创建脚本目录
mkdir -p /opt/nginx_check
cat>> /opt/nginx_check/nginx_check.sh << EOF
#!/bin/bash
A=`ps-C nginx --no-header |wc -l`
if[ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0];then
killall keepalived
fi
fi
EOF
执行权限
chmod755 /opt/nginx_check/nginx_check.sh
192.168.205.150(主) vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
vrrp_skip_check_adv_addr
!vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.205.159
}
}
192.168.205.151(备) vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL2
vrrp_skip_check_adv_addr
!vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.205.159
}
}
systemctl start keepalived && systemctl enable keepalived
把192.168.205.150 关闭
可以看到已经飘到192.168.205.151
192.168.205.150测试
192.168.205.159:8090
访问静态页面
http://192.168.205.159:8090/testweb/index.jsp
把192.168.205.150宕机
可以看到已经漂移到192.168.205.151
测试前把浏览器缓存清理一下
Shift+ctrl+delete 清理缓存
192.168.205.151测试
访问静态网页
http://192.168.205.159:8090/testweb/index.jsp