• freebsd下通过carp来实现互热备和负载均衡


    29.13 Common Access Redundancy Protocol (CARP, 共用地址冗余协议)

    原作 Tom Rhodes.

      Common Access Redundancy Protocol, 或简称 CARP 能够使多台主机共享同一 IP 地址。 在某些配置中, 这样做可以提高可用性, 或实现负载均衡。 下面的例子中, 这些主机也可以同时使用其他的不同的 IP 地址。

      要启用 CARP 支持, 必须在 FreeBSD 内核配置中增加下列选项, 并重新联编内核:

    device carp

      这样就可以使用 CARP 功能了, 一些具体的参数, 可以通过一系列 sysctl OID 来调整。 设备可以通过 ifconfig 命令来加载:

    # ifconfig carp0 create

      在真实环境中, 这些接口需要一个称作 VHID 的标识编号。 这个 VHID 或 Virtual Host Identification (虚拟主机标识) 用于在网络上区分主机。

    29.13.1 使用 CARP 来改善服务的可用性 (CARP)

      如前面提到的那样, CARP 的作用之一是改善服务的可用性。 这个例子中, 将为三台主机提供故障转移服务, 这三台服务器各自有独立的 IP 地址, 并提供完全一样的 web 内容。 三台机器以 DNS 轮询的方式提供服务。 用于故障转移的机器有两个 CARP 接口, 分别配置另外两台服务器的 IP 地址。 当有服务器发生故障时, 这台机器会自动得到故障机的 IP 地址。 这样以来, 用户就完全感觉不到发生了故障。 故障转移的服务器提供的内容和服务, 应与其为之提供热备份的服务器一致。

      两台机器的配置, 除了主机名和 VHID 之外应完全一致。 在我们的例子中, 这两台机器的主机名分别是 hosta.example.orghostb.example.org。 首先, 需要将 CARP 配置加入到 rc.conf。 对于 hosta.example.org 而言, rc.conf 文件中应包含下列配置:

    hostname="hosta.example.org"
    ifconfig_fxp0="inet 192.168.1.3 netmask 255.255.255.0"
    cloned_interfaces="carp0"
    ifconfig_carp0="vhid 1 pass testpast 192.168.1.50/24"

      在 hostb.example.org 上, 对应的 rc.conf 配置则是:

    hostname="hostb.example.org"
    ifconfig_fxp0="inet 192.168.1.4 netmask 255.255.255.0"
    cloned_interfaces="carp0"
    ifconfig_carp0="vhid 2 pass testpass 192.168.1.51/24"

    注意: 在两台机器上由 ifconfigpass 选项指定的密码必须是一致的, 这一点非常重要。 carp 设备只会监听和接受来自持有正确密码的机器的公告。 此外, 不同虚拟主机的 VHID 必须不同。

      第三台机器, provider.example.org 需要进行配置, 以便在另外两台机器出现问题时接管。 这台机器需要两个 carp 设备, 分别处理两个机器。 对应的 rc.conf 配置类似下面这样:

    hostname="provider.example.org"
    ifconfig_fxp0="inet 192.168.1.5 netmask 255.255.255.0"
    cloned_interfaces="carp0 carp1"
    ifconfig_carp0="vhid 1 advskew 100 pass testpass 192.168.1.50/24"
    ifconfig_carp1="vhid 2 advskew 100 pass testpass 192.168.1.51/24"

      配置两个 carp 设备, 能够让 provider.example.org 在两台机器中的任何一个停止响应时, 立即接管其 IP 地址。

    注意: 默认的 FreeBSD 内核 可能 启用了主机间抢占。 如果是这样的话, provider.example.org 可能在正式的内容服务器恢复时不释放 IP 地址。 此时, 管理员可以 “提醒” 一下接口。 具体做法是在 provider.example.org 上使用下面的命令:

    # ifconfig carp0 down && ifconfig carp0 up

    这个操作需要在与出现问题的主机对应的那个 carp 接口上进行。

      现在您已经完成了 CARP 的配置, 并可以开始测试了。 测试过程中, 可以随时重启或切断两台机器的网络。

      如欲了解更多细节, 请参见 carp(4) 联机手册。


    CARP(4)                FreeBSD Kernel Interfaces Manual                CARP(4)

    NAME
    carp -- Common Address Redundancy Protocol

    SYNOPSIS
    device carp

    DESCRIPTION
    The carp interface is a pseudo-device that implements and controls the
    CARP protocol. CARP allows multiple hosts on the same local network to
    share a set of IP addresses. Its primary purpose is to ensure that these
    addresses are always available, but in some configurations carp can also
    provide load balancing functionality.

    A carp interface can be created at runtime using the ifconfig carpN
    create command or by configuring it via cloned_interfaces in the
    /etc/rc.conf file.

    To use carp, the administrator needs to configure at minimum a common
    virtual host ID (VHID) and virtual host IP address on each machine which
    is to take part in the virtual group. Additional parameters can also be
    set on a per-interface basis: advbase and advskew, which are used to con-
    trol how frequently the host sends advertisements when it is the master
    for a virtual host, and pass which is used to authenticate carp adver-
    tisements. The advbase parameter stands for ``advertisement base''. It
    is measured in seconds and specifies the base of the advertisement inter-
    val. The advskew parameter stands for ``advertisement skew''. It is
    measured in 1/256 of seconds. It is added to the base advertisement
    interval to make one host advertise a bit slower that the other does.
    Both advbase and advskew are put inside CARP advertisements. These con-
    figurations can be done using ifconfig(8), or through the SIOCSVH
    ioctl(2).

    Additionally, there are a number of global parameters which can be set
    using sysctl(8):

    net.inet.carp.allow Accept incoming carp packets. Enabled by
    default.

    net.inet.carp.preempt Allow virtual hosts to preempt each other. It
    is also used to failover carp interfaces as a
    group. When the option is enabled and one of
    the carp enabled physical interfaces goes down,
    advskew is changed to 240 on all carp inter-
    faces. See also the first example. Disabled
    by default.

    net.inet.carp.log Value of 0 disables any logging. Value of 1
    enables logging of bad carp packets. Values
    above 1 enable logging state changes of carp
    interfaces. Default value is 1.

    net.inet.carp.arpbalance Balance local traffic using ARP (see below).
    Disabled by default.

    net.inet.carp.suppress_preempt
    A read only value showing the status of preemp-
    tion suppression. Preemption can be suppressed
    if link on an interface is down or when
    pfsync(4) interface is not synchronized. Value
    of 0 means that preemption is not suppressed,
    since no problems are detected. Every problem
    increments suppression counter.

    ARP level load balancing
    The carp has limited abilities for load balancing the incoming connec-
    tions between hosts in Ethernet network. For load balancing operation,
    one needs several CARP interfaces that are configured to the same IP
    address, but to a different VHIDs. Once an ARP request is received, the
    CARP protocol will use a hashing function against the source IP address
    in the ARP request to determine which VHID should this request belong to.
    If the corresponding CARP interface is in master state, the ARP request
    will be replied, otherwise it will be ignored. See the EXAMPLES section
    for a practical example of load balancing.

    The ARP load balancing has some limitations. First, ARP balancing only
    works on the local network segment. It cannot balance traffic that
    crosses a router, because the router itself will always be balanced to
    the same virtual host. Second, ARP load balancing can lead to asymmetric
    routing of incoming and outgoing traffic, and thus combining it with
    pfsync(4) is dangerous, because this creates a race condition between
    balanced routers and a host they are serving. Imagine an incoming packet
    creating state on the first router, being forwarded to its destination,
    and destination replying faster than the state information is packed and
    synced with the second router. If the reply would be load balanced to
    second router, it will be dropped due to no state.

    EXAMPLES
    For firewalls and routers with multiple interfaces, it is desirable to
    failover all of the carp interfaces together, when one of the physical
    interfaces goes down. This is achieved by the preempt option. Enable it
    on both host A and B:

    sysctl net.inet.carp.preempt=1

    Assume that host A is the preferred master and 192.168.1.x/24 is config-
    ured on one physical interface and 192.168.2.y/24 on another. This is
    the setup for host A:

    ifconfig carp0 create
    ifconfig carp0 vhid 1 pass mekmitasdigoat 192.168.1.1/24
    ifconfig carp1 create
    ifconfig carp1 vhid 2 pass mekmitasdigoat 192.168.2.1/24

    The setup for host B is identical, but it has a higher advskew:

    ifconfig carp0 create
    ifconfig carp0 vhid 1 advskew 100 pass mekmitasdigoat 192.168.1.1/24
    ifconfig carp1 create
    ifconfig carp1 vhid 2 advskew 100 pass mekmitasdigoat 192.168.2.1/24

    Because of the preempt option, when one of the physical interfaces of
    host A fails, advskew is adjusted to 240 on all its carp interfaces.
    This will cause host B to preempt on both interfaces instead of just the
    failed one.

    In order to set up an ARP balanced virtual host, it is necessary to con-
    figure one virtual host for each physical host which would respond to ARP
    requests and thus handle the traffic. In the following example, two vir-
    tual hosts are configured on two hosts to provide balancing and failover
    for the IP address 192.168.1.10.

    First the carp interfaces on host A are configured. The advskew of 100
    on the second virtual host means that its advertisements will be sent out
    slightly less frequently.

    ifconfig carp0 create
    ifconfig carp0 vhid 1 pass mekmitasdigoat 192.168.1.10/24
    ifconfig carp1 create
    ifconfig carp1 vhid 2 advskew 100 pass mekmitasdigoat 192.168.1.10/24

    The configuration for host B is identical, except the advskew is on vir-
    tual host 1 rather than virtual host 2.

    ifconfig carp0 create
    ifconfig carp0 vhid 1 advskew 100 pass mekmitasdigoat 192.168.1.10/24
    ifconfig carp1 create
    ifconfig carp1 vhid 2 pass mekmitasdigoat 192.168.1.10/24

    Finally, the ARP balancing feature must be enabled on both hosts:

    sysctl net.inet.carp.arpbalance=1

    When the hosts receive an ARP request for 192.168.1.10, the source IP
    address of the request is used to compute which virtual host should
    answer the request. The host which is master of the selected virtual
    host will reply to the request, the other(s) will ignore it.

    This way, locally connected systems will receive different ARP replies
    and subsequent IP traffic will be balanced among the hosts. If one of
    the hosts fails, the other will take over the virtual MAC address, and
    begin answering ARP requests on its behalf.

    SEE ALSO
    inet(4), pfsync(4), rc.conf(5), ifconfig(8), sysctl(8)

    HISTORY
    The carp device first appeared in OpenBSD 3.5. The carp device was
    imported into FreeBSD 5.4.

    FreeBSD 6.2 June 6, 2006 FreeBSD 6.2
  • 相关阅读:
    739. Daily Temperatures
    535. Encode and Decode TinyURL
    811. Subdomain Visit Count
    706. Design HashMap
    C++-static作用(转)
    大学四年应当如何渡过(转)
    计算机导论第八章-总结
    计算机导论第四章习题
    计算机导论-第一章习题
    20世纪最伟大的十大算法
  • 原文地址:https://www.cnblogs.com/studio313/p/1099544.html
Copyright © 2020-2023  润新知