Redis

部署redis集群

1,准备6台redis服务器并启用集群配置
2,部署管理主机
3,创建集群
4,测试集群
5,管理集群
5.1添加新主机 (master先添加,再分给它哈希槽。slave) 数据是跟哈希槽有关的,哈希槽在哪里,数据就在哪里!
5.2删除主机 (master先释放哈希槽再删 。slave直接删) 数据是跟哈希槽有关的,哈希槽在哪里,数据就在哪里!
6,把删除的master 或 slave 角色的 redis服务器再添加到集群里的步骤:
>cluster reset;
>exit

# rm -rf /var/lib/redis/6379/*
# /etc/init.d/redis_6379 restart
在管理主机上添加新主机

7 redis集群在什么情况下会无法访问!也就是集群什么时候会坏掉!现在一共有3组主从,如51主,52是对应的从。53主,54是对应的从。55主,56是对应的从。只要有一组主从全坏了,51和52都坏了,它们身上的对应哈希槽数据都会丢失了。或者是主库没有从库。解决方式是: 1主2从!也就是一共要有9台机子!
#####################################################################
51

真机拷贝redis-cluster 文件夹给51

# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
(empty list or set)
192.168.4.51:6351> flushall 清空内存的数据。不管它原本是不是空的,都要再清空一次。
OK
192.168.4.51:6351> exit
# redis-cli -h 192.168.4.52 -p 6352 在51登陆其他主机52-56的redis
192.168.4.52:6352> keys *
(empty list or set)
192.168.4.52:6352> flushall
OK
192.168.4.52:6352> exit
# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
(empty list or set)
192.168.4.53:6353> flushall
OK
192.168.4.53:6353> exit
# redis-cli -h 192.168.4.54 -p 6354
192.168.4.54:6354> flushall
OK
192.168.4.54:6354> exit
# redis-cli -h 192.168.4.55 -p 6355
192.168.4.55:6355> keys *
(empty list or set)
192.168.4.55:6355> flushall
OK
192.168.4.55:6355> exit
[root@mysql51 redis-cluster]# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> keys *
(empty list or set)
192.168.4.56:6356> flushall
OK
192.168.4.56:6356> exit
####################################################
51-56 都要启用集群功能。做同样的步骤。

# ss -tunlp | grep :6351
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=999,fd=6))

# ss -tunlp | grep redis-server 也可以这样过滤,用6351这个端口的进程是redis-server
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=999,fd=6))

# vim /etc/redis/6379.conf 要确保这个配置文件里启用的IP是本机eth0的IP,不要写本机的回环地址127.0.0.1
...
815 cluster-enabled yes 打开注释。启用集群。
823 cluster-config-file nodes-6351.conf 打开注释,文件名改为6351。52-56主机改为对应的就可以了。指定集群信息文件。
829 cluster-node-timeout 5000 打开注释,改为5000。请求超时5秒。
...

# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...

# ss -tunlp | grep redis-server 多出了一个端口16351(正好比原端口号6351多了1万)。就证明集群起来了。52-56是会显示对应的端口。
tcp LISTEN 0 128 192.168.4.51:6351 *:* users:(("redis-server",pid=2044,fd=6))
tcp LISTEN 0 128 192.168.4.51:16351 *:* users:(("redis-server",pid=2044,fd=8))
########################################################
63

# ss -tunlp | grep redis-server
tcp LISTEN 0 128 192.168.4.56:6356 *:* users:(("redis-server",pid=2027,fd=6))
tcp LISTEN 0 128 192.168.4.56:16356 *:* users:(("redis-server",pid=2027,fd=8))

# cd /var/lib/redis/6379/ 这是redis的数据库目录
# ls
dump.rdb nodes-6356.conf 这是我们指定了名称的配置文件

# cat nodes-6356.conf 它要全部都是0才是对的:myself,master - 0 0 0 connected ... 0
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0


前面是随机生成的id值。myself(我自己),master(角色).0 0 0现在这些数据应该都是0
#########################################################################
常见错误:

192.168.4.58:6358> CLUSTER NODES 查看节点的信息。打这条命令出现的下面的数据其实就是这个文件里的内容/var/lib/redis/6379/nodes-6356.conf
b7c53f41b7cbaed582293d3c1dd068548dd99439 :6358@16358 myself,master - 0 0 0 connected 5798 这里最后的数字不是0,所以是错的!
192.168.4.58:6358> CLUSTER reset
(error) ERR CLUSTER RESET can't be called with master nodes containing keys 提示无法清空内存
192.168.4.58:6358> keys *
1) "name" 可以看到这里有一个变量
192.168.4.58:6358> get name 无法获取变量的值
(error) CLUSTERDOWN The cluster is down

