redis 5 集群, 设置,使用,案例,各种命令,测试等

Redis 集群

  • 多个redis节点网络互联,数据共享
  • 所有的节点都是一主一从(可以是多个从),其中从不提供服务,仅作为备用
  • 不支持同时处理多个键(如mset/mget),因为redis需要把键均匀分布在各个节点上,并发量很高的情况下同时创建键值会降低性能并导致不可预测的行为。
  • 支持在线增加、删除节点
  • 客户端可以连任何一个主节点进行读写
    在这里插入图片描述

Redis集群配置实例

  • 模拟三个节点,每个节点一主一从;
  • 使用两台虚拟机,每一台虚拟机启动三个redis分别使用不同端口;
  • 在149机器上的端口为7000, 7002, 7003;
  • 在150机器上的端口为7001, 7002, 7003;
  • 设置集群
  1. 两台机器上各启动三个不同端口的redis,大概步骤如下:
  • 编译安装redis 5.03
  • 每台机器上新增3个配置文件,新增3个数据路径
  • 两台机器各启动三个不同端口的redis
 1304  wget http://download.redis.io/releases/redis-5.0.3.tar.gz -c
 1306  tar zxf redis-5.0.3.tar.gz 
 1307  cd redis-5.0.3/
 1308  make
 1309  make install

三个配置文件如下:
[root@draft src]# cat /etc/redis.conf /etc/redis_7002.conf /etc/redis_7004.conf 
port 7000
bind 192.168.87.149
daemonize yes
pidfile /var/run/redis_7000.pid
dir /data/redis_data/7000
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10100
appendonly yes

port 7002
bind 192.168.87.149
daemonize yes
pidfile /var/run/redis_7002.pid
dir /data/redis_data/7002
cluster-enabled yes
cluster-config-file nodes_7002.conf
cluster-node-timeout 10100
appendonly yes

port 7004
bind 192.168.87.149
daemonize yes
pidfile /var/run/redis_7004.pid
dir /data/redis_data/7004
cluster-enabled yes
cluster-config-file nodes_7004.conf
cluster-node-timeout 10100
appendonly yes

数据目录如下:
[root@draft src]# ll !$
ll /data/redis_data/
总用量 0
drwxr-xr-x 2 root root 67 12月 17 12:08 7000
drwxr-xr-x 2 root root 67 12月 17 12:14 7002
drwxr-xr-x 2 root root 67 12月 17 12:14 7004

启动
 1355  redis-server /etc/redis.conf 
 1356  redis-server /etc/redis_7002.conf
 1357  redis-server /etc/redis_7004.conf


  1. 另一台机器设置
rsync 192.168.87.149:/usr/local/src/redis-5.0.3 /usr/local/src/
cd /usr/local/src/redis-5.0.3
make install

配置文件
[root@second redis-5.0.3]# cat /etc/redis.conf /etc/redis_7003.conf /etc/redis_7005.conf 
port 7001
bind 192.168.87.150
daemonize yes
pidfile /var/run/redis_7001.pid
dir /data/redis_data/7001
cluster-enabled yes
cluster-config-file nodes_7001.conf
cluster-node-timeout 10100
appendonly yes

port 7003
bind 192.168.87.150
daemonize yes
pidfile /var/run/redis_7003.pid
dir /data/redis_data/7003
cluster-enabled yes
cluster-config-file nodes_7003.conf
cluster-node-timeout 10100
appendonly yes

port 7005
bind 192.168.87.150
daemonize yes
pidfile /var/run/redis_7005.pid
dir /data/redis_data/7005
cluster-enabled yes
cluster-config-file nodes_7005.conf
cluster-node-timeout 10100
appendonly yes

数据路径
[root@second redis-5.0.3]# ll /data/redis_data/
总用量 0
drwxr-xr-x 2 root root 67 12月 17 12:08 7001
drwxr-xr-x 2 root root 67 12月 17 12:14 7003
drwxr-xr-x 2 root root 67 12月 17 12:14 7005

