[20260511] HAProxy 로드밸런싱을 활용한 WordPress 3-Tier 아키텍처 구축

📋 학습 요약

  • 주제: 로드밸런서(HAProxy)를 통한 트래픽 분산 및 WordPress 인프라 자동화 구축
  • 도구: Rocky Linux 9, HAProxy, Apache(httpd), PHP, MySQL, Shell Script
  • 핵심 구조:
    • L4: HAProxy (10.0.0.11)
    • WEB: Apache/PHP (10.0.0.12, 10.0.0.13)
    • DB: MySQL (10.0.0.14)

💻 1. 데이터베이스(DB) 서버 구성 (rocky9-4)

WordPress의 데이터를 저장할 MySQL 서버를 구축하고 외부 접속 권한을 설정합니다.

  • 핵심 작업:
    • mysql-server 설치 및 3306 포트 개방.
    • WordPress용 사용자(csm) 생성 및 원격 접속(%) 권한 부여.
    • 데이터베이스(wordpress) 생성.

💻 2. 웹(WEB) 서버 구성 및 WordPress 설치 (rocky9-2, rocky9-3)

Apache와 PHP를 연동하여 WordPress를 구동합니다. sed 명령어를 활용해 설정 파일 내 데이터베이스 정보를 수정하는 과정이 중요합니다.

  • 핵심 작업:
    • php-mysqlnd 패키지를 통해 웹과 DB 연동.
    • wp-config.php에서 DB 호스트를 10.0.0.14(DB 서버 IP)로 지정.
    • DirectoryIndex index.php 설정을 통해 PHP 파일 우선 순위 적용.
    • Health Check: 각 서버에 health.html을 생성하여 로드밸런싱 여부 확인.

💻 3. 로드밸런서(L4) 서버 구성 (rocky9-1)

HAProxy를 사용하여 클라이언트의 요청을 두 대의 웹 서버로 분산합니다.

  • 설정 포인트 (haproxy.cfg):
    • frontend: 80 포트로 들어오는 요청 수신.
    • backend: roundrobin 알고리즘을 사용하여 app1(10.0.0.12)과 app2(10.0.0.13)로 분산.

🛠️ 4. 인프라 구축 자동화 스크립트 (Shell Script)

복잡한 설치 과정을 한 번에 실행할 수 있도록 작성한 스크립트들입니다. 나중에 서버 증설 시 매우 유용하게 쓰일 수 있습니다.

A. 웹 서버 자동 설치 스크립트 (word.sh)

Bash
 
#! /bin/bash
dnf install -y wget tar httpd php php-cli php-gd php-mysqlnd
wget https://ko.wordpress.org/wordpress-6.9.4-ko_KR.tar.gz
tar xfvz wordpress-6.9.4-ko_KR.tar.gz
cp -ar wordpress/* /var/www/html/
sed -i "s/DirectoryIndex index.html/DirectoryIndex index.php/g" /etc/httpd/conf/httpd.conf
cp /var/www/html/{wp-config-sample.php,wp-config.php}
# DB 정보 자동 수정
sed -i "s/database_name_here/wordpress/g" /var/www/html/wp-config.php
sed -i "s/username_here/csm/g" /var/www/html/wp-config.php
sed -i "s/password_here/It12345@/g" /var/www/html/wp-config.php
sed -i "s/localhost/10.0.0.14/g" /var/www/html/wp-config.php
echo '$HOSTNAME' > /var/www/html/health.html
systemctl enable --now httpd
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

B. 로드밸런서 설정 자동화

Bash
 
#! /bin/bash
dnf install -y haproxy
sed -i "s/*:5000/*:80/g" /etc/haproxy/haproxy.cfg
sed -i "s/use_backend static/use_backend app/g" /etc/haproxy/haproxy.cfg
# 실제 웹 서버 IP 매핑
sed -i "s/127.0.0.1:5001/10.0.0.12:80/g" /etc/haproxy/haproxy.cfg
sed -i "s/127.0.0.1:5002/10.0.0.13:80/g" /etc/haproxy/haproxy.cfg
systemctl enable --now haproxy
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

🔍 주요 트러블슈팅 포인트

  • SCP 전송: scp word.sh root@10.0.0.13:/root/ 명령어를 통해 원격 서버로 스크립트를 배포하여 작업 효율을 극대화했습니다.
  • SELinux/Firewall: 웹, DB, HAProxy 간의 통신이 원활하도록 각 서비스 포트(80, 3306)를 정확히 개방하는 것이 필수입니다.
  • DB 권한: csm'@'%' 설정을 통해 로컬이 아닌 외부 웹 서버에서도 DB 접속이 가능하도록 조치했습니다.

📝 오늘의 회고

개별 서버 구축을 넘어 서비스의 고가용성(HA)을 위한 로드밸런싱 구조를 실습하며 인프라의 전체적인 흐름을 이해할 수 있었다. 특히 sed 명령어를 조합한 쉘 스크립트 자동화는 수동 작업의 실수를 줄여주고 속도를 획기적으로 높여준다는 점에서 엔지니어에게 필수적인 역량임을 다시 한번 체감했다.