zoukankan      html  css  js  c++  java
  • 03linux架构-heartbeat实现web服务器高可用

    准备工作:web1,web2

    1. 修改主机名,永久生效

    # vim /etc/sysconfig/network

    HOSTNAME=web1.cn

    2. 解析  

    # vim /etc/hosts

    192.168.1.63   web1.cn

    192.168.1.64   web2.cn

    3. 关防火墙,关掉selinux

    # iptables -F

    # getenforce

    Permissive

    配置NFS为NFS服务器,提供存储资源

    安装NFS服务器:(3台主机均安装)

    [root@NFS ~]# yum -y install nfs-utils

    [root@NFS ~]# mkdir /wwwdir

    [root@NFS ~]# echo 'Heartbeat Web test!'> /wwwdir/index.html

    [root@NFS ~]#cat /etc/exports

    /wwwdir   192.168.1.0/24(rw)

    配置好目录权限:

    [root@NFS ~]# ll -d /wwwdir/

    drwxr-xr-x 2 root root 4096 May 29 18:52 /wwwdir/

    [root@NFS ~]# chmod 777 -R /wwwdir/

    [root@NFS ~]# ll -d /wwwdir/

    drwxrwxrwx 2 root root 4096 May 29 18:52 /wwwdir/

    开启nfs服务

    [root@NFS ~]# systemctl start nfs

    [root@NFS ~]# systemctl enable nfs

    [root@NFS ~]# showmount –e 查看一下

    web1测试nfs 存储挂载并安装httpd web服务器:

    [root@web1 ~]# yum install nfs-utils httpd -y

    [root@web1 ~]# systemctl start nfs

    [root@web1 ~]# showmount -e 192.168.1.62

    Export list for 192.168.1.62:

    /wwwdir 192.168.1.0/24

    [root@web1 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/

    [root@web1 ~]# systemctl start httpd

    测试:

    [root@web1 ~]# yum install elinks -y

    [root@web1 ~]# elinks --dump 192.168.1.63

      Heartbeat Web test!

    卸载资源:后期这些资源通过heartbeat直接加载

    [root@web1 ~]# umount /var/www/html/

    [root@web1 ~]# systemctl stop httpd

    [root@web1 ~]# systemctl disable httpd

    web2测试nfs 存储挂载并安装httpd web服务器:

    [root@web2 ~]# cat /etc/sysconfig/network

    NETWORKING=yes

    HOSTNAME=web2.cn

    [root@web1 ~]# scp /etc/hosts 192.168.1.64:/etc/

    [root@web2 ~]# yum install nfs-utils httpd -y

    [root@web1 ~]# systemctl start nfs

    [root@web2 ~]# showmount -e 192.168.1.62

    Export list for 192.168.1.62:

    /wwwdir 192.168.1.0/24

    [root@web2 ~]# mount -t nfs 192.168.1.62:/wwwdir /var/www/html/

    [root@web2 ~]# systemctl start httpd

    测试:

    [root@web2 ~]# yum install elinks -y

    [root@web2 ~]# elinks --dump 192.168.1.64

      Heartbeat Web test!

    卸载资源:后期这些资源通过heartbeat直接加载

    [root@web2 ~]# umount /var/www/html/

    [root@web2 ~]# systemctl stop httpd

    [root@web2 ~]# systemctl disable httpd

    web1安装heartbeat

    heartbeat在Ceontos7下编译步骤

    下载:http://www.linux-ha.org/wiki/Downloads,从官方下载最新的HeartBeat版本:

    Heartbeat 3.0.6

    Cluster Glue 1.0.12

    Resource Agents 3.9.6

    1.配置基础环境(配置好网络yum源)

    安装依赖包

    yum install -y bzip2 bzip2-devel gcc gcc-c++ autoconf automake libtool e2fsprogs-devel glib2-devel libxml2 libxml2-devel libtool-ltdl-devel asciidoc libuuid-devel docbook

    如果LTDL包安装不成功,下载rpm包安装:

    rpm -ivh libtool-ltdl-devel-2.4.2-22.el7_3.x86_64.rpm

    2.编译Cluster Glue

       # tar jxf Reusable-Cluster-Components-glue.tar.bz2

    # cd Reusable-Cluster-Components-glue--0a7add1d9996/

    # groupadd haclient  创建用户组

    # useradd -g haclient hacluster   创建用户

    # ./autogen.sh

    # ./configure --prefix=/usr/local/heartbeat/

    # make && make install

    3.编译Resource Agents

       # tar -zxf resource-agents-3.9.6.tar.gz

    # cd resource-agents-3.9.6

    # ./autogen.sh

    #./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'

    # make

    # make install

    4.编译Heartbeat

    # tar -jxvf Heartbeat-3.0.6.tar.bz2

    # cd Heartbeat-3-0-958e11be8686/

    # ./bootstrap

    # export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"

    # ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'

    # make 

    # make install

    生成配置文件:

    # cd /usr/local/heartbeat/etc/ha.d/

    # cp /root/Heartbeat-3-0-958e11be8686/doc/{ha.cf,haresources,authkeys} .

    # chkconfig --add heartbeat

    # chkconfig heartbeat on

    # chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys

    # mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

    # cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/

    #

    # ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/

    5、Heartbeat配置

    Heartbeat的配置主要涉及到ha.cf、haresources、authkeys这三个文件。

    ha.cf:  主配置文件,

    haresource:  用来配置要让Heartbeat托管的服务,

    authkey:  是用来指定Heartbeat的认证方式。

    ⑴ 配置ha.cf----主配置文件

    # vim /usr/local/heartbeat/etc/ha.d/ha.cf (注:前面数字是行号)

    24 debugfile /var/log/ha-debug           ##用于记录heartbeat的调试信息

    29 logfile/var/log/ha-log                ##用于记录heartbeat的日志信息

    34 logfacilitylocal0                    ##设置heartbeat的日志,这里用的是系统日志

    48 keepalive 2                              ##设定心跳(监测)时间时间为2秒

    56 deadtime 30         ##指定若备用节点在30秒内未收到主节点心跳信号,则接管主服务器资源

    61 warntime 10           ##指定心跳延迟的时间为10秒,10秒内备节点不能接收主节点心跳信号,

                                      即往日志写入警告日志,但不会切换服务

    71 initdead 60         ##系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍

    76 udpport  694                         ##广播/单播通讯使用的Udp端口

    91 #bcast ens32   # Linux             ##使用网卡eno32发送心跳检测

    113 #mcast eth0 225.0.0.1 694 1 0    ##采用网卡eth0的Udp多播来组织心跳,一般在备用节点

    Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的的方式,任选其一

    121 ucast ens32 192.168.2.65   ##采用网卡eth32的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址

    157 auto_failback on                 ##定义当主节点恢复后,是否将服务自动切回

    211 node web1.cn                     ##主节点名称

    212 node web2.cn                     ##备用节点名称

    220 ping 192.168.2.1                ##通过ping网关检测心跳是否正常,仅用来测试网络

    253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail   ##指定和heartbeat一起启动、关闭的进程

    259 apiauth ipfail gid=haclient uid=hacluster     ##设置启动IPfail的用户和组

    配置haresources-----资源文件

    Haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址及启动服务集群资源,文件每一行可包含一个或多个资源脚本名,资源间使用空格隔开,参数间使用两个冒号隔开,主节点和备份节点中资源文件haresources要完全一样

    一般格式为:

    node-name network  <resource-group>

    node-name表示主节点的主机名,必须和ha.cf文件中指定的节点名一致。network用于设定集群的

    IP地址、子网掩码和网络设备标识等。resource-group用于指定需Heartbeat托管的服务(即这些

    服务可由Heartbeat来启动和关闭)。

    注意:这里指定的IP地址就是集群对外服务的IP地址

       如要托管这些服务,必须将服务写成可通过start/stop来启动或关闭的脚本,放到/etc/init.d/

       /etc/ha.d/resource.d/目录下Heartbeat会根据脚本名称自动去/etc/init.d或者

       /etc/ha.d/resource.d目录下找到相应脚本进行启动或关闭操作。

    例:

    # vim /usr/local/heartbeat/etc/ha.d/haresources (注:前面数字为行号)

    44 web1.cn IPaddr::192.168.2.111/24/ens32 Filesystem::192.168.2.62:/wwwdir::/va

        r/www/html::nfs  httpd

    注:web1.cn是主服务器的主机名, web2上不需要修改。这样资源默认会加一这个主机上。当web1坏了,web2会再接管。

    IPaddr::192.168.2.111/24/eth32  #指定VIP及绑定到哪个网卡上

    Filesystem::192.168.2.62:/wwwdir::/var/www/html::nfs   #指定要挂载的存储

    httpd  #指定要启动的服务。这个服务必须是在/etc/init.d下或者/usr/local/heartbeat/etc/ha.d/resource.d目录下

    ⑶ 配置authkeys-----心跳密钥验证文件

    #vim /usr/local/heartbeat/etc/ha.d/authkeys 

    auth 3

    3 md5 Hello!

    :auth后填序号,可任意填写,但第二行开头必须为序号名,然后为验证方式,支持三种( crc md5 sha1 )方式验证,最后面是自定义密钥。我应该选哪种验证?

    如果Heartbeat运行于安全网络之上,如本例中的交叉线,可以使用crc,从资源的角度来看,这是代价最低的方法。如果网络并不安全,但也希望降低CPU使用,则使用md5。最后,如果想得到最好的认证,而不考虑CPU使用情况,则使用sha1,它在三者之中最难破解。

    ⑷ 编写httpd启动脚本

    # vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd 

    #!/bin/bash

    /bin/systemctl $1 httpd     

    # chmod 755 !$

    ⑸ 复制配置文件至备机

    # scp –rp /usr/local/heartbeat/etc/ha.d/* root@192.168.2.64:/usr/local/heartbeat/etc/ha.d/

    测试:

    手动加载VIP 192.168.1.111到ens32上

    [root@web1 ~]# cd /usr/local/heartbeat/etc/ha.d/resource.d

    [root@web1 resource.d]# ./IPaddr 192.168.1.111/24/ens32 start

    IPaddr[7116]: INFO:  Success

    INFO:  Success

    查看 VIP:

    [root@web1 ~]# ifconfig    #可以看到 ens32:1  IP为:192.168.1.111

    手动加载NFS存储资源到/var/www/html (加载需要fuser支持,yum install psmisc -y)

    [root@web1 resource.d]# ./Filesystem 192.168.1.62:/wwwdir /var/www/html/ nfs start

    Filesystem[23567]: INFO:  Success

    INFO:  Success

    [root@web1 ~]# ls /var/www/html/index.html

    /var/www/html/index.html

    [root@web1 ~]# df  -h

    ………

    192.168.2.62:/wwwdir    18G  1.1G   17G    6% /var/www/html

    手动启动httpd服务

    [root@web1 ~]# systemctl start httpd

    测试打开主服务页面http://192.168.1.63

    web2上配置heartbeat

    修改文件权限:

    [root@web2 ~]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys  #此文件必须是600权限 否则heartbeat启动不成功

    修改单播地址:

    [root@web2 ~]# vim /usr/local/heartbeat/etc/ha.d/ha.cf

    改:

    ucast ens32 192.168.1.64

    为:

    ucast ens32 192.168.1.63

    挂载需要fuser支持,yum install psmisc -y

    两机器启动heartbeat服务:

    [root@web1 ~]# /etc/init.d/heartbeat restart

    [root@web2 ~]# /etc/init.d/heartbeat restart

    查看端口号:

    [root@web1 ~]# netstat -antup | grep 694

    udp        0      0 0.0.0.0:694        0.0.0.0:*           6895/heartbeat: wr

    查看端口号:

    [root@web2 ~]# netstat -antup | grep 694

    udp        0      0 0.0.0.0:694        0.0.0.0:*           6895/heartbeat: wri

    web1查看集群资源:

    [root@web1 ~]# ifconfig  #查看VIP加载

    ens32:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

            inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255

            ether 00:0c:29:f0:fc:2c  txqueuelen 1000  (Ethernet) [root@web1 ~]# df -h

    [root@web1 ~]# df  -h #查看挂载

    ………

    192.168.2.62:/wwwdir    18G  1.1G   17G    6% /var/www/html

    [root@web1 ~]# netstat -anput | grep httpd   #查看httpd

    web2上查看,没有任务资源:

    [root@web2 ~]# ifconfig

    [root@web2 ~]# df -h

    [root@web2 ~]# netstat -anput | grep httpd

    通过VIP测试:

    两台主机都开启时,所有请求到转到了web1上。访问:http://192.168.1.111/ 正常

     

    当掉web1,关闭网卡,等30秒钟,所有请求都转到web2上了

    [root@web1 ha.d]# ifdown ens32

    [root@web2 ~]# ifconfig

    ...

    eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:48:80:95  

              inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0

    [root@web2 ~]# df -h

    ...

    192.168.1.62:/wwwdir  9.7G  3.4G  5.8G  37% /var/www/html

    [root@web2 ~]#systemctl status httpd

    httpd (pid  6375) is running...

    web1上把ens32网卡再次开启:

    [root@web1 ~]# ifup ens32

    30秒后,查看:

    [root@web1 ~]# df -h

    ...

    192.168.1.62:/wwwdir  9.7G  3.4G  5.8G  37% /var/www/html    #已经加载了httpd资源

    [root@web1 ~]# systemctl status httpd

    httpd (pid  27097) is running...

    [root@web1 ~]# ifconfig

    。。。

    eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:12:EC:1E  

              inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0

    资源已经回切过来。

    web2上查看释放资料:

    [root@web2 ~]# ifconfig   #查看不到ens32:0  192.168.1.111这个IP地址

    [root@web2 ~]# df -h

    Filesystem            Size  Used Avail Use% Mounted on

    /dev/sda2             9.7G  3.7G  5.5G  41% /

    tmpfs                 569M     0  569M   0% /dev/shm

    /dev/sda1             194M   28M  157M  15% /boot

    /dev/sr0              3.4G  3.4G     0 100% /mnt

    [root@web2 ~]# service httpd status

    httpd is stopped

    扩展:
    heartbeat自带的断网切换的工具-ipfail
    ipfail断网切换的原理

       关于ipfail这个断网切换的原理很简单,首先heartbeat要判断自己的网络是否正常其实就是通过ping某个ip,如果可以ping的通,说明网络是通的,如果ping不通了,说明是网络断了,或者是主服务器的网卡坏了,然后执行切换的动作。

    ping一个group的ipfail配置:

    ping_group group1 172.16.103.254 172.16.103.212

  • 相关阅读:
    Pytorch cpu版离线安装:win10 + Anaconda + Pip
    离线配置Opencv-python: Anaconda3 + Pycharm+ Win10 + Python3.6
    HSmartWindowControl 之 摄像头实时显示( 使用 WPF )
    OpencvSharp 在WPF的Image控件中显示图像
    HSmartWindowControl 之 显示图像
    HSmartWindowControl之安装篇 (Visual Studio 2013 & Halcon 18)
    使用ILMerge 打包C# 绿色免安装版程序
    C# 日志输出工具库—log4net 安装、配置及简单应用
    Python:windows下scikit-learn 安装和更新
    C# 开源仪表盘库—Agauge App
  • 原文地址:https://www.cnblogs.com/hackerlin/p/11383418.html
Copyright © 2011-2022 走看看