方案:可以flushall来清空!或者直接删掉这个目录下的所有东西 /var/lib/redis/6379/*
# ls /var/lib/redis/6379/
dump.rdb nodes-6358.conf

# rm -rf /var/lib/redis/6379/*
######################################################################
也可以通过redis来
# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> cluster info
cluster_state:fail 状态:失败,因为现在还没建集群
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

192.168.4.56:6356> CLUSTER INFO 这个是可以tab出来的

192.168.4.56:6356> CLUSTER NODES 查看集群的节点
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 :6356@16356 myself,master - 0 0 0 connected
####################################################################
51 安装一个ruby脚本的运行环境

ruby是解释ruby这个创建集群的脚本的程序
rubygems是.gem结尾的命令的软件包

# rpm -q ruby
未安装软件包 ruby
# rpm -q rubygems
未安装软件包 rubygems

# yum -y install ruby rubygems

# cd /root/redis-cluster/
# ls
redis-3.2.1.gem ruby-devel-2.0.0.648-30.el7.x86_64.rpm

# rpm -ivh ruby-devel-2.0.0.648-30.el7.x86_64.rpm

# gem install redis-3.2.1.gem 用gem这个命令来安装.gem这个包名
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
#######################################################

#cd /root/redis2/redis-4.0.8/src
# ls *.rb
redis-trib.rb 这是一个创建集群的脚本

# echo $PATH
/root/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

# mkdir /root/bin
# cp redis-trib.rb /root/bin/
# ls /root/bin/
redis-trib.rb

# redis-trib.rb help 查看redis-trib.rb这个命令的帮助信息
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.
############################################
replicas 后面跟的数字,指每个主库对应有多少台从库。而且它要求,即集群必须有3台主库。
即replicas 1。每个主库对应有1台从库,3台主库,3台从库,一共6台。
replicas 2。每个主库对应有2台从库,3台主库,6台从库,一共9台。
replicas 3。每个主库对应有3台从库,3台主库,9台从库,一共12台。

# redis-trib.rb create --replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
这个命令指,每个主库1个从库,--replicas 1 后面接的是6个主机ip和端口号,在这里我们设置3个主库,一般前面都是ip都是主库。后面的ip是从库,但是从库对应的是哪个主库是谁,是服务自动随机分配的。

会出现以下页面:
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351 M是主库,后面是主库ID,本机ip,端口,slots是槽,0-5460是槽的范围,一共有5461个槽
slots:0-5460 (5461 slots) master
M: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354 S是从库。后面是ID。从库是没有槽的。最后是它的主库的ID。可以看到ID尾数63的库是主机53
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
S: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
replicates d60177fe8fc689af558dda36c8402c17ccf005d4
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.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
M: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots: (0 slots) slave
replicates d60177fe8fc689af558dda36c8402c17ccf005d4
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. 出现这个提示,代表成功!
##################################################################
常见错误1:

Waiting for the cluster to join...
如果一直停在这里,有以下3种原因:

原因1:就是127.0.0.1这个回环地址,在配置文件/etc/redis/6379.conf里被启用了。
应该把这个回环地址删掉127.0.0.1,只写本机eth0的IP就行了。

原因2:配置文件里的集群没有启用。

原因3:redis数据库里没有被清空。应该进入redis数据库,然后情况清空flushall。
如:# redis-cli -h 192.168.4.56 -p 6356
192.168.4.56:6356> flushall
##################################################################
常见错误2:

Can I set the above configuration? (type 'yes' to accept): yes
...busy...

如果它显示槽被占用,显示busy,那么当我们cat /var/lib/redis/6379/nodes-6351.conf对应的主机集群文件信息时,会看到里面最后的数字不是0。 这种情况的话,要在每一个显示非0的主机里,都要执行以下操作:

# cd /var/lib/redis/6379/
# ls
dump.rdb nodes-6351.conf 这是内存往硬盘里存的数据dump.rdb,删掉这个就等于flushall

# rm -rf /var/lib/redis/6379/* 删掉这个目录下的所有有关集群和数据库数据的文件
# /etc/init.d/redis_6379 restart 重启服务
##################################################################
# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> CLUSTER INFO
cluster_state:ok 状态OK
cluster_slots_assigned:16384 一共6个槽位数
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3 主库的个数
cluster_current_epoch:6 一共6台
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1114
cluster_stats_messages_pong_sent:1167
cluster_stats_messages_sent:2281
cluster_stats_messages_ping_received:1162
cluster_stats_messages_pong_received:1114
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2281

192.168.4.51:6351> CLUSTER NODES 有多少台,就有多少行
8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353@16353 master - 0 1543632549039 3 connected 10923-16383
aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354@16354 slave 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 0 1543632547000 4 connected
d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352@16352 master - 0 1543632547031 2 connected 5461-10922
d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351@16351 myself,master - 0 1543632548000 1 connected 0-5460
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356@16356 slave d60177fe8fc689af558dda36c8402c17ccf005d4 0 1543632548035 6 connected
734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355@16355 slave d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 0 1543632548536 5 connected


上面的信息,就是来自这个集群的配置文件里
# cat /var/lib/redis/6379/nodes-6351.conf
8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353@16353 master - 0 1543631812366 3 connected 10923-16383
aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354@16354 slave 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 0 1543631813872 4 connected
d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352@16352 master - 0 1543631813000 2 connected 5461-10922
d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351@16351 myself,master - 0 1543631813000 1 connected 0-5460
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356@16356 slave d60177fe8fc689af558dda36c8402c17ccf005d4 0 1543631813369 6 connected
734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355@16355 slave d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 0 1543631814072 5 connected
vars currentEpoch 6 lastVoteEpoch 0
#######################################################################
检测集群

# redis-trib.rb check 192.168.4.51:6351 命令用的是check,后面跟的IP和端口可以是任意一个群里的主机,但是写本机它会检测得更快!
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
M: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots: (0 slots) slave
replicates d60177fe8fc689af558dda36c8402c17ccf005d4
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 


# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
(empty list or set)
192.168.4.51:6351> set x 101
-> Redirected to slot [16287] located at 192.168.4.53:6353
OK
192.168.4.53:6353> keys *
1) "x"

slots:10923-16383

 

# redis-cli -h 192.168.4.53 -p 6353
192.168.4.53:6353> keys *
1) "x"

# redis-cli -h 192.168.4.54 -p 6354
192.168.4.54:6354> keys * 自动同步到本机
1) "x"
192.168.4.54:6354> get x 从库能看到变量,但是不能获取值。但是查不了
(error) MOVED 16287 192.168.4.53:6353


# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> keys *
(empty list or set)


192.168.4.52:6352> keys *
(empty list or set)

客户端连接三台主库的任意一个时,当前在哪个主库里,keys *只能看到对应主库的变量。如果客户端在别的主库也新建了别的变量,它也能看到变量的值。但是在别的库里时,keys *,它只能看到当前主库的所有变量。不管变量存在了哪个库,你都能获取它的值。


hash slots 哈希槽
当我们用set j 3来存一个变量j,存的值是3。那么redis会拿变量j用crc16算法来计算得出一个数字,然后用那个数字%16384相除来取余,得出的那个余数,在哪个主库的槽位数范围内,就把那个变量存到那个主库里去。因为从库没有槽,所以它是无法存变量的,只能同步主库的数据。因为槽的数值是0-16383,一共有16384。不管主库是多少个,槽的个数都是16384个!

客户端连接集群中的主库只能看到当前主库上的所有变量!当你知道别的主库里的变量的时候,也能查到,它会会去对应库里帮你找出来!前提是你要知道别的库里的变量!

客户端访问集群中的主机,不管是主库还是从库,都要 -c 说明是连接集群cluster里的。
连接集群里的从库,也可以设置新的变量,和读取变量,但是写和读都是跳转到主库里的,所以还是直接访问集群的主库是最好的!
#################################################################
当主库52坏了,对应的从库56会自动升级为主库!52修好后,会自动成为主库56的从库!

# redis-trib.rb check 192.168.4.51:6351 当前集群里的6个主库都是好的,所以能查到有6个库的信息
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
M: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352 52当前是主库
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356 56当前是从库,它对应的主库是52
slots: (0 slots) slave
replicates d60177fe8fc689af558dda36c8402c17ccf005d4
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
###########################################################################
52 关闭52主库服务,模拟坏掉了

# ss -tunlp | grep redis-server
tcp LISTEN 0 128 192.168.4.52:6352 *:* users:(("redis-server",pid=2042,fd=6))
tcp LISTEN 0 128 192.168.4.52:16352 *:* users:(("redis-server",pid=2042,fd=8))
[root@mysql52 ~]# /etc/init.d/redis_6379 stop
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
[root@mysql52 ~]# ss -tunlp | grep redis-server
#########################################################################
51 检测集群的状态(52坏了后,检测到只有5个库了)

# redis-trib.rb check 192.168.4.51:6351 52坏了,里面找不到52的信息了!所以集群里只有5个库的信息!
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356 56当前自动升级为主库
slots:5461-10922 (5462 slots) master
0 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
########################################################################
50 客户端连接新升级为主库的56,设置新的变量

# redis-cli -c -h 192.168.4.56 -p 6356 连接集群中的主库56
192.168.4.56:6356> keys * 能看到之前56同步主库52时的数据
1) "g"
2) "c"
3) "v3"
4) "z"
192.168.4.56:6356> flushall 清空
OK
192.168.4.56:6356> keys *
(empty list or set)
192.168.4.56:6356> set v1 1 设置变量v1时,它算出的槽值在51的范围内,所以存入51
-> Redirected to slot [1165] located at 192.168.4.51:6351
OK
192.168.4.51:6351> set yy2 2 设置变量yy2,它的槽值在56范围内,所以存入56
-> Redirected to slot [8777] located at 192.168.4.56:6356
OK
192.168.4.56:6356> get yy2 查看yy2的值
"2"
... ...
192.168.4.51:6351> set yy6 6 设置变量yy6,它的槽值在56范围内,所以存入56
-> Redirected to slot [8909] located at 192.168.4.56:6356
OK
192.168.4.56:6356> get yy6 查看yy6的值
"6"
########################################################################
52 开启52主库服务,模拟修好了

# /etc/init.d/redis_6379 start
Starting Redis server...
[root@mysql52 ~]# ss -tunlp | grep redis-server
tcp LISTEN 0 128 192.168.4.52:6352 *:* users:(("redis-server",pid=5176,fd=6))
tcp LISTEN 0 128 192.168.4.52:16352 *:* users:(("redis-server",pid=5176,fd=8))

# redis-cli -h 192.168.4.52 -p 6352 自己连自己的数据库
192.168.4.52:6352> keys * 因为当前自动变为56的从库,所以也能看到客户端之前存入56的变量
1) "yy6"
2) "yy2"
#########################################################################
51 检测集群的状态(52修好后,又检测到有6个库了)

# redis-trib.rb check 192.168.4.51:6351 集群里一共有6个库了!
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352 52修好后,自动成为56的从库
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
######################################################################
57、58 安装redis并且指定ip和端口,开启集群配置。

# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped

# vim /etc/redis/6379.conf 指定ip和端口,开启集群

# /etc/init.d/redis_6379 start
Starting Redis server...

# redis-cli -h 192.168.4.57 -p 6357
192.168.4.57:6357> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.57:6357> CLUSTER NODES
ecb7257f903874b19efc0dfa4850539fe3b34da1 :6357@16357 myself,master - 0 0 0 connected
192.168.4.57:6357>


# cat /var/lib/redis/6379/nodes-6357.conf
ecb7257f903874b19efc0dfa4850539fe3b34da1 :0@0 myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0
############################################################
51 把57加入已经有的集群

# redis-trib.rb add-node 192.168.4.57:6357 192.168.4.51:6351 添加57到已经有的集群
>>> Adding node 192.168.4.57:6357 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.4.57:6357 to make it join the cluster.
[OK] New node added correctly.


# redis-trib.rb check 192.168.4.51:6351 查看集群状态
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357 当前57已经加入了集群,但是没有哈希槽
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.
####################################################################
57

192.168.4.57:6357> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:7
cluster_my_epoch:0
cluster_stats_messages_ping_sent:297
cluster_stats_messages_pong_sent:279
cluster_stats_messages_meet_sent:6
cluster_stats_messages_sent:582
cluster_stats_messages_ping_received:279
cluster_stats_messages_pong_received:303
cluster_stats_messages_received:582
192.168.4.57:6357> CLUSTER NODES
d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352@16352 slave 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 0 1543649495460 7 connected
ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357@16357 myself,master - 0 1543649494000 0 connected
734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355@16355 slave d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 0 1543649495059 1 connected
aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354@16354 slave 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 0 1543649495000 3 connected
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356@16356 master - 0 1543649496464 7 connected 5461-10922
d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351@16351 master - 0 1543649495000 1 connected 0-5460
8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353@16353 master - 0 1543649496000 3 connected 10923-16383
###########################################################
51

# redis-trib.rb reshard 192.168.4.51:6351 重新分片
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357 这个是57的ID,要记住,等下分片要用到。(分片,就是分槽的意思)
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)? 4096 你想从集群里的主库里拿出多少个槽来分给57?可以用16384/4=4096个槽,每个主库平均分得4096个槽。也可以随便指定一个数值的槽。
What is the receiving node ID? ecb7257f903874b19efc0dfa4850539fe3b34da1 你想把分出来的4096个槽给哪台主机?比如给57,那就写57的ID!记住是ID,是一长串的那个字符串,而不是IP!
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 all代表从所有的主库里分别拿出槽来给新的主库57,如果想从指定的某个主库比如51里拿出4096个槽给57的话,要写上51的ID!记住是ID,是一长串的那个字符串,而不是IP!

Ready to move 4096 slots.
Source nodes:
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
1 additional replica(s)
Destination node:
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots: (0 slots) master
0 additional replica(s)
Resharding plan:
Moving slot 5461 from 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
Moving slot 5462 from 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
.... ...
Moving slot 10924 from 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
Moving slot 10925 from 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
... ...
Moving slot 0 from d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
Moving slot 1 from d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
... ...
Moving slot 1363 from d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
Moving slot 1364 from d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
... ...

Do you want to proceed with the proposed reshard plan (yes/no)? yes 这里问你真的要执行这个计划吗?回答yes
Moving slot 5461 from 192.168.4.56:6356 to 192.168.4.57:6357:
Moving slot 5462 from 192.168.4.56:6356 to 192.168.4.57:6357:
... ...
Moving slot 10924 from 192.168.4.53:6353 to 192.168.4.57:6357:
Moving slot 10925 from 192.168.4.53:6353 to 192.168.4.57:6357:
... ...
Moving slot 0 from 192.168.4.51:6351 to 192.168.4.57:6357:
Moving slot 1 from 192.168.4.51:6351 to 192.168.4.57:6357:
... ...
Moving slot 1363 from 192.168.4.51:6351 to 192.168.4.57:6357:
Moving slot 1364 from 192.168.4.51:6351 to 192.168.4.57:6357:


# redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 slots) master 57已经有了槽,分别是3个从其他主库那里分过来的,所有共有3段
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

####################################################################
50

# redis-cli -c -h 192.168.4.57 -p 6357 客户端连接集群里的主库57
192.168.4.57:6357> keys * 因为别的主库的槽已经分了一些给它,所有它会有存储在这个槽的数值范围内的变量。
1) "yy4"
2) "v1"
3) "y"
4) "d"
192.168.4.57:6357> get d
"4"
192.168.4.57:6357> set h1 1 可以设置新的变量
-> Redirected to slot [9457] located at 192.168.4.56:6356
OK
192.168.4.56:6356> set h2 2 可以设置新的变量
-> Redirected to slot [5266] located at 192.168.4.51:6351
OK
192.168.4.51:6351> set h3 3 可以设置新的变量
-> Redirected to slot [1203] located at 192.168.4.57:6357
OK
192.168.4.57:6357> get h3 可以查到刚设置的变量
"3"
###################################################################
51 添加58为从库

# redis-trib.rb add-node --slave 192.168.4.58:6358 192.168.4.51:6351 添加从库,如果不指定它的主库是谁的话,它会默认是刚加入的主库57
>>> Adding node 192.168.4.58:6358 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 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.
Automatically selected master 192.168.4.57:6357
>>> Send CLUSTER MEET to node 192.168.4.58:6358 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.57:6357.
[OK] New node added correctly.


# redis-trib.rb check 192.168.4.51:6351 再次检测集群的状态,现在一共有8个库了
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: c56c9a20cd6bbca2f073ee2c4551db8379ba0fa7 192.168.4.58:6358 可以看到58已经是57的从库了
slots: (0 slots) slave
replicates ecb7257f903874b19efc0dfa4850539fe3b34da1
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 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.
###########################################################################################
50 测试

客户端访问集群中的主机,不管是主库还是从库,都要 -c 说明是连接集群cluster里的。
连接集群里的从库,也可以设置新的变量,和读取变量,但是写和读都是跳转到主库里的,所以还是直接访问集群的主库是最好的!

# redis-cli -c -h 192.168.4.58 -p 6358
192.168.4.58:6358> keys * 也可以看到它对应的主库57的所有变量
1) "y"
2) "v1"
3) "d"
4) "h3"
5) "yy4"
192.168.4.58:6358> set wo 1 存变量时,它会跳转到别的主库51
-> Redirected to slot [3207] located at 192.168.4.51:6351
OK
192.168.4.51:6351> get wo 可以查看其他主库里的变量的值
"1"
... ...
192.168.4.51:6351> set wo3 3 存变量wo3时,它现在存到了对应的主库57上了
-> Redirected to slot [188] located at 192.168.4.57:6357
OK
192.168.4.57:6357> get wo3 可以查看变量的值!!!如果连接时不指定是集群的话,它会报错的!
"3"
-------------------------------------------------------------------------------
50

# redis-cli -h 192.168.4.58 -p 6358 连接58,但是并没有指定是连接集群里的58
192.168.4.58:6358> keys * 也可以看到它对应的主库57的所有变量
1) "y"
2) "v1"
3) "wo3"
4) "d"
5) "h3"
6) "yy4"
192.168.4.58:6358> get wo3 但是想查看变量的值,会报错!!!
(error) MOVED 188 192.168.4.57:6357
########################################################################
51 移除从库58

# redis-trib.rb check 192.168.4.51:6351
... ...
S: c56c9a20cd6bbca2f073ee2c4551db8379ba0fa7 192.168.4.58:6358 等下移除要用到ID
slots: (0 slots) slave
replicates ecb7257f903874b19efc0dfa4850539fe3b34da1
... ...


# redis-trib.rb del-node 192.168.4.51:6351 c56c9a20cd6bbca2f073ee2c4551db8379ba0fa7 移除从库58,要用del-node 加上集群里任意一个主机的IP和端口,再加上移除的58的ID
>>> Removing node c56c9a20cd6bbca2f073ee2c4551db8379ba0fa7 from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.


# redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 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.

#################################################################
58 改成独立的库

# redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> keys * 还能看到集群的对应主库57的所有变量
1) "wo3"
2) "y"
3) "h3"
4) "v1"
5) "d"
6) "yy4"
192.168.4.58:6358> get wo3 但无法查看变量的值
(error) MOVED 188 192.168.4.57:6357
192.168.4.58:6358> set wo4 4 无法设置新的变量
(error) MOVED 12379 192.168.4.53:6353

192.168.4.58:6358> CLUSTER INFO 查看集群状态
cluster_state:ok 集群状态还是显示ok的
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:8
cluster_size:4
cluster_current_epoch:8
cluster_my_epoch:8
cluster_stats_messages_ping_sent:367
cluster_stats_messages_sent:367
cluster_stats_messages_pong_received:367
cluster_stats_messages_received:367

192.168.4.58:6358> CLUSTER NODES 还能查到集群的库的信息
d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351@16351 master - 0 1543653602349 1 connected 1365-5460
aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354@16354 slave 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 0 1543653603352 3 connected
c56c9a20cd6bbca2f073ee2c4551db8379ba0fa7 192.168.4.58:6358@16358 myself,slave ecb7257f903874b19efc0dfa4850539fe3b34da1 0 1543653453868 0 connected
ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357@16357 master - 0 1543653602000 8 connected 0-1364 5461-6826 10923-12287
734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355@16355 slave d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 0 1543653602549 1 connected
8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353@16353 master - 0 1543653603552 3 connected 12288-16383
7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356@16356 master - 0 1543653602851 7 connected 6827-10922
d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352@16352 slave 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 0 1543653603854 7 connected


# redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> CLUSTER reset
OK
192.168.4.58:6358> keys *
(empty list or set)
192.168.4.58:6358> exit

# /etc/init.d/redis_6379 stop

# vim /etc/redis/6379.conf
注释掉集群相关的所有配置项
815 #cluster-enabled yes
823 #cluster-config-file nodes-6358.conf
829 #cluster-node-timeout 5000

# rm -rf /var/lib/redis/6379/nodes-6358.conf 删除集群的配置文件


# /etc/init.d/redis_6379 start
Starting Redis server...

# redis-cli -h 192.168.4.58 -p 6358
192.168.4.58:6358> keys * 刚才集群清空了,所以查不到了
(empty list or set)
192.168.4.58:6358> set name lisi 现在变成了独立的库,所以可以正常存变量了
OK
192.168.4.58:6358> get name 也能正常查看变量的值了
"lisi"
# redis-cli -h 192.168.4.58 -p 6358

192.168.4.58:6358> CLUSTER INFO 如果再看集群的信息的话,会报错。因为我们已经把它的集群配置给注释了!
ERR This instance has cluster support disabled 这个并不是错误的提示,是正确的。它是提示这个主机没有支持集群的配置。
##################################################################
51

# redis-trib.rb check 192.168.4.51:6351 先检测当前集群里的库
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s) 删除了从库58之后,57已近没有从库了,这里显示0个
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


# redis-trib.rb reshard 192.168.4.51:6351 重新分片
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 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)? 4096 要分多少槽出去?4096个槽
What is the receiving node ID? 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 你要分给谁?我们想分给56,那就写56的ID
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:ecb7257f903874b19efc0dfa4850539fe3b34da1 从那里分槽出来?我们从57里分出来,那就写57的ID
Source node #2:done 分结束了就写done

Ready to move 4096 slots.
Source nodes:
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
Destination node:
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from ecb7257f903874b19efc0dfa4850539fe3b34da1
Moving slot 1 from ecb7257f903874b19efc0dfa4850539fe3b34da1
... ...
Moving slot 12286 from ecb7257f903874b19efc0dfa4850539fe3b34da1
Moving slot 12287 from ecb7257f903874b19efc0dfa4850539fe3b34da1
... ...
Do you want to proceed with the proposed reshard plan (yes/no)? yes 你确定要执行这个分片计划吗?yes。如果你分错了,就写no,取消分片,然后再重新分。
Moving slot 0 from 192.168.4.57:6357 to 192.168.4.56:6356:
Moving slot 1 from 192.168.4.57:6357 to 192.168.4.56:6356:
... ...
Moving slot 12286 from 192.168.4.57:6357 to 192.168.4.56:6356:
Moving slot 12287 from 192.168.4.57:6357 to 192.168.4.56:6356:


# redis-trib.rb check 192.168.4.51:6351 重新检测集群的状态,发现57已经没有槽了
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:0-1364,5461-12287 (8192 slots) master 槽已经分给了56,所以它的槽变多了
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
M: ecb7257f903874b19efc0dfa4850539fe3b34da1 192.168.4.57:6357
slots: (0 slots) master 57已经没有槽了
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


# redis-trib.rb del-node 192.168.4.51:6351 ecb7257f903874b19efc0dfa4850539fe3b34da1 从集群里删除57
>>> Removing node ecb7257f903874b19efc0dfa4850539fe3b34da1 from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

# redis-trib.rb check 192.168.4.51:6351 再次查看集群的状态,发现已经成功删除了57,这里已经没有它了
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: d8408779792b8f9c8b9e1ac84365fdbb3eec1d99 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 8cd0d0bd6ebe10c9da25e48456636ccf3a592463 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: aa4eeacb2f7fe50e8165bcf0b8d8235298d94c1f 192.168.4.54:6354
slots: (0 slots) slave
replicates 8cd0d0bd6ebe10c9da25e48456636ccf3a592463
S: d60177fe8fc689af558dda36c8402c17ccf005d4 192.168.4.52:6352
slots: (0 slots) slave
replicates 7c42f40b7ba00ae78f63163865f93e9e3282e7d3
M: 7c42f40b7ba00ae78f63163865f93e9e3282e7d3 192.168.4.56:6356
slots:0-1364,5461-12287 (8192 slots) master
1 additional replica(s)
S: 734536a55488a277c784561af2003dbbe8e169b5 192.168.4.55:6355
slots: (0 slots) slave
replicates d8408779792b8f9c8b9e1ac84365fdbb3eec1d99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
######################################################################
50 访问集群里的56

# redis-cli -c -h 192.168.4.56 -p 6356 数据是跟哈希槽有关的,哈希槽在哪里,数据就在哪里!
192.168.4.56:6356> keys * 57的哈希槽给了56,所以对应的数据现在56这里。56的数据变多了
1) "h1"
2) "yy4"
3) "yy6"
4) "yy2"
5) "h3"
6) "d"
7) "wo3"
8) "v1"
9) "y"

 

转载于:https://www.cnblogs.com/summer2/p/10798410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值