分布式大环境下你还在用单机redis?手把手教你高可用redis集群搭建(此处模拟单机多进程,多服务器同理)
环境信息
redis版本 :5.0.8
集群搭建
-
修改配置文件
pidfile /var/run/redis_6000.pid dir /home/beauty/soft/app/redis/redis6000 port 6000port 6000 # Normal Redis instances can't be part of a Redis Cluster; only nodes that are # started as cluster nodes can. In order to start a Redis instance as a # cluster node enable the cluster support uncommenting the following: # cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not # intended to be edited by hand. It is created and updated by Redis nodes. # Every Redis Cluster node requires a different cluster configuration file. # Make sure that instances running in the same system do not have # overlapping cluster configuration file names. # cluster-config-file nodes-6000.conf # Cluster node timeout is the amount of milliseconds a node must be unreachable # for it to be considered in failure state. # Most other internal time limits are multiple of the node timeout. # cluster-node-timeout 15000 bind 127.0.0.1
-
启动集群所有节点 redis server
redis-server 配置文件path
-
关联节点分配槽位
-
查看 集群 命令
./redis-5.0.8/src/redis-cli --cluster help
-
关联节点 分配 插槽
# example
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
# 具体指令
./redis-5.0.8/src/redis-cli --cluster create 127.0.0.1:6000 127.0.0.1:6002 127.0.0.1:6004 127.0.0.1:6001 127.0.0.1:6003 127.0.0.1:6005 --cluster-replicas 1
M: 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 127.0.0.1:6000
slots:[0-5460] (5461 slots) master
M: f265191897573b8c97ab8174bb4546b1b540ae96 127.0.0.1:6002
slots:[5461-10922] (5462 slots) master
M: 9ebe0787584760de9a8e14e678e78f0b9e882eaa 127.0.0.1:6004
slots:[10923-16383] (5461 slots) master
S: c249e8366c0a74ca6f48d9f0b08f632a3e003002 127.0.0.1:6001
replicates 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654
S: 37277e8196a93a6eae427f29b1d139d76329aa52 127.0.0.1:6003
replicates f265191897573b8c97ab8174bb4546b1b540ae96
S: cfef7d3d7a20cd997953bed0b57acd5914f6ce87 127.0.0.1:6005
replicates 9ebe0787584760de9a8e14e678e78f0b9e882eaa
Can I set the above configuration? (type 'yes' to accept): yes
任意链接一台 机器 查看集群信息
beauty@ubuntu1:~/soft/app$ ./redis-5.0.8/src/redis-cli -p 6000
127.0.0.1:6000> cluster nodes
cfef7d3d7a20cd997953bed0b57acd5914f6ce87 127.0.0.1:6005@16005 slave 9ebe0787584760de9a8e14e678e78f0b9e882eaa 0 1588828991000 6 connected
f265191897573b8c97ab8174bb4546b1b540ae96 127.0.0.1:6002@16002 master - 0 1588828990000 2 connected 5461-10922
9ebe0787584760de9a8e14e678e78f0b9e882eaa 127.0.0.1:6004@16004 master - 0 1588828991041 3 connected 10923-16383
c249e8366c0a74ca6f48d9f0b08f632a3e003002 127.0.0.1:6001@16001 slave 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 0 1588828990035 4 connected
4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 127.0.0.1:6000@16000 myself,master - 0 1588828989000 1 connected 0-5460
37277e8196a93a6eae427f29b1d139d76329aa52 127.0.0.1:6003@16003 slave f265191897573b8c97ab8174bb4546b1b540ae96 0 1588828992049 5 connected
127.0.0.1:6000>
扩容
扩容命令
add-node new_host:new_port existing_host:existing_port
–cluster-slave
–cluster-master-id
给集群添加新节点
# 添加主节点
./redis-5.0.8/src/redis-cli --cluster add-node 127.0.0.1:6006 127.0.0.1:6000
# 添加 从节点
./redis-5.0.8/src/redis-cli --cluster add-node 127.0.0.1:6007 127.0.0.1:6000 --cluster-slave --cluster-master-id a8ac56acd900531559e6ab8688850f46674975a9
分配槽位
分配槽位指令
reshard host:port
–cluster-from
–cluster-to
–cluster-slots
–cluster-yes
–cluster-timeout
–cluster-pipeline
–cluster-replace
M: a8ac56acd900531559e6ab8688850f46674975a9 127.0.0.1:6006
slots: (0 slots) master
1 additional replica(s)
M: f265191897573b8c97ab8174bb4546b1b540ae96 127.0.0.1:6002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 9ebe0787584760de9a8e14e678e78f0b9e882eaa 127.0.0.1:6004
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 127.0.0.1:6000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 37277e8196a93a6eae427f29b1d139d76329aa52 127.0.0.1:6003
slots: (0 slots) slave
replicates f265191897573b8c97ab8174bb4546b1b540ae96
S: c249e8366c0a74ca6f48d9f0b08f632a3e003002 127.0.0.1:6001
slots: (0 slots) slave
replicates 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654
S: 3aadec41cd6cc6c79b35f5aecdae5ecb9b27a531 127.0.0.1:6007
slots: (0 slots) slave
replicates a8ac56acd900531559e6ab8688850f46674975a9
S: cfef7d3d7a20cd997953bed0b57acd5914f6ce87 127.0.0.1:6005
slots: (0 slots) slave
replicates 9ebe0787584760de9a8e14e678e78f0b9e882eaa
具体操作步骤
# 1
./redis-5.0.8/src/redis-cli --cluster reshard 127.0.0.1:6006(集群里的 任意一个主节点)
# 2 给扩容的 节点分配多少 槽位
How many slots do you want to move (from 1 to 16384)? 500
# 3 指定分配槽位的 主节点 id
What is the receiving node ID? a8ac56acd900531559e6ab8688850f46674975a9
# 4 指定分配策略 从哪些节点 拿出来给新节点
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654
Source node #2: 9ebe0787584760de9a8e14e678e78f0b9e882eaa
Source node #3: done
# 5 验证
./redis-5.0.8/src/redis-cli -p 6000
127.0.0.1:6000> cluster nodes
cfef7d3d7a20cd997953bed0b57acd5914f6ce87 127.0.0.1:6005@16005 slave 9ebe0787584760de9a8e14e678e78f0b9e882eaa 0 1588831479000 6 connected
f265191897573b8c97ab8174bb4546b1b540ae96 127.0.0.1:6002@16002 master - 0 1588831478000 2 connected 5461-10922
9ebe0787584760de9a8e14e678e78f0b9e882eaa 127.0.0.1:6004@16004 master - 0 1588831479000 3 connected 11173-16383
3aadec41cd6cc6c79b35f5aecdae5ecb9b27a531 127.0.0.1:6007@16007 slave a8ac56acd900531559e6ab8688850f46674975a9 0 1588831477000 8 connected
a8ac56acd900531559e6ab8688850f46674975a9 127.0.0.1:6006@16006 master - 0 1588831479000 8 connected 0-249 10923-11172
c249e8366c0a74ca6f48d9f0b08f632a3e003002 127.0.0.1:6001@16001 slave 4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 0 1588831479890 4 connected
4fd02b6697bf7d112c0e6eb1a140ed7b6c3f3654 127.0.0.1:6000@16000 myself,master - 0 1588831479000 1 connected 250-5460
37277e8196a93a6eae427f29b1d139d76329aa52 127.0.0.1:6003@16003 slave f265191897573b8c97ab8174bb4546b1b540ae96 0 1588831480897 5 connected
127.0.0.1:6000>
# 已成功