반응형
우분투 24.04에서 Keepalived(VIP)와 Predixy(로드밸런스)를 사용해 Redis 클러스터를 구성하는 방법
Redis 클러스터 아키텍처
Keepalived(VIP): 단일 접속점 제공, Predixy 장애 시 VIP를 Backup으로 이동
Predixy(Load Balancer): Redis 클러스터 로드밸런서, 클러스터 구조를 자동 감지
Redis Cluster: 3노드 × 2포트, Master/Replica 구성

mermaid(Link)
더보기
---
graph TD
subgraph Client
A[클라이언트<br/>redis-cli, 앱]
end
subgraph LoadBalancer
B[VIP<br/>192.168.0.110]
end
subgraph Proxy
P1[Predixy<br/>192.168.0.111:6379]
P2[Predixy<br/>192.168.0.112:6379]
P3[Predixy<br/>192.168.0.113:6379]
end
subgraph "Redis Cluster (3 Master + 3 Replica)"
M1[node111:6381<br/>Master<br/>0–5460]
S1[node112:6382<br/>Slave of M1]
M2[node112:6381<br/>Master<br/>5461–10922]
S2[node113:6382<br/>Slave of M2]
M3[node113:6381<br/>Master<br/>10923–16383]
S3[node111:6382<br/>Slave of M3]
end
A --> B
B --> P1
B --> P2
B --> P3
P1 --> M1 & M2 & M3
P2 --> M1 & M2 & M3
P3 --> M1 & M2 & M3
M1 --> S1
M2 --> S2
M3 --> S3
style B fill:#4CAF50,stroke:#333,color:white
style P1 fill:#2196F3,stroke:#333,color:white
style P2 fill:#2196F3,stroke:#333,color:white
style P3 fill:#2196F3,stroke:#333,color:white
style M1 fill:#FF5722,stroke:#333,color:white
style M2 fill:#FF5722,stroke:#333,color:white
style M3 fill:#FF5722,stroke:#333,color:white
style S1 fill:#FFC107,stroke:#333,color:black
style S2 fill:#FFC107,stroke:#333,color:black
style S3 fill:#FFC107,stroke:#333,color:black
---
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
sudo systemctl disable --now redis-server
Redis 설정
sudo mkdir -pv /var/lib/redis/{6381,6382}
sudo chown redis:redis /var/lib/redis/6381
sudo chown redis:redis /var/lib/redis/6382
Redis 포트별 설정
- redis-6381.conf
- redis-6382.conf
더보기
---
redis-6381.conf
cat <<'EOF' | sudo tee /etc/redis/redis6381.conf > /dev/null
# 네트워크 설정
bind 0.0.0.0
port 6381
daemonize yes
protected-mode no
# 보안 설정
requirepass redis_password
masterauth redis_password
# 파일 경로
dir /var/lib/redis/6381
pidfile /var/run/redis6381.pid
logfile "/var/log/redis/redis6381.log"
# 클러스터 설정
cluster-enabled yes
cluster-config-file nodes6381.conf
cluster-node-timeout 3000
# 지속성 (Persistence)
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
# 메모리 및 퍼포먼스
maxmemory-policy allkeys-lru
tcp-keepalive 60
timeout 0
tcp-backlog 511
hz 10
# 로그 레벨
loglevel notice
# 백그라운드 저장 실패 시 쓰기 중단 방지 (운영 환경 권장)
stop-writes-on-bgsave-error no
# RDB / AOF 병행 사용 시 안전 모드
rdbcompression yes
rdbchecksum yes
aof-use-rdb-preamble yes
# 기타
supervised no
EOF
redis-6381.con2
cat <<'EOF' | sudo tee /etc/redis/redis6381.conf > /dev/null
# 네트워크 설정
bind 0.0.0.0
port 6381
daemonize yes
protected-mode no
# 보안 설정
requirepass redis_password
masterauth redis_password
# 파일 경로
dir /var/lib/redis/6381
pidfile /var/run/redis6381.pid
logfile "/var/log/redis/redis6381.log"
# 클러스터 설정
cluster-enabled yes
cluster-config-file nodes6381.conf
cluster-node-timeout 3000
# 지속성 (Persistence)
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
dbfilename dump.rdb
save 900 1
save 300 10
save 60 10000
# 메모리 및 퍼포먼스
maxmemory-policy allkeys-lru
tcp-keepalive 60
timeout 0
tcp-backlog 511
hz 10
# 로그 레벨
loglevel notice
# 백그라운드 저장 실패 시 쓰기 중단 방지 (운영 환경 권장)
stop-writes-on-bgsave-error no
# RDB / AOF 병행 사용 시 안전 모드
rdbcompression yes
rdbchecksum yes
aof-use-rdb-preamble yes
# 기타
supervised no
EOF
---
Redis 비밀번호(REDISCLI_AUTH) 저장
- Redis 비밀번호 추가
export REDISCLI_AUTH=redis_password
- Redis 비밀번호 삭제
unset REDISCLI_AUTH
인스턴스 실행
redis-server /etc/redis/redis6381.conf
redis-server /etc/redis/redis6382.conf
인스턴스 종료
redis-cli -p 6381 SHUTDOWN
redis-cli -p 6382 SHUTDOWN
redis-server 프로세스 종료
ps -ef | grep redis-server | grep -v grep | awk '{print $2}' | xargs sudo kill
클러스터 생성
redis-cli --cluster create \
192.168.0.111:6381 192.168.0.111:6382 \
192.168.0.112:6381 192.168.0.112:6382 \
192.168.0.113:6381 192.168.0.113:6382 \
--cluster-replicas 1
728x90
Predixy 구성(Docker)
mkdir -p /docker-container
cd /docker-container
git clone https://github.com/haandol/predixy.git
cd predixy
vim docker-compose.yml
services:
predixy:
image: haandol/predixy:latest
container_name: predixy
hostname: predixy
restart: always
network_mode: host
volumes:
- ./conf:/etc/predixy/conf
- ./logs:/var/log/predixy
- /etc/localtime:/etc/localtime:ro
ports:
- 6379:6379
Predixy 설정 파일
- predixy.conf
더보기
---
cat <<'EOF' | sudo tee conf/predixy.conf > /dev/null
Name PredixyExample
Bind 0.0.0.0:6379
WorkerThreads 4
MaxMemory 0
ClientTimeout 300
Log /var/log/predixy/predixy.log
LogRotate 1d 1G
LogVerbSample 0
LogDebugSample 0
LogInfoSample 10000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1
Include auth.conf
Include cluster.conf
Include latency.conf
EOF
---
- cluster.conf
vim conf/cluster.conf
ClusterServerPool {
Password redis_password
MasterReadPriority 60
StaticSlaveReadPriority 50
DynamicSlaveReadPriority 50
RefreshInterval 1
ServerTimeout 1
ServerFailureLimit 10
ServerRetryTimeout 1
Servers {
+ 192.168.0.111:6381
+ 192.168.0.111:6382
+ 192.168.0.112:6381
+ 192.168.0.112:6382
+ 192.168.0.113:6381
+ 192.168.0.113:6382
}
}
- auth.conf
더보기
---
cat <<'EOF' | sudo tee conf/auth.conf > /dev/null
Authority {
Auth redis_password {
Mode write
}
Auth {
Mode write
}
Auth "#a complex password#" {
Mode admin
}
}
EOF
---
Docker Compose 실행
docker compose up -d
Docker Compose 중지
docker compose down -v
Keepalived 구성(VIP)
설치
sudo apt install -y keepalived
Predixy 상태 체크 스크립트
vim /usr/local/bin/chk_predixy.sh
#!/bin/bash
CONTAINER_NAME="predixy"
PORT=6379
LOG="/var/log/keepalived/chk_predixy.log"
log_msg() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$LOG"
}
# 1. Docker 컨테이너 상태 확인
if ! docker ps --filter "name=${CONTAINER_NAME}" --filter "status=running" | grep -q ${CONTAINER_NAME}; then
log_msg "ERROR: Predixy container is not running."
exit 1
fi
# 2. 6379 포트가 Host에서 LISTEN 상태인지 확인
if ! nc -z localhost ${PORT}; then
log_msg "ERROR: Predixy is not responding on port ${PORT}."
exit 1
fi
# 3. 컨테이너 내부에서 Predixy 프로세스 확인
if ! docker exec ${CONTAINER_NAME} sh -c "pgrep -f predixy > /dev/null"; then
log_msg "ERROR: Predixy process is not running inside the container."
exit 1
fi
log_msg "INFO: Predixy is healthy."
exit 0
chmod +x /usr/local/bin/chk_predixy.sh
Keepalived 설정
- MASTER(state MASTER, priority 100)
- BACKUP(state BACKUP, priority 99)
- BACKUP(state BACKUP, priority 98)
vim /etc/keepalived/keepalived.conf
더보기
---
MASTER : state MASTER, priority 100
cat <<'EOF' | sudo tee /etc/keepalived/keepalived.conf > /dev/null
vrrp_script chk_predixy {
script "/usr/local/bin/chk_predixy.sh"
interval 3
weight -20
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass redis123
}
virtual_ipaddress {
192.168.0.110
}
track_script {
chk_predixy
}
}
EOF
BACKUP : state BACKUP, priority 99
cat <<'EOF' | sudo tee /etc/keepalived/keepalived.conf > /dev/null
vrrp_script chk_predixy {
script "/usr/local/bin/chk_predixy.sh"
interval 3
weight -20
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass redis123
}
virtual_ipaddress {
192.168.0.110
}
track_script {
chk_predixy
}
}
EOF
BACKUP : state BACKUP, priority 98
cat <<'EOF' | sudo tee /etc/keepalived/keepalived.conf > /dev/null
vrrp_script chk_predixy {
script "/usr/local/bin/chk_predixy.sh"
interval 3
weight -20
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 98
advert_int 1
authentication {
auth_type PASS
auth_pass redis123
}
virtual_ipaddress {
192.168.0.110
}
track_script {
chk_predixy
}
}
EOF
---
설정 구문 검사
sudo keepalived -t -f /etc/keepalived/keepalived.conf
재시작
sudo systemctl enable --now keepalived
VIP 확인
ip addr show enp0s3 | grep "192.168.0.100"
클라이언트 접속
직접 Redis 노드에 접속하여 클러스터 명령어 실행
redis-cli -h 192.168.0.111 -p 6381 ping
redis-cli -h 192.168.0.111 -p 6381 cluster info
redis-cli -h 192.168.0.111 -p 6381 cluster nodes
redis-cli -h 192.168.0.111 -p 6381 info replication
Predixy를 통한 기본 명령어
redis-cli -h 192.168.0.110 -p 6379 ping
redis-cli -h 192.168.0.110 -p 6379 cluster info
redis-cli -h 192.168.0.110 -p 6379 cluster nodes
redis-cli -h 192.168.0.110 -p 6379 set test "hello"
redis-cli -h 192.168.0.110 -p 6379 get test
참고URL
- GitHub : predixy
728x90
반응형
'리눅스' 카테고리의 다른 글
| 우분투에서 기본 쉘을 dash에서 bash로 변경하는 방법 (0) | 2025.11.17 |
|---|---|
| HAProxy Statistics Page(Statistics Dashboard) (0) | 2025.11.14 |
| 일반 사용자 계정으로 root 소유의 파일을 직접 쓰는 방법 (0) | 2025.11.14 |
| Apache에 커스텀 헤더 추가하기 (0) | 2025.11.13 |
| HAProxy의 Stats 페이지에서 백엔드 서버의 상태를 제어할 때 사용하는 관리 명령 (0) | 2025.11.07 |