启动
 1036  redis-server /etc/redis.conf 
 1037  redis-server /etc/redis_7003.conf 
 1038  redis-server /etc/redis_7005.conf 
  1. 建立集群
 1362  redis-cli --cluster create 192.168.87.149:7000 192.168.87.149:7002 192.168.87.149:7004 192.168.87.150:7001 192.168.87.150:7003 192.168.87.150:7005 --cluster-replicas 1   #后面参数说明是一主一从,主从自动分配;

[root@second redis-5.0.3]# redis-cli --cluster check 192.168.87.149:7000   #集群情况查看
192.168.87.149:7000 (c077ca44...) -> 5 keys | 5461 slots | 1 slaves.
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5461 slots | 1 slaves.
192.168.87.149:7002 (7edd2df7...) -> 4 keys | 5462 slots | 1 slaves.
[OK] 11 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.87.149:7000)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7     #从机会显示主机ID;
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: d289bc65fdc510b35beb47f77b499a1160d3a5fd 192.168.87.150:7003
   slots: (0 slots) slave
   replicates 7edd2df701545f1845ee8ce1f0202cf02166f2e9
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 7edd2df701545f1845ee8ce1f0202cf02166f2e9 192.168.87.149:7002
   slots:[5461-10922] (5462 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.
  1. 集群其他命令使用
  • 删除一个主机,需要移除所有slot,再删除节点
  • 每个集群有16384个slot, slot主要用于分配数据存放在哪个主机,有算法实现,同样的键存取都通过同一台主机;一般主机平均分配这些slot,机器多每个主机的slot就少;分配好的slot在加入主机时可重新分配,灵活的增加主机;分配的slot多也即存放数据的权重高;
[root@second ~]# redis-cli --cluster reshard 192.168.87.149:7002   #从随便一个主节点进入分配slot的过程里;
>>> Performing Cluster Check (using node 192.168.87.149:7002)   #先显示目前状态;
M: 7edd2df701545f1845ee8ce1f0202cf02166f2e9 192.168.87.149:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: d289bc65fdc510b35beb47f77b499a1160d3a5fd 192.168.87.150:7003
   slots: (0 slots) slave
   replicates 7edd2df701545f1845ee8ce1f0202cf02166f2e9
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   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.      #每个集群有16384个slot, slot主要用于分配数据存放在哪个主机,有算法实现,同样的键存取都通过同一台主机;一般主机平均分配这些slot,机器多每个主机的slot就少;分配好的slot在加入主机时可重新分配,灵活的增加主机;分配的slot多也即存放数据的权重高;
How many slots do you want to move (from 1 to 16384)? 5462    #要删除节点的全部slot数;
What is the receiving node ID? c077ca44511b89275f5f36d619d4cf3b9fe6fb3e   #接受slot的主机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: 7edd2df701545f1845ee8ce1f0202cf02166f2e9     #要删除节点的ID;
Source node #2: done   #只从一个主机里删除,done结束;   后续出现转移slot计划,输入yes;

Ready to move 5462 slots.
  Source nodes:
    M: 7edd2df701545f1845ee8ce1f0202cf02166f2e9 192.168.87.149:7002
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
  Destination node:
    M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
  Resharding plan:    #省略5000千行转移过程;

[root@second ~]# redis-cli --cluster check 192.168.87.149:7000     #查看最新状态;
192.168.87.149:7000 (c077ca44...) -> 9 keys | 10923 slots | 2 slaves.
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5461 slots | 1 slaves.
192.168.87.149:7002 (7edd2df7...) -> 0 keys | 0 slots | 0 slaves.
[OK] 11 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.87.149:7000)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[0-10922] (10923 slots) master
   2 additional replica(s)
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: d289bc65fdc510b35beb47f77b499a1160d3a5fd 192.168.87.150:7003     #原来192.168.87.149:7002的从机,变成192.168.87.149:7000的从机;
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 7edd2df701545f1845ee8ce1f0202cf02166f2e9 192.168.87.149:7002
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 关闭节点
[root@second ~]# redis-cli --cluster del-node 192.168.87.149:7002 7edd2df701545f1845ee8ce1f0202cf02166f2e9
>>> Removing node 7edd2df701545f1845ee8ce1f0202cf02166f2e9 from cluster 192.168.87.149:7002
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

