首页 > 代码库 > Redis集群搭建案例

Redis集群搭建案例

版本及系统说明

系统:CentOS 6.8 64X

Redis版本:redis-3.2.5

集群说明:

  1. 总共6个节点

  2. 3个Master节点,分别为7000、8000、9000.

  3. 3个Slave节点,分别为7001、8001、9001.


安装Redis

# wget http://download.redis.io/releases/redis-3.2.5.tar.gz

# yum -y install gcc tcl

# mkdir /usr/local/redis

# tar xvf redis-3.2.5.tar.gz

# cd redis-3.2.5

# make PREFIX=/usr/local/redis/ install

 

设置Redis到环境变量

# vim /etc/profile

export PATH=$PATH:/usr/local/redis/bin

# source /etc/profile


集群准备

创建集群所需要的目录

# mkdir /usr/local/cluster

# mkdir /usr/local/cluster

# mkdir 7000  7001 8000  8001  9000  9001

 

分别在7000、7001、8000、8001、9000、9001下创建log、data、pid、initscript conf

# mkdir 7000  7001 8000  8001  9000  9001

# mkdir data  initscript log  pid  conf

修改配置文件

# cp /home/roger/redis-3.2.5/redis.conf  /usr/local/cluster/7000/conf/

# vim  /usr/local/cluster/7000/conf/redis.conf

1.注释bind项

# bind 127.0.0.1

2.关闭保护模式

protected-mode no

3.修改端口为7000

port 7000

4.设置为后台启动

daemonize yes

5.修改pid的存储目录

Pidfile /usr/local/cluster/7000/pid/redis.pid

6.修改日志存储目录

logfile "/usr/local/cluster/7000/log/redis.log"

7.开启AOF功能

appendonly yes

8.设置AOP文件存储目录

dir /usr/local/cluster/7000/data/

9.开启Redis集群

cluster-enabled yes

10.开启集群配置文件

cluster-config-file nodes-7000.conf

11.开启集群节点间超时时间

cluster-node-timeout 15000

 

修改完redis.conf配置文件后把这个配置文件分别复制到7001/8000/8001/9000/9001中的conf文件中

# cp /usr/local/cluster/7000/conf/redis.conf  /usr/local/cluster/7001/conf/

# cp /usr/local/cluster/7000/conf/redis.conf  /usr/local/cluster/8000/conf/

# cp /usr/local/cluster/7000/conf/redis.conf  /usr/local/cluster/8001/conf/

# cp /usr/local/cluster/7000/conf/redis.conf  /usr/local/cluster/9000/conf/

# cp /usr/local/cluster/7000/conf/redis.conf  /usr/local/cluster/9001/conf/

注意:拷贝完后要分别修改对应的相关配置为指定的端口

配置Redis的启动脚本

修改端口、redis安装目录、pid文件目录、配置文件目录

# cp  /home/roger/redis-3.2.5/utils/redis_init_script  /usr/local/cluster/7000/initscript/redis_7000

# vim  /usr/local/cluster/7000/initscript/redis_7000

#!/bin/sh

#

# Simple Redis init.d script conceived to  work on Linux systems

# as it does use of the /proc filesystem.

 

REDISPORT=7000

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

 

PIDFILE=/usr/local/cluster/${REDISPORT}/pid/redis.pid

CONF="/usr/local/cluster/${REDISPORT}/conf/redis.conf"

 

case "$1" in

     start)

         if [ -f $PIDFILE ]

         then

                echo "$PIDFILE exists,  process is already running or crashed"

         else

                echo "Starting Redis  server..."

                 $EXEC $CONF

         fi

         ;;

     stop)

         if [ ! -f $PIDFILE ]

         then

                echo "$PIDFILE does not  exist, process is not running"

         else

                PID=$(cat $PIDFILE)

                echo "Stopping ..."

                 $CLIEXEC -p  $REDISPORT shutdown

                while [ -x /proc/${PID} ]

                do

                    echo "Waiting for  Redis to shutdown ..."

                    sleep 1

                done

                echo "Redis  stopped"

         fi

         ;;

     *)

         echo "Please use start or stop as first argument"

         ;;

Esac

 

修改问启动脚本后分别将Redis的启动脚本拷贝到7001/8000/8001/9000/9001中的initscript文件中

# cp  /usr/local/cluster/7000/initscript/redis_7000  /usr/local/cluster/7001/initscript/redis_7001

# cp  /usr/local/cluster/7000/initscript/redis_7000  /usr/local/cluster/8000/initscript/redis_8000

