Redis集群模式搭建
Redis主要有三种集群方案:
- 主从模式
- 哨兵模式
- 集群模式
集群模式搭建
Redis3.0版本之后才可用,推荐3主3从集群模式。
1. 启动6台Redis
配置文件如下 ,redis_7002.conf...类似修改
# redis_7001.conf
# 端口
port 7001
# 启用集群模式
cluster-enabled yes
# 集群节点信息配置
cluster-config-file nodes_7001.conf
# 超时时间
cluster-node-timeout 5000
appendonly yes
# 后台运行
daemonize yes
# 非保护模式
protected-mode no
pidfile /var/run/redis_7001.pid
启动redis
./redis-server redis_7001.conf
启动后查看进程
# ps -ef | grep redis
redis 19196 1 0 10:52 ? 00:00:00 redis-server *:7001 [cluster]
redis 19336 1 0 10:58 ? 00:00:00 redis-server *:7002 [cluster]
redis 19341 1 0 10:58 ? 00:00:00 redis-server *:7003 [cluster]
redis 19346 1 0 10:58 ? 00:00:00 redis-server *:7004 [cluster]
redis 19351 1 0 10:58 ? 00:00:00 redis-server *:7005 [cluster]
redis 19356 1 0 10:58 ? 00:00:00 redis-server *:7006 [cluster]
2. 建立通信
# 建立通信
redis-cli -p 7001 cluster meet 127.0.0.1 7002
redis-cli -p 7001 cluster meet 127.0.0.1 7003
redis-cli -p 7001 cluster meet 127.0.0.1 7004
redis-cli -p 7001 cluster meet 127.0.0.1 7005
redis-cli -p 7001 cluster meet 127.0.0.1 7006
# 查看集群状态
# redis-cli -p 7001 cluster nodes
37d5fc8893126bea6895c694bd58448c89850354 127.0.0.1:7002@17002 master - 0 1661339366084 1 connected
16ceb3377a22dd6131b82d3da2f00bf1bea9a450 127.0.0.1:7006@17006 master - 0 1661339364075 5 connected
c2f26926df0a0e1b41c5d762a50e2255cea04611 127.0.0.1:7001@17001 myself,master - 0 1661339365000 0 connected
468020945e3ae2bbe96f19d19766354700cdee59 127.0.0.1:7004@17004 master - 0 1661339366000 4 connected
6bf6034bd4106d423d12d2123d254dc9c87d0481 127.0.0.1:7005@17005 master - 0 1661339364577 3 connected
1738af4b9fbff887dc74fa8acf56084c89de9496 127.0.0.1:7003@17003 master - 0 1661339365079 2 connected
3. 分配槽位
# 查看集群状态
# redis-cli -p 7001 cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
# 分配槽位
redis-cli -p 7001 cluster addslots {0..5460}
redis-cli -p 7001 cluster addslots {5461..10922}
redis-cli -p 7001 cluster addslots {10923..16383}
# 查看集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
4. 确定主从
# NodeID可以通过cluster nodes命令查看
redis-cli -p 7004 cluster replicate c2f26926df0a0e1b41c5d762a50e2255cea04611
redis-cli -p 7005 cluster replicate 37d5fc8893126bea6895c694bd58448c89850354
redis-cli -p 7006 cluster replicate 1738af4b9fbff887dc74fa8acf56084c89de9496
# 查看集群状态
# redis-cli -p 7001 cluster nodes
37d5fc8893126bea6895c694bd58448c89850354 127.0.0.1:7002@17002 master - 0 1661340450922 1 connected
16ceb3377a22dd6131b82d3da2f00bf1bea9a450 127.0.0.1:7006@17006 slave 1738af4b9fbff887dc74fa8acf56084c89de9496 0 1661340449000 5 connected
c2f26926df0a0e1b41c5d762a50e2255cea04611 127.0.0.1:7001@17001 myself,master - 0 1661340449000 0 connected 0-16383
468020945e3ae2bbe96f19d19766354700cdee59 127.0.0.1:7004@17004 slave c2f26926df0a0e1b41c5d762a50e2255cea04611 0 1661340450520 4 connected
6bf6034bd4106d423d12d2123d254dc9c87d0481 127.0.0.1:7005@17005 slave 37d5fc8893126bea6895c694bd58448c89850354 0 1661340450420 3 connected
1738af4b9fbff887dc74fa8acf56084c89de9496 127.0.0.1:7003@17003 master - 0 1661340449415 2 connected