重启7002主机
[root@draft src]# redis-server /etc/redis_7002.conf
57635:C 17 Dec 2019 13:13:30.789 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
57635:C 17 Dec 2019 13:13:30.789 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=57635, just started
57635:C 17 Dec 2019 13:13:30.789 # Configuration loaded
  • 重新添加主机
[root@second ~]# redis-cli --cluster add-node 192.168.87.149:7002 192.168.87.149:7000    #添加为主机的命令;前面IP是要添加的节点,后面的IP是指从某一个主机登陆集群;
[ERR] Node 192.168.87.149:7002 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.   #存在历史数据,错误;

删除数据重启端口
[root@draft src]# kill 57636
[root@draft src]# rm -rf /data/redis_data/7002/*
[root@draft src]# redis-server /etc/redis_7002.conf

添加成功
[root@second ~]# redis-cli --cluster add-node 192.168.87.149:7002 192.168.87.149:7000
>>> Adding node 192.168.87.149:7002 to cluster 192.168.87.149:7000
>>> Performing Cluster Check (using node 192.168.87.149:7000)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[0-10922] (10923 slots) master
   2 additional replica(s)
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: d289bc65fdc510b35beb47f77b499a1160d3a5fd 192.168.87.150:7003
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   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.
>>> Send CLUSTER MEET to node 192.168.87.149:7002 to make it join the cluster.
[OK] New node added correctly.
[root@second ~]# redis-cli --cluster check 192.168.87.149:7000
192.168.87.149:7000 (c077ca44...) -> 9 keys | 10923 slots | 2 slaves.
192.168.87.149:7002 (76e196de...) -> 0 keys | 0 slots | 0 slaves.
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 11 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.87.149:7000)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[0-10922] (10923 slots) master
   2 additional replica(s)
M: 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002
   slots: (0 slots) master
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: d289bc65fdc510b35beb47f77b499a1160d3a5fd 192.168.87.150:7003
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   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.
  • 删除一个从机,再重新添加到集群
[root@second ~]# redis-cli --cluster del-node 192.168.87.150:7003 d289bc65fdc510b35beb47f77b499a1160d3a5fd
[root@second ~]# rm -rf /data/redis_data/7003/*
[root@second ~]# redis-server /etc/redis_7003.conf 
[root@second ~]# redis-cli --cluster add-node 192.168.87.150:7003 192.168.87.149:7000 --cluster-slave   #不指定主,但是会自动分配到没有从机的主机上;指定主机的命令如下;
#redis-cli --cluster add-node 192.168.222.129:6380 192.168.222.128:6379 --cluster-slave --cluster-master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e #添加从并指定主
  • 重新分配slot
[root@second ~]# redis-cli --cluster reshard 192.168.87.150:7001
>>> Performing Cluster Check (using node 192.168.87.150:7001)
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   slots:[0-2730],[10923-16383] (8192 slots) master
   1 additional replica(s)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[2731-10922] (8192 slots) master
   1 additional replica(s)
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
S: a8f0b13dc9aba59a90d2159138b1d78623a733ba 192.168.87.150:7003
   slots: (0 slots) slave
   replicates 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc
M: 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002
   slots: (0 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.
How many slots do you want to move (from 1 to 16384)? 5461
What is the receiving node ID? 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.    #直接输入all,会从所有主机上取出相应slot;
  Type 'done' once you entered all the source nodes IDs.
Source node #1: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
Source node #2: c90e83d793579927ba55d3b2272e4e6de5984bd7
Source node #3: done

Ready to move 5461 slots.
  Source nodes:
    M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
       slots:[2731-10922] (8192 slots) master
       1 additional replica(s)
    M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
       slots:[0-2730],[10923-16383] (8192 slots) master
       1 additional replica(s)
  Destination node:
    M: 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002
       slots: (0 slots) master
       1 additional replica(s)

[root@second ~]# redis-cli --cluster check 192.168.87.149:7000
192.168.87.149:7000 (c077ca44...) -> 4 keys | 5461 slots | 1 slaves.
192.168.87.149:7002 (76e196de...) -> 5 keys | 5461 slots | 1 slaves.
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 11 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.87.149:7000)
M: c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000
   slots:[5462-10922] (5461 slots) master
   1 additional replica(s)
