简单介绍
tomcat是一轻量级的应用服务器,可以运行java程序。
Nginx是一个高性能的HTTP反向代理服务器。
keepalive用来搭建高可用,来避免服务的单点故障
架构的简单描述
用户通过域名请求到DNS服务器上,解析以后返回对应的IP地址,而这个IP地址是一个LVS,是使用keepalived映射的虚拟IP,映射到相应的Nginx的服务器上。
通过使用keepalived来使nginx达到高可用,然后nginx服务器来完成负载均衡,将请求路由到对应的Tomcat服务上。
搭建过程
两台服务器的IP分别为
192.168.128.111
192.168.128.129
首先我们需要在两台主机上安装相关的依赖(首先需要配置好yum源)
yum install gcc gcc-c++ automake pcre-devel openssl-devel -y
然后在两台主机上安转tomcat
tar -zxvf apache-tomcat-8.5.24.tar.gz
随后进行源码的nginx的安装
tar -zxvf nginx-1.13.8.tar.gz
解压
进入解压目录,然后编译
./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx
make && make install
随后是keepalive的安装
解压安装包
tar -zxvf keepalived-1.3.9.tar.gz
然后编译
./configure --prefix=/usr/local/keepalived --sysconf=/etc
make && make install
安装好必要的软件以后我们需要进行相应的配置
编辑两台nginx主机的配置文件
vim /usr/local/nginx/conf/nginx.conf
配置如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream localhost{
server 192.168.128.111:8080 weight=2;
server 192.168.128.129:8080 weight=2;
#使用upstream模块来达到负载均衡,并且转到tomcat的8080端口上
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://localhost;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
随后为keepalived的配置
keepalived的配置文件在/etc/keepalived/keepalived.conf
主keepalive的配置文件如下
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx {
script "/home/project/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #备用keepalived只需要将MASTER改为BACKUP即可
interface enp0s25
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.128.200
}
}
然后编写nginx状态检查的脚本
编写如下
#!/bin/bash
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne 0 ];then
$NGINX -s stop
$NGINX
sleep 5
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne 0 ] && cd /usr/common/keepalived/sbin
echo "stoped"
fi
然后启动keepalived、nginx、tomcat即可
tomcat注意需要配置jdk环境