본문 바로가기

리눅스

우분투 24.04에서 Redis 클러스터를 구성하고 HAProxy + Keepalived로 고가용성을 확보하는 방법

반응형

우분투 24.04에서 Redis 클러스터를 구성하고 HAProxy + Keepalived로 고가용성을 확보하는 방법

서버 구성 개요

서버 IP 주소 Redis 인스턴스 역할
VIP 192.168.0.100 - 클라이언트 접속용
node1 192.168.0.101 6381, 6382 Redis + HAProxy + Keepalived
node2 192.168.0.102 6381, 6382 Redis + HAProxy + Keepalived
node3 192.168.0.103 6381, 6382 Redis + HAProxy + Keepalived

필요 패키지 설치

sudo apt update
sudo apt install -y curl gnupg lsb-release

커널 튜닝(Redis 성능 최적화)

Swap 비활성화

sudo swapoff -a

(또는)

sudo sed -i.bak '/swap/s/^/#/' /etc/fstab

파일 디스크립터 증가

echo "redis soft nofile 65535" | sudo tee -a /etc/security/limits.conf
echo "redis hard nofile 65535" | sudo tee -a /etc/security/limits.conf

(또는)

cat <<EOF | sudo tee -a /etc/security/limits.conf > /dev/null

# Add parameter for WebServer
*               soft    nofile          65535
*               hard    nofile          65535
*               soft    nproc           unlimited
*               hard    nproc           unlimited
EOF

TCP 커널 파라미터

cat <<'EOF' | sudo tee -a /etc/sysctl.conf > /dev/null

# === Optimized TCP Settings (High Traffic) ===
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 1024 65535
EOF

sudo sysctl -p

TCP backlog 설정

echo "net.core.somaxconn = 65536" | sudo tee -a /etc/sysctl.conf

sudo sysctl -p

Overcommit Memory 설정

echo "vm.overcommit_memory=1" | sudo tee -a /etc/sysctl.conf

sudo sysctl -p

Transparent Huge Pages(THP) 비활성화

echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag

(또는) rc.local 활성화

- 변군이글루 블로그 : 우분투 24.04에서 rc.local을 활성화하는 방법

cat <<EOF | sudo tee -a /etc/rc.local > /dev/null

# Transparent Huge Pages 비활성화
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
EOF
sudo chmod +x /etc/rc.local
cat <<EOF | sudo tee -a /lib/systemd/system/rc-local.service > /dev/null

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now rc-local.service

Redis 설치 및 클러스터 구성

Redis 설치

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt update
sudo apt install -y redis-server
redis-server --version
Redis server v=8.2.3 sha=00000000:1 malloc=jemalloc-5.3.0 bits=64 build=8ef393086911f0f6
sudo systemctl disable --now redis-server

Redis 설정

port 6381

cat <<'EOF' | sudo tee /etc/redis/redis6381.conf > /dev/null
bind 0.0.0.0
port 6381
dir /var/lib/redis6381
pidfile /var/run/redis6381.pid
logfile "/var/log/redis/redis6381.log"

daemonize yes

cluster-enabled yes
cluster-config-file nodes6381.conf
cluster-node-timeout 3000

appendonly yes
appendfilename "appendonly.aof"
EOF

port 6382

cat <<'EOF' | sudo tee /etc/redis/redis6382.conf > /dev/null
bind 0.0.0.0
port 6382
dir /var/lib/redis6382
pidfile /var/run/redis6382.pid
logfile "/var/log/redis/redis6382.log"

daemonize yes

cluster-enabled yes
cluster-config-file nodes6382.conf
cluster-node-timeout 3000

appendonly yes
appendfilename "appendonly.aof"
EOF

인스턴스별 디렉토리 생성 후 실행

redis-server /etc/redis/redis6381.conf
redis-server /etc/redis/redis6382.conf

클러스터 생성

redis-cli --cluster create \
  192.168.0.101:6381 192.168.0.102:6381 192.168.0.103:6381 \
  192.168.0.101:6382 192.168.0.102:6382 192.168.0.103:6382 \
  --cluster-replicas 1
  • 마스터 3개, 슬레이브 3개 자동 배치

클러스터 상태 확인

redis-cli -c -p 6381 cluster nodes

3. HAProxy 설치 및 설정

HAProxy 설치

sudo apt install -y haproxy
sudo systemctl enable --now haproxy

HAProxy 설정

sudo vim /etc/haproxy/haproxy.cfg
frontend redis_front
    bind *:6379
    mode tcp
    default_backend redis_cluster

backend redis_cluster
    mode tcp
    balance roundrobin
    option tcp-check
    tcp-check send PING\r\n
    tcp-check expect string +PONG
    tcp-check send QUIT\r\n
    tcp-check expect string +OK

    server node1-6381 192.168.0.101:6381 check
    server node1-6382 192.168.0.101:6382 check
    server node2-6381 192.168.0.102:6381 check
    server node2-6382 192.168.0.102:6382 check
    server node3-6381 192.168.0.103:6381 check
    server node3-6382 192.168.0.103:6382 check
  • 클라이언트는 VIP:6379로 접속
  • HAProxy가 정상 Redis 노드로 트래픽 전달

4. Keepalived 설치 및 VIP 구성

Keepalived 설치

sudo apt install -y keepalived
sudo systemctl enable --now keepalived

Keepalived 설정

VIP 헬스체크 스크립트

vim /usr/bin/chk_haproxy.sh
#!/bin/bash
if pidof haproxy > /dev/null; then
  exit 0
else
  exit 1
fi
chmod +x /usr/bin/chk_haproxy.sh

MASTER

vim /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
    script "/usr/bin/chk_haproxy.sh"
    interval 2
    weight 2
    fall 2
    rise 2
}

vrrp_instance VI_1 {
    interface eth0
    state MASTER
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redis_pass
    }
    virtual_ipaddress {
        192.168.0.100
    }
    track_script {
        chk_haproxy
    }
}
  • MASTER/BACKUP 설정은 서버마다 priority 조정
  • VIP는 HAProxy가 설치된 서버에만 존재

 

728x90
반응형