zoukankan      html  css  js  c++  java
  • http+mysql结合keepalived做热备

    preface

    公司要求http+mysql+redis+二次开发的ldap要求做高可用,所以此处写写keepalived在这种
    环境下的高可用。keepalived这个软件我就不啰嗦了,众所周知,基于VRRP协议做的高可用,VRRP就是virtual route protocol。把这个协议了解透了,keepalived自然也就明白怎么回事了。

    实践

    环境如下

    ip 角色
    172.16.160.189 master
    172.16.160.179 backup

    两台服务器都安装好了http,mysql,redis,以及ldap,确保两台服务器的http,mysql,redis以及ldap服务正常运行。
    接下来重点讲解keepalived的配置文件

    master 上配置keepalived

    配置文件如下:

    [root@localhost script]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       router_id LVS_DEVEL         
    }
    
    vrrp_script monitor_server {     # monitor_server是自定义名字
         script "/etc/keepalived/script/check_server.sh"   # 设定的脚本,脚本内容下面会贴出来
         interval 1    #探测间隔,实际也是脚本的执行间隔
         weight 0      # 权重,脚本执行后的返回值会与这个做加减法,然后再把值与下面vrrp_instance的权重值做加减。
    }
    
    vrrp_instance VI_1 {
        state MASTER
        interface eth0
        lvs_sync_daemon_inteface eth0
        virtual_router_id 51    # 与备份机的id值必须一样的。
        priority 100        # 权重值
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            172.16.160.250
        }
        track_script {
           monitor_server    # 调用脚本模块
        }
    }
    virtual_server 172.16.160.250 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol http    # 因为这里要访问http协议,所以protocol写成http,这样浏览器才能访问,不然写成TCP的话,浏览器没法访问。
    
    #    real_server 172.16.160.179 80 {
    #        weight 100
    #	HTTP_CHECK {
    #        connect_timeout 2      #(10秒无响应超时)
    #        nb_get_retry 1
    #        delay_before_retry 1
    #        connect_port 80
    #	}
    #        }
    #    real_server 172.16.160.189 80 {
    #        weight 100
    #	HTTP_CHECK {
    #        connect_timeout 2       #(10秒无响应超时)
    #        nb_get_retry 1
    #        delay_before_retry 1
    #        connect_port 80
    #	}
    #        }
        }
    
    backup上的配置
    [root@localhost keepalived]# cat /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    
    global_defs {
       notification_email {
         acassen@firewall.loc
         failover@firewall.loc
         sysadmin@firewall.loc
       }
       notification_email_from Alexandre.Cassen@firewall.loc
       router_id LVS_DEVEL1   # 本机id
    }
    
    vrrp_script monitor_server{
        script "/etc/keepalived/script/check_server.sh"  # 同上
        interval 1
        weight 0
    }
    
    vrrp_instance VI_1 {
        state BACKUP    #角色改为backup
        interface eth0
        lvs_sync_daemon_inteface eth0
        virtual_router_id 51    
        priority 90    # 权重值调小即可
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            172.16.160.250
        }
        track_script {
            monitor_server 
        }
    }
    virtual_server 172.16.160.250 80 {
        delay_loop 6
        lb_algo rr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol HTTP
    
    #    real_server 172.16.160.179 80 {
    #        weight 100
    #	TCP_CHECK {
    #        connect_timeout 2      #(10秒无响应超时)
    #        nb_get_retry 1
    #        delay_before_retry 1
    #        connect_port 80
    #	}
    #        }
    #    real_server 172.16.160.189 80 {
    #        weight 100
    #	TCP_CHECK {
    #        connect_timeout 2       #(10秒无响应超时)
    #        nb_get_retry 1
    #        delay_before_retry 1
    #        connect_port 80
    #	}
    #        }
        }
    
    脚本内容

    脚本编写思路很简单,检测每个服务是否在,如果不在,尝试脚本启动,启动不了的话,停止keepalived,这样vip就切换走了。

    function judge_ge_1 {
       if [ "$1" -ge "1" ];then
           export result="true"
       else
           export result="false"
       fi
    }
    
    function stop_keepalived {
       if [ "$1" == "false" ];then
        service  keepalived stop
       fi
    }
    
    ################# check redis
    exist_port=`netstat -lnpt |grep -v grep | grep 6379 |wc -l`
    judge_ge_1 $exist_port
    if [ "$result" == "false" ];then
        /etc/init.d/redis start
    fi
    exist_port=`netstat -lnpt |grep -v grep | grep 6379 |wc -l`
    judge_ge_1 $exist_port
    stop_keepalived $result
    
    ###################### check mysql
    exist_port=`netstat -lnpt |grep -v greo | grep 3306 |wc -l`
    judge_ge_1 $exist_port
    if [ "$result" == "false" ];then
        service mysqld restart
    fi
    exist_port=`netstat -lnpt |grep -v greo | grep 3306 |wc -l`
    judge_ge_1 $exist_port
    stop_keepalived $result
    
    
    ################### check http
    exist_port=`netstat -lnpt |grep -v grep | grep httpd |wc -l`
    judge_ge_1 $exist_port 
    if [ "$result" == "false" ];then
        service httpd restart
    fi
    exist_port=`netstat -lnpt |grep -v grep | grep httpd |wc -l`
    judge_ge_1 $exist_port
    stop_keepalived $result
    
    
    [root@localhost script]# chmod 777 /etc/keepalived/script/check_server.sh   # 最后不要忘记添加777的权限,保证具有可执行权限
    

    有问题请随时与我联系,18500777133@sina.cn

  • 相关阅读:
    Android Fragment与Activity通讯详解
    Fragment之间的交互
    理解Fragment的生命周期
    Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
    【APP UI 设计模式】(一)APP UI 设计原则和流程
    白话经典算法系列之六 高速排序 高速搞定
    究竟什么是推荐?
    Mongo散记--聚合(aggregation)& 查询(Query)
    C/C++程序猿必须熟练应用的开源项目
    PL/SQL连接oracle数据库
  • 原文地址:https://www.cnblogs.com/liaojiafa/p/6054661.html
Copyright © 2011-2022 走看看