AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 666850
Accepted
Franz Kafka
Franz Kafka
Asked: 2015-02-12 09:21:11 +0800 CST2015-02-12 09:21:11 +0800 CST 2015-02-12 09:21:11 +0800 CST

具有故障转移功能的 Softlayer HAProxy

  • 772

我在 Softlayer 上有 2 个虚拟服务器,都运行 HAProxy。我正在尝试使用 keepalived 设置故障转移。每台服务器都有一个私有 IP 和一个公共 IP,它们在同一个 VLAN 上。我为keepalived尝试了许多不同的设置,但是在master上停止HAProxy,它不会故障转移到BACKUP。

我读到不支持多播,因此我将设置更改为单播。现在,备份/主服务器上的设置基本上是这样的:

vrrp_script chk_haproxy {
  script "pidof haproxy"
  interval 2
}

vrrp_instance VI_1 {
  debug 2
  interface eth1
  state MASTER
  virtual_router_id 51
  priority 101
  unicast_src_ip 1.2.3.4        # My IP
  unicast_peer {
    5.6.7.8                   # peer IP
  }
  track_script {
    chk_haproxy
  }
}

其中 MYIP 是 conf 文件所在服务器的公共 IP 地址,PEERIP 是对等方的公共 IP 地址。仍然无法正常工作。停止主服务器上的 HAProxy,它不会故障转移到备份。

我想知道是否有人在 Softlayer 上设置了带有故障转移的 HAProxy,以及他们是如何完成它的?

haproxy
  • 1 1 个回答
  • 1584 Views

