Deploy a 3‑Master‑3‑Slave Redis Cluster Without Pitfalls
This guide walks through setting up a Redis Cluster with three master and three replica nodes on Rocky 9.7 VMs, covering environment preparation, source compilation, configuration files, service creation, cluster creation, verification, and production‑grade tuning.
Environment specifications: three virtual machines (2 CPU/2 GB each) running Rocky 9.7, IPs 192.168.1.200‑202, deployment directory /app/redis-cluster, master port 7000, replica port 7001 (cluster bus ports 17000‑17001).
1. Environment preparation (run on all nodes)
1.1 Disable firewall and SELinux
# Disable firewall
systemctl stop firewalld
systemctl disable firewalld
# Disable SELinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config1.2 Install build dependencies
yum install -y gcc gcc-c++ make wget tar1.3 Create deployment directory and redis user
# Create redis user
useradd -s /sbin/nologin redis
# Create deployment directories
mkdir -p /app/redis-cluster/{7000,7001}/{data,log}
chown -R redis:redis /app/redis-cluster2. Compile and install Redis from source (all nodes)
2.1 Download Redis source
cd /usr/local/src
wget https://download.redis.io/releases/redis-7.2.4.tar.gz
tar -zxf redis-7.2.4.tar.gz2.2 Build and install
cd redis-7.2.4
make MALLOC=libc
make PREFIX=/app/redis install2.3 Configure environment variables
echo 'export PATH=$PATH:/app/redis/bin' >> /etc/profile
source /etc/profile
# Verify installation
redis-cli --version3. Write Redis configuration files (all nodes)
3.1 Master node configuration (port 7000)
cat > /app/redis-cluster/7000/redis.conf << EOF
# Basic settings
bind 0.0.0.0
port 7000
daemonize yes
pidfile /app/redis-cluster/7000/redis.pid
logfile /app/redis-cluster/7000/log/redis.log
dir /app/redis-cluster/7000/data
# Memory settings – volatile‑lru
maxmemory 512mb
maxmemory-policy volatile-lru
# Cluster settings
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
# Persistence
appendonly yes
appendfilename "appendonly-7000.aof"
save 900 1
save 300 10
save 60 10000
# Security
requirepass "redis123"
masterauth "redis123"
EOF3.2 Replica node configuration (port 7001)
cat > /app/redis-cluster/7001/redis.conf << EOF
# Basic settings
bind 0.0.0.0
port 7001
daemonize yes
pidfile /app/redis-cluster/7001/redis.pid
logfile /app/redis-cluster/7001/log/redis.log
dir /app/redis-cluster/7001/data
# Memory settings – allkeys‑lru (recommended for 2 GB machines)
maxmemory 512mb
maxmemory-policy allkeys-lru
# Cluster settings
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 15000
cluster-require-full-coverage no
# Persistence
appendonly yes
appendfilename "appendonly-7001.aof"
save 900 1
save 300 10
save 60 10000
# Security
requirepass "redis123"
masterauth "redis123"
EOF3.3 Adjust file permissions
chown -R redis:redis /app/redis-cluster4. Start all Redis instances (all nodes)
# Start master instance
su -s /bin/bash redis -c "/app/redis/bin/redis-server /app/redis-cluster/7000/redis.conf"
# Start replica instance
su -s /bin/bash redis -c "/app/redis/bin/redis-server /app/redis-cluster/7001/redis.conf"
# Verify processes
ps -ef | grep redis5. Create the Redis cluster (run on any node, e.g., 192.168.1.200)
5.1 Cluster creation command
redis-cli -a redis123 --cluster create \
192.168.1.200:7000 192.168.1.201:7000 192.168.1.202:7000 \
192.168.1.200:7001 192.168.1.201:7001 192.168.1.202:7001 \
--cluster-replicas 15.2 Confirm creation
When prompted, type yes and press Enter.
Can I set the above configuration? (type 'yes' to accept): yes5.3 Expected success output
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.6. Verify cluster status
6.1 View node information
redis-cli -c -a redis123 -h 192.168.1.200 -p 7000 cluster nodesThree master nodes (marked with master)
Three replica nodes (marked with slave and pointing to their masters)
All 16384 hash slots assigned
6.2 Check cluster health
redis-cli -a redis123 --cluster check 192.168.1.200:70006.3 Test write and read
# Connect to the cluster
redis-cli -c -a redis123 -h 192.168.1.200 -p 7000
# Write test keys
set testkey testvalue
set key1 value1
set key2 value2
# Read test keys
get testkey
get key1
get key2
# Find the slot of a key
cluster keyslot testkey7. Configure automatic start (all nodes)
7.1 Service file for port 7000
cat > /etc/systemd/system/redis-cluster-7000.service << EOF
[Unit]
Description=Redis Cluster 7000
After=network.target
[Service]
Type=forking
User=redis
Group=redis
ExecStart=/app/redis/bin/redis-server /app/redis-cluster/7000/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/app/redis/bin/redis-cli -a redis123 -p 7000 shutdown
Restart=always
RestartSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF7.2 Service file for port 7001
cat > /etc/systemd/system/redis-cluster-7001.service << EOF
[Unit]
Description=Redis Cluster 7001
After=network.target
[Service]
Type=forking
User=redis
Group=redis
ExecStart=/app/redis/bin/redis-server /app/redis-cluster/7001/redis.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/app/redis/bin/redis-cli -a redis123 -p 7001 shutdown
Restart=always
RestartSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF7.3 Enable and start services
systemctl daemon-reload
systemctl enable redis-cluster-7000 redis-cluster-7001
systemctl start redis-cluster-7000 redis-cluster-7001
# Verify status
systemctl status redis-cluster-7000 redis-cluster-70018. Common cluster operation commands
8.1 View cluster information
redis-cli -c -a redis123 -h 192.168.1.200 -p 7000 cluster info8.2 Manual failover test
# Trigger failover on a replica
redis-cli -a redis123 -h 192.168.1.200 -p 7001 cluster failover
# Check node state changes
redis-cli -c -a redis123 -h 192.168.1.200 -p 7000 cluster nodes8.3 Backup cluster data (caution for very large databases)
# Run on each node
redis-cli -a redis123 -p 7000 bgsave
redis-cli -a redis123 -p 7001 bgsave
# Backup files are stored in /app/redis-cluster/7000/data/ and /app/redis-cluster/7001/data/9. Production‑grade configuration (64 GB example)
Key adjustments: memory allocation, persistence policy, kernel tuning, security hardening, and cluster parameters.
9.1 Full production config for master (port 7000)
# Basic settings
bind 192.168.1.200 127.0.0.1
port 7000
daemonize yes
pidfile /app/redis-cluster/7000/redis.pid
logfile /app/redis-cluster/7000/log/redis.log
dir /app/redis-cluster/7000/data
loglevel notice
# Memory settings
maxmemory 24gb
maxmemory-policy volatile-lru
activedefrag yes
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10
active-defrag-threshold-upper 100
# Persistence
save 900 1
save 3600 100
rdbcompression yes
rdbchecksum yes
stop-writes-on-bgsave-error yes
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 1gb
no-appendfsync-on-rewrite yes
aof-use-rdb-preamble yes
# Network
tcp-backlog 1024
timeout 300
tcp-keepalive 300
maxclients 10000
# Client output buffers
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 2gb 1gb 60
client-output-buffer-limit pubsub 32mb 8mb 60
# Cluster
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 10000
cluster-require-full-coverage no
cluster-migration-barrier 1
# Replication
repl-diskless-sync yes
repl-diskless-sync-delay 5
repl-ping-replica-period 10
repl-timeout 60
# Security
requirepass "Redis@Prod2024!StrongPass"
masterauth "Redis@Prod2024!StrongPass"
rename-command FLUSHDB "RedisFlushDB_Prod2024"
rename-command FLUSHALL "RedisFlushALL_Prod2024"
rename-command KEYS "RedisKeys_Prod2024"
rename-command CONFIG "RedisConfig_Prod2024"
rename-command SHUTDOWN "RedisShutdown_Prod2024"
rename-command DEBUG ""The replica (port 7001) uses the same file with port, pidfile, logfile, dir, and cluster-config-file values changed to 7001.
9.2 System kernel optimizations (all nodes)
9.2.1 Kernel parameters
cat >> /etc/sysctl.conf << EOF
# Redis‑specific tuning
vm.overcommit_memory = 1
vm.swappiness = 0
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 1024
net.core.netdev_max_backlog = 1024
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
EOF
# Apply changes
sysctl -p9.2.2 File descriptor limits
cat >> /etc/security/limits.conf << EOF
redis soft nofile 65535
redis hard nofile 65535
redis soft nproc 65535
redis hard nproc 65535
EOF9.2.3 Disable Transparent Huge Pages (THP)
# Temporary disable
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
# Permanent disable
cat >> /etc/rc.local << EOF
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
EOF
chmod +x /etc/rc.localSigned-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.
Linux Cloud-Native Ops Stack
Focused on practical internet operations, sharing server monitoring, troubleshooting, automated deployment, and cloud-native tech insights. From Linux basics to advanced K8s, from ops tools to architecture optimization, helping engineers avoid pitfalls, grow quickly, and become your tech companion.
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.
