ตั้ง Redis Sentinel High Availability บน VPS
Redis Sentinel คือระบบ High Availability ของ Redis ที่ monitor สถานะ master และ slave nodes เมื่อ master ล่ม Sentinel จะเลือก slave ที่ดีที่สุดขึ้นมาเป็น master ใหม่โดยอัตโนมัติ application ที่ connect ผ่าน Sentinel จะไม่เห็น downtime เหมาะสำหรับ production system ที่ใช้ Redis เป็น session store หรือ job queue
สถาปัตยกรรม Redis Sentinel
ต้องมี node อย่างน้อย 3 ตัวเพื่อ quorum ในการตัดสินใจ failover:
- Node 1: Redis Master (รับ write)
- Node 2: Redis Slave + Sentinel
- Node 3: Redis Slave + Sentinel (ถ้ามี VPS เดียวรัน 3 Sentinel ใน process แยกกันได้)
ติดตั้ง Redis บนทุก Node
sudo apt update
sudo apt install redis-server -y
# ตรวจ version
redis-cli --version
redis-server --version
ตั้งค่า Redis Master
sudo nano /etc/redis/redis.conf
# bind ให้ฟัง all interface (ใช้ firewall ควบคุม access แทน)
bind 0.0.0.0
# ตั้ง password (requirepass)
requirepass YourStrongRedisPassword
# ไม่ต้องตั้ง replicaof สำหรับ master
sudo systemctl restart redis-server
ตั้งค่า Redis Slave
sudo nano /etc/redis/redis.conf
bind 0.0.0.0
requirepass YourStrongRedisPassword
# ชี้ไปที่ master
replicaof MASTER-IP 6379
masterauth YourStrongRedisPassword
# Slave รับ read-only
replica-read-only yes
sudo systemctl restart redis-server
ตั้งค่า Redis Sentinel
sudo nano /etc/redis/sentinel.conf
port 26379
daemonize yes
logfile /var/log/redis/sentinel.log
pidfile /var/run/redis/redis-sentinel.pid
# Monitor master ชื่อ "mymaster"
sentinel monitor mymaster MASTER-IP 6379 2
# quorum = 2 หมายถึงต้องมี Sentinel เห็นด้วยอย่างน้อย 2 ตัว
sentinel auth-pass mymaster YourStrongRedisPassword
# Failover หลัง master ไม่ตอบสนอง 5 วินาที
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sudo redis-sentinel /etc/redis/sentinel.conf
# หรือรันเป็น service
sudo systemctl enable --now redis-sentinel
ตรวจสอบว่า Redis Sentinel ทำงานถูกต้อง
# ตรวจสถานะ replication จาก master
redis-cli -a YourStrongRedisPassword info replication
# ตรวจ Sentinel status
redis-cli -p 26379 sentinel masters
redis-cli -p 26379 sentinel slaves mymaster
# ดู Sentinel log
sudo tail -f /var/log/redis/sentinel.log
# ทดสอบ failover (หยุด master แล้วดูว่า slave ขึ้นมาแทน)
sudo systemctl stop redis-server # บน master node
# รอสัก 10 วิ แล้วตรวจจาก slave
redis-cli -a YourStrongRedisPassword info replication
info replication หาบรรทัด role:master หรือ role:slave เพื่อยืนยันว่า node นั้นทำหน้าที่อะไรอยู่
แก้ปัญหาที่พบบ่อย
Slave ไม่ sync กับ Master (repl-backlog เต็ม)
เพิ่ม replication buffer ใน redis.conf ของ master:
sudo nano /etc/redis/redis.conf
repl-backlog-size 64mb
client-output-buffer-limit replica 256mb 64mb 60
sudo systemctl restart redis-server
Sentinel ไม่ทำ failover (quorum ไม่ครบ)
ตรวจว่า Sentinel ทุกตัว connect ถึงกันได้:
# ตรวจ Sentinel เห็นกันทั้งหมดไหม
redis-cli -p 26379 sentinel sentinels mymaster
# ต้องเห็น Sentinel ทุกตัวในรายการ
# ตรวจ firewall port 26379
sudo ufw allow from OTHER-SENTINEL-IP to any port 26379
Application connect ไม่ได้หลัง failover
Application ต้องใช้ Sentinel-aware client library ไม่ใช่ connect ตรงไปที่ Redis IP:
# PHP (Predis)
$sentinels = [
['127.0.0.1', 26379],
['NODE2-IP', 26379],
];
$client = new Predis\Client($sentinels, ['replication' => 'sentinel', 'service' => 'mymaster']);
# Python (redis-py)
from redis.sentinel import Sentinel
sentinel = Sentinel([('127.0.0.1', 26379), ('NODE2-IP', 26379)])
master = sentinel.master_for('mymaster', password='YourPassword')
Error: "NOAUTH Authentication required"
ลืมตั้ง masterauth ใน slave config:
sudo nano /etc/redis/redis.conf
masterauth YourStrongRedisPassword
sudo systemctl restart redis-server
คำถามที่พบบ่อย
Redis Sentinel กับ Redis Cluster ต่างกันอย่างไร?
Redis Sentinel ทำ High Availability โดยสลับ master อัตโนมัติ แต่ข้อมูลยังอยู่บน node เดียว Redis Cluster ทำ horizontal sharding กระจายข้อมูลหลาย shard พร้อม HA ในตัว เหมาะสำหรับ dataset ใหญ่กว่า RAM node เดียว Sentinel เหมาะสำหรับส่วนใหญ่ที่ต้องการ HA ไม่ใช่ scaling
ต้องมี VPS กี่ตัวสำหรับ Redis Sentinel?
อย่างน้อย 3 node สำหรับ quorum ที่ถูกต้อง ถ้ามีแค่ 2 Sentinel จะไม่สามารถทำ failover ได้เพราะ quorum ไม่ครบ ถ้า budget จำกัดสามารถรัน Sentinel 3 process บน 2 VPS ได้แต่ไม่เหมาะกับ production จริง
Redis Sentinel แจ้งเตือนได้ไหมเมื่อ failover เกิดขึ้น?
ได้ ผ่าน sentinel notification-script ที่ตั้งใน sentinel.conf Sentinel จะเรียก script ที่กำหนดเมื่อ sdown หรือ odown event เกิดขึ้น script รับ arguments เป็น event type และ master name แล้วส่งแจ้งเตือนได้ตามต้องการ