Redis集群
基本介绍
Redis
集群是一个可以在多个 Redis
节点之间进行数据共享的设施installation
。
Redis
集群不支持那些需要同时处理多个键的 Redis
命令, 因为执行这些命令需要在多个 Redis
节点之间移动数据, 并且在高负载的情况下, 这些命令将降低Redis
集群的性能, 并导致不可预测的行为。
Redis
集群通过分区partition
来提供一定程度的可用性availability
: 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
Redis
集群提供了以下两个好处:
- 将数据自动切分
split
到多个节点的能力。 - 当集群中的一部分节点失效或者无法进行通讯时, 仍然可以继续处理命令请求的能力。
集群原理
redis-cluster架构图
-
所有的
redis
节点彼此互联(PING
-PONG
机制),内部使用二进制协议优化传输速度和带宽。 -
节点的
fail
是通过集群中超过半数的节点检测失效时才生效。 -
客户端与
redis
节点直连,不需要中间proxy
层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。 -
redis-cluster
把所有的物理节点映射到[0-16383
]slot
上,cluster
负责维护node
<->slot
<->value
Redis
集群中内置了 16384
个哈希槽,当需要在 Redis
集群中放置一个 key-value
时,redis
先对key
使用 crc16
算法算出一个结果,然后把结果对 16384
求余数,这样每个 key
都会对应一个编号在 0-16383
之间的哈希槽,redis
会根据节点数量大致均等的将哈希槽映射到不同的节点
redis-cluster投票:容错
-
投票过程是集群中所有
master
参与,如果半数以上master
节点与master
节点通信超时(cluster-node-timeout
),认为当前master
节点挂掉. -
什么时候整个集群不可用(
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环境
使用
-
yum -y install ruby
-
yum -y install rubygems
安装ruby环境。
网上下载redis-3.0.0.gem
,执行gem install redis-3.0.0.gem
安装。
redis配置文件修改
现在已经准备好了,6
份干净的redis
,如下所示
-
[root@localhost redis-cluster]# pwd
-
/usr/local/redis/redis-cluster
-
[root@localhost redis-cluster]# ll
-
total 72
-
drwxr-xr-x 2 root root 4096 Nov 2 00:17 redis1
-
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis2
-
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis3
-
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis4
-
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis5
-
drwxr-xr-x 2 root root 4096 Nov 2 00:25 redis6
-
-rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
-
[root@localhost redis-cluster]#
PS:注意,这里已经将redis
源文件src
目录下的redis-trib.rb
文件拷贝过来了。 redis-trib.rb
这个文件是redis
集群的管理文件,ruby
脚本。
将六个节点的redis.conf
配置文件按照如下进行修改
-
################################ GENERAL #####################################
-
-
# By default Redis does not run as a daemon. Use 'yes' if you need it.
-
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
-
daemonize yes
-
-
# Accept connections on the specified port, default is 6379.
-
# If port 0 is specified Redis will not listen on a TCP socket.
-
port *
-
-
################################ REDIS CLUSTER ###############################
-
#
-
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
-
# in order to mark it as "mature" we need to wait for a non trivial percentage
-
# of users to deploy it in production.
-
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
#
-
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
-
# started as cluster nodes can. In order to start a Redis instance as a
-
# cluster node enable the cluster support uncommenting the following:
-
#
-
cluster-enabled yes
PS:端口号如果是同一台主机的话,必须不同。不同主机可以相同。
PS:我这里是使用一台主机,所以我将六个节点的端口号修改为7001
-7006
编写集群启动脚本和停止脚本
启动脚本start-all.sh
-
cd redis1
-
./redis-server redis.conf
-
cd ..
-
cd redis2
-
./redis-server redis.conf
-
cd ..
-
cd redis3
-
./redis-server redis.conf
-
cd ..
-
cd redis4
-
./redis-server redis.conf
-
cd ..
-
cd redis5
-
./redis-server redis.conf
-
cd ..
-
cd redis6
-
./redis-server redis.conf
-
cd ..
停止脚本stop-all.sh
-
./redis1/redis-cli -p 7001 shutdown
-
./redis1/redis-cli -p 7002 shutdown
-
./redis1/redis-cli -p 7003 shutdown
-
./redis1/redis-cli -p 7004 shutdown
-
./redis1/redis-cli -p 7005 shutdown
-
./redis1/redis-cli -p 7006 shutdown
PS:两个脚本都放在如下所属目录
-
[root@localhost redis-cluster]# pwd
-
/usr/local/redis/redis-cluster
-
[root@localhost redis-cluster]# ll
-
total 80
-
drwxr-xr-x 2 root root 4096 Nov 2 00:52 redis1
-
drwxr-xr-x 2 root root 4096 Nov 2 00:51 redis2
-
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis3
-
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis4
-
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis5
-
drwxr-xr-x 2 root root 4096 Nov 2 00:53 redis6
-
-rwxr-xr-x 1 root root 48141 Nov 2 00:16 redis-trib.rb
-
-rw-r--r-- 1 root root 252 Nov 2 00:55 start-all.sh
-
-rw-r--r-- 1 root root 216 Nov 2 00:57 stop-all.sh
-
[root@localhost redis-cluster]#
修改权限
[root@localhost redis-cluster]# chmod -u+x start-all.sh stop-all.sh
启动节点
-
[root@localhost redis-cluster]# ./start-all.sh
-
[root@localhost redis-cluster]# ps aux | grep redis
-
root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:53 ./redis-server *:6379 [cluster]
-
root 11924 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7001 [cluster]
-
root 11928 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7002 [cluster]
-
root 11932 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7003 [cluster]
-
root 11936 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7004 [cluster]
-
root 11940 0.0 0.1 33936 1952 ? Ssl 01:01 0:00 ./redis-server *:7005 [cluster]
-
root 11944 0.0 0.1 33936 1948 ? Ssl 01:01 0:00 ./redis-server *:7006 [cluster]
-
root 11948 0.0 0.0 4360 748 pts/2 S+ 01:01 0:00 grep redis
-
[root@localhost redis-cluster]#
执行创建集群命令
-
[root@localhost redis-cluster]# pwd
-
/usr/local/redis/redis-cluster
-
[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
成功启动信息
-
>>> Creating cluster
-
Connecting to node 192.168.37.131:7001: OK
-
Connecting to node 192.168.37.131:7002: OK
-
Connecting to node 192.168.37.131:7003: OK
-
Connecting to node 192.168.37.131:7004: OK
-
Connecting to node 192.168.37.131:7005: OK
-
Connecting to node 192.168.37.131:7006: OK
-
>>> Performing hash slots allocation on 6 nodes...
-
Using 3 masters:
-
192.168.37.131:7001
-
192.168.37.131:7002
-
192.168.37.131:7003
-
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
-
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
-
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
-
M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
-
slots:0-5460 (5461 slots) master
-
M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
-
slots:5461-10922 (5462 slots) master
-
M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
-
slots:10923-16383 (5461 slots) master
-
S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
-
replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
-
S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
-
replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
-
S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
-
replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
-
Can I set the above configuration? (type 'yes'to accept): y
-
*** Aborting...
-
[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
-
>>> Creating cluster
-
Connecting to node 192.168.37.131:7001: OK
-
Connecting to node 192.168.37.131:7002: OK
-
Connecting to node 192.168.37.131:7003: OK
-
Connecting to node 192.168.37.131:7004: OK
-
Connecting to node 192.168.37.131:7005: OK
-
Connecting to node 192.168.37.131:7006: OK
-
>>> Performing hash slots allocation on 6 nodes...
-
Using 3 masters:
-
192.168.37.131:7001
-
192.168.37.131:7002
-
192.168.37.131:7003
-
Adding replica 192.168.37.131:7004 to 192.168.37.131:7001
-
Adding replica 192.168.37.131:7005 to 192.168.37.131:7002
-
Adding replica 192.168.37.131:7006 to 192.168.37.131:7003
-
M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
-
slots:0-5460 (5461 slots) master
-
M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
-
slots:5461-10922 (5462 slots) master
-
M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
-
slots:10923-16383 (5461 slots) master
-
S: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
-
replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
-
S: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
-
replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
-
S: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
-
replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
-
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.37.131:7001)
-
M: 8b153503b52f83634e04b0077f32ef629ad91ee6 192.168.37.131:7001
-
slots:0-5460 (5461 slots) master
-
M: f89799066dd8ecaaa1430559be4ce9c8c87055d8 192.168.37.131:7002
-
slots:5461-10922 (5462 slots) master
-
M: 53d698ad56b09f89cfef34850213e2d0a44154dd 192.168.37.131:7003
-
slots:10923-16383 (5461 slots) master
-
M: e73204399d08c14def1f71d0c5377cbc757dc4b8 192.168.37.131:7004
-
slots: (0 slots) master
-
replicates 8b153503b52f83634e04b0077f32ef629ad91ee6
-
M: 1d5dcc8d1ccb6bce55efc3e3aadc690dc77808d8 192.168.37.131:7005
-
slots: (0 slots) master
-
replicates f89799066dd8ecaaa1430559be4ce9c8c87055d8
-
M: e9458233cb85bd897ff694003e6d8a834eba2b44 192.168.37.131:7006
-
slots: (0 slots) master
-
replicates 53d698ad56b09f89cfef34850213e2d0a44154dd
-
[OK] All nodes agree about slots configuration.
-
>>> Check for open slots...
-
>>> Check slots coverage...
-
[OK] All 16384 slots covered.
-
[root@localhost redis-cluster]#
异常
-
>>> Creating cluster
-
Connecting to node 192.168.37.131:7001: OK
-
[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.aof
、dump.rdb
首先,使用stop-all.sh
停止服务
-
[root@localhost redis-cluster]# ./stop-all.sh
-
[root@localhost redis-cluster]# ps aux | grep redis
-
root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster]
-
root 11969 0.0 0.0 4360 744 pts/2 S+ 01:10 0:00 grep redis
-
[root@localhost redis-cluster]#
然后每个节点中的appendonly.aof
、dump.rdb
、nodes.conf
。
-
[root@localhost redis-cluster]# rm -f redis*/dump.rdb
-
[root@localhost redis-cluster]# rm -f redis*/appendonly.aof
-
[root@localhost redis-cluster]# rm -f redis*/nodes.conf
然后使用脚本start-all.sh
启动,再启动集群管理服务。
-
[root@localhost redis-cluster]# ./start-all.sh
-
[root@localhost redis-cluster]# ps aux | grep redis
-
root 2924 0.8 0.1 33932 2048 ? Ssl Nov01 3:54 ./redis-server *:6379 [cluster]
-
root 11980 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7001 [cluster]
-
root 11982 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7002 [cluster]
-
root 11984 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7003 [cluster]
-
root 11986 0.0 0.1 33936 1952 ? Ssl 01:12 0:00 ./redis-server *:7004 [cluster]
-
root 11988 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7005 [cluster]
-
root 11990 0.0 0.1 33936 1948 ? Ssl 01:12 0:00 ./redis-server *:7006 [cluster]
-
root 12004 0.0 0.0 4360 748 pts/2 S+ 01:12 0:00 grep redis
-
[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
-
[root@localhost redis-cluster]#
Redis集群节点的操作
查询集群信息
集群创建成功登陆任意redis
结点查询集群中的节点情况。
客户端以集群方式登陆:
-
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
-
192.168.37.131:7001>
说明: ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
,其中-c
表示以集群方式连接redis
,-h
指定ip
地址,-p
指定端口号
cluster nodes
查询集群结点信息
-
192.168.37.131:7001> cluster nodes
-
8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085160899 3 connected 10923-16383
-
80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085156858 8 connected 0-5460
-
652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085158876 9 connected
-
1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
-
3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085159889 9 connected 5461-10922
-
cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085157867 6 connected
-
192.168.37.131:7001>
cluster info
查询集群状态信息
-
192.168.37.131:7001> cluster info
-
cluster_state:ok
-
cluster_slots_assigned:16384
-
cluster_slots_ok:16384
-
cluster_slots_pfail:0
-
cluster_slots_fail:0
-
cluster_known_nodes:6
-
cluster_size:3
-
cluster_current_epoch:9
-
cluster_my_epoch:8
-
cluster_stats_messages_sent:12727
-
cluster_stats_messages_received:10820
-
192.168.37.131:7001>
添加主节点
集群创建成功后可以向集群中添加节点,下面是添加一个master
主节点。
首先,准备一个干净的redis
节点。按上面集群版修改redis
配置文件。开启该redis
节点。
查看redis
进程
-
[root@localhost redis-cluster]# !ps
-
ps aux | grep redis
-
root 2924 0.6 0.1 33932 1800 ? Ssl Nov01 4:08 ./redis-server *:6379 [cluster]
-
root 11980 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7001 [cluster]
-
root 11982 0.0 0.2 33936 2244 ? Ssl 01:12 0:09 ./redis-server *:7002 [cluster]
-
root 11984 0.0 0.2 33936 2220 ? Ssl 01:12 0:09 ./redis-server *:7003 [cluster]
-
root 11986 0.0 0.2 33936 2216 ? Ssl 01:12 0:09 ./redis-server *:7004 [cluster]
-
root 11988 0.0 0.2 33936 2228 ? Ssl 01:12 0:09 ./redis-server *:7005 [cluster]
-
root 11990 0.0 0.2 33936 2212 ? Ssl 01:12 0:09 ./redis-server *:7006 [cluster]
-
root 13913 0.0 0.1 33936 1952 ? Ssl 04:21 0:00 ./redis7/redis-server *:7007 [cluster]
-
root 13917 0.0 0.0 4360 728 pts/3 S+ 04:21 0:00 grep redis
-
[root@localhost redis-cluster]#
执行下边命令:
-
[root@localhost redis-cluster]# ./redis-trib.rb add-node 192.168.37.131:7007 192.168.37.131:7001
-
>>> Adding node 192.168.37.131:7007 to cluster 192.168.37.131:7001
-
Connecting to node 192.168.37.131:7001: OK
-
Connecting to node 192.168.37.131:7003: OK
-
Connecting to node 192.168.37.131:7004: OK
-
Connecting to node 192.168.37.131:7005: OK
-
Connecting to node 192.168.37.131:7002: OK
-
Connecting to node 192.168.37.131:7006: OK
-
>>> Performing Cluster Check (using node 192.168.37.131:7001)
-
S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
-
slots: (0 slots) slave
-
replicates 80de7003738f74134a3403fc939fed253b7774f2
-
M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
-
slots:10923-16383 (5461 slots) master
-
1 additional replica(s)
-
M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
-
slots:0-5460 (5461 slots) master
-
1 additional replica(s)
-
S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
-
slots: (0 slots) slave
-
replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
-
M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
-
slots:5461-10922 (5462 slots) master
-
1 additional replica(s)
-
S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
-
slots: (0 slots) slave
-
replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
-
[OK] All nodes agree about slots configuration.
-
>>> Check for open slots...
-
>>> Check slots coverage...
-
[OK] All 16384 slots covered.
-
Connecting to node 192.168.37.131:7007: OK
-
>>> Send CLUSTER MEET to node 192.168.37.131:7007 to make it join the cluster.
-
[OK] New node added correctly.
-
[root@localhost redis-cluster]#
查看集群结点发现7007
已添加到集群中:
-
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
-
192.168.37.131:7001> cluster nodes
-
8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478085870038 3 connected 10923-16383
-
80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478085868020 8 connected 0-5460
-
652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478085874075 9 connected
-
1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
-
3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478085873064 9 connected 5461-10922
-
cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478085875086 6 connected
-
5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478085872056 0 connected
-
192.168.37.131:7001>
hash槽重新分配
添加完主节点需要对主节点进行hash
槽分配这样该主节才可以存储数据。 redis
集群有16384
个槽,集群中的每个master
结点分配一些槽,通过查看集群结点可以看到槽占用情况。
给刚添加的7007
结点分配槽:
第一步:连接上集群 ./redis-trib.rb reshard 192.168.37.131:7001
(连接集群中任意一个可用结点都行)
-
[root@localhost redis-cluster]# ./redis-trib.rb reshard 192.168.37.131:7001
-
Connecting to node 192.168.37.131:7001: OK
-
Connecting to node 192.168.37.131:7003: OK
-
Connecting to node 192.168.37.131:7004: OK
-
Connecting to node 192.168.37.131:7005: OK
-
Connecting to node 192.168.37.131:7002: OK
-
Connecting to node 192.168.37.131:7006: OK
-
Connecting to node 192.168.37.131:7007: OK
-
>>> Performing Cluster Check (using node 192.168.37.131:7001)
-
S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
-
slots: (0 slots) slave
-
replicates 80de7003738f74134a3403fc939fed253b7774f2
-
M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
-
slots:10923-16383 (5461 slots) master
-
1 additional replica(s)
-
M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
-
slots:0-5460 (5461 slots) master
-
1 additional replica(s)
-
S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
-
slots: (0 slots) slave
-
replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
-
M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
-
slots:5461-10922 (5462 slots) master
-
1 additional replica(s)
-
S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
-
slots: (0 slots) slave
-
replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
-
M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007
-
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)?
第二步:输入要分配的槽数量
-
How many slots do you want to move (from 1 to 16384)? 1000
-
What is the receiving node ID?
输入1000
表示要分配1000
个槽
第三步:输入接收槽的结点id
这里准备给7007
分配槽,通过cluster nodes
查看7007
结点id
为5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
输入:5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
-
What is the receiving node ID? 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
-
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:
第四步:输入源结点id
输入源结点id
,槽将从源结点中拿,分配后的槽在源结点中就不存在了。
输入all
表示从所有源结点中获取槽。
输入done
取消分配。
这里输入all
-
Source node #1:all
-
-
省略了很多
-
-
Moving slot 11253 from 8e1186475f87c928e8a146d3804d9a2697246ad0
-
Moving slot 11254 from 8e1186475f87c928e8a146d3804d9a2697246ad0
-
Moving slot 11255 from 8e1186475f87c928e8a146d3804d9a2697246ad0
-
Do you want to proceed with the proposed reshard plan (yes/no)?
第五步:输入yes
开始移动槽到目标结点id
-
省略了很多
-
-
Moving slot 11253 from 192.168.37.131:7003 to 192.168.37.131:7007:
-
Moving slot 11254 from 192.168.37.131:7003 to 192.168.37.131:7007:
-
Moving slot 11255 from 192.168.37.131:7003 to 192.168.37.131:7007:
-
[root@localhost redis-cluster]#
第六步:查看结点信息
-
[root@localhost redis-cluster]# ./redis1/redis-cli -c -h 192.168.37.131 -p 7001
-
192.168.37.131:7001> cluster nodes
-
8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478086754466 3 connected 11256-16383
-
80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478086758509 8 connected 333-5460
-
652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478086756490 9 connected
-
1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
-
3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478086757500 9 connected 5795-10922
-
cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478086755477 6 connected
-
5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478086759518 10 connected 0-332 5461-5794 10923-11255
-
192.168.37.131:7001>
查看结点信息,可以发现7007
结点已经从三个主节点中获取了slot
。
添加从节点
集群创建成功后可以向集群中添加节点,下面是添加一个slave
从节点。
添加7008
从结点,将7008
作为7007
的从结点。
首先,准备一个干净的redis
节点。按上面集群版修改redis
配置文件。开启该redis
节点。
查看redis
进程
-
[root@localhost redis-cluster]# ps aux | grep redis
-
root 2924 0.5 0.1 33932 1800 ? Ssl Nov01 4:12 ./redis-server *:6379 [cluster]
-
root 11980 0.0 0.2 33936 2308 ? Ssl 01:12 0:13 ./redis-server *:7001 [cluster]
-
root 11982 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7002 [cluster]
-
root 11984 0.0 0.2 33936 2236 ? Ssl 01:12 0:13 ./redis-server *:7003 [cluster]
-
root 11986 0.0 0.2 33936 2288 ? Ssl 01:12 0:13 ./redis-server *:7004 [cluster]
-
root 11988 0.0 0.2 33936 2248 ? Ssl 01:12 0:13 ./redis-server *:7005 [cluster]
-
root 11990 0.0 0.2 33936 2244 ? Ssl 01:12 0:13 ./redis-server *:7006 [cluster]
-
root 13913 0.1 0.2 33936 2092 ? Ssl 04:21 0:04 ./redis7/redis-server *:7007 [cluster]
-
root 14000 0.1 0.1 33936 1948 ? Ssl 05:24 0:00 ./redis-server *:7008 [cluster]
-
root 14006 0.0 0.0 4360 732 pts/3 S+ 05:24 0:00 grep redis
-
[root@localhost redis-cluster]#
命令格式为:
./redis-trib.rb add-node --slave --master-id 主节点id 添加节点的ip和端口 集群中已存在节点ip和端口
执行如下命令:
-
[root@localhost redis-cluster]# ./redis-trib.rb add-node --slave --master-id 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7008 192.168.37.131:7001
-
>>> Adding node 192.168.37.131:7008 to cluster 192.168.37.131:7001
-
Connecting to node 192.168.37.131:7001: OK
-
Connecting to node 192.168.37.131:7003: OK
-
Connecting to node 192.168.37.131:7004: OK
-
Connecting to node 192.168.37.131:7005: OK
-
Connecting to node 192.168.37.131:7002: OK
-
Connecting to node 192.168.37.131:7006: OK
-
Connecting to node 192.168.37.131:7007: OK
-
>>> Performing Cluster Check (using node 192.168.37.131:7001)
-
S: 1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001
-
slots: (0 slots) slave
-
replicates 80de7003738f74134a3403fc939fed253b7774f2
-
M: 8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003
-
slots:11256-16383 (5128 slots) master
-
1 additional replica(s)
-
M: 80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004
-
slots:333-5460 (5128 slots) master
-
1 additional replica(s)
-
S: 652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005
-
slots: (0 slots) slave
-
replicates 3adba62fdcc331ce231ca580cd2c8701e047bc6d
-
M: 3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002
-
slots:5795-10922 (5128 slots) master
-
1 additional replica(s)
-
S: cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006
-
slots: (0 slots) slave
-
replicates 8e1186475f87c928e8a146d3804d9a2697246ad0
-
M: 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007
-
slots:0-332,5461-5794,10923-11255 (1000 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.
-
Connecting to node 192.168.37.131:7008: OK
-
>>> Send CLUSTER MEET to node 192.168.37.131:7008 to make it join the cluster.
-
Waiting for the cluster to join.
-
>>> Configure node as replica of 192.168.37.131:7007.
-
[OK] New node added correctly.
-
[root@localhost redis-cluster]#
5d6c61ecff23bff3b0fb01a86c66d882f2d402a0
是7007
结点的id
,可以通过cluster nodes
查看。
查看集群中的结点,刚添加的7008
为7007
的从节点:
-
[root@localhost redis-cluster]# ./redis1/redis-cli -p 7001 -c 127.0.0.1:7001> cluster nodes
-
8e1186475f87c928e8a146d3804d9a2697246ad0 192.168.37.131:7003 master - 0 1478089964769 3 connected 11256-16383
-
80de7003738f74134a3403fc939fed253b7774f2 192.168.37.131:7004 master - 0 1478089966584 8 connected 333-5460
-
652caf5daf7971135679951324eba7b50e99251a 192.168.37.131:7005 slave 3adba62fdcc331ce231ca580cd2c8701e047bc6d 0 1478089963748 9 connected
-
1cd6482fd7038d78ad556b52b0cb9e2590ad5598 192.168.37.131:7001 myself,slave 80de7003738f74134a3403fc939fed253b7774f2 0 0 1 connected
-
3adba62fdcc331ce231ca580cd2c8701e047bc6d 192.168.37.131:7002 master - 0 1478089966787 9 connected 5795-10922
-
cf23ca6d78cba3d3924065e1f7a394f6c51d4b28 192.168.37.131:7006 slave 8e1186475f87c928e8a146d3804d9a2697246ad0 0 1478089962729 6 connected
-
5c97e8eab019c40ea3df4925c7400fe7df1846bb 192.168.37.131:7008 slave 5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 0 1478089961713 10 connected
-
5d6c61ecff23bff3b0fb01a86c66d882f2d402a0 192.168.37.131:7007 master - 0 1478089965777 10 connected 0-332 5461-5794 10923-11255
-
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命令行客户端连接
-
[root@localhost redis-cluster]# ./redis1/redis-cli -p 7001 -c
-
127.0.0.1:7001> get a
-
-> Redirected to slot [15495] located at 192.168.37.131:7003
-
(nil)
-
192.168.37.131:7003>
一定要加-c
参数,节点之间就可以互相跳转
使用jedis连接
-
package com.pc.jedis.test;
-
-
import java.util.HashSet;
-
import java.util.Set;
-
-
import redis.clients.jedis.HostAndPort;
-
import redis.clients.jedis.JedisCluster;
-
-
/**
-
* Jedis集群测试
-
*
-
* @author Switch
-
* @data 2017年2月11日
-
* @version V1.0
-
*/
-
public classJedisClusterTest{
-
publicstaticvoidmain(String[] args) {
-
// 创建并填充节点信息
-
Set<HostAndPort> nodes = new HashSet<>();
-
nodes.add(new HostAndPort("192.168.37.131", 7001));
-
nodes.add(new HostAndPort("192.168.37.131", 7002));
-
nodes.add(new HostAndPort("192.168.37.131", 7003));
-
nodes.add(new HostAndPort("192.168.37.131", 7004));
-
nodes.add(new HostAndPort("192.168.37.131", 7005));
-
nodes.add(new HostAndPort("192.168.37.131", 7006));
-
-
// 创建JedisCluster对象
-
JedisCluster jedisCluster = new JedisCluster(nodes);
-
-
// 使用jedisCluster操作redis
-
String key = "jedisCluster";
-
String setResult = jedisCluster.set(key, "hello redis!");
-
System.out.println(setResult);
-
-
String getResult = jedisCluster.get(key);
-
System.out.println(getResult);
-
-
// 关闭jedisCluster(程序执行完后才能关闭,内部封装了连接池)
-
jedisCluster.close();
-
}
-
}
——参考《Redis官方文档》
相关推荐
CentOs7.3部署nginx+tomcat+redis集群说明.docx
标题中的“rancher2.3.6部署redis集群配置教程.pdf”暗示了这份文档是针对特定版本的Rancher软件(版本号2.3.6)的使用教程,旨在指导用户如何部署Redis集群。Rancher是一款开源的容器管理平台,可以用来部署、管理...
linux redis单个和redis集群部署完成 技术:redis集群 说明包含: redis安装包 Redis集群负载均衡部署帮助文档 Redis集群负载均衡使用帮助文档 Redis集群负载均衡遇到问题解决文档 redis网页资料 linux内网...
1.1 Redis 集群说明 Redis 集群是 Redis 的一种高可用性解决方案,可以实现高可用性和扩展性。Redis 集群由多个 Redis 节点组成,每个节点都是一个独立的 Redis 实例。这些节点之间可以相互通信,实现数据的复制和...
在本资源包中,提供了搭建Redis集群所需的各种工具和教程,帮助你在Windows环境下进行操作。 首先,让我们来了解Redis集群的基础知识。Redis集群通过分片(Sharding)技术将数据分散到多个节点上,实现数据的冗余和...
Redis集群搭建与验证的知识点主要包括以下几个方面: 1. Redis集群概述 Redis集群是Redis提供的分布式数据库解决方案,它可以将数据自动切分到多个Redis节点上。集群通过分区来提供一定程度的可用性,在部分节点...
#### 一、Redis集群概述与服务器说明 Redis集群是Redis的一种分布式部署方式,通过多个Redis实例组成集群,实现数据的高可用性和负载均衡。本部署文档介绍了一个由六台虚拟服务器组成的Redis集群,这些服务器运行于...
然而,描述中没有具体说明需要哪个 `jar` 文件,所以我们假设这里可能是为了运行 Redis 的集群管理工具或者连接器。 Ruby 是一种动态、面向对象的脚本语言,它提供了丰富的库和工具,包括对 Redis 的支持。`ruby-...
在Windows上部署Redis集群,我们可以借助PowerShell脚本来自动化这一过程。 首先,`redis.conf`是Redis的配置文件,它包含了Redis服务器的各项参数设定,如端口号、数据持久化策略、内存限制等。在设置Redis集群时...
如果能成功读取到之前写入的值,那么说明Redis集群已经成功搭建。 #### 四、总结 通过上述步骤,我们已经成功地部署了一个包含六个节点的Redis集群。此集群具有良好的可扩展性和高可用性,适用于需要处理大量并发...
如果您需要部署一个Redis集群,Docker可以帮助您更轻松地完成此任务。以下是关于使用Docker部署Redis集群节点的一些资源: - [Redis Docker官方镜像文档](https://hub.docker.com/_/redis):官方文档,包含了Redis ...
**Redis集群搭建手册** Redis,全称Remote Dictionary Server,是一种高性能的键值对数据库,以其丰富的数据类型、高效的内存管理以及支持持久化等特性,在分布式系统中被广泛应用于缓存、消息中间件以及数据存储等...
为了构建一个健壮的Redis集群,至少需要三个主节点(master)和三个从节点(slave),共计六个节点。在测试环境中,可以在同一台物理机上启动六个Redis实例来模拟集群环境;而在生产环境中,则推荐使用至少三台不同的...
内容概要:Redis集群教程文档和Redis集群规范文档。 适用人群:有Redis单机部署经验,对Redis较为熟悉,需要搭建集群环境的技术人员。 使用场景和目标:适用于高并发,高可用,强一致性等场景。适合做分布式环境下的...
在整个过程中,务必参照`redis集群离线部署.doc`文档进行操作,因为它包含了具体配置和命令的详细说明,确保每个步骤都按照文档要求执行,以确保离线部署的顺利进行。离线部署虽然比在线部署复杂,但通过精心的准备...
本篇文章将深入探讨 Redis 集群的安装与测试,包括其基本概念、集群模式、版本说明以及测试环境。 1. Redis 简介 Redis 提供了多种数据类型,如字符串(string)、列表(list)、集合(set)、有序集合(zset)和哈希(hash)...
Redis是一种高性能的键值对数据存储系统,常...以上就是Redis集群的总结、搭建过程以及如何在SpringBoot应用中进行整合测试的详细说明。通过实践,可以更深入地理解和掌握Redis集群的使用,提升系统的稳定性和性能。
Kubernetes 上的 Redis 集群此模块旨在简化 Kubernetes 中 Redis 集群部署的创建和操作。我不建议您在生产中运行此模块 - 它只是一个非平凡的 Stateful Set 部署的说明性示例。要求Kubernetes 1.17.0+Minikube 在...
Redis 集群环境搭建与Java操作指南 Redis 是一款高性能的键值数据库,广泛应用于缓存、消息队列等领域。为了实现高可用性和数据冗余,Redis 提供了集群(Cluster)功能。本指南将详细阐述如何搭建 Redis 集群环境,...
标题中的“jfinal redis cluster plugin”指的是一个专为JFinal框架设计的Redis集群插件,旨在帮助开发者在使用JFinal时能便捷地接入并管理Redis集群。JFinal是一款基于Java的轻量级Web开发框架,它以其简洁的API...