Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建详解架构师

Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建

  • Nginx 是一个高性能的 HTTP反向代理服务器

  • Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免服务的单点故障

  • Tomcat 是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器。

所需下载:

jdk-8u151-linux-x64.rpm 
apache-tomcat-7.0.52.tar.gz 
http://nginx.org/download/nginx-1.12.2.tar.gz 
http://www.keepalived.org/software/keepalived-1.3.2.tar.gz
服务器准备和规划: 
         IP地址               部署服务                                  主从关系 
         192.168.153.128     Nginx+keepalived                         Maser 
         192.168.153.129     Nginx+keepalived                         Slave 
         192.168.153.130     Java1.8+tomcat7[8080+ tomcat8090]          - 

Nginx的高可用负载均衡架构

Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建详解架构师

(1)用户通过域名请求到DNS,由DNS解析域名后返回对应的IP地址,该IP及为Keepalived映射服务器的虚拟IP

(2)通过该虚拟IP访问到对应的负载均衡器(Nginx),这里Nginx部署两个,然后通过Keepalived来保证NG的高可用,正常情况下由Keepalived-M将虚拟IP映射转发至Nginx-M,如果Nginx-M出现故障,此时Keepalived会切换至Keepalived-S开始工作,从而保证了NG的单点故障问题。

(3)通过Nginx负载均衡器,将请求路由到对应的Tomcat服务。

搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构

1.安装需要的依赖包

yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel 

2.安装Nginx服务:  