M: 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002
   slots:[0-2729],[2731-5461] (5461 slots) master
   1 additional replica(s)
S: 48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005
   slots: (0 slots) slave
   replicates c90e83d793579927ba55d3b2272e4e6de5984bd7
S: 45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001
   slots: (0 slots) slave
   replicates c077ca44511b89275f5f36d619d4cf3b9fe6fb3e
S: a8f0b13dc9aba59a90d2159138b1d78623a733ba 192.168.87.150:7003
   slots: (0 slots) slave
   replicates 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc
M: c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004
   slots:[2730],[10923-16383] (5462 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.
  • 自动分配slot的命令
    #redis-cli --cluster rebalance --cluster-threshold 1 192.168.222.128:6379 #试用了一下,在刚加入也第三个主节点时,自动分配,只在原来两个主机上重新分配,这第三个节点slot仍为零;

  • 将集群外部redis实例中的数据导入到集群中去

redis-cli --cluster import 192.168.222.130:6379 --cluster-from 192.168.222.200:6379 --cluster-copy

Cluster-from后面跟外部redis的ip和port 如果只使用cluster-copy,则要导入集群中的key跟实例的key不能重复;
如果集群中已有同样的key,并需要替换,可以cluster-copy和cluster-replace联用,这样集群中的key就会被替换为外部的;

  • 保存设置
[root@draft src]# redis-cli -c -h 192.168.87.149 -p 7000
192.168.87.149:7000> cluster saveconfig
OK
192.168.87.149:7000> exit
[root@draft src]# ll /data/redis_data/7000/nodes_7000.conf  
-rw-r--r-- 1 root root 833 12月 17 15:10 /data/redis_data/7000/nodes_7000.conf
[root@draft src]# cat !$
cat /data/redis_data/7000/nodes_7000.conf     #cluster nodes命令显示相同信息;
76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002@17002 master - 0 1576566627694 12 connected 0-2729 2731-5461
48a4cac0150afabb71e5e0a3033bd2a5e8ba38e4 192.168.87.150:7005@17005 slave c90e83d793579927ba55d3b2272e4e6de5984bd7 0 1576566626000 11 connected
45feed02c3b2cd7f50e3a2bc993c880d37cd0577 192.168.87.150:7001@17001 slave c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 0 1576566626000 10 connected
a8f0b13dc9aba59a90d2159138b1d78623a733ba 192.168.87.150:7003@17003 slave 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 0 1576566627000 12 connected
c90e83d793579927ba55d3b2272e4e6de5984bd7 192.168.87.149:7004@17004 master - 0 1576566625664 11 connected 2730 10923-16383
c077ca44511b89275f5f36d619d4cf3b9fe6fb3e 192.168.87.149:7000@17000 myself,master - 0 1576566627000 10 connected 5462-10922
vars currentEpoch 12 lastVoteEpoch 0
  • 进入cli后的cluster命令
192.168.87.149:7000> CLUSTER Help   #进去cli以后的cluster命令,也可实现增加,删减,查看,slave操作;
 1) CLUSTER <subcommand> arg arg ... arg. Subcommands are:
 2) ADDSLOTS <slot> [slot ...] -- Assign slots to current node.
 3) BUMPEPOCH -- Advance the cluster config epoch.
 4) COUNT-failure-reports <node-id> -- Return number of failure reports for <node-id>.
 5) COUNTKEYSINSLOT <slot> - Return the number of keys in <slot>.
 6) DELSLOTS <slot> [slot ...] -- Delete slots information from current node.
 7) FAILOVER [force|takeover] -- Promote current replica node to being a master.
 8) FORGET <node-id> -- Remove a node from the cluster.
 9) GETKEYSINSLOT <slot> <count> -- Return key names stored by current node in a slot.
