假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
一,集群原理
1.1,redis-cluster架构图
1.2.架构细节:
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点
示例如下:
1.3,redis-cluster投票:容错
(1)集群中所有master参与投票,如果半数以上master节点与其中一个master节点通信超过(cluster-node-timeout),认为该master节点挂掉.
(2):什么时候整个集群不可用(cluster_state:fail)?
- 如果集群任意master挂掉,且当前master没有slave,则集群进入fail状态。也可以理解成集群的[0-16383]slot映射不完全时进入fail状态。
- 如果集群超过半数以上master挂掉,无论是否有slave,集群进入fail状态。
二,环境搭建
2.1,搭建Ruby环境
2.1.1,redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境。
yum -y install ruby
yum -y install rubygems
2.1.2,安装ruby和redis的接口程序
网上下载redis-3.0.0.gem
,
[root@linux02 local]# gem install /usr/local/redis-3.0.0.gem
2.1.3,将Redis集群搭建脚本文件复制到/usr/local/redis0707目录下
[root@redis01 /]# cd /root/redis-3.0.0/src/ [root@redis01 src]# ll *.rb -rwxrwxr-x. 1 root root 48141 4月 1 2015 redis-trib.rb [root@redis01 src]# cp redis-trib.rb /usr/local/redis0707/ -r
2.2,开始搭建集群
2.2.1,现在已经准备好了,6
份干净的redis
2.2.2,将六个节点的redis.conf
配置文件按照如下进行修改
修改redis.conf配置文件,打开Cluster-enable yes
PS:端口号如果是同一台主机的话,必须不同。不同主机可以相同。
PS:我这里是使用一台主机,所以我将六个节点的端口号修改为7001
-7006
2.2.3,编写集群启动脚本和停止脚本
启动脚本start-all.sh
cd .. cd redis7001/bin ./redis-server redis.conf cd .. cd .. cd redis7002/bin ./redis-server redis.conf cd .. cd .. cd redis7003/bin ./redis-server redis.conf cd .. cd .. cd redis7004/bin ./redis-server redis.conf cd .. cd .. cd redis7005/bin ./redis-server redis.conf cd .. cd .. cd redis7006/bin ./redis-server redis.conf cd .. cd ..
停止脚本stop-all.sh
cd ..
cd redis7001/bin
./redis-cli -p 7001 shutdown
cd ..
cd ..
cd redis7002/bin
./redis-cli -p 7002 shutdown
cd ..
cd ..
cd redis7003/bin
./redis-cli -p 7003 shutdown
cd ..
cd ..
cd redis7004/bin
./redis-cli -p 7004 shutdown
cd ..
cd ..
cd redis7005/bin
./redis-cli -p 7005 shutdown
cd ..
cd ..
cd redis7006/bin
./redis-cli -p 7006 shutdown
PS:两个脚本都放在如下所属目录/usr/local/redis-cluster[root@localhost redis-cluster]# pwd
/usr/local/redis/redis-cluster [root@localhost redis-cluster]# ll total 80 drwxr-xr-x 2 root root 4096 Nov 2 00:52 redis1 drwxr-xr-x 2 root root 4096 Nov 2 00:51 redis2 drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis3 drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis4 drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis5 drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis6 -rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb -rw-r--r-- 1 root root 252 Nov 2 00:55 start-all.sh -rw-r--r-- 1 root root 216 Nov 2 00:57 stop-all.sh [root@localhost redis-cluster]#
修改权限
[root@localhost redis-cluster]# chmod -u+x start-all.sh stop-all.sh
2.2.4,启动节点
[root@localhost redis-cluster]# ./start-all.sh [root@localhost redis-cluster]# ps aux | grep redis root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:53 ./redis-server *:6379 [cluster] root 11924 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7001 [cluster] root 11928 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7002 [cluster] root 11932 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7003 [cluster] root 11936 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7004 [cluster] root 11940 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7005 [cluster] root 11944 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7006 [cluster] root 11948 0.0 0.0 4360 748 pts/2 S+ 01:01 0:00 grep redis [root@localhost redis-cluster]#
2.2.5,执行创建集群命令
[root@localhost redis-cluster]# pwd /usr/local/redis/redis-cluster [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 192.168.37.131:7004 192.168.37.131:7005 192.168.37.131:7006
成功启动信息
>>> Creating cluster Connecting to node 192.168.37.131:7001: OK Connecting to node 192.168.37.131:7002: OK Connecting to node 192.168.37.131:7003: OK Connecting to node 192.168.37.131:7004: OK Connecting to node 192.168.37.131:7005: OK Connecting to node 192.168.37.131:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 Adding replica 192.168.37.131:7004 to 192.168.37.131:7001 Adding replica 192.168.37.131:7005 to 192.168.37.131:7002 Adding replica 192.168.37.131:7006 to 192.168.37.131:7003 M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001 slots:0-5460 (5461 slots) master M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002 slots:5461-10922 (5462 slots) master M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003 slots:10923-16383 (5461 slots) master S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004 replicates 8b153503b52f83634e04b0077f32ef629ad91ee6 S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005 replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8 S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006 replicates 53d698ad56b09f89cfef34850213e2d0a44154dd Can I set the above configuration? (type 'yes' to accept): y *** Aborting... [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 192.168.37.131:7004 192.168.37.131:7005 192.168.37.131:7006 >>> Creating cluster Connecting to node 192.168.37.131:7001: OK Connecting to node 192.168.37.131:7002: OK Connecting to node 192.168.37.131:7003: OK Connecting to node 192.168.37.131:7004: OK Connecting to node 192.168.37.131:7005: OK Connecting to node 192.168.37.131:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 Adding replica 192.168.37.131:7004 to 192.168.37.131:7001 Adding replica 192.168.37.131:7005 to 192.168.37.131:7002 Adding replica 192.168.37.131:7006 to 192.168.37.131:7003 M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001 slots:0-5460 (5461 slots) master M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002 slots:5461-10922 (5462 slots) master M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003 slots:10923-16383 (5461 slots) master S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004 replicates 8b153503b52f83634e04b0077f32ef629ad91ee6 S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005 replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8 S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006 replicates 53d698ad56b09f89cfef34850213e2d0a44154dd 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.37.131:7001) M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001 slots:0-5460 (5461 slots) master M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002 slots:5461-10922 (5462 slots) master M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003 slots:10923-16383 (5461 slots) master M: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004 slots: (0 slots) master replicates 8b153503b52f83634e04b0077f32ef629ad91ee6 M: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005 slots: (0 slots) master replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8 M: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006 slots: (0 slots) master replicates 53d698ad56b09f89cfef34850213e2d0a44154dd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@localhost redis-cluster]#
异常
>>> Creating cluster Connecting to node 192.168.37.131:7001: OK [ERR] Node 192.168.37.131:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方法是删除生成的配置文件nodes.conf
,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis
的持久化文件后再重启redis
,比如:appendonly.aof
、dump.rdb
首先,使用stop-all.sh
停止服务
解决方法是删除生成的配置文件nodes.conf
,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis
的持久化文件后再重启redis
,比如:appendonly.aof
、dump.rdb
[root@localhost redis-cluster]# ./stop-all.sh [root@localhost redis-cluster]# ps aux | grep redis root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster] root 11969 0.0 0.0 4360 744 pts/2 S+ 01:10 0:00 grep redis [root@localhost redis-cluster]#
然后每个节点中的appendonly.aof
、dump.rdb
、nodes.conf
。
[root@localhost redis-cluster]# rm -f redis*/dump.rdb
[root@localhost redis-cluster]# rm -f redis*/appendonly.aof
[root@localhost redis-cluster]# rm -f redis*/nodes.conf
然后使用脚本start-all.sh
启动,再启动集群管理服务。
[root@localhost redis-cluster]# ./start-all.sh [root@localhost redis-cluster]# ps aux | grep redis root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster] root 11980 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7001 [cluster] root 11982 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7002 [cluster] root 11984 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7003 [cluster] root 11986 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7004 [cluster] root 11988 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7005 [cluster] root 11990 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7006 [cluster] root 12004 0.0 0.0 4360 748 pts/2 S+ 01:12 0:00 grep redis [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.37.131:7001 192.168.37.131:7002 192.168.37.131:7003 192.168.37.131:7004 192.168.37.131:7005 192.168.37.131:7006 [root@localhost redis-cluster]#
./redis7001/bin/redis-cli -c -h 172.16.0.4 -p 7001 用这种方式启动
检验:
三,redis集群节点操作
3.1,查询集群信息
集群创建成功登陆任意redis
结点查询集群中的节点情况。
客户端以集群方式登陆:
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001 192.168.37.131:7001>
说明: ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
,其中-c
表示以集群方式连接redis
,-h
指定ip
地址,-p
指定端口号
3.2,cluster nodes
查询集群结点信息
192.168.37.131:7001> cluster nodes 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085160899 3 connected 10923-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085156858 8 connected 0-5460 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085158876 9 connected 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085159889 9 connected 5461-10922 cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085157867 6 connected 192.168.37.131:7001>
3.3,cluster info
查询集群状态信息
192.168.37.131:7001> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:9 cluster_my_epoch:8 cluster_stats_messages_sent:12727 cluster_stats_messages_received:10820 192.168.37.131:7001>
3.4.添加主节点
集群创建成功后可以向集群中添加节点,下面是添加一个master
主节点。
首先,准备一个干净的redis
节点。按上面集群版修改redis
配置文件。开启该redis
节点。
3.4.1,查看redis
进程
[root@localhost redis-cluster]# !ps ps aux | grep redis root 2924 0.6 0.1 33932 1800 ? Ssl Nov01 4:08 ./redis-server *:6379 [cluster] root 11980 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7001 [cluster] root 11982 0.0 0.2 33936 2244 ? Ssl 01:12 0:09 ./redis-server *:7002 [cluster] root 11984 0.0 0.2 33936 2220 ? Ssl 01:12 0:09 ./redis-server *:7003 [cluster] root 11986 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7004 [cluster] root 11988 0.0 0.2 33936 2228 ? Ssl 01:12 0:09 ./redis-server *:7005 [cluster] root 11990 0.0 0.2 33936 2212 ? Ssl 01:12 0:09 ./redis-server *:7006 [cluster] root 13913 0.0 0.1 33936 1952 ? Ssl 04:21 0:00 ./redis7/redis-server *:7007 [cluster] root 13917 0.0 0.0 4360 728 pts/3 S+ 04:21 0:00 grep redis [root@localhost redis-cluster]#
3.4.2,执行下边命令:
[root@localhost redis-cluster]# ./redis-trib.rb add-node 192.168.37.131:7007 192.168.37.131:7001 >>> Adding node 192.168.37.131:7007 to cluster 192.168.37.131:7001 Connecting to node 192.168.37.131:7001: OK Connecting to node 192.168.37.131:7003: OK Connecting to node 192.168.37.131:7004: OK Connecting to node 192.168.37.131:7005: OK Connecting to node 192.168.37.131:7002: OK Connecting to node 192.168.37.131:7006: OK >>> Performing Cluster Check (using node 192.168.37.131:7001) S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 slots: (0 slots) slave replicates 80de7003738f74134a3403fc939fed253b7774f2 M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slots: (0 slots) slave replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slots: (0 slots) slave replicates 8e1186475f87c928e8a146d3804d9a2697246ad0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 192.168.37.131:7007: OK >>> Send CLUSTER MEET to node 192.168.37.131:7007 to make it join the cluster. [OK] New node added correctly. [root@localhost redis-cluster]#
3.4.3,查看集群结点发现7007
已添加到集群中:
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001 192.168.37.131:7001> cluster nodes 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085870038 3 connected 10923-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085868020 8 connected 0-5460 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085874075 9 connected 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085873064 9 connected 5461-10922 cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085875086 6 connected 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478085872056 0 connected 192.168.37.131:7001>
3.4.4,hash槽重新分配
添加完主节点需要对主节点进行hash
槽分配这样该主节才可以存储数据。 redis
集群有16384
个槽,集群中的每个master
结点分配一些槽,通过查看集群结点可以看到槽占用情况。
给刚添加的7007
结点分配槽:
第一步:连接上集群 ./redis-trib.rb reshard 192.168.37.131:7001
(连接集群中任意一个可用结点都行)
[root@localhost redis-cluster]# ./redis-trib.rb reshard 192.168.37.131:7001 Connecting to node 192.168.37.131:7001: OK Connecting to node 192.168.37.131:7003: OK Connecting to node 192.168.37.131:7004: OK Connecting to node 192.168.37.131:7005: OK Connecting to node 192.168.37.131:7002: OK Connecting to node 192.168.37.131:7006: OK Connecting to node 192.168.37.131:7007: OK >>> Performing Cluster Check (using node 192.168.37.131:7001) S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 slots: (0 slots) slave replicates 80de7003738f74134a3403fc939fed253b7774f2 M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 slots:10923-16383 (5461 slots) master 1 additional replica(s) M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 slots:0-5460 (5461 slots) master 1 additional replica(s) S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slots: (0 slots) slave replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slots: (0 slots) slave replicates 8e1186475f87c928e8a146d3804d9a2697246ad0 M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 slots: (0 slots) master 0 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)?
第二步:输入要分配的槽数量
How many slots do you want to move (from 1 to 16384)? 1000 What is the receiving node ID?
第三步:输入接收槽的结点id
这里准备给7007
分配槽,通过cluster nodes
查看7007
结点id
为5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
输入:5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
What is the receiving node ID? 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 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:
第四步:输入源结点id
输入源结点id
,槽将从源结点中拿,分配后的槽在源结点中就不存在了。
输入all
表示从所有源结点中获取槽。
输入done
取消分配。
这里输入all
Source node #1:all 省略了很多 Moving slot 11253 from 8e1186475f87c928e8a146d3804d9a2697246ad0 Moving slot 11254 from 8e1186475f87c928e8a146d3804d9a2697246ad0 Moving slot 11255 from 8e1186475f87c928e8a146d3804d9a2697246ad0 Do you want to proceed with the proposed reshard plan (yes/no)?
第五步:输入yes
开始移动槽到目标结点id
省略了很多 Moving slot 11253 from 192.168.37.131:7003 to 192.168.37.131:7007: Moving slot 11254 from 192.168.37.131:7003 to 192.168.37.131:7007: Moving slot 11255 from 192.168.37.131:7003 to 192.168.37.131:7007: [root@localhost redis-cluster]#
第六步:查看结点信息
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001 192.168.37.131:7001> cluster nodes 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478086754466 3 connected 11256-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478086758509 8 connected 333-5460 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478086756490 9 connected 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478086757500 9 connected 5795-10922 cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478086755477 6 connected 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478086759518 10 connected 0-332 5461-5794 10923-11255 192.168.37.131:7001>
查看结点信息,可以发现7007
结点已经从三个主节点中获取了slot
。
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001 192.168.37.131:7001> cluster nodes 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478086754466 3 connected 11256-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478086758509 8 connected 333-5460 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478086756490 9 connected 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478086757500 9 connected 5795-10922 cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478086755477 6 connected 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478086759518 10 connected 0-332 5461-5794 10923-11255 192.168.37.131:7001>
查看结点信息,可以发现7007
结点已经从三个主节点中获取了slot
。
PS
我们在这里设置了1000个槽点,其实就是在3个主机中每个原来的槽点从头截取了333个
看这里的对比
添加之后的 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478086754466 3 connected 11256-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478086758509 8 connected 333-5460 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478086757500 9 connected 5795-10922 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478086759518 10 connected 0-332 5461-5794 10923-11255 添加之前的 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085870038 3 connected 10923-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085868020 8 connected 0-5460 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085873064 9 connected 5461-10922 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478085872056 0 connected
3.5,添加从节点
集群创建成功后可以向集群中添加节点,下面是添加一个slave
从节点。
添加7008
从结点,将7008
作为7007
的从结点。
首先,准备一个干净的redis
节点。按上面集群版修改redis
配置文件。开启该redis
节点。
3.5.1,查看redis
进程
root@localhost redis-cluster]# ps aux | grep redis root 2924 0.5 0.1 33932 1800 ? Ssl Nov01 4:12 ./redis-server *:6379 [cluster] root 11980 0.0 0.2 33936 2308 ? Ssl 01:12 0:13 ./redis-server *:7001 [cluster] root 11982 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7002 [cluster] root 11984 0.0 0.2 33936 2236 ? Ssl 01:12 0:13 ./redis-server *:7003 [cluster] root 11986 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7004 [cluster] root 11988 0.0 0.2 33936 2248 ? Ssl 01:12 0:13 ./redis-server *:7005 [cluster] root 11990 0.0 0.2 33936 2244 ? Ssl 01:12 0:13 ./redis-server *:7006 [cluster] root 13913 0.1 0.2 33936 2092 ? Ssl 04:21 0:04 ./redis7/redis-server *:7007 [cluster] root 14000 0.1 0.1 33936 1948 ? Ssl 05:24 0:00 ./redis-server *:7008 [cluster] root 14006 0.0 0.0 4360 732 pts/3 S+ 05:24 0:00 grep redis [root@localhost redis-cluster]#
3.5.2,命令格式为:
./redis-trib.rb add-node --slave --master-id 主节点id 添加节点的ip和端口 集群中已存在节点ip和端口
执行如下命令:
[root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7008 192.168.37.131:7001 >>> Adding node 192.168.37.131:7008 to cluster 192.168.37.131:7001 Connecting to node 192.168.37.131:7001: OK Connecting to node 192.168.37.131:7003: OK Connecting to node 192.168.37.131:7004: OK Connecting to node 192.168.37.131:7005: OK Connecting to node 192.168.37.131:7002: OK Connecting to node 192.168.37.131:7006: OK Connecting to node 192.168.37.131:7007: OK >>> Performing Cluster Check (using node 192.168.37.131:7001) S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 slots: (0 slots) slave replicates 80de7003738f74134a3403fc939fed253b7774f2 M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 slots:11256-16383 (5128 slots) master 1 additional replica(s) M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 slots:333-5460 (5128 slots) master 1 additional replica(s) S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slots: (0 slots) slave replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 slots:5795-10922 (5128 slots) master 1 additional replica(s) S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slots: (0 slots) slave replicates 8e1186475f87c928e8a146d3804d9a2697246ad0 M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 slots:0-332,5461-5794,10923-11255 (1000 slots) master 0 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. Connecting to node 192.168.37.131:7008: OK >>> Send CLUSTER MEET to node 192.168.37.131:7008 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 192.168.37.131:7007. [OK] New node added correctly. [root@localhost redis-cluster]#
5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
是7007
结点的id
,可以通过cluster nodes
查看。
查看集群中的结点,刚添加的7008
为7007
的从节点:
[root@localhost redis-cluster]# ./redis1/redis-cli -p 7001 -c 127.0.0.1:7001> cluster nodes 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478089964769 3 connected 11256-16383 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478089966584 8 connected 333-5460 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478089963748 9 connected 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478089966787 9 connected 5795-10922 cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478089962729 6 connected 5c97e8eab019c40ea3df4925c7400fe7df1846bb 192.168.37.131:7008 slave 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 0 1478089961713 10 connected 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478089965777 10 connected 0-332 5461-5794 10923-11255 127.0.0.1:7001>
3.6,删除节点
./redis-trib.rb del-node 192.168.37.131:7007 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
删除已经占有hash
槽的结点会失败,报错如下:
[ERR] Node 192.168.37.131:7007 is not empty! Reshard data away and try again.
需要将该结点占用的hash
槽分配出去,请参考《hash槽重新分配》这段内容。
假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html
gg