`

Redis集群说明

 
阅读更多

Redis集群

基本介绍

Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施installation

Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需要在多个 Redis 节点之间移动数据, 并且在高负载的情况下, 这些命令将降低Redis集群的性能, 并导致不可预测的行为。

Redis 集群通过分区partition来提供一定程度的可用性availability: 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。

Redis集群提供了以下两个好处:

  • 将数据自动切分split到多个节点的能力。
  • 当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力。

集群原理

redis-cluster架构图

redis-cluster架构图

  1. 所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。

  2. 节点的fail是通过集群中超过半数的节点检测失效时才生效。

  3. 客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。

  4. redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value

Redis集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点

redis-cluster投票:容错

redis-cluster投票:容错

  1. 投票过程是集群中所有master参与,如果半数以上master节点与master节点通信超时(cluster-node-timeout),认为当前master节点挂掉.

  2. 什么时候整个集群不可用(cluster_state:fail)?

    • 如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完整时进入fail状态. 
      • redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.
    • 如果集群超过半数以上master挂掉,无论是否有slave,集群进入fail状态.

Redis集群搭建

Redis单机版的安装见博客《redis入门——安装篇》,安装好之后,将redis复制成6份,注意要将.rdb.aof后缀的文件删除,如果有的话。

Ruby环境

使用

  1.  
    yum -y install ruby
  2.  
    yum -y install rubygems

安装ruby环境。 
网上下载redis-3.0.0.gem,执行gem install redis-3.0.0.gem安装。

redis配置文件修改

现在已经准备好了,6份干净的redis,如下所示

  1.  
    [root@localhost redis-cluster]# pwd
  2.  
    /usr/local/redis/redis-cluster
  3.  
    [root@localhost redis-cluster]# ll
  4.  
    total 72
  5.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:17 redis1
  6.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis2
  7.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis3
  8.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis4
  9.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis5
  10.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis6
  11.  
    -rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
  12.  
    [root@localhost redis-cluster]#

PS:注意,这里已经将redis源文件src目录下的redis-trib.rb文件拷贝过来了。 
redis-trib.rb这个文件是redis集群的管理文件,ruby脚本。

将六个节点的redis.conf配置文件按照如下进行修改

  1.  
    ################################ GENERAL #####################################
  2.  
     
  3.  
    # By default Redis does not run as a daemon. Use 'yes' if you need it.
  4.  
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
  5.  
    daemonize yes
  6.  
     
  7.  
    # Accept connections on the specified port, default is 6379.
  8.  
    # If port 0 is specified Redis will not listen on a TCP socket.
  9.  
    port *
  10.  
     
  11.  
    ################################ REDIS CLUSTER ###############################
  12.  
    #
  13.  
    # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14.  
    # WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
  15.  
    # in order to mark it as "mature" we need to wait for a non trivial percentage
  16.  
    # of users to deploy it in production.
  17.  
    # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  18.  
    #
  19.  
    # Normal Redis instances can't be part of a Redis Cluster; only nodes that are
  20.  
    # started as cluster nodes can. In order to start a Redis instance as a
  21.  
    # cluster node enable the cluster support uncommenting the following:
  22.  
    #
  23.  
    cluster-enabled yes

PS:端口号如果是同一台主机的话,必须不同。不同主机可以相同。

PS:我这里是使用一台主机,所以我将六个节点的端口号修改为7001-7006

编写集群启动脚本和停止脚本

启动脚本start-all.sh
  1.  
    cd redis1
  2.  
    ./redis-server redis.conf
  3.  
    cd ..
  4.  
    cd redis2
  5.  
    ./redis-server redis.conf
  6.  
    cd ..
  7.  
    cd redis3
  8.  
    ./redis-server redis.conf
  9.  
    cd ..
  10.  
    cd redis4
  11.  
    ./redis-server redis.conf
  12.  
    cd ..
  13.  
    cd redis5
  14.  
    ./redis-server redis.conf
  15.  
    cd ..
  16.  
    cd redis6
  17.  
    ./redis-server redis.conf
  18.  
    cd ..
停止脚本stop-all.sh
  1.  
    ./redis1/redis-cli -p 7001 shutdown
  2.  
    ./redis1/redis-cli -p 7002 shutdown
  3.  
    ./redis1/redis-cli -p 7003 shutdown
  4.  
    ./redis1/redis-cli -p 7004 shutdown
  5.  
    ./redis1/redis-cli -p 7005 shutdown
  6.  
    ./redis1/redis-cli -p 7006 shutdown

PS:两个脚本都放在如下所属目录

  1.  
    [root@localhost redis-cluster]# pwd
  2.  
    /usr/local/redis/redis-cluster
  3.  
    [root@localhost redis-cluster]# ll
  4.  
    total 80
  5.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:52 redis1
  6.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:51 redis2
  7.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis3
  8.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis4
  9.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis5
  10.  
    drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis6
  11.  
    -rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
  12.  
    -rw-r--r-- 1 root root 252 Nov 2 00:55 start-all.sh
  13.  
    -rw-r--r-- 1 root root 216 Nov 2 00:57 stop-all.sh
  14.  
    [root@localhost redis-cluster]#
修改权限
[root@localhost redis-cluster]# chmod -u+x start-all.sh stop-all.sh 

启动节点

  1.  
    [root@localhost redis-cluster]# ./start-all.sh
  2.  
    [root@localhost redis-cluster]# ps aux | grep redis
  3.  
    root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:53 ./redis-server *:6379 [cluster]
  4.  
    root 11924 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7001 [cluster]
  5.  
    root 11928 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7002 [cluster]
  6.  
    root 11932 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7003 [cluster]
  7.  
    root 11936 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7004 [cluster]
  8.  
    root 11940 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7005 [cluster]
  9.  
    root 11944 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7006 [cluster]
  10.  
    root 11948 0.0 0.0 4360 748 pts/2 S+ 01:01 0:00 grep redis
  11.  
    [root@localhost redis-cluster]#

执行创建集群命令

  1.  
    [root@localhost redis-cluster]# pwd
  2.  
    /usr/local/redis/redis-cluster
  3.  
    [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1192.168.37.131:7001192.168.37.131:7002192.168.37.131:7003192.168.37.131:7004192.168.37.131:7005192.168.37.131:7006
成功启动信息
  1.  
    >>> Creating cluster
  2.  
    Connecting to node 192.168.37.131:7001: OK
  3.  
    Connecting to node 192.168.37.131:7002: OK
  4.  
    Connecting to node 192.168.37.131:7003: OK
  5.  
    Connecting to node 192.168.37.131:7004: OK
  6.  
    Connecting to node 192.168.37.131:7005: OK
  7.  
    Connecting to node 192.168.37.131:7006: OK
  8.  
    >>> Performing hash slots allocation on 6 nodes...
  9.  
    Using 3 masters:
  10.  
    192.168.37.131:7001
  11.  
    192.168.37.131:7002
  12.  
    192.168.37.131:7003
  13.  
    Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
  14.  
    Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
  15.  
    Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
  16.  
    M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
  17.  
    slots:0-5460 (5461 slots) master
  18.  
    M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
  19.  
    slots:5461-10922 (5462 slots) master
  20.  
    M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
  21.  
    slots:10923-16383 (5461 slots) master
  22.  
    S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
  23.  
    replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
  24.  
    S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
  25.  
    replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
  26.  
    S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
  27.  
    replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
  28.  
    Can I set the above configuration? (type 'yes'to accept): y
  29.  
    *** Aborting...
  30.  
    [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1192.168.37.131:7001192.168.37.131:7002192.168.37.131:7003192.168.37.131:7004192.168.37.131:7005192.168.37.131:7006
  31.  
    >>> Creating cluster
  32.  
    Connecting to node 192.168.37.131:7001: OK
  33.  
    Connecting to node 192.168.37.131:7002: OK
  34.  
    Connecting to node 192.168.37.131:7003: OK
  35.  
    Connecting to node 192.168.37.131:7004: OK
  36.  
    Connecting to node 192.168.37.131:7005: OK
  37.  
    Connecting to node 192.168.37.131:7006: OK
  38.  
    >>> Performing hash slots allocation on 6 nodes...
  39.  
    Using 3 masters:
  40.  
    192.168.37.131:7001
  41.  
    192.168.37.131:7002
  42.  
    192.168.37.131:7003
  43.  
    Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
  44.  
    Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
  45.  
    Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
  46.  
    M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
  47.  
    slots:0-5460 (5461 slots) master
  48.  
    M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
  49.  
    slots:5461-10922 (5462 slots) master
  50.  
    M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
  51.  
    slots:10923-16383 (5461 slots) master
  52.  
    S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
  53.  
    replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
  54.  
    S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
  55.  
    replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
  56.  
    S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
  57.  
    replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
  58.  
    Can I set the above configuration? (type 'yes'to accept): yes
  59.  
    >>> Nodes configuration updated
  60.  
    >>> Assign a different config epoch to each node
  61.  
    >>> Sending CLUSTER MEET messages to join the cluster
  62.  
    Waiting for the cluster to join.....
  63.  
    >>> Performing Cluster Check (using node 192.168.37.131:7001)
  64.  
    M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
  65.  
    slots:0-5460 (5461 slots) master
  66.  
    M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
  67.  
    slots:5461-10922 (5462 slots) master
  68.  
    M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
  69.  
    slots:10923-16383 (5461 slots) master
  70.  
    M: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
  71.  
    slots: (0 slots) master
  72.  
    replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
  73.  
    M: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
  74.  
    slots: (0 slots) master
  75.  
    replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
  76.  
    M: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
  77.  
    slots: (0 slots) master
  78.  
    replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
  79.  
    [OK] All nodes agree about slots configuration.
  80.  
    >>> Check for open slots...
  81.  
    >>> Check slots coverage...
  82.  
    [OK] All 16384 slots covered.
  83.  
    [root@localhost redis-cluster]#
异常
  1.  
    >>> Creating cluster
  2.  
    Connecting to node 192.168.37.131:7001: OK
  3.  
    [ERR] Node 192.168.37.131:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决方法是删除生成的配置文件nodes.conf,如果不行则说明现在创建的结点包括了旧集群的结点信息,需要删除redis的持久化文件后再重启redis,比如:appendonly.aofdump.rdb

首先,使用stop-all.sh停止服务

  1.  
    [root@localhost redis-cluster]# ./stop-all.sh
  2.  
    [root@localhost redis-cluster]# ps aux | grep redis
  3.  
    root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster]
  4.  
    root 11969 0.0 0.0 4360 744 pts/2 S+ 01:10 0:00 grep redis
  5.  
    [root@localhost redis-cluster]#

然后每个节点中的appendonly.aofdump.rdb 、nodes.conf

  1.  
    [root@localhost redis-cluster]# rm -f redis*/dump.rdb
  2.  
    [root@localhost redis-cluster]# rm -f redis*/appendonly.aof
  3.  
    [root@localhost redis-cluster]# rm -f redis*/nodes.conf

然后使用脚本start-all.sh启动,再启动集群管理服务。

  1.  
    [root@localhost redis-cluster]# ./start-all.sh
  2.  
    [root@localhost redis-cluster]# ps aux | grep redis
  3.  
    root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster]
  4.  
    root 11980 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7001 [cluster]
  5.  
    root 11982 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7002 [cluster]
  6.  
    root 11984 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7003 [cluster]
  7.  
    root 11986 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7004 [cluster]
  8.  
    root 11988 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7005 [cluster]
  9.  
    root 11990 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7006 [cluster]
  10.  
    root 12004 0.0 0.0 4360 748 pts/2 S+ 01:12 0:00 grep redis
  11.  
    [root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1192.168.37.131:7001192.168.37.131:7002192.168.37.131:7003192.168.37.131:7004192.168.37.131:7005192.168.37.131:7006
  12.  
    [root@localhost redis-cluster]#

Redis集群节点的操作

查询集群信息

集群创建成功登陆任意redis结点查询集群中的节点情况。

客户端以集群方式登陆:

  1.  
    [root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
  2.  
    192.168.37.131:7001>

说明: 
./redis1/redis-cli -c -h 192.168.37.131 -p 7001 ,其中-c表示以集群方式连接redis-h指定ip地址,-p指定端口号

cluster nodes 查询集群结点信息

  1.  
    192.168.37.131:7001> cluster nodes
  2.  
    8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085160899 3 connected 10923-16383
  3.  
    80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085156858 8 connected 0-5460
  4.  
    652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085158876 9 connected
  5.  
    1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
  6.  
    3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085159889 9 connected 5461-10922
  7.  
    cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085157867 6 connected
  8.  
    192.168.37.131:7001>

cluster info 查询集群状态信息

  1.  
    192.168.37.131:7001> cluster info
  2.  
    cluster_state:ok
  3.  
    cluster_slots_assigned:16384
  4.  
    cluster_slots_ok:16384
  5.  
    cluster_slots_pfail:0
  6.  
    cluster_slots_fail:0
  7.  
    cluster_known_nodes:6
  8.  
    cluster_size:3
  9.  
    cluster_current_epoch:9
  10.  
    cluster_my_epoch:8
  11.  
    cluster_stats_messages_sent:12727
  12.  
    cluster_stats_messages_received:10820
  13.  
    192.168.37.131:7001>

添加主节点

集群创建成功后可以向集群中添加节点,下面是添加一个master主节点。

首先,准备一个干净的redis节点。按上面集群版修改redis配置文件。开启该redis节点。

查看redis进程

  1.  
    [root@localhost redis-cluster]# !ps
  2.  
    ps aux | grep redis
  3.  
    root 2924 0.6 0.1 33932 1800 ? Ssl Nov01 4:08 ./redis-server *:6379 [cluster]
  4.  
    root 11980 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7001 [cluster]
  5.  
    root 11982 0.0 0.2 33936 2244 ? Ssl 01:12 0:09 ./redis-server *:7002 [cluster]
  6.  
    root 11984 0.0 0.2 33936 2220 ? Ssl 01:12 0:09 ./redis-server *:7003 [cluster]
  7.  
    root 11986 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7004 [cluster]
  8.  
    root 11988 0.0 0.2 33936 2228 ? Ssl 01:12 0:09 ./redis-server *:7005 [cluster]
  9.  
    root 11990 0.0 0.2 33936 2212 ? Ssl 01:12 0:09 ./redis-server *:7006 [cluster]
  10.  
    root 13913 0.0 0.1 33936 1952 ? Ssl 04:21 0:00 ./redis7/redis-server *:7007 [cluster]
  11.  
    root 13917 0.0 0.0 4360 728 pts/3 S+ 04:21 0:00 grep redis
  12.  
    [root@localhost redis-cluster]#

执行下边命令:

  1.  
    [root@localhost redis-cluster]# ./redis-trib.rb add-node 192.168.37.131:7007 192.168.37.131:7001
  2.  
    >>> Adding node 192.168.37.131:7007 to cluster 192.168.37.131:7001
  3.  
    Connecting to node 192.168.37.131:7001: OK
  4.  
    Connecting to node 192.168.37.131:7003: OK
  5.  
    Connecting to node 192.168.37.131:7004: OK
  6.  
    Connecting to node 192.168.37.131:7005: OK
  7.  
    Connecting to node 192.168.37.131:7002: OK
  8.  
    Connecting to node 192.168.37.131:7006: OK
  9.  
    >>> Performing Cluster Check (using node 192.168.37.131:7001)
  10.  
    S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
  11.  
    slots: (0 slots) slave
  12.  
    replicates 80de7003738f74134a3403fc939fed253b7774f2
  13.  
    M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
  14.  
    slots:10923-16383 (5461 slots) master
  15.  
    1 additional replica(s)
  16.  
    M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
  17.  
    slots:0-5460 (5461 slots) master
  18.  
    1 additional replica(s)
  19.  
    S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
  20.  
    slots: (0 slots) slave
  21.  
    replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
  22.  
    M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
  23.  
    slots:5461-10922 (5462 slots) master
  24.  
    1 additional replica(s)
  25.  
    S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
  26.  
    slots: (0 slots) slave
  27.  
    replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
  28.  
    [OK] All nodes agree about slots configuration.
  29.  
    >>> Check for open slots...
  30.  
    >>> Check slots coverage...
  31.  
    [OK] All 16384 slots covered.
  32.  
    Connecting to node 192.168.37.131:7007: OK
  33.  
    >>> Send CLUSTER MEET to node 192.168.37.131:7007 to make it join the cluster.
  34.  
    [OK] New node added correctly.
  35.  
    [root@localhost redis-cluster]#

查看集群结点发现7007已添加到集群中:

  1.  
    [root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
  2.  
    192.168.37.131:7001> cluster nodes
  3.  
    8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085870038 3 connected 10923-16383
  4.  
    80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085868020 8 connected 0-5460
  5.  
    652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085874075 9 connected
  6.  
    1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
  7.  
    3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085873064 9 connected 5461-10922
  8.  
    cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085875086 6 connected
  9.  
    5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478085872056 0 connected
  10.  
    192.168.37.131:7001>
hash槽重新分配

添加完主节点需要对主节点进行hash槽分配这样该主节才可以存储数据。 
redis集群有16384个槽,集群中的每个master结点分配一些槽,通过查看集群结点可以看到槽占用情况。

给刚添加的7007结点分配槽:

第一步:连接上集群 
./redis-trib.rb reshard 192.168.37.131:7001(连接集群中任意一个可用结点都行)

  1.  
    [root@localhost redis-cluster]# ./redis-trib.rb reshard 192.168.37.131:7001
  2.  
    Connecting to node 192.168.37.131:7001: OK
  3.  
    Connecting to node 192.168.37.131:7003: OK
  4.  
    Connecting to node 192.168.37.131:7004: OK
  5.  
    Connecting to node 192.168.37.131:7005: OK
  6.  
    Connecting to node 192.168.37.131:7002: OK
  7.  
    Connecting to node 192.168.37.131:7006: OK
  8.  
    Connecting to node 192.168.37.131:7007: OK
  9.  
    >>> Performing Cluster Check (using node 192.168.37.131:7001)
  10.  
    S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
  11.  
    slots: (0 slots) slave
  12.  
    replicates 80de7003738f74134a3403fc939fed253b7774f2
  13.  
    M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
  14.  
    slots:10923-16383 (5461 slots) master
  15.  
    1 additional replica(s)
  16.  
    M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
  17.  
    slots:0-5460 (5461 slots) master
  18.  
    1 additional replica(s)
  19.  
    S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
  20.  
    slots: (0 slots) slave
  21.  
    replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
  22.  
    M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
  23.  
    slots:5461-10922 (5462 slots) master
  24.  
    1 additional replica(s)
  25.  
    S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
  26.  
    slots: (0 slots) slave
  27.  
    replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
  28.  
    M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007
  29.  
    slots: (0 slots) master
  30.  
    0 additional replica(s)
  31.  
    [OK] All nodes agree about slots configuration.
  32.  
    >>> Check for open slots...
  33.  
    >>> Check slots coverage...
  34.  
    [OK] All 16384 slots covered.
  35.  
    How many slots do you want to move (from 1 to 16384)?

第二步:输入要分配的槽数量

  1.  
    How many slots do you want to move (from 1 to 16384)? 1000
  2.  
    What is the receiving node ID?

输入1000表示要分配1000个槽

第三步:输入接收槽的结点id

这里准备给7007分配槽,通过cluster nodes查看7007结点id5d6c61ecff23bff3b0fb01a86c66d882f2d402a0

输入:5d6c61ecff23bff3b0fb01a86c66d882f2d402a0

  1.  
    What is the receiving node ID? 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
  2.  
    Please enter all the source node IDs.
  3.  
    Type 'all' to use all the nodes as source nodes for the hash slots.
  4.  
    Type 'done' once you entered all the source nodes IDs.
  5.  
    Source node #1:

第四步:输入源结点id 
输入源结点id,槽将从源结点中拿,分配后的槽在源结点中就不存在了。 
输入all表示从所有源结点中获取槽。 
输入done取消分配。

这里输入all

  1.  
    Source node #1:all
  2.  
     
  3.  
    省略了很多
  4.  
     
  5.  
    Moving slot 11253 from 8e1186475f87c928e8a146d3804d9a2697246ad0
  6.  
    Moving slot 11254 from 8e1186475f87c928e8a146d3804d9a2697246ad0
  7.  
    Moving slot 11255 from 8e1186475f87c928e8a146d3804d9a2697246ad0
  8.  
    Do you want to proceed with the proposed reshard plan (yes/no)?

第五步:输入yes开始移动槽到目标结点id

  1.  
    省略了很多
  2.  
     
  3.  
    Moving slot 11253 from 192.168.37.131:7003 to 192.168.37.131:7007:
  4.  
    Moving slot 11254 from 192.168.37.131:7003 to 192.168.37.131:7007:
  5.  
    Moving slot 11255 from 192.168.37.131:7003 to 192.168.37.131:7007:
  6.  
    [root@localhost redis-cluster]#

第六步:查看结点信息

  1.  
    [root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
  2.  
    192.168.37.131:7001> cluster nodes
  3.  
    8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478086754466 3 connected 11256-16383
  4.  
    80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478086758509 8 connected 333-5460
  5.  
    652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478086756490 9 connected
  6.  
    1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
  7.  
    3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478086757500 9 connected 5795-10922
  8.  
    cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478086755477 6 connected
  9.  
    5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478086759518 10 connected 0-332 5461-5794 10923-11255
  10.  
    192.168.37.131:7001>

查看结点信息,可以发现7007结点已经从三个主节点中获取了slot

添加从节点

集群创建成功后可以向集群中添加节点,下面是添加一个slave从节点。 
添加7008从结点,将7008作为7007的从结点。

首先,准备一个干净的redis节点。按上面集群版修改redis配置文件。开启该redis节点。

查看redis进程

  1.  
    [root@localhost redis-cluster]# ps aux | grep redis
  2.  
    root 2924 0.5 0.1 33932 1800 ? Ssl Nov01 4:12 ./redis-server *:6379 [cluster]
  3.  
    root 11980 0.0 0.2 33936 2308 ? Ssl 01:12 0:13 ./redis-server *:7001 [cluster]
  4.  
    root 11982 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7002 [cluster]
  5.  
    root 11984 0.0 0.2 33936 2236 ? Ssl 01:12 0:13 ./redis-server *:7003 [cluster]
  6.  
    root 11986 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7004 [cluster]
  7.  
    root 11988 0.0 0.2 33936 2248 ? Ssl 01:12 0:13 ./redis-server *:7005 [cluster]
  8.  
    root 11990 0.0 0.2 33936 2244 ? Ssl 01:12 0:13 ./redis-server *:7006 [cluster]
  9.  
    root 13913 0.1 0.2 33936 2092 ? Ssl 04:21 0:04 ./redis7/redis-server *:7007 [cluster]
  10.  
    root 14000 0.1 0.1 33936 1948 ? Ssl 05:24 0:00 ./redis-server *:7008 [cluster]
  11.  
    root 14006 0.0 0.0 4360 732 pts/3 S+ 05:24 0:00 grep redis
  12.  
    [root@localhost redis-cluster]#

命令格式为:

./redis-trib.rb add-node --slave --master-id 主节点id 添加节点的ip和端口 集群中已存在节点ip和端口

执行如下命令:

  1.  
    [root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7008 192.168.37.131:7001
  2.  
    >>> Adding node 192.168.37.131:7008 to cluster 192.168.37.131:7001
  3.  
    Connecting to node 192.168.37.131:7001: OK
  4.  
    Connecting to node 192.168.37.131:7003: OK
  5.  
    Connecting to node 192.168.37.131:7004: OK
  6.  
    Connecting to node 192.168.37.131:7005: OK
  7.  
    Connecting to node 192.168.37.131:7002: OK
  8.  
    Connecting to node 192.168.37.131:7006: OK
  9.  
    Connecting to node 192.168.37.131:7007: OK
  10.  
    >>> Performing Cluster Check (using node 192.168.37.131:7001)
  11.  
    S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
  12.  
    slots: (0 slots) slave
  13.  
    replicates 80de7003738f74134a3403fc939fed253b7774f2
  14.  
    M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
  15.  
    slots:11256-16383 (5128 slots) master
  16.  
    1 additional replica(s)
  17.  
    M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
  18.  
    slots:333-5460 (5128 slots) master
  19.  
    1 additional replica(s)
  20.  
    S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
  21.  
    slots: (0 slots) slave
  22.  
    replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
  23.  
    M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
  24.  
    slots:5795-10922 (5128 slots) master
  25.  
    1 additional replica(s)
  26.  
    S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
  27.  
    slots: (0 slots) slave
  28.  
    replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
  29.  
    M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007
  30.  
    slots:0-332,5461-5794,10923-11255 (1000 slots) master
  31.  
    0 additional replica(s)
  32.  
    [OK] All nodes agree about slots configuration.
  33.  
    >>> Check for open slots...
  34.  
    >>> Check slots coverage...
  35.  
    [OK] All 16384 slots covered.
  36.  
    Connecting to node 192.168.37.131:7008: OK
  37.  
    >>> Send CLUSTER MEET to node 192.168.37.131:7008 to make it join the cluster.
  38.  
    Waiting for the cluster to join.
  39.  
    >>> Configure node as replica of 192.168.37.131:7007.
  40.  
    [OK] New node added correctly.
  41.  
    [root@localhost redis-cluster]#

5d6c61ecff23bff3b0fb01a86c66d882f2d402a07007结点的id,可以通过cluster nodes查看。

查看集群中的结点,刚添加的70087007的从节点:

  1.  
    [root@localhost redis-cluster]# ./redis1/redis-cli -p 7001 -c 127.0.0.1:7001> cluster nodes
  2.  
    8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478089964769 3 connected 11256-16383
  3.  
    80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478089966584 8 connected 333-5460
  4.  
    652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478089963748 9 connected
  5.  
    1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
  6.  
    3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478089966787 9 connected 5795-10922
  7.  
    cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478089962729 6 connected
  8.  
    5c97e8eab019c40ea3df4925c7400fe7df1846bb 192.168.37.131:7008 slave 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 0 1478089961713 10 connected
  9.  
    5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478089965777 10 connected 0-332 5461-5794 10923-11255
  10.  
    127.0.0.1:7001>

删除结点

./redis-trib.rb del-node 192.168.37.131:7007 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0

删除已经占有hash槽的结点会失败,报错如下:

[ERR] Node 192.168.37.131:7007 is not empty! Reshard data away and try again.

需要将该结点占用的hash槽分配出去,请参考《hash槽重新分配》这段内容。

客户端对Redis集群的使用方法

使用redis命令行客户端连接

  1.  
    [root@localhost redis-cluster]# ./redis1/redis-cli -p 7001 -c
  2.  
    127.0.0.1:7001> get a
  3.  
    -> Redirected to slot [15495] located at 192.168.37.131:7003
  4.  
    (nil)
  5.  
    192.168.37.131:7003>

一定要加-c参数,节点之间就可以互相跳转

使用jedis连接

  1.  
    package com.pc.jedis.test;
  2.  
     
  3.  
    import java.util.HashSet;
  4.  
    import java.util.Set;
  5.  
     
  6.  
    import redis.clients.jedis.HostAndPort;
  7.  
    import redis.clients.jedis.JedisCluster;
  8.  
     
  9.  
    /**
  10.  
    * Jedis集群测试
  11.  
    *
  12.  
    * @author Switch
  13.  
    * @data 2017年2月11日
  14.  
    * @version V1.0
  15.  
    */
  16.  
    public classJedisClusterTest{
  17.  
    publicstaticvoidmain(String[] args) {
  18.  
    // 创建并填充节点信息
  19.  
    Set<HostAndPort> nodes = new HashSet<>();
  20.  
    nodes.add(new HostAndPort("192.168.37.131", 7001));
  21.  
    nodes.add(new HostAndPort("192.168.37.131", 7002));
  22.  
    nodes.add(new HostAndPort("192.168.37.131", 7003));
  23.  
    nodes.add(new HostAndPort("192.168.37.131", 7004));
  24.  
    nodes.add(new HostAndPort("192.168.37.131", 7005));
  25.  
    nodes.add(new HostAndPort("192.168.37.131", 7006));
  26.  
     
  27.  
    // 创建JedisCluster对象
  28.  
    JedisCluster jedisCluster = new JedisCluster(nodes);
  29.  
     
  30.  
    // 使用jedisCluster操作redis
  31.  
    String key = "jedisCluster";
  32.  
    String setResult = jedisCluster.set(key, "hello redis!");
  33.  
    System.out.println(setResult);
  34.  
     
  35.  
    String getResult = jedisCluster.get(key);
  36.  
    System.out.println(getResult);
  37.  
     
  38.  
    // 关闭jedisCluster(程序执行完后才能关闭,内部封装了连接池)
  39.  
    jedisCluster.close();
  40.  
    }
  41.  
    }

——参考《Redis官方文档》

分享到:
评论

相关推荐

    CentOs7.3部署nginx+tomcat+redis集群说明.docx

    CentOs7.3部署nginx+tomcat+redis集群说明.docx

    rancher2.3.6部署redis集群配置教程.pdf

    标题中的“rancher2.3.6部署redis集群配置教程.pdf”暗示了这份文档是针对特定版本的Rancher软件(版本号2.3.6)的使用教程,旨在指导用户如何部署Redis集群。Rancher是一款开源的容器管理平台,可以用来部署、管理...

    linux Redis集群负载均衡部署完成

    linux redis单个和redis集群部署完成 技术:redis集群 说明包含: redis安装包 Redis集群负载均衡部署帮助文档 Redis集群负载均衡使用帮助文档 Redis集群负载均衡遇到问题解决文档 redis网页资料 linux内网...

    redis集群搭建步骤.docx

    1.1 Redis 集群说明 Redis 集群是 Redis 的一种高可用性解决方案,可以实现高可用性和扩展性。Redis 集群由多个 Redis 节点组成,每个节点都是一个独立的 Redis 实例。这些节点之间可以相互通信,实现数据的复制和...

    Redis集群搭建工具及教程

    在本资源包中,提供了搭建Redis集群所需的各种工具和教程,帮助你在Windows环境下进行操作。 首先,让我们来了解Redis集群的基础知识。Redis集群通过分片(Sharding)技术将数据分散到多个节点上,实现数据的冗余和...

    Redis集群搭建与验证.pdf

    Redis集群搭建与验证的知识点主要包括以下几个方面: 1. Redis集群概述 Redis集群是Redis提供的分布式数据库解决方案,它可以将数据自动切分到多个Redis节点上。集群通过分区来提供一定程度的可用性,在部分节点...

    Redis集群部署文档

    #### 一、Redis集群概述与服务器说明 Redis集群是Redis的一种分布式部署方式,通过多个Redis实例组成集群,实现数据的高可用性和负载均衡。本部署文档介绍了一个由六台虚拟服务器组成的Redis集群,这些服务器运行于...

    redis 集群安装 ruby 资源报

    然而,描述中没有具体说明需要哪个 `jar` 文件,所以我们假设这里可能是为了运行 Redis 的集群管理工具或者连接器。 Ruby 是一种动态、面向对象的脚本语言,它提供了丰富的库和工具,包括对 Redis 的支持。`ruby-...

    windows后台服务形式启动redis集群及powershell脚本

    在Windows上部署Redis集群,我们可以借助PowerShell脚本来自动化这一过程。 首先,`redis.conf`是Redis的配置文件,它包含了Redis服务器的各项参数设定,如端口号、数据持久化策略、内存限制等。在设置Redis集群时...

    Redis集群部署

    如果能成功读取到之前写入的值,那么说明Redis集群已经成功搭建。 #### 四、总结 通过上述步骤,我们已经成功地部署了一个包含六个节点的Redis集群。此集群具有良好的可扩展性和高可用性,适用于需要处理大量并发...

    docker 部署Redis集群节点.pdf

    如果您需要部署一个Redis集群,Docker可以帮助您更轻松地完成此任务。以下是关于使用Docker部署Redis集群节点的一些资源: - [Redis Docker官方镜像文档](https://hub.docker.com/_/redis):官方文档,包含了Redis ...

    redis集群搭建手册.docx

    **Redis集群搭建手册** Redis,全称Remote Dictionary Server,是一种高性能的键值对数据库,以其丰富的数据类型、高效的内存管理以及支持持久化等特性,在分布式系统中被广泛应用于缓存、消息中间件以及数据存储等...

    Redis-5.0.0集群配置

    为了构建一个健壮的Redis集群,至少需要三个主节点(master)和三个从节点(slave),共计六个节点。在测试环境中,可以在同一台物理机上启动六个Redis实例来模拟集群环境;而在生产环境中,则推荐使用至少三台不同的...

    Redis集群教程和集群规范.zip

    内容概要:Redis集群教程文档和Redis集群规范文档。 适用人群:有Redis单机部署经验,对Redis较为熟悉,需要搭建集群环境的技术人员。 使用场景和目标:适用于高并发,高可用,强一致性等场景。适合做分布式环境下的...

    离线安装redis集群.zip

    在整个过程中,务必参照`redis集群离线部署.doc`文档进行操作,因为它包含了具体配置和命令的详细说明,确保每个步骤都按照文档要求执行,以确保离线部署的顺利进行。离线部署虽然比在线部署复杂,但通过精心的准备...

    Redis集群安装与测试.docx

    本篇文章将深入探讨 Redis 集群的安装与测试,包括其基本概念、集群模式、版本说明以及测试环境。 1. Redis 简介 Redis 提供了多种数据类型,如字符串(string)、列表(list)、集合(set)、有序集合(zset)和哈希(hash)...

    redis集群的总结、搭建、 springboot整合测试

    Redis是一种高性能的键值对数据存储系统,常...以上就是Redis集群的总结、搭建过程以及如何在SpringBoot应用中进行整合测试的详细说明。通过实践,可以更深入地理解和掌握Redis集群的使用,提升系统的稳定性和性能。

    redis集群环境搭建(含基本增删改查和文档说明)

    Redis 集群环境搭建与Java操作指南 Redis 是一款高性能的键值数据库,广泛应用于缓存、消息队列等领域。为了实现高可用性和数据冗余,Redis 提供了集群(Cluster)功能。本指南将详细阐述如何搭建 Redis 集群环境,...

    jfinal redis cluster plugin-JFinal redis cluster集群插件 带连接池和Jedis包

    标题中的“jfinal redis cluster plugin”指的是一个专为JFinal框架设计的Redis集群插件,旨在帮助开发者在使用JFinal时能便捷地接入并管理Redis集群。JFinal是一款基于Java的轻量级Web开发框架,它以其简洁的API...

    windows下redis集群安装

    在Windows环境下搭建Redis集群虽然相比Linux有一些额外的步骤,但通过以上详细说明,你应该能够成功地构建一个稳定、高效的Redis集群。记得在生产环境中,还需要考虑安全配置、监控和备份等重要因素,以确保数据的...

Global site tag (gtag.js) - Google Analytics