# cp  /usr/local/cluster/7000/initscript/redis_7000  /usr/local/cluster/8001/initscript/redis_8001

# cp  /usr/local/cluster/7000/initscript/redis_7000  /usr/local/cluster/9000/initscript/redis_9000

# cp  /usr/local/cluster/7000/initscript/redis_7000  /usr/local/cluster/9001/initscript/redis_9001 

 

拷贝完后分别修改各个的启动脚本端口

启动Redis实例

分别启动这6个Redis实例

# /usr/local/cluster/7000/initscript/redis_7000  start

# /usr/local/cluster/7001/initscript/redis_7001  start

# /usr/local/cluster/8000/initscript/redis_8000  start

# /usr/local/cluster/8001/initscript/redis_8001  start

# /usr/local/cluster/9000/initscript/redis_9000  start

# /usr/local/cluster/9001/initscript/redis_9001  start

 

启动后通过命令查看

# ps -ef | grep redis

如下显示则说明启动成功

技术分享

升级ruby安装gem

安装gem需要ruby的版本在1.8.7以上。默认安装的是1.8.7版本。

# yum install  ruby  ruby-devel  rubygems  rpm-build

 

检查ruby版本

# ruby -v

ruby 1.8.7 (2013-06-27 patchlevel 374)  [x86_64-linux]

 

检查是否安装rubygems

# rpm -qa | grep ruby

ruby-1.8.7.374-4.el6_6.x86_64

ruby-rdoc-1.8.7.374-4.el6_6.x86_64

ruby-libs-1.8.7.374-4.el6_6.x86_64

rubygems-1.3.7-5.el6.noarch

ruby-devel-1.8.7.374-4.el6_6.x86_64

ruby-irb-1.8.7.374-4.el6_6.x86_64

 

gem安装redis ruby接口

# gem install redis

Successfully installed redis-3.3.2

1 gem installed

Installing ri documentation for  redis-3.3.2...

Installing RDoc documentation for  redis-3.3.2...

集群开始

创建集群

通过集群管理器创建集群,集群管理器所在位置

# cd /home/roger/redis-3.2.5/src/

说明:

  1. --replicas是指定Redis Cluster中的每个master节点配备几个Slave节点。

  2. 节点角色由顺序决定,先是Master,后是Slave。

  3. 下边的命令则是:创建一个Redis集群,每个Master节点配备一个Slave节点,前边三个是Master节点,后边三个是Slave节点。

 

# cd /home/roger/redis-3.2.5/src/

# ./redis-trib.rb  create  --replicas  1  192.168.0.203:7000   192.168.0.203:8000    192.168.0.203:9000  192.168.0.203:7001  192.168.0.203:8001  192.168.0.203:9001

如下显示这redis集群创建成功

>>> Creating cluster

>>> Performing hash slots  allocation on 6 nodes...

Using 3 masters:

192.168.0.203:7000

192.168.0.203:8000

192.168.0.203:9000

Adding replica 192.168.0.203:7001  to 192.168.0.203:7000

Adding replica  192.168.0.203:8001 to 192.168.0.203:8000

Adding replica  192.168.0.203:9001 to 192.168.0.203:9000

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5460 (5461 slots) master

