How to Build a Redis High‑Availability Cluster with Sentinel and VIP
This guide walks through setting up a Redis high‑availability solution using master‑slave replication, Redis Sentinel for automatic failover, and a floating VIP to provide a stable endpoint, covering environment preparation, configuration files, firewall rules, testing, and client integration.
Architecture Overview
Deploy three Redis instances (one master, two slaves) on separate hosts and use Redis Sentinel for automatic failover. Optionally use a floating virtual IP (VIP) that always points to the current master.
Installation
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make
cd src
sudo cp redis-cli redis-server redis-sentinel /usr/sbin/
sudo cp redis.conf sentinel.conf /etc/Master Configuration (redis.conf)
port 8000
daemonize yes
bind 0.0.0.0
pidfile /var/run/redis-8000.pid
logfile /var/log/redis/redis-8000.logSlave Configuration (redis.conf)
port 8000
daemonize yes
bind 0.0.0.0
pidfile /var/run/redis-8000.pid
logfile /var/log/redis/redis-8000.log
slaveof 192.168.56.101 8000Start Redis Servers
sudo redis-server /etc/redis.confRun the above command on each host, using the master configuration on the designated master and the slave configuration on the other two hosts.
Verify Replication
redis-cli -h 192.168.56.101 -p 8000 info replicationIf no slave information appears, open firewall ports 8000 and 6800:
sudo vim /etc/sysconfig/iptables # add rules for 8000 and 6800
sudo service iptables restartSentinel Configuration (sentinel.conf)
daemonize yes
port 6800
logfile /var/log/redis/sentinel.log
pidfile /var/run/sentinel.pid
sentinel monitor master8000 192.168.56.101 8000 2
sentinel down-after-milliseconds master8000 5000
sentinel failover-timeout master8000 15000
# Optional VIP migration script
sentinel client-reconfig-script master8000 /opt/notify_master6800.shStart Sentinel
redis-sentinel /etc/sentinel.confFailover Test
Shut down the master: redis-cli -h 192.168.56.101 -p 8000 shutdown Query any Sentinel for the new master address: SENTINEL get-master-addr-by-name <master-name> Sentinel will return the IP and port of the promoted slave (e.g., 192.168.56.102).
Client Integration
Before each Redis operation, a client can request the current master address using the Sentinel command above. A PHP example is available at https://github.com/huyanping/redis-sentinel (plain text reference).
VIP Migration (optional)
Assign a floating IP (e.g., 192.168.56.250) to the master. When Sentinel promotes a new master, it runs a script to move the VIP.
notify_master6800.sh
#!/bin/bash
MASTER_IP=$6 # new master IP passed by Sentinel
LOCAL_IP='192.168.56.101' # change per host
VIP='192.168.56.250'
NETMASK='24'
INTERFACE='eth1'
if [ "$MASTER_IP" = "$LOCAL_IP" ]; then
/sbin/ip addr add ${VIP}/${NETMASK} dev ${INTERFACE}
/sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
else
/sbin/ip addr del ${VIP}/${NETMASK} dev ${INTERFACE}
fi
exit 0Manually bind the VIP to the current master (example for 192.168.56.102):
/sbin/ip addr add 192.168.56.250/24 dev eth1
/sbin/arping -q -c 3 -A 192.168.56.250 -I eth1Summary
This configuration provides high availability for a single Redis instance using master‑slave replication, Sentinel automatic failover, and optional VIP migration. For larger workloads, consider Redis Cluster or other clustering solutions.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
