반응형
우분투 24.04에서 Redis Cluster를 구성하는 방법
구성 환경
서버 이름 | 아이피 주소 | 구분 | 비고 |
192.168.0.231 | VIP | Redis Port 6379 | |
cache-232 | 192.168.0.232 | redis-server, haproxy, keepalived | Redis Port 6381, 6382 |
cache-233 | 192.168.0.233 | redis-server, haproxy, keepalived | Redis Port 6381, 6382 |
cache-234 | 192.168.0.234 | redis-server | Redis Port 6381, 6382 |
1. 필요 패키지 설치
sudo apt update
sudo apt install -y curl gnupg lsb-release
2. 커널/시스템 튜닝
Swap 비활성화
- Redis는 메모리 기반 DB라 swap이 발생하면 지연이 심해집니다.
sudo swapoff -a
Overcommit Memory
- Redis는 fork()로 RDB/AOF 스냅샷을 뜨는데, Linux 기본 overcommit_memory=0이면 fork 실패 가능성이 있습니다.
echo "vm.overcommit_memory=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Transparent Huge Pages (THP) 비활성화
- THP는 큰 페이지 메모리 할당 시 성능에 도움을 주지만, Redis에서는 성능 저하/지연을 유발합니다.
cat /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" | sudo tee -a /etc/rc.local
sudo chmod +x /etc/rc.local
파일 디스크립터 제한
cat <<EOF | sudo tee -a /etc/security/limits.conf
* soft nproc unlimited
* hard nproc unlimited
* soft nofile 65536
* hard nofile 65536
EOF
TCP 커널 파라미터
- 네트워크 튜닝으로 동시 연결 안정성 강화
cat <<EOF | sudo tee -a /etc/sysctl.conf
# Redis kernel tuning
net.core.somaxconn=65535
net.ipv4.tcp_max_syn_backlog=8192
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_fin_timeout=15
EOF
sudo sysctl -p
3-1. Redis Server 설치
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
3.-2. Redis Server 설정
sudo mkdir -p /etc/redis/6381 /etc/redis/6382
sudo chown -R redis:redis /etc/redis/6381 /etc/redis/6382
- cache-232-6381[Port 6381]
cat <<EOF | sudo tee /etc/redis/6381/redis.conf > /dev/null
port 6381
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize no
pidfile /var/run/redis/nodes-6381.pid
logfile /var/log/redis/nodes-6381.log
dir /var/lib/redis/6381
bind 0.0.0.0
protected-mode no
EOF
- cache-232-6382[Port 6382]
cat <<EOF | sudo tee /etc/redis/6382/redis.conf > /dev/null
port 6382
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize no
pidfile /var/run/redis/nodes-6382.pid
logfile /var/log/redis/nodes-6382.log
dir /var/lib/redis/6382
bind 0.0.0.0
protected-mode no
EOF
3-3. 디렉토리 생성 및 권한 설정
sudo mkdir -p /var/lib/redis/6381 /var/lib/redis/6382
sudo mkdir -p /var/log/redis
sudo chown -R redis:redis /var/lib/redis /var/log/redis
3-4. Redis Server 서비스 등록(systemd)
각 포트별로 Redis Server 서비스 생성
- cache-232-6381[Port 6881]
cat <<EOF | sudo tee /etc/systemd/system/redis6381.service > /dev/null
[Unit]
Description=Redis 6381 Server
After=network.target
[Service]
Type=notify
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/6381/redis.conf --supervised systemd
ExecStop=/usr/bin/redis-cli -p 6381 shutdown
Restart=always
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
- cache-232-6382[Port 6882]
cat <<EOF | sudo tee /etc/systemd/system/redis6382.service > /dev/null
[Unit]
Description=Redis 6382 Server
After=network.target
[Service]
Type=notify
User=redis
Group=redis
ExecStart=/usr/bin/redis-server /etc/redis/6382/redis.conf --supervised systemd
ExecStop=/usr/bin/redis-cli -p 6382 shutdown
Restart=always
LimitNOFILE=65535
[Install]
WantedBy=multi-user.target
EOF
Redis Server 서비스 시작
sudo systemctl daemon-reload
- cache-232-6381[Port 6381]
sudo systemctl enable redis6381
sudo systemctl start redis6381
- cache-232-6382[Port 6382]
sudo systemctl enable redis6382
sudo systemctl start redis6382
4-1. Redis Cluster 생성
더보기
---
redis-cli 설치
sudo apt install -y redis-tools
---
클러스터 생성
redis-cli --cluster create \
192.168.0.232:6381 \
192.168.0.232:6382 \
192.168.0.233:6381 \
192.168.0.233:6382 \
192.168.0.234:6381 \
192.168.0.234:6382 \
--cluster-replicas 1
- --cluster-replicas 1 : 각 마스터당 1개의 슬레이브를 배치
- 명령 실행 시 확인 메시지에 yes 입력
4-2. 클러스터 상태 확인
redis-cli -c -p 6381 cluster nodes
728x90
5. HAProxy 설정(cache232, cache233)
HAProxy 설치
sudo apt install -y haproxy
HAProxy 시작
sudo systemctl enable --now haproxy
HAProxy 설정
sudo vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 4096
defaults
log global
mode tcp
option tcplog
option dontlognull
timeout connect 5s
timeout client 50s
timeout server 50s
# Redis Cluster VIP Frontend
frontend redis_front
bind *:6379
mode tcp
default_backend redis_cluster
# Redis Cluster Backend
backend redis_cluster
mode tcp
balance roundrobin
option tcp-check
# Health-check (PING → PONG → QUIT)
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send QUIT\r\n
tcp-check expect string +OK
# Redis 노드들 등록
server cache-232-6381 192.168.0.232:6381 check inter 2s fall 3 rise 2
server cache-232-6382 192.168.0.232:6382 check inter 2s fall 3 rise 2
server cache-233-6381 192.168.0.233:6381 check inter 2s fall 3 rise 2
server cache-233-6382 192.168.0.233:6382 check inter 2s fall 3 rise 2
server cache-234-6381 192.168.0.234:6381 check inter 2s fall 3 rise 2
server cache-234-6382 192.168.0.234:6382 check inter 2s fall 3 rise 2
6. Keepalived 설정
Keepalived 설치
sudo apt install -y keepalived
Keepalived 시작
sudo systemctl enable --now keepalived
chk_haproxy 스크립트 생성
cat <<EOF | sudo tee /usr/bin/chk_haproxy.sh > /dev/null
#!/bin/bash
# HAProxy 프로세스 확인
if echo "show info" | socat unix-connect:/run/haproxy/admin.sock stdio | grep -q "Nbproc"; then
exit 0 # HAProxy가 실행 중이면 성공 (0)
else
exit 1 # HAProxy가 실행 중이 아니면 실패 (1)
fi
sudo chmod +x /usr/bin/chk_haproxy.sh
네트워크 인터페이스 확인
ip addr show
ip link show
Keepalived 설정
- cache-232(MASTER)
sudo 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 eno1
state MASTER
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass redis_pass
}
virtual_ipaddress {
192.168.0.231/24
}
track_script {
chk_haproxy
}
}
- cache-233(BACKUP)
vrrp_script chk_haproxy {
script "/usr/bin/chk_haproxy.sh"
interval 2
weight 2
fall 2
rise 2
}
vrrp_instance VI_1 {
interface eno1
state BACKUP
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass redis_pass
}
virtual_ipaddress {
192.168.0.231/24
}
track_script {
chk_haproxy
}
}
HAProxy 및 Keepalived 서비스 재시작 및 확인
- HAProxy 재시작
sudo systemctl restart haproxy
- Keepalived 재시작
sudo systemctl restart keepalived
7. VIP 동작 확인
VIP가 정상적으로 할당되었는지 확인
ip addr show
HAProxy 상태 확인
sudo systemctl status haproxy
스크립트 테스트
- 0이 나와야 정상
/usr/bin/chk_haproxy.sh
echo $?
8. 클러스터 상태 확인
클러스터 상태 확인
redis-cli --cluster check 192.168.0.231:6379
노드 정보 확인
redis-cli -h 192.168.0.231 -p 6379 cluster nodes
클러스터 정보 확인
redis-cli -h 192.168.0.231 -p 6379 cluster info
참고URL
- 변군이글루 : 우분투 24.04에 최신 버전의 Redis 서버를 설치하는 방법
- 변군이글루 : 우분투 24.04에서 rc.local을 활성화하는 방법
- 변군이글루 : 우분투에서 스왑 메모리를 비활성화하는 방법
- 변군이글루 : HAProxy를 사용하여 고가용성(High Availability) 구성을 구현하는 방법
- 변군이글루 : HAProxy에서 TLS/SSL 인증서를 생성하고 적용하는 방법
728x90
반응형
'리눅스' 카테고리의 다른 글
Redis Cluster 모니터링를 구축하는 방법 (0) | 2025.08.27 |
---|---|
Kubernetes Cluster 구축하는 방법 (0) | 2025.08.25 |
VMware ESXi의 평가판 라이선스를 갱신하는 방법 (0) | 2025.08.25 |
우분투 24.04에서 PAM 기반 비밀번호 유효성 제한 및 계정 정책 설정 (0) | 2025.08.19 |
CentOS 7에서 pam_faillock 모듈을 사용해서 계정 잠금 정책을 설정하는 방법 (1) | 2025.08.18 |