Jusene's Blog

heartbeat+ldirectord 实现lvs高可用

字数统计: 1.4k阅读时长: 8 min
2017/05/14 Share

ldirectord

heartbeat可以为许多不支持高可用的服务提供高可用服务,前面我们提到过keepalived实现lvs的高可用,那么现在我们来通过heartbeat+ldirectord来实现高可用lvs集群的架构。

规划

vip:10.211.55.24
rs1: 10.211.55.39
rs2: 10.211.55.40
node1: 10.211.55.45
node2: 10.211.55.46

配置lvs集群

  • rs1

    1
    ~]# cat rs.sh
    2
    #!/bin/bash
    3
    4
    vip=10.211.55.24
    5
    eth=lo:0
    6
    7
    8
    case $1 in 
    9
    start)
    10
            echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    11
            echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    12
            echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    13
            echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    14
    15
            ifconfig $eth $vip broadcast $vip netmask 255.255.255.255 up
    16
            route add -host $vip dev $eth
    17
            ;;
    18
    stop)
    19
            echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    20
            echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    21
            echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    22
            echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    23
    24
            ifconfig $eth down
    25
            route del -host $vip dev $eth
    26
            ;;
    27
    status)
    28
            if ifconfig lo:0 | grep $vip &> /dev/null;then
    29
                    echo 'ipvs is running.'
    30
            else
    31
                    echo 'ipvs is stopped.'
    32
            fi
    33
            ;;
    34
    *)
    35
            echo "Usage: `basename $0` {start|stop|status}"
    36
            exit
    37
    esac
    38
    39
    ~]# ./rs.sh start
    40
    ~]# yum install -y httpd
    41
    ~]# echo "realserver 1" > /var/www/html/index.html
    42
    ~]# service httpd start
  • rs2

    1
    ~]# ./rs.sh start
    2
    ~]# yum install -y httpd
    3
    ~]# echo "realserver 2" > /var/www/html/index.html
    4
    ~]# service httpd start
  • node1

    1
    ~]# ifconfig eth0:0 10.211.55.24 broadcast 10.211.55.24 netmask 255.255.255.255 up
    2
    ~]# route add -host 10.211.55.24 dev eth0:0
    3
    ~]# ipvsadm -A -t 10.211.55.24:80 -s rr
    4
    ~]# ipvsadm -a -t 10.211.55.24:80 -r 10.211.55.39 -g 
    5
    ~]# ipvsadm -a -t 10.211.55.24:80 -r 10.211.55.40 -g
    6
    7
    测试lvs dr模型没问题
    8
    ~]# route del -host 10.211.55.24
    9
    ~]# ifconfig eth0:0 down
    10
    ~]# ipvsadm -C
  • node2

    1
    ~]# ifconfig eth0:0 10.211.55.24 broadcast 10.211.55.24 netmask 255.255.255.255 up
    2
    ~]# route add -host 10.211.55.24 dev eth0:0
    3
    ~]# ipvsadm -A -t 10.211.55.24:80 -s rr
    4
    ~]# ipvsadm -a -t 10.211.55.24:80 -r 10.211.55.39 -g 
    5
    ~]# ipvsadm -a -t 10.211.55.24:80 -r 10.211.55.40 -g
    6
    7
    测试lvs dr模型没问题
    8
    ~]# route del -host 10.211.55.24
    9
    ~]# ifconfig eth0:0 down
    10
    ~]# ipvsadm -C

配置ldirectord+heartbeart v2

