Keepalived+Nginx+Tomcat高可用(主从切换)以及灾难恢

首先看下整体的思路

这里写图片描述


解释说明:由于本示例不是为了做业务的集群部署,所以每台ngix都是直接到一台具体的tomcat上,所以这两个tomcat都是独立的并不是集群,而在实际上tomcat应该是借助第三方的rpc框架的业务集群。本文主要是介绍keepalived和nginx的搭建以及灾难恢复。

nginx的安装

这个参考这篇文章,在本文中只是用到了nginx的代理功能:

server { listen 80; server_name mini03; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; # } location / { root html; #index index.html index.htm; proxy_pass http://mini03:8080;#拦截所有请求转向tomcat } } keepalived的安装和使用

下载keepalived官网:

将keepalived解压到/usr/local/src目录下
tar -zxvf keepalived-1.2.19.tar.gz -C /usr/local/src

进入到/usr/local/src/keepalived-1.2.19目录
cd /usr/local/src/keepalived-1.2.19

开始configure
./configure –prefix=/usr/local/keepalived

编译并安装

make && make install

将keepalived添加到系统服务中并开机启动 cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir -p /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf chmod +x /etc/init.d/keepalived chkconfig --add keepalived chkconfig keepalived on 配置keepalived虚拟IP、心跳检查、以及灾难恢复

解释:当然这里的恢复值得是在主机不宕机的情况下进行nginx和tomcat的恢复,就向上面,keepalived加到开机自启动后,nginx和tomcat也就能自动恢复了。
修改配置文件:/etc/keepalived/keepalived.conf
Master的:

global_defs { # notification_email { # zhouxiao@example.com # itsection@example.com # } # notification_email_from itsection@example.com # smtp_server mail.example.com # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.64.100/24 } track_script { chk_nginx } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault" }

backup:

global_defs { # notification_email { # zhouxiao@example.com # itsection@example.com # } # notification_email_from itsection@example.com # smtp_server mail.example.com # smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight -5 fall 3 rise 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 98 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.64.100/24 } track_script { chk_nginx } notify_master "/usr/local/keepalived/sbin/notify.sh master" notify_backup "/usr/local/keepalived/sbin/notify.sh backup" notify_fault "/usr/local/keepalived/sbin/notify.sh fault" }

至于keepalvied中每个字段的含义自行找相应的文档看看,很简单的。
看下心跳检测脚本,也就是主从切换的:
/etc/keepalived/check_nginx.sh:

#!/bin/bash # curl -IL # curl --data "memberName=fengkan&password=22" count=0 for (( k=0; k<2; k++ )) do check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" -o /dev/null ) if [ "$check_code" -ne "200" ]; then count=$(expr $count + 1) sleep 3 continue else count=0 break fi done if [ "$count" != "0" ]; then # /etc/init.d/keepalived stop exit 1 else exit 0 fi

可以看到这里我很取巧的用了访问tomcat的默认的路径,能访问证明,都是好的,不能访问,我重启tomcat和nginx。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/09ff0a12a2160848ece255fc62e7a08a.html