Setting Up a Redis Cluster
1. Compile and Install
# cd /usr/local/src/
# wget http://download.redis.io/releases/redis-5.0.0.tar.gz
# tar zxvf redis-5.0.0.tar.gz
# apt-get update && apt-get install -y gcc automake make
# cd redis-5.0.0 && make && make install
# cp ./src/redis-server /usr/bin/
# cp ./src/redis-cli /usr/bin/
2. Create Service Start/Stop Script (if needed)
# cp ./utils/redis_init_script /etc/init.d/redisd
# service redisd start
3. Create Directories and Configuration Files
# mkdir /tmp/redis-cluster/{7000,7001,7002,7003,7004,7005}/log -pv
# cp /usr/local/src/redis-5.0.0/redis.conf /data/redis-cluster/7000/redis.conf
# cd /data/redis-cluster/ && cat 7000/redis.conf
bind 0.0.0.0
protected-mode yes
port 7000
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_7000.pid
loglevel notice
logfile "/data/redis-cluster/7000/log/redis-7000.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stops-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
masterauth AuDdQdpuEZpXgNthP6CjYjPb
requirepass AuDdQdpuEZpXgNthP6CjYjPb
Modify other port configuration files:
# cp 7000/redis.conf 7001/
# cp 7000/redis.conf 7002/
# cp 7000/redis.conf 7003/
# cp 7000/redis.conf 7004/
# cp 7000/redis.conf 7005/
# sed -i 's/7000/7001/g' 7001/redis.conf
# sed -i 's/7000/7002/g' 7002/redis.conf
# sed -i 's/7000/7003/g' 7003/redis.conf
# sed -i 's/7000/7004/g' 7004/redis.conf
# sed -i 's/7000/7005/g' 7005/redis.conf
4. Adjust Memory Allocation and Apply Changes
# This parameter can be 0, 1, or 2
# 0 means when user space requests more memory, the kernel tries to estimate available memory
# 1 allows the kernel to overcommit memory until it is exhausted
# 2 means the entire memory address space cannot exceed swap + (vm.overcommit_ratio)% of RAM
echo "vm.overcommit_memory=1">>/etc/sysctl.conf
sysctl -p
5. Create Start Script
# cat start-redis-cluster.sh
#!/bin/bash
cd /data/redis-cluster
cd 7000 && /usr/bin/redis-server /data/redis-cluster/7000/redis.conf
cd ../7001 && /usr/bin/redis-server /data/redis-cluster/7001/redis.conf
cd ../7002 && /usr/bin/redis-server /data/redis-cluster/7002/redis.conf
cd ../7003 && /usr/bin/redis-server /data/redis-cluster/7003/redis.conf
cd ../7004 && /usr/bin/redis-server /data/redis-cluster/7004/redis.conf
cd ../7005 && /usr/bin/redis-server /data/redis-cluster/7005/redis.conf
6. Start Services
# bash start-redis-cluster.sh
# ps -ef|grep redis
7. Create Cluster
# redis-cli --cluster create 10.20.71.215:7000 10.20.73.204:7001 10.20.71.67:7002 10.20.71.215:7003 10.20.73.204:7004 10.20.71.67:7005 --cluster-replicas 1 -a AuDdQdpuEZpXgNthP6CjYjPb
8. View Cluster Information
root@mgo-db01cn-t001:/usr/local/src/redis-5.0.0
# redis-cli -c -p 7000
127.0.0.1:7000> auth AuDdQdpuEZpXgNthP6CjYjPb
OK
127.0.0.1:7000> cluster nodes
9. Test Data
root@mgo-db01cn-t001:~# redis-cli -h 10.20.73.204 -p 7001
10.20.73.204:7001> auth AuDdQdpuEZpXgNthP6CjYjPb
OK
10.20.73.204:7001> set name shuke
OK
10.20.73.204:7001> get name
"shuke"
10. cluster saveconfig
Save the node configuration file to disk.
Try it:
127.0.0.1:7009> cluster saveconfig
OK
OK indicates success. It will overwrite the nodes.conf file in the configuration directory. This is done in case the nodes file is lost, so a new node configuration file can be ganerated.
> Tips:
1. If you want to recreate the cluster, log into each node, execute flushdb, then run cluster reset, and restart the nodes.
### Cluster Management
Redis Cluster tutorial, common cluster management operations, and frequently used commands