一、架构说明
分别位于三台机器上,搭建三个一主二从的主从,三个一主二从的节点形成一个高可用的集群
ip | Redis节点 |
---|---|
192.168.137.130 | 8001 |
8004 | |
8007 | |
192.168.137.131 | 8002 |
8005 | |
8008 | |
192.168.137.132 | 8003 |
8006 | |
8009 |
二、搭建过程
2.1 在对应的机器上,创建不同的目录,存放对应端口的redis配置文件
mkdir /opt/module/redis-cluster
cd /opt/module/redis-cluster
mkdir 8001 8004 8007 # 其他节点根据规划,创建不同的目录
cp redis.conf 8001 # 复制redis.conf
2.2 修改redis.conf
主要修改内容如下
1. daemonize yes
2. port 8001 # 不同的节点改成不同的端口
3. dir /usr/local/redis-cluster/8001/ #不同的节点,存放数据的目录
4. cluster-enabled yes # 启动集群模式
5. cluster-config-file nodes-8001.conf # 设置集群的配置文件,用于存放集群的节点信息
6. cluster-node-timeout 5000 # 集群之间节点通讯的超时时间
7. bind 0.0.0.0
8. protected-mode no 关闭保护模式
# 如果想要设置密码,可以添加如下的两个配置
requirepass your_password #设置redis客户端访问时的密码
masterauth your_password #设置访问master时携带的密码
2.3 将此份修改完成之后的配置文件,复制到8004/8007中
2.4 其他机器中,重复2.1-2.3创建出不同的目录即可
2.5 使用脚本启动redis
不同的机器,请修改对应的目录
/opt/module/redis-5.0.8/src/redis-server /opt/module/redis-cluster/8001/redis.conf
/opt/module/redis-5.0.8/src/redis-server /opt/module/redis-cluster/8004/redis.conf
/opt/module/redis-5.0.8/src/redis-server /opt/module/redis-cluster/8007/redis.conf
执行完成,在三台机器中可以看到如下的结果,则证明,redis实例已经启动完毕
[ifan@hadoop130 redis-cluster]$ ps -ef|grep redis
ifan 8096 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8001 [cluster]
ifan 8101 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8004 [cluster]
ifan 8106 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8007 [cluster]
ifan 8118 7377 0 20:19 pts/0 00:00:00 grep --color=auto redis
-------------------------------------------------------------------------
[ifan@hadoop131 redis-cluster]$ ps -ef|grep redis
ifan 2849 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8002 [cluster]
ifan 2851 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8005 [cluster]
ifan 2856 1 0 20:19 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8008 [cluster]
ifan 2866 2252 0 20:19 pts/0 00:00:00 grep --color=auto redis
-------------------------------------------------------------------------
[ifan@hadoop132 redis-cluster]$ ps -ef|grep redis
ifan 2763 1 0 20:20 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8003 [cluster]
ifan 2765 1 0 20:20 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8006 [cluster]
ifan 2770 1 0 20:20 ? 00:00:00 /opt/module/redis-5.0.8/src/redis-server 0.0.0.0:8009 [cluster]
ifan 2778 2315 0 20:20 pts/0 00:00:00 grep --color=auto redis
2.6 启动实例完成之后,需要通过redis-cli,来创建整个集群
通过 redis-cli 查看帮助,可以看到其中有个—cluster,在通过—cluster的帮助,可以看到如下的内容
[ifan@hadoop130 redis-cluster]$ /opt/module/redis-5.0.8/src/redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
最终,通过如下的命令,搭建redis集群(注意,—cluster-replicas后面的数量,为从节点的数量)
/opt/module/redis-5.0.8/src/redis-cli --cluster create --cluster-replicas 2 192.168.137.130:8001 192.168.137.131:8002 192.168.137.132:8003 192.168.137.130:8004 192.168.137.131:8005 192.168.137.132:8006 192.168.137.130:8007 192.168.137.131:8008 192.168.137.132:8009
可以看到输出结果如下
[ifan@hadoop130 redis-cluster]$ ./joinmaster.sh
>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.137.131:8005 to 192.168.137.130:8001
Adding replica 192.168.137.132:8006 to 192.168.137.130:8001
Adding replica 192.168.137.130:8007 to 192.168.137.131:8002
Adding replica 192.168.137.132:8009 to 192.168.137.131:8002
Adding replica 192.168.137.131:8008 to 192.168.137.132:8003
Adding replica 192.168.137.130:8004 to 192.168.137.132:8003
M: 8be4fa220f77092bccd95843d68ff82f4ac952e3 192.168.137.130:8001
slots:[0-5460] (5461 slots) master
M: be7638ba83837f13c3bd610dae5dd252feee2ffd 192.168.137.131:8002
slots:[5461-10922] (5462 slots) master
M: 8ff4e94320635a059f61fc8b339b0e34bb0a2422 192.168.137.132:8003
slots:[10923-16383] (5461 slots) master
S: 2ea09b98a09393f170e39070156bc8c87f199153 192.168.137.130:8004
replicates 8ff4e94320635a059f61fc8b339b0e34bb0a2422
S: 3e6b1162265a0439ef30b766f273bd8a9416f076 192.168.137.131:8005
replicates 8be4fa220f77092bccd95843d68ff82f4ac952e3
S: d79a98f7db5540b5116a9bfe96ded010694319ea 192.168.137.132:8006
replicates 8be4fa220f77092bccd95843d68ff82f4ac952e3
S: 5b301de776eba740131b2b92776725d74896dbcf 192.168.137.130:8007
replicates be7638ba83837f13c3bd610dae5dd252feee2ffd
S: e8da5bfaec54b133d95339d0c354a3ac15daed6c 192.168.137.131:8008
replicates 8ff4e94320635a059f61fc8b339b0e34bb0a2422
S: 9f15c0ba29671d5405f8a87a78cad92574ed1d8d 192.168.137.132:8009
replicates be7638ba83837f13c3bd610dae5dd252feee2ffd
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 192.168.137.130:8001)
M: 8be4fa220f77092bccd95843d68ff82f4ac952e3 192.168.137.130:8001
slots:[0-5460] (5461 slots) master
2 additional replica(s)
S: d79a98f7db5540b5116a9bfe96ded010694319ea 192.168.137.132:8006
slots: (0 slots) slave
replicates 8be4fa220f77092bccd95843d68ff82f4ac952e3
S: 3e6b1162265a0439ef30b766f273bd8a9416f076 192.168.137.131:8005
slots: (0 slots) slave
replicates 8be4fa220f77092bccd95843d68ff82f4ac952e3
S: 2ea09b98a09393f170e39070156bc8c87f199153 192.168.137.130:8004
slots: (0 slots) slave
replicates 8ff4e94320635a059f61fc8b339b0e34bb0a2422
S: 9f15c0ba29671d5405f8a87a78cad92574ed1d8d 192.168.137.132:8009
slots: (0 slots) slave
replicates be7638ba83837f13c3bd610dae5dd252feee2ffd
S: e8da5bfaec54b133d95339d0c354a3ac15daed6c 192.168.137.131:8008
slots: (0 slots) slave
replicates 8ff4e94320635a059f61fc8b339b0e34bb0a2422
S: 5b301de776eba740131b2b92776725d74896dbcf 192.168.137.130:8007
slots: (0 slots) slave
replicates be7638ba83837f13c3bd610dae5dd252feee2ffd
M: 8ff4e94320635a059f61fc8b339b0e34bb0a2422 192.168.137.132:8003
slots:[10923-16383] (5461 slots) master
2 additional replica(s)
M: be7638ba83837f13c3bd610dae5dd252feee2ffd 192.168.137.131:8002
slots:[5461-10922] (5462 slots) master
2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
通过上面的输出,可以看出,实际上,通过redis-cli组成了三个一主二丛的架构
[ifan@hadoop130 redis-cluster]$ cat 8001/nodes-8001.conf
节点的id 节点的ip和端口 节点的角色 slave跟随的主节点id 节点状态
d79a98f7db5540b5116a9bfe96ded010694319ea 192.168.137.132:8006@18006 slave 8be4fa220f77092bccd95843d68ff82f4ac952e3 0 1604541789000 6 connected
3e6b1162265a0439ef30b766f273bd8a9416f076 192.168.137.131:8005@18005 slave 8be4fa220f77092bccd95843d68ff82f4ac952e3 0 1604541788533 5 connected
2ea09b98a09393f170e39070156bc8c87f199153 192.168.137.130:8004@18004 slave 8ff4e94320635a059f61fc8b339b0e34bb0a2422 0 1604541788000 4 connected
9f15c0ba29671d5405f8a87a78cad92574ed1d8d 192.168.137.132:8009@18009 slave be7638ba83837f13c3bd610dae5dd252feee2ffd 0 1604541789139 9 connected
e8da5bfaec54b133d95339d0c354a3ac15daed6c 192.168.137.131:8008@18008 slave 8ff4e94320635a059f61fc8b339b0e34bb0a2422 0 1604541788125 8 connected
5b301de776eba740131b2b92776725d74896dbcf 192.168.137.130:8007@18007 slave be7638ba83837f13c3bd610dae5dd252feee2ffd 0 1604541789000 7 connected
节点的slot范围
8ff4e94320635a059f61fc8b339b0e34bb0a2422 192.168.137.132:8003@18003 master - 0 1604541788000 3 connected 10923-16383
8be4fa220f77092bccd95843d68ff82f4ac952e3 192.168.137.130:8001@18001 myself,master - 0 1604541787000 1 connected 0-5460
be7638ba83837f13c3bd610dae5dd252feee2ffd 192.168.137.131:8002@18002 master - 0 1604541788125 2 connected 5461-10922
vars currentEpoch 9 lastVoteEpoch 0
redis-cli设置值测试,会直接通过hash算法,重定向到对应的节点中。
[ifan@hadoop130 8001]$ /opt/module/redis-5.0.8/src/redis-cli -c -p 8004
127.0.0.1:8004> set k2 v1
-> Redirected to slot [449] located at 192.168.137.130:8001
OK
192.168.137.130:8001> keys *
1) "k2"