HAProxyで簡単ロードバランサー

HAProxyを導入

クライアントiOSアプリのバックエンドサーバーで時々ロードバランサーが機能していない時があり、これを契機にkeepalivedとipvsadmとHAProxyによる高可用性クラスタを組むことになりました。

ただ、データセンター側の制約でHAIP(VIP)が構成出来ないことが発覚して、急きょHAProxyとDNSラウンドロビンでAppサーバーにラウンドロビンすることになりました。以下、設定メモです。

HAProxyのインストール

$ sudo yum install -y haproxy

/etc/haproxy/haproxy.cfgの構成

global
    chroot      /var/lib/haproxy
    daemon
    log         127.0.0.1 daemon
    pidfile     /var/run/haproxy.pid
    maxconn     4192
    user        haproxy
    group       haproxy
    tune.ssl.default-dh-param 2048

defaults
    mode                    http
    log                       global
    option                  httplog
    option                  dontlognull
    option                  http-server-close
    option                  forwardfor
    option                  redispatch
    retries                 3
    timeout http-request    20s
    timeout queue           10m
    timeout connect         30s
    timeout client          10m
    timeout server          10m
    timeout http-keep-alive 30s
    timeout check           40s
    maxconn                 3000


frontend    ssl_proxy
            mode http
            maxconn 1024
            timeout client  40s
            bind    *:443 ssl crt /etc/haproxy/server.pem
            http-request set-header X-Forwarded-Host %[req.hdr(host)]
            option forwardfor
            use_backend default_ssl


frontend    multiple_domains
            mode    http
            maxconn 1024
            timeout client  40s
            bind    *:80
            bind    *:8008
            bind    *:8080
            acl is_v1 hdr_beg(host) -i *****.com
            acl is_v2 hdr_beg(host) -i *****.com
            acl is_v3 hdr_beg(host) -i *****.com
            acl is_v4 hdr_beg(host) -i *****.com
            redirect prefix http://*****.com code 301 if { hdr(host) -i *****.com }

            use_backend virtual_web if is_v1
            use_backend virtual_api if is_v2
            use_backend virtual_admin if is_v3
            use_backend virtual_q if is_v4

frontend  http-in
             bind *:8088
             stats enable
             stats auth admin:********
             stats hide-version
             stats show-node
             stats refresh 60s
             stats uri /haproxy?stats

backend     virtual_web
            mode    http
            balance static-rr
            timeout connect 40s
            timeout server  40s
            timeout check   15s
            option  httplog
            server  default_web01 *****:80 check inter 5000 fall 2


backend     default_ssl
            mode    http
            balance static-rr
            stick-table type ip size 200k expire 60m
            stick on src
            timeout connect 40s
            timeout server  40s
            timeout check   15s
            option  httplog
            server  default_ssl01 ***** check inter 5000 fall 2
            server  default_ssl02 ***** check inter 5000 fall 2
            server  default_ssl03 ***** check inter 5000 fall 2

backend     virtual_api
            mode    http
            balance static-rr
            timeout connect 40s
            timeout server  40s
            timeout check   15s
            option  httplog
            server  appapi01  *****:3000 check inter 5000 fall 2
            server  appapi02  *****:3000 check inter 5000 fall 2
            server  appapi03  *****:3000 check inter 5000 fall 2

backend     virtual_admin
            mode    http
            balance static-rr
            timeout connect 40s
            timeout server  40s
            timeout check   15s
            option  httplog
            server  appadmin01  *****:8080 check inter 5000 fall 2
            server  appadmin02  *****:8080 check inter 5000 fall 2
            server  appadmin03  *****:8080 check inter 5000 fall 2

backend     virtual_q
            mode    http
            balance static-rr
            timeout connect 40s
            timeout server  40s
            timeout check   15s
            option  httplog
            server  appq01  *****:443 check inter 5000 fall 2
            server  appq02  *****:443 check inter 5000 fall 2
            server  appq03  *****:443 check inter 5000 fall 2

追記

デバッグ用にフェイルオーバーする時間が長くなっているので、こちら短くするように調整です。