1 个回答

  • Voted
  1. Best Answer
    Franz Kafka
    2015-02-13T01:32:01+08:002015-02-13T01:32:01+08:00

    我设法完成了这个设置,我是这样做的:

    1. 我使用 SoftLayer 的控制面板创建了一个全局 IP 地址。

    2. 我在两个 HAProxy 虚拟服务器上都有 Debian 7。我将全局 IP 地址添加到两台服务器上的 eth1 接口。

    3. 这是两台服务器上使用的 HAProxy 设置:

      global
        log 127.0.0.1 local0
        log 127.0.0.1 local1 notice
        maxconn 4096
        user haproxy
        group haproxy
      
      defaults
        log global  
        mode http    
        option httplog 
        option dontlognull
        retries 3
        maxconn 2000
        option redispatch
        timeout connect 5000
        timeout client 50000
        timeout server 50000
        stats uri / haproxy
      
      listen webfarm 0.0.0.0:80
        mode http
        stats enable
        stats uri /haproxy?stats
        stats realm Haproxy\ Statistics
        stats auth haproxy:stats
        balance roundrobin
        cookie LBN insert indirect nocache
        option httpclose
        option forwardfor
        server app1-west <public_ip>:8080 cookie node1 check
        server app2-west <public_ip>:8080 cookie node2 check 
      
    4. 这是 MASTER 服务器上的 Keepalived 设置:

      global_defs {
          notification_email {
              admin@mydomain.com
          }
          notification_email_from me@me.com
          smtp_server 127.0.0.1
          smtp_connect_timeout 30
          router_id LB_MASTER_ACTIVE
      }
      
      # Define the script used to check if haproxy is still working
      vrrp_script chk_haproxy {
          script "killall -0 haproxy"   # verify the pid existance
          interval 2                    # check every 2 seconds
          weight 2                      # add 2 points of prio if OK
      }
      
      # Virtual interface.
      vrrp_instance VI_1 {
          state MASTER
          interface eth1
          virtual_router_id 51
          priority 101
          smtp_alert 
      
          authentication {
              auth_type PASS
              auth_pass 1111 #replace with random string
          }
      
          vrrp_unicast_bind <my_private_ip>
          vrrp_unicast_peer <peers_private_ip>
      
          # Check if HAProxy is running or not.
          track_script {
              chk_haproxy
          }
          notify_master /usr/bin/reroute_global
      }
      
    5. 这是 BACKUP 服务器上的 Keepalived 设置:

      global_defs {
          notification_email {
              admin@mydomain.com
          }
          notification_email_from me@me.com
          smtp_server 127.0.0.1
          smtp_connect_timeout 30
          router_id LB_BACKUP_PASSIVE
      }
      
      # Define the script used to check if haproxy is still working
      vrrp_script chk_haproxy {
          script "killall -0 haproxy"   # verify the pid existance
          interval 2                    # check every 2 seconds
          weight 2                      # add 2 points of prio if OK
      }
      
      # Virtual interface.
      vrrp_instance VI_1 {
          state BACKUP
          interface eth1
          virtual_router_id 51
          priority 100
          smtp_alert 
          advert_int 1
      
          authentication {
              auth_type PASS
              auth_pass 1111 #replace with random string
          }
      
          vrrp_unicast_bind <my_private_ip>
          vrrp_unicast_peer <peers_private_ip>
      
          # Check if HAProxy is running or not.
          track_script {
              chk_haproxy
          }
          notify_master /usr/bin/reroute_global
      }
      
    6. 如上所述,我正在运行 Debian 7。从 keepalived 设置中可以看出,我有一个 notify_master 脚本。这是使脚本运行所需的一切:

      apt-get install cpanminus libssl-dev build-essential libxml2-dev libexpat1-dev
      cpanm SOAP::Lite XML::Hash::LX IO::Interface
      git clone https://github.com/softlayer/softlayer-api-perl-client.git
      mv softlayer-api-perl-client/SoftLayer /usr/share/perl5
      
    7. 现在所有依赖项都已到位,脚本应该可以工作。这是我保存为的脚本/usr/bin/reroute_global:

      #!/usr/bin/env perl
      use strict;
      use warnings;
      
      use SoftLayer::API::SOAP;
      use IO::Interface::Simple;
      
      # SoftLayer API Information
      my $api_user = 'YOUR_API_USERNAME';
      my $api_key  = 'YOUR_API_KEY';
      
      # Get the IP address associated with eth1
      my $if   = IO::Interface::Simple->new('eth1');
      
      # Create client object to SoftLayer_Account
      my $client = SoftLayer::API::SOAP->new('SoftLayer_Account', undef, $api_user, $api_key);
      
      # Get global IP address ID of first global IP address.
      my $global_ip_id = $client->getGlobalIpRecords()->result->[0]->{id};
      
      # Create client object to SoftLayer_Network_Subnet_IpAddress_Global
      $client = SoftLayer::API::SOAP->new('SoftLayer_Network_Subnet_IpAddress_Global', $global_ip_id, $api_user, $api_key);
      
      # Reroute global IP address to this systems public IP
      $client->route($if->address);
      

    您需要更改 API_USERNAME/KEY 以匹配您的 API 凭据。该脚本从您的 SoftLayer 全局 IP 地址中获取第一个全局 IP,然后将全局 IP 重新路由到系统。在故障转移的情况下,BACKUP 变为 MASTER 并运行脚本,该脚本将全局 IP 地址路由到自身。

    测试

    1. curl http://<global_IP>
    2. 在主服务器上,service haproxy stop
    3. 备份时:tail -f /var/log/syslog. 您应该看到如下内容:

      Feb 12 01:11:55 proxy2-west Keepalived_vrrp[11816]: VRRP_Script(chk_haproxy) succeeded
      Feb 12 01:11:55 proxy2-west Keepalived_vrrp[11816]: SMTP alert successfully sent.
      Feb 12 01:12:29 proxy2-west Keepalived_vrrp[11816]: VRRP_Instance(VI_1) forcing a new MASTER election
      Feb 12 01:12:29 proxy2-west Keepalived_vrrp[11816]: VRRP_Instance(VI_1) forcing a new MASTER election
      Feb 12 01:12:30 proxy2-west Keepalived_vrrp[11816]: VRRP_Instance(VI_1) Transition to MASTER STATE
      Feb 12 01:12:31 proxy2-west Keepalived_vrrp[11816]: VRRP_Instance(VI_1) Entering MASTER STATE
      Feb 12 01:12:31 proxy2-west Keepalived_vrrp[11816]: Opening script file /usr/bin/reroute_global
      
    4. curl http://<global_IP>(如果故障转移有效,它应该可以工作)
    • 5

相关问题

  • 具有动态路由的代理服务器

  • nginx 访问日志忽略某些请求

  • HAProxy 和“分片”

  • 带有 HAProxy 的远程 IP

  • SSL 网站的高可用性

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve