redis常用的架构有三种,单例、哨兵、集群,其他的都说过了,这里只简单介绍集群搭建。
单例最简单没什么好说的。
哨兵之前说过,该模式下有哨兵节点监视master和slave,若master宕机可自动将slave转为master,但它也有一个问题,就是不能动态扩充,并且存储大小受每个节点的内存大小限制。
集群模式Redis-Cluster,采用无中心结构,每个节点都和集群内其他节点有连接,数据可以跨主机分布式存储,解决了存储大小受主机限制的问题,Redis集群预分好16384个插槽(slot),每个节点分配一部分slot,当需要在 Redis 集群中放置一个 key-value 时,根据哈希算法决定将key放到哪个slot中,进而找到对应存放数据的主机,查询数据也一样。
集群模式内部同样可以配置主从,例如集群有六个数据节点,可以设置三个主节点,每个主节点对应一个从节点,当一个主节点宕机,可以自动将从节点变成主节点,保证整个集群还能用。但是一个主节点和对应的从节点都宕机后集群将不可用。每个主节点可以配置多个从节点。
集群搭建步骤
1、创建几台虚机,(集群模式最少要三个主节点)例如我们搭建一个三主三从的集群,创建六台虚机,当然要测试的话也可以部署在同一台主机上,使用不同的端口模拟不同主机。
2、在每台主机上安装redis,注意redis配置文件需要注意cluster相关的配置,例如修改cluster-enabled 的值是yes,表示开启集群,其他的超时时间等配置根据实际修改或默认。
3、redis安装完之后不会自己变成集群,还需要执行创建集群的命令,这个每个版本不太一样,redis5使用redis-cli --cluster ,redis3使用redis-trib.rb,下面具体说。
redis5集群创建
redis5的cluster相关命令,此处只写创建,当然还支持添加节点、删除、对solt进行操作等
[root@fvb9wbpl-clustermanager-e0hhllme ~]# 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.
创建主从集群命令,后面是集群的ip地址和端口,cluster-replicas 1表示一个主节点对应1个从节点,创建成功后会自动分配主从关系
redis-cli --cluster create --cluster-replicas 1 10.110.30.136:6379 10.110.30.137:6379 10.110.30.138:6379 10.110.30.144:6379 10.110.30.145:6379 10.110.30.146:6379 出现提示后再输入yes,然后开始创建集群
创建成功后查看集群,后面的ip可以是集群中的任意一个节点,结果中的M代表主节点,当M节点宕机后集群会自动把S节点变成M节点,这个可以自行测试
[root@fvb9wbpl-clustermanager-e0hhllme ~]# redis-cli --cluster check 10.110.30.68:6379 10.110.30.70:6379 (5e49929d...) -> 0 keys | 5461 slots | 1 slaves. 10.110.30.69:6379 (6dc594c5...) -> 0 keys | 5462 slots | 1 slaves. 10.110.30.112:6379 (2b9a0210...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.110.30.68:6379) S: 15a969da394187e239baab1cc57bc6ec810996fe 10.110.30.68:6379 slots: (0 slots) slave replicates 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65 S: 095777fb0d8ad380e420b9ef465d202a745a14e6 10.110.30.114:6379 slots: (0 slots) slave replicates 6dc594c5180f655603504ab68591b2e7492c7963 S: 20a33128f5353d36cb084d6ae2bf24c5b4806538 10.110.30.113:6379 slots: (0 slots) slave replicates 5e49929d47e7bd438ea2e25c3731179f460eb387 M: 5e49929d47e7bd438ea2e25c3731179f460eb387 10.110.30.70:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 6dc594c5180f655603504ab68591b2e7492c7963 10.110.30.69:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 2b9a0210b3223a1d865324ec32cc70f8ebcbaf65 10.110.30.112:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
redis3集群创建
redis3不支持cluster,3版本创建集群使用redis-trib.rb,当然还需要ruby等不在这里介绍。
redis-trib.rb的参数跟cluster类似,了解了上面的下面这个一看就懂,基本一样的
[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb help Usage: redis-trib <command> <options> <arguments ...> create host1:port1 ... hostN:portN --replicas <arg> check host:port info host:port fix host:port --timeout <arg> reshard host:port --from <arg> --to <arg> --slots <arg> --yes --timeout <arg> --pipeline <arg> rebalance host:port --weight <arg> --auto-weights --use-empty-masters --timeout <arg> --simulate --pipeline <arg> --threshold <arg> add-node new_host:new_port existing_host:existing_port --slave --master-id <arg> del-node host:port node_id set-timeout host:port milliseconds call host:port command arg arg .. arg import host:port --from <arg> --copy --replace help (show this help) For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
创建主从集群命令,参数含义跟cluster的一样
/usr/local/bin/redis-trib.rb create --replicas 1 10.110.30.74:6379 10.110.30.137:6379 10.110.30.138:6379 10.110.30.144:6379 10.110.30.145:6379 10.110.30.146:6379
检查集群
[root@mucuijwb-clustermanager-o9sprluh bin]# /usr/local/bin/redis-trib.rb check 10.110.30.74:6379 >>> Performing Cluster Check (using node 10.110.30.74:6379) M: 7483d09dadda2ecebd9fdbd6b1f186769e00904d 10.110.30.74:6379 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 76ecc2cf2ddc07fe7f2fda53f2f5318198321761 10.110.30.72:6379 slots:5461-10922 (5462 slots) master 1 additional replica(s) M: 0f60c9c6595726d391805f5acfaf5be13496346a 10.110.30.73:6379 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: 6f56bbe811be2ff97ef654777ae48c3ae5d83824 10.110.30.111:6379 slots: (0 slots) slave replicates 76ecc2cf2ddc07fe7f2fda53f2f5318198321761 S: 8c3bb1ed5e531665dfedf126df5121b76dc604d4 10.110.30.110:6379 slots: (0 slots) slave replicates 7483d09dadda2ecebd9fdbd6b1f186769e00904d S: 543c3b42c93ccac1728eaa5d5c9a43938f922890 10.110.30.108:6379 slots: (0 slots) slave replicates 0f60c9c6595726d391805f5acfaf5be13496346a [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
集群连接
客户端连接集群模式可以直接连接redis节点,不需要再经过中间节点转发,客户端连接集群,需要加 -c 参数,否则插入数据会报错
[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c 127.0.0.1:6379> dbsize (integer) 0 127.0.0.1:6379>
连接其他主机
[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p 6379 10.110.30.113:6379> dbsize (integer) 0 10.110.30.113:6379>
插入数据和查询数据,注意对插槽slot的分配操作,可以看出来是分布式存储的
[root@fvb9wbpl-rediscluster-vddntq84 ~]# redis-cli -c -h 10.110.30.113 -p 6379 10.110.30.113:6379> set foo bar -> Redirected to slot [12182] located at 10.110.30.112:6379 OK10.110.30.112:6379> set c d -> Redirected to slot [7365] located at 10.110.30.69:6379 OK 10.110.30.69:6379> 10.110.30.69:6379> get foo -> Redirected to slot [12182] located at 10.110.30.112:6379 "bar" 10.110.30.112:6379> get c -> Redirected to slot [7365] located at 10.110.30.69:6379 "d" 10.110.30.69:6379>