logo

HA-Proxy를 이용한 Load Balancer 운용

Front-End에서 운용되는 각종 서비스의 Load Balancer의 구성내역을 살펴본다.

서비스 구성

  • 공통: Round-Robin 분산 알고리즘 사용 (Default Value)
  • LDAP
  • InfluxDB
  • www.hongsnet.net
  • newsystem.hongsnet.net
  • edu.hongsnet.net
  • monitor.hongsnet.net
  • django-api.hongsnet.net
  • working.hongsnet.net

구성 내역

  • haproxy.cfg 설정 내역
# cat haproxy.cfg |grep -v '#'
global
    daemon

    # 연결할 수 있는 최대 connection 을 지정한다. 이걸 안하면 기본값이 2000 으로 설정된다.
    maxconn 8192

    # haproxy 프로세스를 구동할 user(gid/uid를 지정할 수도 있다.)
    user    root

    # ssl 을 구성할 때 key size를 지정한다.
    tune.ssl.default-dh-param 2048

    # haproxy는 로그를 남기기 위해서 file io 를 직접 처리하지 않는다. rsyslog 로 UDP 전송을 한다.
    log /dev/log local0
    log /dev/log local1 notice

    # ciphers 설정
    ssl-default-bind-ciphers ECDH+AESGCM:ECDH+AES128:ECDH+AES256:DH+AES128:DH+AES256:DH+CAMELLIA128:DH+CAMELLIA256:DH+SEEDCBC:RSA:!aNULL:!MD5:!eNULL:!RC4

    #소켓을 활성화한다.
    #stats socket /var/run/haproxy/info.sock mode 666 level user
    #stats timeout 2m

cache web_cache
    total-max-size 1024   # MB
    max-object-size 10000 # bytes
    max-age 180            # seconds

defaults
    log         global
    # mode is inherited by sections that follow
    #mode       tcp
    mode        http
    option      http-server-close
    option      forwardfor
    option      accept-invalid-http-request

    timeout     http-request  10s
    timeout     client        20s
    timeout     connect       10s
    timeout     server        30s
    timeout     http-keep-alive   10s

listen stats
    mode http
    bind 0.0.0.0:9000 # Listen on localhost:9000
    stats enable  # Enable stats page
    stats realm Haproxy\ Statistics  # Title text for popup window
    stats uri /haproxy_stats  # Stats URI
    stats auth  admin:패스워드

frontend ldap
    # receives traffic from clients
    bind :389
    default_backend ldap_server

backend ldap_server
    # relays the client messages to servers
    server  ldap1 172.16.0.247:389 check fall 2 rise 1
    server  ldap2 172.24.0.240:389 check fall 2 rise 1

frontend influxdb
    bind :8086
    default_backend influxdb_server

backend influxdb_server
    # relays the client messages to servers
    server  influxdb-slave1 172.16.0.189:8086 check fall 2 rise 1
    server  influsdb-slave2 172.24.0.251:8086 check fall 2 rise 1

frontend monitor_dev
    bind :80
    bind *:443 ssl crt /etc/haproxy/certs/hongs.pem

    # monitor-dev.hongsnet.net
    acl is_monitor_dev_web hdr_end(host) -i monitor-dev.hongsnet.net
    use_backend monitor_dev_web if is_monitor_dev_web

    # django-api.hongsnet.net
    acl is_django_api_web hdr_end(host) -i django-api.hongsnet.net
    use_backend django_api_web if is_django_api_web

    # working.hongsnet.net
    acl is_django_working_web hdr_end(host) -i working.hongsnet.net
    use_backend django_working_web if is_django_working_web

    # www.hongsnet.net
    acl is_www_hongsnet_net hdr_end(host) -i www.hongsnet.net
    use_backend www_hongsnet_net if is_www_hongsnet_net

    # hongsnet.net
    acl is_www_hongsnet_net hdr_end(host) -i hongsnet.net
    use_backend www_hongsnet_net if is_www_hongsnet_net

    # edu.hongsnet.net
    acl is_edu_hongsnet_net hdr_end(host) -i edu.hongsnet.net
    use_backend edu_hongsnet_net if is_edu_hongsnet_net

    # newsystem.hongsnet.net
    acl is_newsystem_hongsnet_net hdr_end(host) -i newsystem.hongsnet.net
    use_backend newsystem_hongsnet_net if is_newsystem_hongsnet_net

    # edu.example.com
    acl is_edu_example_com hdr_end(host) -i edu.example.com
    use_backend edu_example_com if is_edu_example_com

    #default_backend monitor_dev_web

