redis集群常用命令

#1、redis-cli命令进入集群环境

redis-cli -c -p 7000


#2、查看redis整个集群的状况,看谁是主,谁是从,常用两种方式。

#2.1、通过redis-trib.rb check方式查看,

./redis-trib.rb check 127.0.0.1:7000

#2.2、通过redis-cli方式查看,

redis-cli -c -p 7000 cluster nodes


#3、添加Master节点到集群

#3.1、按照Redis集群一的方式,创建端口为7006的新实例,并启动该实例(操作跟其他节点一样,注意修改端口。)

#3.2、将7006添加到集群

cd /data/soft/redis/src/

./redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

#3.3、查看所有集群节点,看看7006节点是否添加成功

redis-cli -c -p 7000 cluster nodes

7adf4b58e321bdc0f5962dfdc1ab0eb3e6835b30 127.0.0.1:7004 slave 757c2dcebb7b8effca5e89d5c44229f9a33f6e54 0 1451322932189 5 connected

f3951a3c666b429a095375799c1ab49e694a291c 127.0.0.1:7005 slave fd92a2277ad03bf7ad51f5865edb613f498cf409 0 1451322933202 6 connected

757c2dcebb7b8effca5e89d5c44229f9a33f6e54 127.0.0.1:7001 master - 0 1451322931685 2 connected 5795-10922

84c29ba2d6aa9e6179d0faeabc54bb8cd443c4ae 127.0.0.1:7003 slave 721d0479cbd308eac871b2abe3b5805db80d2fe3 0 1451322931181 8 connected

fd92a2277ad03bf7ad51f5865edb613f498cf409 127.0.0.1:7002 master - 0 1451322932692 3 connected 11256-16383

cee383ab949eeb33d58aacde89c9cfb8eaa65eab 127.0.0.1:7006 master - 0 1451322933202 0 connected

721d0479cbd308eac871b2abe3b5805db80d2fe3 127.0.0.1:7000 myself,master - 0 0 8 connected 0-5794 10923-11255

#3.4、给新添加的7006节点分配哈希槽

cd /data/soft/redis/src/

./redis-trib.rb reshard 127.0.0.1:7000

#3.4.1、根据提示选择要迁移的slot数量(ps:这里选择1000)

How many slots do you want to move (from 1 to 16384)? 1000

#3.4.2、选择要接受这些slot的node-id(ps:这里选择7006节点的node-id)

What is the receiving node ID? cee383ab949eeb33d58aacde89c9cfb8eaa65eab

#3.4.3、选择slot来源:

#all表示从所有的master重新分配,

#或者数据要提取slot的master节点id,最后用done结束(ps:这里只选择了7000节点的slot)

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:721d0479cbd308eac871b2abe3b5805db80d2fe3

Source node #2:done

#3.4.4、打印被移动的slot后,输入yes开始移动slot以及对应的数据。

Do you want to proceed with the proposed reshard plan (yes/no)? yes

#3.5、查看7006节点是否变成master节点(connected是否在0-999范围)

redis-cli -p 7000 cluster nodes | grep master | grep 7006

cee383ab949eeb33d58aacde89c9cfb8eaa65eab 127.0.0.1:7006 master - 0 1451325140212 9 connected 0-999

#4、添加Slave节点到集群

#4.1、按照Redis集群一的方式,创建端口为7007的新实例,并启动该实例(操作跟其他节点一样,注意修改端口。)

#4.2、将7006添加到集群

cd /data/soft/redis/src/

./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7000

#4.3、查看所有集群节点,看看7007节点是否添加成功

redis-cli -c -p 7000 cluster nodes

7adf4b58e321bdc0f5962dfdc1ab0eb3e6835b30 127.0.0.1:7004 slave 757c2dcebb7b8effca5e89d5c44229f9a33f6e54 0 1451324338685 5 connected

f3951a3c666b429a095375799c1ab49e694a291c 127.0.0.1:7005 slave fd92a2277ad03bf7ad51f5865edb613f498cf409 0 1451324340703 6 connected

757c2dcebb7b8effca5e89d5c44229f9a33f6e54 127.0.0.1:7001 master - 0 1451324340197 2 connected 5795-10922

84c29ba2d6aa9e6179d0faeabc54bb8cd443c4ae 127.0.0.1:7003 slave 721d0479cbd308eac871b2abe3b5805db80d2fe3 0 1451324339190 8 connected

fd92a2277ad03bf7ad51f5865edb613f498cf409 127.0.0.1:7002 master - 0 1451324338685 3 connected 11256-16383

cee383ab949eeb33d58aacde89c9cfb8eaa65eab 127.0.0.1:7006 master - 0 1451324340197 9 connected 0-999

721d0479cbd308eac871b2abe3b5805db80d2fe3 127.0.0.1:7000 myself,master - 0 0 8 connected 1000-5794 10923-11255

9fc511d059eb7fccae41663d495e73275e942aec 127.0.0.1:7007 master - 0 1451324339693 0 connected

#4.4、设置7007成为7006的slave

redis-cli -c -p 7007 cluster replicate cee383ab949eeb33d58aacde89c9cfb8eaa65eab

#4.5、查看7007节点是否变成7006的slave节点

redis-cli -p 7000 cluster nodes | grep slave | grep 7007

9fc511d059eb7fccae41663d495e73275e942aec 127.0.0.1:7007 slave cee383ab949eeb33d58aacde89c9cfb8eaa65eab 0 1451324745834 9 connected


#5、删除一个Slave节点(格式:./redis-trib.rb del-node ip:port node-id,这里以7007节点为例 )

cd /data/soft/redis/src/

./redis-trib.rb del-node 127.0.0.1:7007 '9fc511d059eb7fccae41663d495e73275e942aec'

#6、删除一个Master节点(这里以7006节点为例 )

#6.1、删除master节点之前首先要使用reshard移除master的全部slot

cd /data/soft/redis/src/

./redis-trib.rb reshard 127.0.0.1:7006

#6.1.1、根据提示选择要迁移的slot数量(ps:这里选择1000)

How many slots do you want to move (from 1 to 16384)? 1000

#6.1.2、选择要接受这些slot的node-id(ps:这里选择7000节点的node-id)

What is the receiving node ID? 721d0479cbd308eac871b2abe3b5805db80d2fe3

#6.1.3、选择slot来源:

#all表示从所有的master重新分配,

#或者数据要提取slot的master节点id,最后用done结束(ps:这里选择需要要删除的7006节点slot)

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:cee383ab949eeb33d58aacde89c9cfb8eaa65eab 

Source node #2:done

#6.1.4、打印被移动的slot后,输入yes开始移动slot以及对应的数据。

Do you want to proceed with the proposed reshard plan (yes/no)? yes

#6.2、删除空7006节点

cd /data/soft/redis/src/

./redis-trib.rb del-node 127.0.0.1:7006 'cee383ab949eeb33d58aacde89c9cfb8eaa65eab'


你可能感兴趣的:(redis集群命令)