zoukankan      html  css  js  c++  java
  • Heartbeat原理及部署

      参考:https://blog.csdn.net/wzy0623/article/details/81188814

        http://www.mamicode.com/info-detail-1494631.html

        https://www.jianshu.com/p/12c4b3e43b65

      一,Heartbeat简介

      通过Heartbeat,可以将资源(ip以及程序服务等资源)从一台已经故障的计算机快速转移到另一台正常运转的机器上继续提供服务,一般称之为高可用服务。官方网址:http://www.linux-ha.org/wiki/Heartbeat
            通过修改Heartbeat的配置文件,可以指定一台Heartbeat服务器作为主服务器,另一台自动成为热备服务器。在热备服务器上面配置Heartbeat守护程序来监听来自主服务器的心跳信息。如果在规定时间内,无法监听到心跳信息,那么就启动故障转移,取得主服务器上的相关资源的所有权,接替主服务器继续不间断的提供服务,从而达到资源以及服务高可用的目的。一般故障切换时间在5~20s之间。Heartbeat还支持主主模式,及两台服务器互为主备。
            另外,和Keepalived的服务一样,Heartbeat高可用是服务器级别的,不是服务级别的。它可以监测以下系统问题:
      服务器宕机
      Heartbeat软件故障
      心跳连接线故障
      服务故障不会导致切换,但可以通过服务宕机把Heartbeat服务停掉,从而主机实现自动切换。至少需要两台主机才可以实现高可用服务。两台Heartbeat服务之间可以通过以下方式进行通信:
      穿行电缆,所谓的串口(首选,缺点是距离不能太远)
      一根以太网电缆两网卡直连(常用方式)
      以太网电缆,通过交换机等网络设备连接。(交换机可能出现问题等)
            Heartbeat通过ip地址接管和arp广播进行故障转移的。在主服务器故障时,备用节点接管资源后,会立即强制更新所有客户端本地的arp表,即清楚客户端本地缓存的故障服务器的vip和mac地址的解析记录,确保客户端和新的主服务器的对话。
            真实ip,又被称为管理ip,一般指配置在物理网卡上面的ip。在负载均衡高可用环境中,管理ip是不对外提供访问服务的,仅作为管理服务器使用,如SSH可以通过这个进行服务连接管理。VIP是虚拟ip,实际上就是eth0:X,x为0~255的任意数字,可以在一个网卡上面绑定多个别名。VIP当主服务器故障时,可以自动漂移到备用服务器。
      

      二,安装Heartbeat

      环境查看

       服务器规划

    IP 用途 主机名
    192.168.1.101 主服务器 hdp3
    192.168.1.102 备服务器 hdp4

      初始配置

      禁用防火墙

    systemctl disable firewalld
    

       关闭senlinux

      设置主机名并设置hosts文件 

      两台主机名不一样,hosts文件一样 

    # cat /etc/hosts
    192.168.1.101 hdp3
    192.168.1.102 hdp4
    

       安装依赖包

    yum -y install glib2-devel libtool-ltdl-devl net-snmp-devel bzip2-devel ncurses-devel openssl-devel libtool libxml2 libxml2-devel gettext bison flex zlib-devel mailx which libxslt docbook-dtds docbook-style-xsl PyXML shadow-utils opensp autoconf automake bzip2 e2fsprogs-devel libxslt-devel libtool-ltdl-devel make asciidoc libuuid-devel asciidoc
    

       下载安装包

        Heartbeat 3.0.6:http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2
        Cluster Glue 1.0.12:http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2
        Resource Agents 3.9.6:https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz
    

       添加 haclient 组和 hacluster 账户,这个用户主要用来配置respawn的。

    groupadd haclient
    useradd -g haclient -M -s /sbin/nologin hacluster
    

       安装Cluster Glue 1.0.12

    tar -xf 0a7add1d9996.tar.bz2 
    cd Reusable-Cluster-Components-glue--0a7add1d9996/
    ./autogen.sh
    ./configure --prefix=/usr/local/heartbeat  LIBS='/lib64/libuuid.so.1'
    make && make install
    

      安装Resource Agents

    tar -xf v3.9.6.tar.gz
    cd resource-agents-3.9.6/
    ./autogen.sh
    ./configure --prefix=/usr/local/heartbeat  
    make && make install
    

       安装heartbeat-3-0-6

    tar -xf 958e11be8686.tar.bz2
    cd Heartbeat-3-0-958e11be8686/
    #设置,否则安装会报错
    export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
    #删除配置文件的最后一行
    sed -i '/^#define HA_HBCONF_DIR /d' /usr/local/heartbeat/include/heartbeat/glue_config.h
    ./bootstrap 
    ./configure --prefix=/usr/local/heartbeat  LIBS='/lib64/libuuid.so.1'
    make && make install
    

       拷贝配置文件

    cd /usr/local/heartbeat/share/doc/heartbeat
    cp authkeys ha.cf haresources /usr/local/  heartbeat/etc/ha.d/
    

       设置配置文件权限及软连接

    cd /usr/local/heartbeat/etc/ha.d
    chmod 600 authkeys
    ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/RAExec/* /usr/local/heartbeat/lib/heartbeat/plugins/RAExec/
    ln -svf /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
    

       编辑配置文件

      192.168.1.101上的ha.cf文件内容如下

    # cat /usr/local/heartbeat/etc/ha.d/ha.cf 
    # 保存调试信息文件
    debugfile /var/log/ha-debug         
    # 日志文件
    logfile /var/log/ha-log
    # 表示使用系统日志            
    logfacility local0 
    # 心跳的时间间隔,单位秒                 
    keepalive 1                         
    # 超出该时间间隔未收到对方节点的心跳,则判定对方死亡
    deadtime 30  
    # 超出该时间间隔未收到对方节点的心跳,则发出警告记录到日志中                       
    warntime 10 
    #  在某系统上,系统启动或重启之后需要经过一段时间网络才能正常工作,该选项用于解决这种情况产生的时间间隔,取值至少为deadtime的2倍                         
    initdead 120  
    # 设置广播通信使用的端口,694为默认使用的端口号                           
    udpport 694                         
    # 传播心跳的广播播网卡信息
    bcast eth0      
    # 设置对方机器心跳检测的IP
    # 第二列为网卡名 第三列为心跳IP,生产中心跳需要与数据网络分开                   
    ucast eth0 192.168.1.102  
    # 在该选项设为on的情况下,一旦主节点恢复运行,则自动获取资源并取代从节点         
    auto_failback off              
    # 配置主从的节点信息
    node hdp3                           
    node hdp4           
    # 如果ping不通该地址,就认为当前断网,需要转移vip
    ping 192.168.1.9                   
    # 指定与heartbeat一同启动和关闭的进程,该进程被自动监视,遇到故障则重新启动。最常用的进程是ipfail,该进程用于检测和处理网络故障,需要配合ping语句指定的ping node来检测网络连接。如果你的系统是64bit,请注意该文件的路径
    respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail  
    # 指定用户和组
    apiauth ipfail gid=haclient uid=hacluster
    

       注意:如果复制配置文件可能导致无法启动heartbeat,可能是因为字符编码或者空格等原因,可以直接在原配置文件修改

      192.168.1.102除了以下配置不一样其他一致

    # 设置对方机器心跳检测的IP
    # 第二列为网卡名 第三列为心跳IP,生产中心跳需要与数据网络分开                   
    ucast eth0 192.168.1.101  
    

       编辑认证文件authkeys

      开启2认证,认证秘钥为HI!生产中使用复杂秘钥

    # sed '/#/d' authkeys 
    auth 2
    2 sha1 HI!
    

       编辑资源配置文件

      192.168.1.101配置文件

    #cat haresources 
    hdp3 IPaddr::192.168.1.100/24/eth0
    

       解析:hdp3代表主机名,192.168.1.100为VIP 虚拟在网卡eth0上

      192.168.1.101配置文件

    # cat haresources 
    hdp4 IPaddr::192.168.1.100/24/eth0
    

       启动heartbeat

     systemctl start heartbeat
    

       主服务器上查看日志

     

       备主机没有虚拟VIP

       停止主服务器的heartbeat

    systemctl stop heartbeat
    

       主服务器查看ha日志

       从服务器查看ha日志

       再次启动主服务器的heartbeat

      查看从服务器日志知道hdp3已经触及active状态

       从服务器VIP还保留,除非从服务器heartbeat停止主heartbeat应用正常则主会接收生成VIP

      三,使用heartbeat实现web高可用

      以上只实现的VIP漂移,下面通过heartbeat实现web高可用

      两台主机安装apache

    yum -y install httpd
    

       找到httpd启动程序并负责到heartbeat管理目录

    # which  httpd
    /usr/sbin/httpd
    cp /usr/sbin/httpd /usr/local/heartbeat/etc/ha.d/resource.d/
    

       使用heartbeat需要满足以下条件

    1)httpd命令需要放在/etc/init.d/下或者/etc/ha.d/resource.d下
    
    2)httpd命令需要具备可执行权限(x)
    
    3)httpd必须支持如下启动方式
    
    /etc/init.d/httpd
    
    Usage: httpd {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}
    

       查看httpd帮助可以知道httpd可以使用命令启动,需要加参数-k

       

    httpd -k start
    

       修改haresources

      两台服务器配置一致

    # cat haresources 
    hdp3 IPaddr::192.168.1.100/24/eth0 httpd::-k
    

       重启heartbeat查看日志

    systemctl restart heartbeat
    

      主服务器

       注意:使用heartbeat可以管理应用资源,应用无需使用systemctl enable设置成开机自启动

      高可用测试,为了区分,在两台服务器写入不同的首页

    #主服务器
    echo hdp3>/var/www/html/index.html
    #备服务器
    echo hdp4>/var/www/html/index.html
    

       停止主服务器的heatbeat模拟中断

      备服务器查看日志

       web页面使用VIP访问变成备服务器的首页了

       生产中主备服务器后端内容是一致的,主服务器宕机对于用户来说无感知

       注意:如果heartbeat正常,VIP正常,httpd服务宕了,这个时候不会做高可用切换,写个简单的脚本定时或守护进程判断httpd服务,如果有问题,则停止heartbeat,主动使其上的业务切换到另一台

      

  • 相关阅读:
    Windows netstat
    LOIC Download
    Open CV 环境配置
    C++ strcat_s
    c++ strlen() 函数
    css实现1px 像素线条_解决移动端1px线条的显示方式
    css中line-height的理解_介绍line-height实际应用
    css 分割线样式_css实现文章分割线的多种方法总结
    css获取除第一个之外的子元素
    css实现div多边框_box-shadow模拟多边框、outline描边实现
  • 原文地址:https://www.cnblogs.com/minseo/p/13322635.html
Copyright © 2011-2022 走看看