홈/엔지니어/네트워킹 / 서버/HAProxy 로드밸런서 설치 · 헬스체크 · 스티…
네트워킹 / 서버중급UbuntuDebianCentOShaproxy로드밸런서헬스체크

HAProxy 로드밸런서 설치 · 헬스체크 · 스티키세션

HAProxy 설치부터 라운드로빈·최소연결 알고리즘, 헬스체크, 스티키세션, SSL 종료까지 실무 설정 방법을 단계별로 설명합니다.

목차
  1. 설치
  2. 기본 설정 구조
  3. 헬스체크 설정
  4. 스티키세션 (세션 유지)
  5. Stats 대시보드
  6. 설정 검증 및 재로드

설치

Bash
# Ubuntu / Debian
sudo apt update && sudo apt install -y haproxy

# CentOS / RHEL
sudo yum install -y haproxy

# 버전 확인
haproxy -v

# 서비스 시작
sudo systemctl enable --now haproxy

기본 설정 구조

CODE
/etc/haproxy/haproxy.cfg
HAPROXY
global
    log /dev/log local0
    maxconn 50000
    user haproxy
    group haproxy
    daemon

defaults
    log     global
    mode    http
    option  httplog
    option  dontlognull
    timeout connect 5s
    timeout client  30s
    timeout server  30s
    option  forwardfor        # X-Forwarded-For 헤더 추가
    option  http-server-close

# 프론트엔드 (클라이언트 연결 수신)
frontend web_front
    bind *:80
    bind *:443 ssl crt /etc/ssl/mysite.pem
    redirect scheme https if !{ ssl_fc }   # HTTP → HTTPS 리다이렉트
    default_backend web_back

# 백엔드 (서버 풀)
backend web_back
    balance roundrobin            # 알고리즘: roundrobin, leastconn, source
    option  httpchk GET /health   # 헬스체크 경로

    server web1 192.168.1.10:3000 check weight 1
    server web2 192.168.1.11:3000 check weight 1
    server web3 192.168.1.12:3000 check weight 2  # 가중치 2배

헬스체크 설정

HAPROXY
backend api_back
    option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.example.com
    http-check expect status 200

    server api1 10.0.0.1:8080 check inter 2s rise 2 fall 3
    server api2 10.0.0.2:8080 check inter 2s rise 2 fall 3
    # inter: 체크 간격, rise: 복구 판정 횟수, fall: 장애 판정 횟수

    # 백업 서버 (모든 서버 장애 시 사용)
    server backup 10.0.0.99:8080 check backup

스티키세션 (세션 유지)

HAPROXY
backend app_back
    balance leastconn
    cookie SERVERID insert indirect nocache   # 쿠키 기반 스티키세션

    server app1 10.0.0.1:3000 check cookie app1
    server app2 10.0.0.2:3000 check cookie app2

Stats 대시보드

HAPROXY
frontend stats
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 10s
    stats auth admin:StrongPass123   # 기본 인증
    stats show-legends

브라우저에서 http://서버IP:8404/stats 접속


설정 검증 및 재로드

Bash
# 설정 문법 검사
sudo haproxy -c -f /etc/haproxy/haproxy.cfg

# 무중단 재로드 (기존 연결 유지)
sudo systemctl reload haproxy

# 상태 확인
sudo systemctl status haproxy
#haproxy#로드밸런서#헬스체크#스티키세션#고가용성
편집 안내 · Editorial Note

AI 도구가 초안 작성에 사용될 수 있습니다. 명령어의 실행 여부와 검증 환경은 각 가이드에 기재된 근거를 확인하세요. 적용 전 사용 중인 버전의 공식 문서를 확인하고, 오류는 이메일로 알려주세요. 이메일로 제보

주제 관련 공식 문서(자동 연결 · 문장별 근거 아님)OpenSSL 공식 문서 ↗

질문 & 답변 (Q&A)

이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.