10) FLUSHSLOTS -- Delete current node own slots information.
11) INFO - Return onformation about the cluster.
12) KEYSLOT <key> -- Return the hash slot for <key>.
13) MEET <ip> <port> [bus-port] -- Connect nodes into a working cluster.
14) MYID -- Return the node id.
15) NODES -- Return cluster configuration seen by node. Output format:
16)     <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ... <slot>
17) REPLICATE <node-id> -- Configure current node as replica to <node-id>.
18) RESET [hard|soft] -- Reset current node (default: soft).
19) SET-config-epoch <epoch> - Set config epoch of current node.
20) SETSLOT <slot> (importing|migrating|stable|node <node-id>) -- Set slot state.
21) REPLICAS <node-id> -- Return <node-id> replicas.
22) SLOTS -- Return information about slots range mappings. Each range is made of:
23)     start, end, master and replicas IP addresses, ports and ids

  • 进入cli后,使用cluster命令
192.168.87.149:7000> CLUSTER MEET 192.168.87.150 7007  #添加节点;
192.168.87.149:7000> cluster nodes   
76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 192.168.87.149:7002@17002 master - 0 1576568157878 12 connected 0-2729 2731-5461   
6f97b462cd3d7fd3ea3a1903de0ee5af01e190e2 192.168.87.150:7007@17007 master - 0 1576568158889 0 connected   #添加成主节点;
192.168.87.149:7000> CLUSTER FORGET 6f97b462cd3d7fd3ea3a1903de0ee5af01e190e2   #删除一个节点;没有slot才能forget,有slot需要转移slot,再forget; 登陆集群的节点不能forget自己;
OK
192.168.87.149:7000> CLUSTER MEET 192.168.87.150 7007   #添加节点;
OK
[root@draft src]# redis-cli -c -h 192.168.87.150 -p 7007   #以一个节点登陆集群;
192.168.87.150:7007> CLUSTER REPLICATE 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc   #成为一个主的从;
OK
192.168.87.150:7007> CLUSTER NODES   #查看
6f97b462cd3d7fd3ea3a1903de0ee5af01e190e2 192.168.87.150:7007@17007 myself,slave 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc 0 1576568655000 0 connected
  • cluster使用,set, get都会显示实施操作的节点
 192.168.87.149:7000> get key1
"123"
192.168.87.149:7000> get key2
-> Redirected to slot [4998] located at 192.168.87.149:7002
"345"
192.168.87.149:7002> get key3
"234"
192.168.87.149:7002> get key4
-> Redirected to slot [13120] located at 192.168.87.149:7004
"28784"
192.168.87.149:7004> get key5
-> Redirected to slot [9057] located at 192.168.87.149:7000
"28784"
  • 测试自动切换
[root@second ~]# redis-cli --cluster check 192.168.87.149:7000
Could not connect to Redis at 192.168.87.149:7002: Connection refused   #kill掉了7002进程;
*** WARNING: 192.168.87.150:7007 claims to be slave of unknown node ID 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc.
*** WARNING: 192.168.87.150:7003 claims to be slave of unknown node ID 76e196de2ae3edcf7c8bfde9e4ad67e83319a8dc.
192.168.87.149:7000 (c077ca44...) -> 4 keys | 5461 slots | 1 slaves.
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 6 keys in 2 masters.
0.00 keys per slot on average.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

[root@second ~]# redis-cli --cluster check 192.168.87.149:7000
Could not connect to Redis at 192.168.87.149:7002: Connection refused
192.168.87.149:7000 (c077ca44...) -> 4 keys | 5461 slots | 1 slaves.
192.168.87.150:7003 (a8f0b13d...) -> 5 keys | 5461 slots | 1 slaves.   #7003成为主;
192.168.87.149:7004 (c90e83d7...) -> 2 keys | 5462 slots | 1 slaves.
[OK] 11 keys in 3 masters.
0.00 keys per slot on average.
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

发布了125 篇原创文章 · 获赞 5 · 访问量 4616

猜你喜欢

转载自blog.csdn.net/tanyyinyu/article/details/103567819