고가용성 기본 사항
26.10 버전 차이
26.10에는 HAProxy 3.4.2가 포함되어 있으며 기본 밸런싱 알고리즘을 무작위로 변경하고 레거시 프로그램 섹션을 제거하며 이전에 경고로 처리된 일부 중복 이름을 거부합니다. 업그레이드하기 전에 실제 구성을 검증하십시오. 이전 사례가 여전히 적용된다고 가정하지 마세요. 공식 릴리스 노트
고가용성(HA)은 단일 장애 지점이 발생하더라도 서비스가 계속 작동하도록 보장합니다. 이 문서에서는 세 가지 일반적인 HA 솔루션인 keepalived(VIP 장애 조치), HAProxy(로드 밸런싱) 및 Pacemaker/Corosync(클러스터 리소스 관리)를 소개합니다.
keepalived(가상 IP 장애 조치)
keepalived는 VRRP 프로토콜을 사용하여 자동 가상 IP 주소 장애 조치를 활성화합니다. 기본 노드에 장애가 발생하면 백업 노드가 자동으로 VIP를 인수합니다.
설치
# Install on all nodes
sudo apt update
sudo apt install keepalived -y아키텍처 예
Node 1 (MASTER): 192.168.1.11
Node 2 (BACKUP): 192.168.1.12
Virtual IP (VIP): 192.168.1.100기본 노드 구성
sudo tee /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id LVS_NODE1
script_user root
enable_script_security
}
# Health check script
vrrp_script check_nginx {
script "/usr/bin/curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1 | grep -q 200"
interval 5
weight -20
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass MyS3cretP@ss
}
virtual_ipaddress {
192.168.1.100/24 dev eth0
}
track_script {
check_nginx
}
# Notification scripts (optional)
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
EOF백업 노드 구성
sudo tee /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id LVS_NODE2
script_user root
enable_script_security
}
vrrp_script check_nginx {
script "/usr/bin/curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1 | grep -q 200"
interval 5
weight -20
fall 3
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass MyS3cretP@ss
}
virtual_ipaddress {
192.168.1.100/24 dev eth0
}
track_script {
check_nginx
}
}
EOF알림 스크립트
sudo tee /etc/keepalived/notify.sh << 'SCRIPT'
#!/bin/bash
STATE=$1
DATETIME=$(date '+%Y-%m-%d %H:%M:%S')
echo "${DATETIME} - State changed to: ${STATE}" >> /var/log/keepalived-notify.log
case $STATE in
master)
echo "${DATETIME} - This node became MASTER" >> /var/log/keepalived-notify.log
# You can send alert notifications here
;;
backup)
echo "${DATETIME} - This node became BACKUP" >> /var/log/keepalived-notify.log
;;
fault)
echo "${DATETIME} - This node entered FAULT state" >> /var/log/keepalived-notify.log
;;
esac
SCRIPT
sudo chmod +x /etc/keepalived/notify.sh서비스 시작
# Start on all nodes
sudo systemctl enable --now keepalived
# Check status
sudo systemctl status keepalived
# Verify VIP
ip addr show eth0 | grep 192.168.1.100
# View logs
sudo journalctl -u keepalived -f
# Test failover
# Stop keepalived on the primary node
sudo systemctl stop keepalived
# Observe whether the backup node takes over the VIPHAProxy(로드 밸런싱)
HAProxy는 여러 스케줄링 알고리즘과 상태 확인을 지원하는 고성능 TCP/HTTP 로드 밸런서입니다.
설치
sudo apt update
sudo apt install haproxy -y
# Check version
haproxy -v기본 구성
sudo tee /etc/haproxy/haproxy.cfg << 'EOF'
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Performance tuning
maxconn 50000
tune.ssl.default-dh-param 2048
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
option http-server-close
timeout connect 5000
timeout client 50000
timeout server 50000
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
# Statistics page
listen stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats admin if LOCALHOST
stats auth admin:haproxy_pass
# HTTP frontend
frontend http_front
bind *:80
# HTTPS redirect
# redirect scheme https code 301 if !{ ssl_fc }
default_backend http_back
# HTTPS frontend
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/example.com.pem
http-request set-header X-Forwarded-Proto https
# Domain-based routing
acl host_app1 hdr(host) -i app1.example.com
acl host_app2 hdr(host) -i app2.example.com
use_backend app1_back if host_app1
use_backend app2_back if host_app2
default_backend http_back
# Backend server group
backend http_back
balance roundrobin
option httpchk GET /health
http-check expect status 200
server web1 192.168.1.21:80 check inter 5s fall 3 rise 2
server web2 192.168.1.22:80 check inter 5s fall 3 rise 2
server web3 192.168.1.23:80 check inter 5s fall 3 rise 2 backup
backend app1_back
balance leastconn
option httpchk GET /health
server app1a 192.168.1.31:8080 check
server app1b 192.168.1.32:8080 check
backend app2_back
balance source
server app2a 192.168.1.41:8080 check
server app2b 192.168.1.42:8080 check
EOFTCP 부하 분산(데이터베이스 등)
# Append to haproxy.cfg
cat << 'EOF' | sudo tee -a /etc/haproxy/haproxy.cfg
# MySQL load balancing
listen mysql_cluster
bind *:3306
mode tcp
option mysql-check user haproxy
balance roundrobin
server mysql1 192.168.1.51:3306 check inter 5s
server mysql2 192.168.1.52:3306 check inter 5s backup
# Redis Sentinel
listen redis
bind *:6379
mode tcp
option tcp-check
balance first
server redis1 192.168.1.61:6379 check inter 3s
server redis2 192.168.1.62:6379 check inter 3s
EOF로드 밸런싱 알고리즘
| 연산 | 설명 | 사용 사례 |
|---|---|---|
roundrobin | 라운드 로빈 | 기본; 서버의 성능은 비슷합니다 |
leastconn | 최소 연결 | 긴 연결, 고르지 못한 처리 시간 |
source | 소스 IP 해시 | 세션 지속성이 필요함 |
uri | URI 해시 | 캐시 서버 |
first | 두 번째를 사용하기 전에 첫 번째를 채우십시오. | 자원절약 |
시작 및 확인
# Check configuration
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# Start service
sudo systemctl enable --now haproxy
# View stats page
curl http://localhost:8404/stats
# Firewall
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 8404/tcpkeepalived + HAProxy 조합
가장 일반적인 HA 솔루션은 keepalived + HAProxy입니다. 즉, VIP를 관리하는 keepalived가 있는 활성-대기 모드의 HAProxy 인스턴스 2개입니다.
# keepalived configuration to check HAProxy status
sudo tee /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
router_id HAPROXY_NODE1
script_user root
enable_script_security
}
vrrp_script check_haproxy {
script "/usr/bin/killall -0 haproxy"
interval 2
weight -30
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass HAProxy_HA
}
virtual_ipaddress {
192.168.1.100/24
}
track_script {
check_haproxy
}
}
EOFPacemaker / Corosync(클러스터 리소스 관리)
Pacemaker와 Corosync는 엔터프라이즈급 클러스터 리소스 관리를 제공하여 복잡한 리소스 종속성과 오류 정책을 지원합니다.
설치
# Install on all nodes
sudo apt update
sudo apt install pacemaker corosync pcs resource-agents -y
# Set hacluster user password (same on all nodes)
sudo passwd hacluster
# Start the pcs management service
sudo systemctl enable --now pcsd클러스터 구성
# Authenticate all nodes from one node
sudo pcs host auth node1 node2 -u hacluster -p yourpassword
# Create the cluster
sudo pcs cluster setup ha-cluster node1 node2
# Start the cluster
sudo pcs cluster start --all
sudo pcs cluster enable --all
# Check cluster status
sudo pcs cluster status
sudo pcs status
# For two-node clusters, disable quorum policy
sudo pcs property set no-quorum-policy=ignore
# Disable STONITH (test environments; production should configure fencing devices)
sudo pcs property set stonith-enabled=false클러스터 리소스 구성
# Add a virtual IP resource
sudo pcs resource create cluster_vip ocf:heartbeat:IPaddr2 \
ip=192.168.1.100 \
cidr_netmask=24 \
nic=eth0 \
op monitor interval=10s
# Add an Nginx resource
sudo pcs resource create web_server ocf:heartbeat:nginx \
configfile=/etc/nginx/nginx.conf \
op monitor interval=10s timeout=30s \
op start timeout=60s \
op stop timeout=60s
# Ensure VIP and Nginx are on the same node
sudo pcs constraint colocation add web_server with cluster_vip INFINITY
# Ensure VIP starts before Nginx
sudo pcs constraint order cluster_vip then web_server
# Create a resource group (simpler approach)
sudo pcs resource group add web_group cluster_vip web_server
# View resource status
sudo pcs resource status
sudo pcs constraint show자원 관리
# Manually migrate resources
sudo pcs resource move web_group node2
# Clear migration constraints (must run after migration)
sudo pcs resource clear web_group
# Enable/disable resources
sudo pcs resource disable web_server
sudo pcs resource enable web_server
# Clear resource error state
sudo pcs resource cleanup web_server
# Maintenance mode
sudo pcs node standby node1 # Put node in standby
sudo pcs node unstandby node1 # Restore node
sudo pcs property set maintenance-mode=true # Global maintenance mode
sudo pcs property set maintenance-mode=false클러스터 모니터링
# Real-time cluster status monitoring
sudo crm_mon -1
# View cluster configuration
sudo pcs config show
# View cluster logs
sudo journalctl -u pacemaker -f
sudo journalctl -u corosync -f
# Corosync membership status
sudo corosync-cmapctl | grep members솔루션 비교
| 특징 | 연결 유지 | HAProxy | 심박조율기/Corosync |
|---|---|---|---|
| 주요 기능 | VIP 장애 조치 | 로드 밸런싱 | 클러스터 리소스 관리 |
| 구성 복잡성 | 단순한 | 중간 | 더 복잡함 |
| 리소스 유형 | VIP 전용 | HTTP/TCP 밸런싱 | 모두(VIP, 서비스, 파일 시스템 등) |
| 상태 점검 | 스크립트 기반 | 풍부한 프로토콜 검사 | 리소스 에이전트 기반 |
| 규모 | 노드 2개 | 대판 | 노드 2~32개 |
| 일반적인 사용 | 활성-대기 장애 조치 | 트래픽 분포 | 엔터프라이즈 HA |
추천 가이드
- 간단한 활성-대기: 연결 유지로 충분합니다.
- 웹 로드 밸런싱: HAProxy(HA용 연결 유지와 결합 가능)
- 복잡한 클러스터: Pacemaker/Corosync(다중 리소스, 복잡한 종속성)
- 가장 일반적인 조합: keepalived + HAProxy - 간단하고 효과적입니다.