Apache2 Load Balancing


Apache2 부하 분산 설정 (Apache2 Load Balancing Configuration)

부하 분산의 개념 (Concept of Load Balancing)

부하 분산은 여러 서버에 걸쳐 트래픽을 분산시켜 서버 성능을 최적화하고, 가용성을 높이며, 장애를 예방하는 기술입니다. Apache HTTP Server에서는 mod_proxy_balancer 모듈을 사용하여 부하 분산을 설정할 수 있습니다.

부하 분산 설정 (Setting up Load Balancing with mod_proxy_balancer)

1. 필요한 모듈 활성화

부하 분산을 설정하기 위해 필요한 모듈들을 활성화합니다.

sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo a2enmod proxy_http
2. 기본 부하 분산 설정

Apache 설정 파일 (예: /etc/apache2/sites-available/000-default.conf)에 부하 분산 설정을 추가합니다.

<VirtualHost *:80>
    ServerName www.example.com

    # 부하 분산 프록시 설정
    <Proxy "balancer://mycluster">
        BalancerMember http://backend1.example.com
        BalancerMember http://backend2.example.com
        BalancerMember http://backend3.example.com

        # 추가 설정 (옵션)
        ProxySet lbmethod=byrequests
    </Proxy>

    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/

    # 보안 설정
    <Proxy *>
        Require all granted
    </Proxy>

    # 추가적인 보안 헤더
    Header always set X-Frame-Options DENY
    Header always set X-Content-Type-Options nosniff
    Header always set X-XSS-Protection "1; mode=block"

    # 로그 설정
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

위 설정은 http://www.example.com으로 들어오는 모든 요청을 세 개의 백엔드 서버 (http://backend1.example.com, http://backend2.example.com, http://backend3.example.com)에 분산시킵니다.

3. 로드 밸런싱 방법 설정

Apache는 다양한 로드 밸런싱 방법을 제공합니다. 대표적인 방법은 byrequests, bytraffic, bybusyness, heartbeat입니다.

요청 수 기반 로드 밸런싱 (By Requests)

요청 수를 기준으로 균등하게 분산합니다.

ProxySet lbmethod=byrequests
트래픽 기반 로드 밸런싱 (By Traffic)

트래픽 양을 기준으로 분산합니다.

ProxySet lbmethod=bytraffic
작업량 기반 로드 밸런싱 (By Busyness)

서버의 작업량을 기준으로 분산합니다.

ProxySet lbmethod=bybusyness
심장박동 기반 로드 밸런싱 (Heartbeat)

서버의 상태를 주기적으로 확인하여 분산합니다.

ProxySet lbmethod=heartbeat
4. 헬스 체크 설정 (Health Check Configuration)

서버의 상태를 주기적으로 확인하여 비정상적인 서버를 제외하는 설정을 추가할 수 있습니다.

<Proxy "balancer://mycluster">
    BalancerMember http://backend1.example.com
    BalancerMember http://backend2.example.com
    BalancerMember http://backend3.example.com

    ProxySet lbmethod=byrequests
    ProxySet stickysession=JSESSIONID|jsessionid
    ProxySet failontimeout=on

    <Proxy "balancer://mycluster">
        BalancerMember http://backend1.example.com retry=5
        BalancerMember http://backend2.example.com retry=5
        BalancerMember http://backend3.example.com retry=5
    </Proxy>
</Proxy>
5. 예제 설정 파일

전체적인 설정 파일 예제는 다음과 같습니다.

<VirtualHost *:80>
    ServerName www.example.com

    # 부하 분산 프록시 설정
    <Proxy "balancer://mycluster">
        BalancerMember http://backend1.example.com retry=5
        BalancerMember http://backend2.example.com retry=5
        BalancerMember http://backend3.example.com retry=5

        ProxySet lbmethod=byrequests
        ProxySet stickysession=JSESSIONID|jsessionid
        ProxySet failontimeout=on
    </Proxy>

    ProxyPass / balancer://mycluster/
    ProxyPassReverse / balancer://mycluster/

    # 보안 설정
    <Proxy *>
        Require all granted
    </Proxy>

    # 추가적인 보안 헤더
    Header always set X-Frame-Options DENY
    Header always set X-Content-Type-Options nosniff
    Header always set X-XSS-Protection "1; mode=block"

    # 로그 설정
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

위 설정은 로드 밸런싱, 세션 스티키 (sticky session) 및 서버 상태 확인을 포함하여 전체적인 부하 분산 설정을 다룹니다.

요약 (Summary)

Apache HTTP Server에서 mod_proxy_balancer 모듈을 사용하여 부하 분산을 설정하면, 서버의 성능과 가용성을 높일 수 있습니다. 다양한 로드 밸런싱 방법을 선택하고, 헬스 체크를 통해 서버의 상태를 모니터링하며, 세션 스티키 설정을 통해 사용자 세션을 유지할 수 있습니다. 이러한 설정을 통해 웹 서버의 성능을 최적화하고, 높은 트래픽을 효율적으로 처리할 수 있습니다.


Leave a Reply

Your email address will not be published. Required fields are marked *