keepalived+nginx(Tomcat)实现双机热备

双机热备

双机热备并不是真正意义上的集群模式,但是相比于单机模式优势是可以实现高可用,在主机出现故障的状态下,可以快速的切换到备机,备机替代主机的工作,保证服务可用。这里主要介绍基于KeepAlive实现的双机热备(本质是一种负载均衡的软实现)同时做好监控报警,在自动故障转移的同时能通知到相关的应用负责人检查相关应用,排查隐患。

服务器准备

  • 虚拟机Vmware workstation

  • CentOS-7-x86_64-Minimal-1804.iso 下载地址

  • 主备节点

    资源 节点 IP 备注
    CentOS 7 node1 192.168.17.131 主机
    CentOS 7 node2 192.168.17.132 备机

环境准备

  • 基本工具安装

     yum install openssl-devel libnl3-devel ipset-devel iptables-devel libnfnetlink-devel wget lsof
    
  • 安装JDK

     yum install -y java-1.8.0-openjdk
    
  • 下载tomcat

    wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.35/bin/apache-tomcat-8.0.35.tar.gz
    

    拷贝到node2节点当前目录

    scp apache-tomcat-8.0.35.tar.gz root@192.168.17.131:`pwd`
    

    分别在node1,node2节点解压tomcat

    tar -zxvf apache-tomcat-8.0.35.tar.gz
    

    修改tomcat文件,文件路径/home/tomcat/apache-tomcat-8.0.35/webapps/ROOT

    • 将node1中ROOT项目index.jsp添加标签

      node1

    • 将node1中ROOT项目index.jsp添加标签

      node2

  • 安装Nginx

    添加yum源

    sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    

    安装

    yum install -y nginx
    

配置keepalived

安装keepalive,两台节点都需要安装

yum install -y keepalived

虚拟IP / VIP 192.168.17.133

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

  • 节点 node1 master

    ! Configuration File for keepalived
    vrrp_script check_nginx_alive {
        script "/etc/keepalived/check_nginx_alive.sh"
        interval 2
        weight -10
    }
    global_defs {
       notification_email {
         xiangxc@csizg.com
       }
       #notification_email_from Alexandre.Cassen@firewall.loc
       #smtp_server 127.0.0.1
       #smtp_connect_timeout 30
       router_id LVS_DEVEL_31
       #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
        }
       track_interface {
            ens33
       }
       track_script {
            check_nginx_alive
       }
       virtual_ipaddress {
            192.168.17.133
       }
    }
    
    virtual_server 192.168.17.133 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        #persistence_timeout 50
        protocol TCP
    
        real_server 192.168.17.131 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                retry 3
                delay_before_retry 3
                connect_port 80
            }
        }
    }
    
  • 节点 node2 backup

    ! Configuration File for keepalived
    vrrp_script check_nginx_alive {
        script "/etc/keepalived/check_nginx_alive.sh"
        interval 2
        weight -10
    }
    global_defs {
       notification_email {
         xiangxc@csizg.com
       }
    
       #smtp_server 127.0.0.1
       #smtp_connect_timeout 30
       router_id LVS_DEVEL_32
       #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 50
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
       track_interface {
            ens33
       }
       virtual_ipaddress {
            192.168.17.133
       }
       track_script {
            check_nginx_alive
       }
    }
    
    virtual_server 192.168.17.133 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        #persistence_timeout 50
        protocol TCP
    
        real_server 192.168.17.132 80 {
            weight 1
            TCP_CHECK {
                connect_timeout 3
                nb_get_retry 3
                retry 3
                delay_before_retry 3
                connect_port 80
            }
        }
    }
    
  • 配置脚本文件

    编辑Nginx监控脚本

    vim /etc/keepalived/check_nginx_alive.sh
    

    脚本内容

    #!/bin/bash
    if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
            then
            service keepalived stop
            service nginx start
            sleep 5
            if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" != "" ]
            then
            service keepalived start
            fi
    fi
    

    授予执行权限

    chmod +x /etc/keepalived/check_nginx_alive.sh
    

启动服务

service nginx restart && service keepalived restart

服务验证

  • 正常状况
    节点1

keepalived+nginx(Tomcat)实现双机热备

节点2
keepalived+nginx(Tomcat)实现双机热备

虚拟ip
keepalived+nginx(Tomcat)实现双机热备

  • 异常状况

    关闭节点1的服务,关闭节点1的keepalived(此处可以利用脚本实现),访问虚拟主机,可以看到已经切换到备机
    keepalived+nginx(Tomcat)实现双机热备

    备机切换
    keepalived+nginx(Tomcat)实现双机热备

  • 服务恢复

    恢复主机服务,恢复主机keepalive,可以看到主机上线。
    keepalived+nginx(Tomcat)实现双机热备

Tomcat和Nginx实现主备同理。

keepalived+nginx(Tomcat)实现双机热备

上一篇:redis集群批量删除某些key


下一篇:JQuery Ajax调用asp.net后台方法