M: ba355902c49493a7fff1b9689a19dcc019438a8a  192.168.0.203:8000

    slots:5461-10922 (5462 slots) master

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:10923-16383 (5461 slots) master

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

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.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5460 (5461 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:10923-16383 (5461 slots) master

    1 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5461-10922 (5462 slots) master

    1 additional replica(s)

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

S: 4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda  192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

从上边打印信息可以观察出:

    Redis集群有6个节点,7000、8000、9000是三个Master节点。7001、800、9001是三个Slave节点。M-S的关系为7000(M)-7001(S)、8000(M)-8001(S)、9000(M)-9001(S)。

其中7000有5461个槽点(0-5460),8000有5462个槽点(5461-10922),9000有5461个槽点(10923-16383)。

目前为止Redis搭建成功。

检查集群状态

通过check连接Redis集群中任意节点

 

# /home/roger/redis-3.2.5/src/redis-trib.rb  check   192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5794,10923-11255 (6128 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

查看集群信息

使用redis-cli命令进入集群环境  -c是必须加的参数

# redis-cli -c -p 7000

技术分享

可以通过该命令查看各个节点的节点ID。


设置开机自启动

# vim /etc/rc.local

/usr/local/cluster/7000/initscript/redis_7000  start

/usr/local/cluster/7001/initscript/redis_7001  start

/usr/local/cluster/8000/initscript/redis_8000  start

/usr/local/cluster/8001/initscript/redis_8001  start

/usr/local/cluster/9000/initscript/redis_9000  start

/usr/local/cluster/9001/initscript/redis_9001  start

 

 

集群节点操作

添加主节点

准备Redis实例

# cd /usr/local/cluster

# cp -R 7000 6000

移除6000下rdb、aop、集群节点文件等。只保留其redis.conf和启动脚本。

修改redis.conf配置文件,对比上述的集群准备。

修改启动脚本端口。

启动添加的Redis实例

启动添加的主节点

#/usr/local/cluster/6000/initscript/redis_6000  start

执行添加主节点

命令说明:

    redis-trib.rb  add-node 添加的节点IP:端口  已有的集群主节点IP:口

 

# /home/roger/redis-3.2.5/src/redis-trib.rb  add-node   192.168.0.203:6000    192.168.0.203:7000

>>> Adding node  192.168.0.203:6000 to cluster 192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5460 (5461 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:10923-16383 (5461 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5461-10922 (5462 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[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.0.203:6000 to make it join the cluster.

[OK] New node added correctly.

 

检查集群状态

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5460 (5461 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:10923-16383 (5461 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

   slots: (0 slots) master

   0 additional replica(s)

M: ba355902c49493a7fff1b9689a19dcc019438a8a  192.168.0.203:8000

    slots:5461-10922 (5462 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

    从上面打印的信息可以知道,向Redis集群中添加了一个空的Master节点,集群没有分配任何hash槽,所以需要重新分配hash槽。

重新分配hash

执行命令

命令说明:

         redis-trib.rbreshard 集群中任意一个可用的主节点IP:端口

重新分配hash槽、并输入要分配的hash槽数量

# /home/roger/redis-3.2.5/src/redis-trib.rb  reshard  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5460 (5461 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:10923-16383 (5461 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M: b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d  192.168.0.203:6000

    slots: (0 slots) master

    0 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5461-10922 (5462 slots) master

    1 additional replica(s)

S: 4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda  192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[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)?1000

输入接收节点ID

输入接收hash槽的节点ID(通过查询集群信息可以获取添加的Master的节点ID)。

What is the receiving node ID?b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d

设置hash槽来源

输入hash槽的源节点,可以是单独某个集群节点的node ID,也可以输入all。又集群来分配hash槽。输入done结束槽的设置。(hash槽从源节点中移动过后,分配后的hash槽在源节点中不存在,在新的Master节点中)

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

确认移动hash

输入yes开始移动hash槽到目标节点ID

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

检验集群状态

查看分配后的集群信息,已经重新分配了hash槽

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:333-5460 (5128 slots) master

   1  additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

   slots:0-332,5461-5794,10923-11255 (1000  slots) master

   0 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S: 8b6e6090e576bd31dbb9a5d896b482aab7c6af37  192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

从上边打印信息中可以或者,新添加的Master节点已经分配了1000个hash槽点,hash槽点范围0-332,5461-5794,10923-11255

添加从节点

准备Redis实例

# cd /usr/local/cluster

# cp -R 7001 6001

移除6000下rdb、aop、集群节点文件等。只保留其redis.conf和启动脚本。

修改redis.conf配置文件,对比上述的集群准备。

修改启动脚本端口。

启动添加的Redis实例

启动添加的redis服务

# /usr/local/cluster/6001/initscript/redis_6001  start

执行添加从节点

         命令说明:

./redis-trib.rbadd-node --slave --master-id 主节点id 添加的从节点IP和端口 集群已有的节点IP和端口

# /home/roger/redis-3.2.5/src/redis-trib.rb  add-node   --slave  --master-id  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d  192.168.0.203:6001  192.168.0.203:7000

>>> Adding node  192.168.0.203:6001 to cluster 192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:333-5460 (5128 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

    slots:0-332,5461-5794,10923-11255 (1000 slots) master

    0 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S: 8b6e6090e576bd31dbb9a5d896b482aab7c6af37  192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[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.0.203:6001 to make it join the cluster.

Waiting for the cluster to join.

>>> Configure node as replica of  192.168.0.203:6000.

[OK] New node added correctly.

 

检查集群状态

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:333-5460 (5128 slots) master

    1 additional replica(s)

M: 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab  192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  79f0cbf6221fea7268bf076f57cbf2b3aac2c1d1 192.168.0.203:6001

   slots: (0 slots) slave

   replicates  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d

S: 6bc537e386b0e92c2cbeef4e6f8713aba838f137  192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

    slots:0-332,5461-5794,10923-11255 (1000 slots) master

    1 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

    从上边的打印信息可以获知,已经添加6001为6000的Slave节点。

移除从节点

执行移除操作

         命令说明:

./redis-trib.rb  del-node 需要移除的SlaveIP和端口  该节点ID

# /home/roger/redis-3.2.5/src/redis-trib.rb  del-node  192.168.0.203:6001  79f0cbf6221fea7268bf076f57cbf2b3aac2c1d1

>>> Removing node  79f0cbf6221fea7268bf076f57cbf2b3aac2c1d1 from cluster 192.168.0.203:6001

>>> Sending CLUSTER FORGET  messages to the cluster...

>>> SHUTDOWN the node.

检查集群状态

该节点已移除

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:333-5460 (5128 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

    slots:0-332,5461-5794,10923-11255 (1000 slots) master

    0 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S: 4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda  192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

移除主节点

         Redis集群中移除Master节点,需要先将Master节点上的hash槽先移动到其它的主节点上,再移除一个空的Master节点(没有分配hash槽的Master节点)

重新分配hash

执行命令并设置移除hash槽的数量

    命令说明

        redis-trib.rbreshard  需要移动hash槽的Master节点的IP:端口

 

# /home/roger/redis-3.2.5/src/redis-trib.rb  reshard  192.168.0.203:6000

>>> Performing Cluster Check  (using node 192.168.0.203:6000)

M: b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d  192.168.0.203:6000

   slots:0-332,5461-5794,10923-11255 (1000  slots) master

   0 additional replica(s)

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S:  4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda 192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:333-5460 (5128 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)? 1000

         在这儿设置需要移动的hash槽的数量

设置hash槽的接收节点ID

选择要接收这些hash槽的节点ID(这里目前只能设置一个Master节点的节点ID)。

What is the receiving node ID? ad9aa41d76bd9554c36cfda867c51def07711868

设置移除hash槽的节点并退出设置

输入被移除的节点ID(设置的ID为移除节点的ID),然后输入done退出设置

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

Source node #2:done

确定移动hash

打印被移动的hash槽后,输入yes确认开始移动hash槽

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

 

检查集群状态

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5794,10923-11255 (6128 slots) master

    1 additional replica(s)

M: 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab  192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S:  6bc537e386b0e92c2cbeef4e6f8713aba838f137 192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d 192.168.0.203:6000

   slots: (0 slots) master

   0 additional replica(s)

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S: 4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda  192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

         从上面的打印信息可以获知6000这个Master节点上的所有hash槽点已经移动到其它的Master节点上,当前6000这个Master节点为一个空的Master节点。

移除空的master节点

# /home/roger/redis-3.2.5/src/redis-trib.rb  del-node  192.168.0.203:6000  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d

>>> Removing node  b01491cf1bb07bbcbc4d5abfdf181d04e274eb4d from cluster 192.168.0.203:6000

>>> Sending CLUSTER FORGET  messages to the cluster...

>>> SHUTDOWN the node.

 

检查集群状态

# /home/roger/redis-3.2.5/src/redis-trib.rb  check  192.168.0.203:7000

>>> Performing Cluster Check  (using node 192.168.0.203:7000)

M:  ad9aa41d76bd9554c36cfda867c51def07711868 192.168.0.203:7000

    slots:0-5794,10923-11255 (6128 slots) master

    1 additional replica(s)

M:  7db64e29859983cc57acb6b02ba9f90ed6d4a2ab 192.168.0.203:9000

    slots:11256-16383 (5128 slots) master

    1 additional replica(s)

S: 6bc537e386b0e92c2cbeef4e6f8713aba838f137  192.168.0.203:9001

    slots: (0 slots) slave

    replicates 7db64e29859983cc57acb6b02ba9f90ed6d4a2ab

M:  ba355902c49493a7fff1b9689a19dcc019438a8a 192.168.0.203:8000

    slots:5795-10922 (5128 slots) master

    1 additional replica(s)

S: 4fa3f6acf3a1cbeaab19d3f3b3394957a5729eda  192.168.0.203:8001

    slots: (0 slots) slave

    replicates ba355902c49493a7fff1b9689a19dcc019438a8a

S:  8b6e6090e576bd31dbb9a5d896b482aab7c6af37 192.168.0.203:7001

    slots: (0 slots) slave

    replicates ad9aa41d76bd9554c36cfda867c51def07711868

[OK] All nodes agree about slots  configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

    从上边的打印信息可以获知6000这个Master节点已经从当前Redis集群中移除,所以成功移除了6000这个Master节点。





本文出自 “10950988” 博客,转载请与作者联系!

Redis集群搭建案例