backend monitor_dev_web
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /login
    server  tb3 172.16.0.253:32000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:32000 check fall 2 rise 1

backend django_api_web
    # relays the client messages to servers

    # 에러 파일 설정
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.252:32090 check fall 2 rise 1
    server  tb3 172.24.253:32090 check fall 2 rise 1
    server  tb3-docker 172.24.0.251:32090 check fall 2 rise 1

backend django_working_web
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:32091 check fall 2 rise 1
    server  tb3 172.16.0.253:32091 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:32091 check fall 2 rise 1

frontend monitor_nodejs
    bind :8081
    bind *:4438 ssl crt /etc/haproxy/certs/hongs.pem
    default_backend monitor_nodejs_api

backend monitor_nodejs_api
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /version
    server  tb3 172.16.0.253:32001 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:32001 check fall 2 rise 1

frontend monitor_nodejs_db
    bind :8900
    default_backend monitor_nodejs_db_server

backend monitor_nodejs_db_server
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /status
    server  tb3 172.16.0.253:32002 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:32002 check fall 2 rise 1

backend www_hongsnet_net
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:30000 check fall 2 rise 1
    server  tb3 172.16.0.253:30000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:30000 check fall 2 rise 1

backend edu_hongsnet_net
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:30000 check fall 2 rise 1
    server  tb3 172.16.0.253:30000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:30000 check fall 2 rise 1

backend newsystem_hongsnet_net
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:30000 check fall 2 rise 1
    server  tb3 172.16.0.253:30000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:30000 check fall 2 rise 1

backend edu_example_com
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:30000 check fall 2 rise 1
    server  tb3 172.16.0.253:30000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:30000 check fall 2 rise 1

backend study_hongsnet_net
    # relays the client messages to servers
    option httpchk
    http-check send meth GET  uri /
    server  tb2 172.24.0.252:30000 check fall 2 rise 1
    server  tb3 172.16.0.253:30000 check fall 2 rise 1
    server  tb3-docker 172.16.0.251:30000 check fall 2 rise 1

주요설정 내역

  • Mode
mode http

http 프로토콜을 지원하도록 설정한다.

  • X-Forwarded-For Add Header
option    forwardfor

request를 서버로 보낼 때 Backend에서는 HA-Proxy의 IP가 로그가 기록된다. 따라서 이 옵션을 추가하면 실제 클라이언트의 IP주소가 기록된다.

  • ACL 설정
acl is_www_hongsnet_net hdr_end(host) -i www.hongsnet.net 
use_backend www_hongsnet_net if is_www_hongsnet_net

요청되는 호스트의 URI에 www.hongsnet.net 이 검출되면, is_www_hongsnet_net으로 설정하고, 이 설정은 www_hongsnet_net의 Backend로 라우팅되도록 설정한다.

  • Back-end 설정
server  tb2.hongsnet.net 172.24.0.151:30000 fall 2 rise 1
server  tb3.hongsnet.net 172.16.0.158:30000 fall 2 rise 1
server  tb3-docker.hongsnet.net 172.16.0.251:30000 fall 2 rise 1

연결할 Back-end 서버들의 리스트다. 2번 health check가 실패하면 down으로 판단되고, 1번 성공하면 다시 Load Balancer 대상에 포함시킨다. 그리고 backup의 경우 서버점검 시 사용한다. 30000번 포트는 K8s의 NodePort 설정으로 외부에 노출되는 서비스 포트 이다.