14.5.2. 集群安装
准备6个节点:
M节点:10.196.126.29、10.196.126.30、10.196.126.31
S节点:10.196.126.32、10.196.126.33、10.196.126.34
sudo apt update
sudo apt install -y redis-server
vim /etc/redis/redis.conf
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
sudo systemctl enable redis-server
sudo systemctl restart redis-server
redis-cli --cluster create 10.196.126.29:6379 10.196.126.30:6379 10.196.126.31:6379 10.196.126.32:6379 10.196.126.33:6379 10.196.126.34:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.196.126.33:6379 to 10.196.126.29:6379
Adding replica 10.196.126.34:6379 to 10.196.126.30:6379
Adding replica 10.196.126.32:6379 to 10.196.126.31:6379
M: cd6cb228e25eca711a48d8cf730fd6ec34d2bd23 10.196.126.29:6379
slots:[0-5460] (5461 slots) master
M: d546c2b848786372ca697556434962fb6095da36 10.196.126.30:6379
slots:[5461-10922] (5462 slots) master
M: 9c5d117489fdf9a6ac40b04665ad665245f1aedf 10.196.126.31:6379
slots:[10923-16383] (5461 slots) master
S: 705ee43fb0093831734736277904c129ed9d27f6 10.196.126.32:6379
replicates 9c5d117489fdf9a6ac40b04665ad665245f1aedf
S: 352ab063bff5fcaf2f3389038a6a86ef4f8db2b5 10.196.126.33:6379
replicates cd6cb228e25eca711a48d8cf730fd6ec34d2bd23
S: be1cd3b71ddb2083219297d38c97c63e2f056406 10.196.126.34:6379
replicates d546c2b848786372ca697556434962fb6095da36
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 10.196.126.29:6379)
M: cd6cb228e25eca711a48d8cf730fd6ec34d2bd23 10.196.126.29:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 352ab063bff5fcaf2f3389038a6a86ef4f8db2b5 10.196.126.33:6379
slots: (0 slots) slave
replicates cd6cb228e25eca711a48d8cf730fd6ec34d2bd23
S: be1cd3b71ddb2083219297d38c97c63e2f056406 10.196.126.34:6379
slots: (0 slots) slave
replicates d546c2b848786372ca697556434962fb6095da36
M: d546c2b848786372ca697556434962fb6095da36 10.196.126.30:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 9c5d117489fdf9a6ac40b04665ad665245f1aedf 10.196.126.31:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 705ee43fb0093831734736277904c129ed9d27f6 10.196.126.32:6379
slots: (0 slots) slave
replicates 9c5d117489fdf9a6ac40b04665ad665245f1aedf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
使用 –cluster-replicas 1 参数配置集群中每个主节点有1个从节点。
创建集群后,可以使用以下命令来验证集群是否成功创建:
redis-cli -h 10.196.126.29 -p 6379 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:86
cluster_stats_messages_pong_sent:87
cluster_stats_messages_sent:173
cluster_stats_messages_ping_received:82
cluster_stats_messages_pong_received:86
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:173
你可以在任一节点上运行以下命令来测试集群的基本功能:
redis-cli -c -h 10.196.126.29 -p 6379 set foo bar
redis-cli -c -h 10.196.126.32 -p 6379 get foo
为了确保Redis集群正常运行,需要设置适当的持久化机制(例如:RDB或AOF),并启用监控和高可用性(如使用Redis Sentinel)。