redis集群配置与管理
redis在3.0版本以后开始支持集群,经过中间几个版本的不断更新优化,最新的版本集群功能已经非常完善。本文简单介绍一下redis集群搭建的过程和配置方法,redis版本是5.0.4,操作系统是中标麒麟(和centos内核基本一致)。
1、redis集群原理
redis 集群是一个提供在多个redis间节点间共享数据的程序集,集群节点共同构建了一个去中心化的网络,集群中的每个节点拥有平等的身份,节点各自保存各自的数据和集群状态。节点之间采用gossip协议进行通信,保证了节点状态的信息同步。
redis 集群数据通过分区来进行管理,每个节点保存集群数据的一个子集。数据的分配采用一种叫做哈希槽(hash slot)
的方式来分配,和传统的一致性哈希不太相同。redis 集群有16384个哈希槽,每个key通过crc16校验后对16384取模来决定放置哪个槽。
为了使在部分节点失败或者大部分节点无法通信的情况下集群仍然可用,集群使用了主从复制模型。读取数据时,根据一致性哈希算法到对应的 master 节点获取数据,如果master 挂掉之后,会启动一个对应的 salve 节点来充当 master 。
2、环境准备
这里准备在一台pc上搭建一个3主3从的redis集群。
在/opt/目录下新建一个文件夹rediscluster,用来存放集群节点目录。
然后分别新建server10、server11、server20、server21、server30、server31 6个文件夹准备6个redis节点,这些节点分别使用6379、6380、6381、6382、6383、6384端口,以server10为例配置如下:
port 6379 daemonize yes pidfile /var/run/redis_6379.pid cluster-enabled yes cluster-node-timeout 15000 cluster-config-file nodes-6379.conf
其他节点只需修改端口和文件名,依次按此进行配置即可,配置完成后启动这些节点。
[root@localhost rediscluster]# ./server10/redis-server ./server10/redis.conf & [root@localhost rediscluster]# ./server11/redis-server ./server11/redis.conf & [root@localhost rediscluster]# ./server20/redis-server ./server20/redis.conf & [root@localhost rediscluster]# ./server21/redis-server ./server21/redis.conf & [root@localhost rediscluster]# ./server30/redis-server ./server30/redis.conf & [root@localhost rediscluster]# ./server31/redis-server ./server31/redis.conf &
查看启动状态:
[root@localhost rediscluster]# ps -ef|grep redis
root 11842 1 0 15:03 ? 00:00:12 ./server10/redis-server 127.0.0.1:6379 [cluster]
root 11950 1 0 15:03 ? 00:00:13 ./server11/redis-server 127.0.0.1:6380 [cluster]
root 12074 1 0 15:04 ? 00:00:13 ./server20/redis-server 127.0.0.1:6381 [cluster]
root 12181 1 0 15:04 ? 00:00:12 ./server21/redis-server 127.0.0.1:6382 [cluster]
root 12297 1 0 15:04 ? 00:00:12 ./server30/redis-server 127.0.0.1:6383 [cluster]
root 12404 1 0 15:04 ? 00:00:12 ./server31/redis-server 127.0.0.1:6384 [cluster]
3、集群配置
非常简单:redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster -replicas 1
其中-replicas 1表示每个主节点1个从节点
[root@localhost rediscluster]# ./server10/redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --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 127.0.0.1:6383 to 127.0.0.1:6379 adding replica 127.0.0.1:6384 to 127.0.0.1:6380 adding replica 127.0.0.1:6382 to 127.0.0.1:6381 >>> trying to optimize slaves allocation for anti-affinity [warning] some slaves are in the same host as their master m: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master m: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master m: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master s: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 replicates 63e20c75984e493892265ddd2a441c81bcdc575c s: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 replicates d9a79ed6204e558b2fcee78ea05218b4de006acd s: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 replicates efa84a74525749b8ea20585074dda81b852e9c29 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 127.0.0.1:6379) m: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) m: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) s: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c s: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 m: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) s: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [ok] all nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [ok] all 16384 slots covered.
创建完成,主从节点分配如下:
adding replica 127.0.0.1:6383 to 127.0.0.1:6379 adding replica 127.0.0.1:6384 to 127.0.0.1:6380 adding replica 127.0.0.1:6382 to 127.0.0.1:6381
4、集群测试
通过6379客户端连接后进行测试,发现转向了6381:
[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6379 127.0.0.1:6379> set foo bar -> redirected to slot [12182] located at 127.0.0.1:6381 ok 127.0.0.1:6381> get foo "bar"
在6381上连接测试:
[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6381 127.0.0.1:6381> get foo "bar"
结果相同,说明集群配置正常。
5、集群节点扩容
在rediscluster目录下在新增两个目录server40和server41,新增2个redis节点配置6385和6386两个端口。将6385作为新增的master节点,6386作为从节点,然后启动节点:
[root@localhost server41]# ps -ef|grep redis root 11842 1 0 15:03 ? 00:00:18 ./server10/redis-server 127.0.0.1:6379 [cluster] root 11950 1 0 15:03 ? 00:00:19 ./server11/redis-server 127.0.0.1:6380 [cluster] root 12074 1 0 15:04 ? 00:00:18 ./server20/redis-server 127.0.0.1:6381 [cluster] root 12181 1 0 15:04 ? 00:00:18 ./server21/redis-server 127.0.0.1:6382 [cluster] root 12297 1 0 15:04 ? 00:00:17 ./server30/redis-server 127.0.0.1:6383 [cluster] root 12404 1 0 15:04 ? 00:00:18 ./server31/redis-server 127.0.0.1:6384 [cluster] root 30563 1 0 18:01 ? 00:00:00 ./redis-server 127.0.0.1:6385 [cluster] root 30582 1 0 18:02 ? 00:00:00 ./redis-server 127.0.0.1:6386 [cluster]
添加主节点:
[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379 >>> adding node 127.0.0.1:6385 to cluster 127.0.0.1:6379 >>> performing cluster check (using node 127.0.0.1:6379) m: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) m: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) s: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c s: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 m: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) s: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [ok] all nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [ok] all 16384 slots covered. >>> send cluster meet to node 127.0.0.1:6385 to make it join the cluster. [ok] new node added correctly.
查看节点列表:
[root@localhost server41]# ./redis-cli 127.0.0.1:6379> cluster nodes 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064037664 0 connected efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064036000 1 connected 0-5460 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064038666 3 connected 10923-16383 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064035000 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064037000 6 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064037000 2 connected 5461-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064037000 5 connected
添加从节点:
[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6379 --cluster-slave --cluster-master-id 22e8a8e97d6f7cc7d627e577a986384d4d181a4f >>> adding node 127.0.0.1:6386 to cluster 127.0.0.1:6379 >>> performing cluster check (using node 127.0.0.1:6379) m: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) m: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385 slots: (0 slots) master m: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) s: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c s: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 m: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) s: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [ok] all nodes agree about slots configuration. >>> check for open slots... >>> check slots coverage... [ok] all 16384 slots covered. >>> send cluster meet to node 127.0.0.1:6386 to make it join the cluster. waiting for the cluster to join >>> configure node as replica of 127.0.0.1:6385. [ok] new node added correctly.
添加成功后,为新节点分配数据:
[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385 how many slots do you want to move (from 1 to 16384)? 1000 what is the receiving node id? 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 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: all
这样就新增完毕了,可以通过cluster nodes命令查看一下新增后的slot分布
127.0.0.1:6379> cluster nodes 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064706000 7 connected 0-332 5461-5794 10923-11255 efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064707000 1 connected 333-5460 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064705000 3 connected 11256-16383 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e 127.0.0.1:6386@16386 slave 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 0 1555064705000 7 connected 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064707000 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064707236 6 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064706000 2 connected 5795-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064708238 5 connected
6、集群节点缩减
缩减节点时先缩减从节点:
[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6386 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e >>> removing node 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e from cluster 127.0.0.1:6386 >>> sending cluster forget messages to the cluster... >>> shutdown the node.
然后进行主节点slot转移:
[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385 how many slots do you want to move (from 1 to 16384)? 1000 what is the receiving node id? efa84a74525749b8ea20585074dda81b852e9c29 //要移到的节点 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: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f //要删除的主节点 source node #2: done
最后在缩减主节点
[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6385 22e8a8e97d6f7cc7d627e577a986384d4d181a4f >>> removing node 22e8a8e97d6f7cc7d627e577a986384d4d181a4f from cluster 127.0.0.1:6385 >>> sending cluster forget messages to the cluster... >>> shutdown the node. [root@localhost server41]# ./redis-cli 127.0.0.1:6379> cluster nodes efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555066937000 8 connected 0-6250 10923-11710 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555066938698 3 connected 11711-16383 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555066939700 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555066936693 8 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555066937696 2 connected 6251-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555066937000 5 connected
缩减完成!
推荐阅读
-
redis集群配置与管理
-
redis主要启动主要参数与配置文件说明(redis 2.8)(转)
-
【转 Tomcat集群session管理解决方案(关于sticky session、session replication与使用memcached缓存sess tomcatmemcachedsticky sessionnon sticky session
-
mongo 3.4分片集群系列之七:配置数据库管理
-
基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置
-
使用 redis_builder 管理 redis 集群
-
php 在 windows 下扩展redis 的配置与说明
-
Spring Boot 与 Kotlin 使用Redis数据库的配置方法
-
Redis集群与SSM整合使用方法
-
Spring Boot 与 Kotlin 使用Redis数据库的配置方法