ldirectord依赖于自己的配置文件生成ipvs规则,因此,定义集群rs都在配置文件中指定,而无须手动执行ipvsadm命令。
ldirectory能够依赖对rs的健康状态做检测,并能够实现按需添加或删除rs。

  • node1

    1
    ~]# rpm -ivh heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-gui-2.1.4-12.el6.x86_64.rpm
    2
    ~]# cp /usr/share/doc/heartbeat-ldirectord-2.1.4/ldirectord.cf /etc/ha.d/
    3
    ~]# cp /usr/share/doc/{ha.cf,authkeys} /etc/ha.d/
    4
    ~]# chmod 600 /etc/ha.d/authkeys
    5
    ~]# cat /etc/ha.d/authkeys
    6
    auth 2
    7
    1 crc
    8
    2 sha1 HI!@@@
    9
    3 md5 Hello!
    10
    ~}# cat  /etc/ha.d/ldirectord.cf
    11
    checktimeout=3
    12
    checkinterval=1
    13
    #fallback=127.0.0.1:80
    14
    autoreload=yes
    15
    logfile="/var/log/ldirectord.log"
    16
    emailalert="root"
    17
    #emailalertfreq=3600
    18
    #emailalertstatus=all
    19
    quiescent=yes
    20
    21
    # Sample for an http virtual service
    22
    virtual=10.211.55.24:80
    23
            real=10.211.55.39:80 gate
    24
            real=10.211.55.40:80 gate
    25
            fallback=127.0.0.1:80 gate
    26
            service=http
    27
            request=".health.html"
    28
            receive="OK"
    29
            scheduler=rr
    30
            #persistent=600
    31
            #netmask=255.255.255.255
    32
            protocol=tcp
    33
            checktype=negotiate
    34
            checkport=80
    35
    36
    ~]# cat /etc/ha.d/ha.cf
    37
    crm on
    38
    logfile /var/log/ha-log
    39
    mcast eth0 225.0.12.1 694 1 0
    40
    auto_failback on
    41
    node    node1
    42
    node    node2
    43
    ping 10.211.55.1
    44
    ~]# scp /etc/ha.d/{ha.cf,ldirectord.cf,authkeys} /etc/ha.d/
    45
    ~]# service heartbeat start;ssh node2 "service heartbeart start"
    46
    ~]# yum install -y httpd
    47
    ~]# echo 'sorry server' > /var/www/html/index.html
    48
    ~]# service httpd start
  • node2

    1
    ~]# yum install -y httpd
    2
    ~]# echo 'sorry server' > /var/www/html/index.html
    3
    ~]# service httpd start

我们需要配置的有两个资源,vip资源和ipvs资源,我们还需要配置排列约束和顺序约束。

查看结果

1
[root@node2 heartbeat2]# ipvsadm -L -n
2
IP Virtual Server version 1.2.1 (size=4096)
3
Prot LocalAddress:Port Scheduler Flags
4
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
5
TCP  10.211.55.24:80 rr
6
  -> 10.211.55.39:80              Route   1      0          0  
7
  -> 10.211.55.40:80              Route   1      0          0       
8
[root@node2 heartbeat2]# ifconfig eth0:0
9
eth0:0    Link encap:Ethernet  HWaddr 00:1C:42:E2:B9:94  
10
          inet addr:10.211.55.24  Bcast:10.211.55.255  Mask:255.255.255.0
11
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

我们关闭连个节点的httpd

1
[root@node2 heartbeat2]# ipvsadm -L -n
2
IP Virtual Server version 1.2.1 (size=4096)
3
Prot LocalAddress:Port Scheduler Flags
4
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
5
TCP  10.211.55.24:80 rr
6
  -> 10.211.55.39:80              Route   0      0          0  
7
  -> 10.211.55.40:80              Route   0      0          0
8
  -> 127.0.0.1:80                 Local   1      0          0

配置ldirectord+heartbeart v1

  • node1
    1
    ~]# vim /etc/ha.d/ha.cf  去掉crm on
    2
    ~]# cp /usr/share/doc/doc/heartbeat-2.1.4/haresources /etc/ha.d/
    3
    ~]# vim /etc/ha.d/haresources
    4
    node1 10.211.55.24/24/eth0/10.211.55.24  ldirectord
    5
    ~]# ll /etc/init.d/ldirectord
    6
    ~]# scp /etc/ha.d/{ha.cf,haresources} node2:/etc/ha.d/
    7
    ~]# service heartbeat start;ssh node2 'service heartbeat start'
    1
    [root@node1 heartbeat-2.1.4]# ifconfgi eth0:0
    2
    -bash: ifconfgi: command not found
    3
    [root@node1 heartbeat-2.1.4]# ifconfig eth0:0
    4
    eth0:0    Link encap:Ethernet  HWaddr 00:1C:42:31:37:0C  
    5
              inet addr:10.211.55.24  Bcast:10.211.55.24  Mask:255.255.255.0
    6
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    7
    8
    [root@node1 heartbeat-2.1.4]# ipvsadm -L -n
    9
    IP Virtual Server version 1.2.1 (size=4096)
    10
    Prot LocalAddress:Port Scheduler Flags
    11
      -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
    12
    TCP  10.211.55.24:80 rr
    13
      -> 10.211.55.39:80              Route   1      0          0 
    14
      -> 10.211.55.40:80              Route   1      0          0
CATALOG
  1. 1. ldirectord
  2. 2. 规划
  3. 3. 配置lvs集群
  4. 4. 配置ldirectord+heartbeart v2
  5. 5. 配置ldirectord+heartbeart v1