[[email protected] opt]# tar -zxvf nginx-1.12.2.tar.gz 
[[email protected] opt]# cd nginx-1.12.2 
[[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
[[email protected] nginx-1.12.2]# make && make install

3.安装keepalived:

[[email protected] opt]# tar -zxvf keepalived-1.3.2.tar.gz  
[[email protected] opt]# cd keepalived-1.3.2/ 
[[email protected] keepalived-1.3.2]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc/ 
[[email protected] keepalived-1.3.2]# make && make install

4.配置Nginx服务:

worker_processes  1; 
events { 
    worker_connections  1024; 
} 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
    sendfile        on; 
    keepalive_timeout  65; 
 
     
    server { 
        listen       80; 
        server_name  localhost; 
 
        location / { 
            root   html; 
            index  index.html index.htm; 
            proxy_connect_timeout 3; 
            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; 
        } 
    } 
    upstream localhost { 
       server 192.168.153.130:8080 weight=2; 
       #server 192.168.153.130:8090 weight=2; 
    } 
 
}

检查语法并启动Nginx服务:

[[email protected] ~]# /usr/local/nginx/sbin/nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 
[[email protected] ~]# /usr/local/nginx/sbin/nginx 

Keepalived服务配置

1.主Keepalived配置(192.168.153.128)

修改keepalived配置文件,vim /etc/keepalived/keepalived.conf

#! Configuration File for keepalived 
#全局配置 
global_defs { 
   #keepalived切换的时候,发消息到指定的email,可配置多个email 
   notification_email { 
     [email protected] 
   } 
    
   #通知邮件从哪个地址发出 
   notification_email_from [email protected] 
   #通知邮件的smtp地址 
   smtp_server smtp.exmail.qq.com 
   #连接smtp服务器的超时时间,单位秒 
   smtp_connect_timeout 30 
   #Keepalived的机器标识,一个网络内保持唯一 
   router_id nginx-master 
} 
#执行脚本配置 
vrrp_script chk_nginx { 
#脚本所在路径 
    script "/usr/local/keepalived/etc/check_nginx.sh" 
#脚本执行间隔时间,秒 
    interval 2 
#优先级 
    weight 2 
} 
#keepalived实例配置 
 
vrrp_instance VI_1 { 
#指定实例的初始状态,MASTER或BACKUP两种状态,并且需要大写 
    state MASTER 
#实例绑定的网卡 
    interface eth0 
#虚拟路由标识,是一个数字,整个VRRP内唯一,如果keepalived配置了主备,需要相同 
    virtual_router_id 51 
#优先级,数值愈大,优先级越高 
    priority 100 
#MASTER与BACKUP之间同步检查的时间间隔,单位为秒 
    advert_int 1 
#通信验证 
    authentication { 
        auth_type PASS 
        auth_pass 123456 
    }     
#追踪外围脚本 
    track_script { 
    #这里配置vrrp_script的名称 
        chk_nginx 
    }     
     
#虚拟ip配置,可配置多个 
    virtual_ipaddress { 
        192.168.153.200 
    } 
}

Keepalived备服务配置(192.168.153.129):

修改keepalived配置文件,vim /etc/keepalived/keepalived.conf

#! Configuration File for keepalived 
#全局配置 
global_defs { 
   #keepalived切换的时候,发消息到指定的email,可配置多个email 
   notification_email { 
     [email protected] 
   }  
   #通知邮件从哪个地址发出 
   notification_email_from [email protected] 
   #通知邮件的smtp地址 
   smtp_server smtp.exmail.qq.com 
   #连接smtp服务器的超时时间,单位秒 
   smtp_connect_timeout 30 
   #Keepalived的机器标识,一个网络内保持唯一 
   router_id nginx-master 
} 
#执行脚本配置 
vrrp_script chk_nginx { 
#脚本所在路径 
    script "/usr/local/keepalived/etc/check_nginx.sh" 
#脚本执行间隔时间,秒 
    interval 2 
#优先级 
    weight 2 
} 
 
#keepalived实例配置 
vrrp_instance VI_1 { 
#指定实例的初始状态,MASTER或BACKUP两种状态,并且需要大写 
    state MASTER 
#实例绑定的网卡 
    interface eth0 
#虚拟路由标识,是一个数字,整个VRRP内唯一,如果keepalived配置了主备,需要相同 
    virtual_router_id 51 
#优先级,数值愈大,优先级越高 
    priority 99 
#MASTER与BACKUP之间同步检查的时间间隔,单位为秒 
    advert_int 1 
#通信验证 
    authentication { 
        auth_type PASS 
        auth_pass 123456 
    } 
     
#追踪外围脚本 
    track_script { 
    #这里配置vrrp_script的名称 
        chk_nginx 
    } 
     
     
#虚拟ip配置,可配置多个 
    virtual_ipaddress { 
        192.168.153.200 
    } 
} 

Nginx状态检查脚本创建
新建Nginx的状态检查脚本:check_nginx.sh

vim /usr/local/keepalived/etc/check_nginx.sh 
#!/bin/sh 
NGINX=/usr/local/nginx/sbin/nginx 
PORT=80 
nmap localhost -p $PORT | grep "$PORT/tcp open" 
#echo $? 
if [ $? -ne 0 ];then 
    $NGINX -s stop 
    #这里再次尝试启动NG 
    $NGINX 
    sleep 5 
    nmap localhost -p $PORT | grep "$PORT/tcp open" 
    [ $? -ne 0 ] && cd /usr/local/keepalived/sbin && pkill keepalived 
    echo "stoped" 
fi

启动keepalive:

[[email protected] ~]#ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 
[[email protected] ~]# keepalived -D -f /etc/keepalived/keepalived.conf 
[[email protected] ~]#tail -100f /var/log/messages

安装Tomcat并启动(在192.168.153.130上面安装两个tomcat,端口分别为8080、8090即可.)

运行测试

1)为了更直观的查看到keepalived切换的效果,将153.128服务器中的nginx的upstream服务只配置130:8080的tomcat服务地址,153.129服务器中的upstream服务只配置130的tomcat8090服务地址,这样只需要观察将153.128服务器中的nginx关闭看使用虚拟ip是否可以访问到130服务器的tomcat8090即可。

2)分别启动两个服务器中的tomcat、nginx、keepalived,访问虚拟ip:192.168.153.200,可以查看到访问的是主keepalived服务器的tomcat

Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建详解架构师

3)关闭153.128服务器的nginx,nginx -s stop,再次访问虚拟ip,如下:说明主keepalived通过配置的脚本检测到了本服务的nginx服务挂掉了,所以立马切换至了备的keepalived,这时153.129服务器的keepalived升为了主,所以就访问到了130服务器的tomcat8090端口。

 Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建详解架构师

 

至此keepalived+Nginx主从服务就搭建和测试完.

参考文档:

http://mp.weixin.qq.com/s?__biz=MzA5ODM5MDU3MA==&mid=2650863435&idx=2&sn=46b01634b178d5d990232ce6d9a6c62b&chksm=8b66100ebc119918cd4c8d66a6e7dd051f8c0b858c40f0bc40c546beeeae570b7e04a53858c2&mpshare=1&scene=23&srcid=1219GWsSmQ4rgyrDPvQduodk#rd

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/architecture/6887.html

(0)
上一篇 2021年7月17日 01:43
下一篇 2021年7月17日 01:43

相关推荐

发表回复

登录后才能评论