redis集群

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。redis集群,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

环境准备

redis集群最少需要6台主机,3主3从

节点(6台redis,一台管理节点,后新加2台redis) IP地址
host150 192.168.44.150
host151 192.168.44.151
host152 192.168.44.152
host153 192.168.44.153
host154 192.168.44.154
host155 192.168.44.155
mgm 192.168.44.139
host156 192.168.44.156
host157 192.168.44.157

配置集群

内存里不允许有数据 不能设置连接密码

配置redis服务

  1. 安装redis软件并做初始化配置
  2. 停止按照初始化配置启动的Redis服务
  3. 修改主配置文件(启用集群功能)
  4. 启动redis服务 并查看端口号

所有节点安装redis软件并做初始化配置

yum -y install gcc 
tar -xf redis-4.0.8.tar.gz 
cd redis-4.0.8/
make && make install
./utils/install_server.sh 遇到提示就回车

所有节点停止按照初始化配置启动的Redis服务

/etc/init.d/redis_6379 stop

配置主配置文件

host150

[root@host150 ~]# vim /etc/redis/6379.conf
[root@host150 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.150			#本机IP地址
port 6379					#服务使用端口,#端口号可以不是6379 但是要按使用的端口添加集群											
cluster-enabled yes			#开启集群功能
cluster-config-file nodes-6379.conf		#保存集群信息的配置文件
cluster-node-timeout 5000				#集群中主机的连接超时时间

host151

[root@host151 ~]# vim /etc/redis/6379.conf
[root@host151 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.151
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

host152

[root@host152 ~]# vim /etc/redis/6379.conf
[root@host152 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.152
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

host153

[root@host153 ~]# vim /etc/redis/6379.conf
[root@host153 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.153
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

host154

[root@host154 ~]# vim /etc/redis/6379.conf
[root@host154 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.154
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

host155

[root@host155 ~]# vim /etc/redis/6379.conf
[root@host155 ~]# sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.155
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000

所有节点启动redis

host150

[root@host150 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host150 ~]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.150:6379                  *:*                   users:(("redis-server",pid=4905,fd=6))
tcp    LISTEN     0      128    192.168.44.150:16379                 *:*                   users:(("redis-server",pid=4905,fd=8))

host151

[root@host151 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host151 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.151:6379                  *:*                   users:(("redis-server",pid=4837,fd=6))
tcp    LISTEN     0      128    192.168.44.151:16379                 *:*                   users:(("redis-server",pid=4837,fd=8))

host152

[root@host152 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host152 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.152:6379                  *:*                   users:(("redis-server",pid=4816,fd=6))
tcp    LISTEN     0      128    192.168.44.152:16379                 *:*                   users:(("redis-server",pid=4816,fd=8))

host153

[root@host153 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host153 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.153:6379                  *:*                   users:(("redis-server",pid=4790,fd=6))
tcp    LISTEN     0      128    192.168.44.153:16379                 *:*                   users:(("redis-server",pid=4790,fd=8))

host154

[root@host154 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host154 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.154:6379                  *:*                   users:(("redis-server",pid=4789,fd=6))
tcp    LISTEN     0      128    192.168.44.154:16379                 *:*                   users:(("redis-server",pid=4789,fd=8))

host155

[root@host155 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host155 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.155:6379                  *:*                   users:(("redis-server",pid=4781,fd=6))
tcp    LISTEN     0      128    192.168.44.155:16379                 *:*                   users:(("redis-server",pid=4781,fd=8))

配置管理节点

  1. 准备ruby脚本的运行环境
  2. 创建脚本
  3. 查看脚本帮助信息
yum -y install  rubygems  ruby 
gem install redis-3.2.1.gem
mkdir /root/bin   				#创建命令检索目录
tar -xf redis-4.0.8.tar.gz
cp redis-4.0.8/src/redis-trib.rb  /root/bin/ 	
chmod +x  /root/bin/redis-trib.rb	 	#设置执行权限
redis-trib.rb  help  					#(能看到帮助信息为成功)
[root@mgm ~]# ls
anaconda-ks.cfg  redis-3.2.1.gem  redis-4.0.8.tar.gz
bin              redis-4.0.8      ruby-devel-2.0.0.648-30.el7.x86_64.rpm

在管理节点创建集群

创建集群时,会把前3台服务器 配置为主服务器,剩下的其他主机全做从服务器
–replicas 从服务器的台数(指定每个主服务器有几台从服务器)

创建集群的命令格式

redis-trib.rb create --replicas 数字  ip地址:端口 ip地址:端口 ip地址:端口 ....

创建集群

[root@mgm ~]# redis-trib.rb create --replicas 1 192.168.44.150:6379 192.168.44.151:6379 192.168.44.152:6379 192.168.44.153:6379 192.168.44.154:6379 192.168.44.155:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...	#6台服务器
Using 3 masters:									#3主
192.168.44.150:6379
192.168.44.151:6379
192.168.44.152:6379
Adding replica 192.168.44.154:6379 to 192.168.44.150:6379	#给主添加从
Adding replica 192.168.44.155:6379 to 192.168.44.151:6379
Adding replica 192.168.44.153:6379 to 192.168.44.152:6379
M: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots:0-5460 (5461 slots) master		#主的id	ip:端口	哈希槽0-5460 共有5461
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c	#从的id	主的ip:端口	主的id	
S: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   replicates 1bda9222afa671f55f820e8eb744112b8063f970
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
Can I set the above configuration? (type 'yes' to accept): yes	#同意 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.44.150:6379)
M: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)	##主的id	ip:端口	哈希槽0-5460 共有5461	添加一个从 
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave	##从的id	主的ip:端口	从不占用哈希槽
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots: (0 slots) slave
   replicates 1bda9222afa671f55f820e8eb744112b8063f970
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.	#16348哈希槽

查看集群信息,连接任意一台服务器均可

查看集群统计信息

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.150:6379 (1bda9222...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

查看集群详细信息

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.150:6379 (1bda9222...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@mgm ~]# redis-trib.rb check 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
M: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots: (0 slots) slave
   replicates 1bda9222afa671f55f820e8eb744112b8063f970
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

在管理节点使用集群

命令格式

 redis-cli -c  -h redis服务器的ip  -p 端口号

说明:
连接集群中的任意一台服务器都可以查询数据和存储数据)
-c 连接集群中的主机 使用集群算法存储数据
向集群中的主机存储数据一次只能存储一个变量集群算法每次只有一个计算结果

在host150服务器演示

host150存储数据

[root@host150 ~]# redis-cli -c -h 192.168.44.150
192.168.44.150:6379> ping
PONG
192.168.44.150:6379> set a 1
-> Redirected to slot [15495] located at 192.168.44.152:6379
OK		#存储在哈希槽15495	哈希槽15495在host152	host153是host152的从
192.168.44.152:6379> keys *		#查询数据直接跳转到host152
1) "a"

host152查看数据

[root@host152 redis-4.0.8]# redis-cli -c -h 192.168.44.152
192.168.44.152:6379> keys *
1) "a"

host153查看数据

[root@host153 redis-4.0.8]# redis-cli -c -h 192.168.44.153
192.168.44.153:6379> keys *
1) "a"

集群存储数据的原理

Redis集群是如何实现数据的分布式存储的?
存储数据和查询数据时 调用集群算法 集群算法会计算出1个数字, 数字
在哪个主服务哈希槽占用的范围内 ,就连接对应的主服务器存储数据或查询数据。

管理集群(在管理节点操作)

向集群里添加新服务器

配置新服务器

配置host156,host157为redis服务器

yum -y install make gcc
tar xf redis-4.0.8.tar.gz
cd redis-4.0.8/
make && make install
./utils/install_server.sh

修改配置文件,开启集群功能

host156
vim /etc/redis/6379.conf
sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.156
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
host157
vim /etc/redis/6379.conf
sed -n '70p;93p;815p;823p;829p' /etc/redis/6379.conf
bind 192.168.44.157
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
host156,host157重启redis服务
 /etc/init.d/redis_6379 restart

添加master角色的服务器(添加时不指定角色,默认为master)

语法格式

 redis-trib.rb add-node 新主机Ip:端口 集群中已有主机的ip:端口

添加服务器

[root@mgm ~]#   redis-trib.rb add-node 192.168.44.156:6379 192.168.44.150:6379
>>> Adding node 192.168.44.156:6379 to cluster 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.44.156:6379 to make it join the cluster.
[OK] New node added correctly.

查看集群信息

新添加的master角色没有slave,也没有hash slots

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 0 slots | 0 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

给新添加的master分配hast slots

master角色的服务器没有hast slots得不到存储数据机会
命令格式

redis-trib.rb  reshard 集群中已有主机的ip:端口
[root@mgm ~]# redis-trib.rb  reshard 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots: (0 slots) master
   0 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
#移除多少个hast slots
How many slots do you want to move (from 1 to 16384)? 4096
#把4096个槽给哪台主数据库服务器 (host156主机的id) 
What is the receiving node ID? 98ece26b4c119f70ace66cc6dd72266944f18c8b
#提供4096个槽 主机的id (all 表示当前所有主服务器一起提供4096个槽给host156)
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
#确认前3步的配置 yes 同意 no 退出
Do you want to proceed with the proposed reshard plan (yes/no)? #yes 
Ready to move 4096 slots.
  Source nodes:
    M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
    M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
    M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
  Destination node:
    M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots: (0 slots) master
   0 additional replica(s)
 Resharding plan:
    Moving slot 5461 from 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
    Moving slot 5462 from 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
    Moving slot 5463 from 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
    Moving slot 5464 from 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
...
...
...
	Moving slot 12284 from 192.168.44.152:6379 to 192.168.44.156:6379:
	Moving slot 12285 from 192.168.44.152:6379 to 192.168.44.156:6379:
	Moving slot 12286 from 192.168.44.152:6379 to 192.168.44.156:6379:
	Moving slot 12287 from 192.168.44.152:6379 to 192.168.44.156:6379:

查看集群信息

新的master已被分配hast slots

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 4096 slots | 0 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

具体查看host156 占用4096个槽的范围

[root@mgm ~]# redis-trib.rb check 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379	#host156
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:12288-16383 (4096 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.

添加slave角色的服务器

slave服务器的数据是从master服务器同步来的,因此slave角色的服务器不需要分配hash slots
只要把主机添加到集群了做slave服务器就可以,会自动做从服务器个数最少的 master服务器的从服务器。
命令格式

redis-trib.rb add-node  --slave 新主机Ip:端口 集群中已有主机的ip:端口
[root@mgm ~]# redis-trib.rb add-node  --slave 192.168.44.157:6379 192.168.44.150:6379
>>> Adding node 192.168.44.157:6379 to cluster 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:12288-16383 (4096 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.
Automatically selected master 192.168.44.156:6379
>>> Send CLUSTER MEET to node 192.168.44.157:6379 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.44.156:6379.
[OK] New node added correctly.

查看集群状态

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 4096 slots | 1 slaves.	#host156
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

集群详细信息

[root@mgm ~]# redis-trib.rb check 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 5a97a0acaaa62a3a6cb43199c16a337ee6517dbd 192.168.44.157:6379
   slots: (0 slots) slave
   replicates 98ece26b4c119f70ace66cc6dd72266944f18c8b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

在集群中移除服务器

主机被移除集群后redis服务会自动停止

移除slave角色的主机

slave角色的主机的没有hash slots 直接移除即可
命令格式

redis-trib.rb del-node 集群中任意主机的Ip:端口 被移除主机的id
[root@mgm ~]# redis-trib.rb del-node 192.168.44.150:6379 5a97a0acaaa62a3a6cb43199c16a337ee6517dbd	#移除host157
>>> Removing node 5a97a0acaaa62a3a6cb43199c16a337ee6517dbd from cluster 192.168.44.150:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.	##把服务给停止了

查看集群信息

host156已没有slave

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 4096 slots | 0 slaves.	#0台从服务器
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

host157的redis服务已停止

[root@host157 redis-4.0.8]# netstat -utnlp | grep redis-server

移除master角色的主机

master角色的服务器会占用hash slots 要先释放hash slots 再执行移除主机的命令
释放 hash slots (再次执行reshard命令)

[root@mgm ~]# redis-trib.rb reshard 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:12288-16383 (4096 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.
#释放hash slots 的个数
How many slots do you want to move (from 1 to 16384)? 4096
#接收4096的个hash slots的主数据库服务器的ID  
What is the receiving node ID? 1cab1f6fa4608576d498aadd42c8bf0e87eca583	#host154
#4096个hash slots从哪个主机移除
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:98ece26b4c119f70ace66cc6dd72266944f18c8b		#host156
Source node #2:done											#done

Ready to move 4096 slots.
  Source nodes:
    M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
  Destination node:
    M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
   #确认配置 yes同意 no 退出		
Do you want to proceed with the proposed reshard plan (yes/no)? yes #同意
  Resharding plan:
    Moving slot 0 from 98ece26b4c119f70ace66cc6dd72266944f18c8b
    Moving slot 1 from 98ece26b4c119f70ace66cc6dd72266944f18c8b
    Moving slot 2 from 98ece26b4c119f70ace66cc6dd72266944f18c8b
    Moving slot 3 from 98ece26b4c119f70ace66cc6dd72266944f18c8b
...
...
...
Moving slot 12284 from 192.168.44.156:6379 to 192.168.44.154:6379:
Moving slot 12285 from 192.168.44.156:6379 to 192.168.44.154:6379:
Moving slot 12286 from 192.168.44.156:6379 to 192.168.44.154:6379:
Moving slot 12287 from 192.168.44.156:6379 to 192.168.44.154:6379:

查看集群状态

host156已没有hash slots,host151多了hash slots

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 0 slots | 0 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 8192 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

移除主机 (执行删除主机的命令)

查询host156的id

[root@mgm ~]# redis-trib.rb check 192.168.44.150:6379 | grep 192.168.44.156
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379

删除host156

[root@mgm ~]# redis-trib.rb del-node 192.168.44.150:6379 98ece26b4c119f70ace66cc6dd72266944f18c8b
>>> Removing node 98ece26b4c119f70ace66cc6dd72266944f18c8b from cluster 192.168.44.150:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看集群信息

集群内已没有host156

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 8192 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.

在集群中平均哈希槽

平均分配要求每个主机都已经有槽

此时集群hash slots不平均

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 8192 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.

平均分配hash slots

[root@mgm ~]# redis-trib.rb rebalance 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3
Moving 1366 slots from 192.168.44.154:6379 to 192.168.44.151:6379
######################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1365 slots from 192.168.44.154:6379 to 192.168.44.152:6379
#####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

查看集群信息

hash slots已平均

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.

把移除的主机再次添加到集群做master

启动Redis服务并清除前世记忆(在Redis服务器本机操作)

[root@host156 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host156 redis-4.0.8]# netstat -utnlp | grep redis-server
tcp        0      0 192.168.44.156:6379     0.0.0.0:*               LISTEN      17608/redis-server
tcp        0      0 192.168.44.156:16379    0.0.0.0:*               LISTEN      17608/redis-server

清空内存里的数据(在Redis服务器本机操作)

[root@host156 redis-4.0.8]# redis-cli -h 192.168.44.156 -p 6379
192.168.44.156:6379> ping
PONG
192.168.44.156:6379> cluster nodes	#本机查看集群内成员的信息 或者查看集群配置文件
1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379@16379 master - 0 1661261939000 9 connected 2731-6826 10923-12287
6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379@16379 master - 0 1661261939626 10 connected 0-1365 6827-10922
9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379@16379 slave 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 0 1661261940643 10 connected
98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379@16379 myself,master - 0 1661261822637 8 connected
fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379@16379 master - 0 1661261939115 11 connected 1366-2730 12288-16383
c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379@16379 slave fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 0 1661261940000 11 connected
1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379@16379 slave 1cab1f6fa4608576d498aadd42c8bf0e87eca583 0 1661261939523 9 connected
192.168.44.156:6379> cluster info	#查看本机的集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:11
cluster_my_epoch:8
cluster_stats_messages_ping_sent:283
cluster_stats_messages_sent:283
cluster_stats_messages_pong_received:283
cluster_stats_messages_received:283
192.168.44.156:6379> cluster reset	#清除前世记忆 根据情况是否保留数据 有数据但不保留再执行flushall
OK
192.168.44.156:6379> keys *
(empty list or set)
192.168.44.156:6379> exit

在管理主机执行执行添加命令(在管理主机执行)

[root@mgm ~]# redis-trib.rb add-node 192.168.44.156:6379 192.168.44.150:6379
>>> Adding node 192.168.44.156:6379 to cluster 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:0-1365,6827-10922 (5462 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:2731-6826,10923-12287 (5461 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:1366-2730,12288-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.44.156:6379 to make it join the cluster.
[OK] New node added correctly.

查看是否添加成功(在管理主机执行)

集群增加master服务器host156

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 0 slots | 0 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

把移除的主机再次添加到集群做slave

启动Redis服务并清除前世记忆(在Redis服务器本机操作)

[root@host157 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host157 redis-4.0.8]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.157:6379                  *:*                   users:(("redis-server",pid=17611,fd=6))
tcp    LISTEN     0      128    192.168.44.157:16379                 *:*                   users:(("redis-server",pid=17611,fd=8))

清空内存里的数据(在Redis服务器本机操作)

[root@host157 redis-4.0.8]# redis-cli -h 192.168.44.157 -p 6379
192.168.44.157:6379> ping
PONG
192.168.44.157:6379> cluster nodes	#本机查看集群内成员的信息 或者查看集群配置文件
1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379@16379 slave 1cab1f6fa4608576d498aadd42c8bf0e87eca583 0 1661262492566 9 connected
c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379@16379 slave fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 0 1661262492566 11 connected
98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379@16379 master - 0 1661262492000 8 connected
1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379@16379 master - 0 1661262493889 9 connected 2731-6826 10923-12287
5a97a0acaaa62a3a6cb43199c16a337ee6517dbd 192.168.44.157:6379@16379 myself,slave 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 0 1661262418021 0 connected
6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379@16379 master - 0 1661262492000 10 connected 0-1365 6827-10922
fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379@16379 master - 0 1661262493582 11 connected 1366-2730 12288-16383
9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379@16379 slave 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 0 1661262492868 10 connected
192.168.44.157:6379> cluster info	#查看本机的集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:8
cluster_size:3
cluster_current_epoch:11
cluster_my_epoch:10
cluster_stats_messages_ping_sent:204
cluster_stats_messages_sent:204
cluster_stats_messages_pong_received:204
cluster_stats_messages_received:204
192.168.44.157:6379> cluster reset	#清除前世记忆 根据情况是否保留数据 有数据但不保留再执行flushall
OK
192.168.44.157:6379> keys *
(empty list or set)
192.168.44.157:6379> exit

在管理主机执行执行添加命令(在管理主机执行)

把移除的host157 再次添加到集群 指定做host156主机的slave数据库服务器
命令格式

redis-trib.rb add-node --slave --master-id 主服务器的id  新服务器的ip:端口 集群中已有机器的Ip:端口 
[root@mgm ~]# redis-trib.rb add-node --slave --master-id 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.157:6379 192.168.44.150:6379
>>> Adding node 192.168.44.157:6379 to cluster 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots: (0 slots) master
   0 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:0-1365,6827-10922 (5462 slots) master
   1 additional replica(s)
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:2731-6826,10923-12287 (5461 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:1366-2730,12288-16383 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.44.157:6379 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.44.156:6379.
[OK] New node added correctly.

查看是否添加成功(在管理主机执行)

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 0 slots | 1 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 5462 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

给host156分配hash slots

先给host156分100槽,再在集群平均分槽(平均分槽必须所有主机都已有槽)

给host156分100槽

[root@mgm ~]# redis-trib.rb reshard 192.168.44.156:6379
>>> Performing Cluster Check (using node 192.168.44.156:6379)
M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots: (0 slots) master
   1 additional replica(s)
S: 5a97a0acaaa62a3a6cb43199c16a337ee6517dbd 192.168.44.157:6379
   slots: (0 slots) slave
   replicates 98ece26b4c119f70ace66cc6dd72266944f18c8b
M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:2731-6826,10923-12287 (5461 slots) master
   1 additional replica(s)
M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:0-1365,6827-10922 (5462 slots) master
   1 additional replica(s)
S: 9c0597d3bedea74557059239dc1455d76caed47e 192.168.44.155:6379
   slots: (0 slots) slave
   replicates 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9
M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:1366-2730,12288-16383 (5461 slots) master
   1 additional replica(s)
S: c15fe1a4f9962c26f707369c8826744cb6cf6865 192.168.44.153:6379
   slots: (0 slots) slave
   replicates fbe02f388f8d841e7350c5c3a278062e0ae6fd0c
S: 1bda9222afa671f55f820e8eb744112b8063f970 192.168.44.150:6379
   slots: (0 slots) slave
   replicates 1cab1f6fa4608576d498aadd42c8bf0e87eca583
[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)? 100
What is the receiving node ID? 98ece26b4c119f70ace66cc6dd72266944f18c8b
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

Ready to move 100 slots.
  Source nodes:
    M: 1cab1f6fa4608576d498aadd42c8bf0e87eca583 192.168.44.154:6379
   slots:2731-6826,10923-12287 (5461 slots) master
   1 additional replica(s)
    M: 6e5fca15c10b303d4e1b2dc47abd5594ee4609a9 192.168.44.151:6379
   slots:0-1365,6827-10922 (5462 slots) master
   1 additional replica(s)
    M: fbe02f388f8d841e7350c5c3a278062e0ae6fd0c 192.168.44.152:6379
   slots:1366-2730,12288-16383 (5461 slots) master
   1 additional replica(s)
  Destination node:
    M: 98ece26b4c119f70ace66cc6dd72266944f18c8b 192.168.44.156:6379
   slots: (0 slots) master
   1 additional replica(s)
...
...
...
Moving slot 1396 from 192.168.44.152:6379 to 192.168.44.156:6379:
Moving slot 1397 from 192.168.44.152:6379 to 192.168.44.156:6379:
Moving slot 1398 from 192.168.44.152:6379 to 192.168.44.156:6379:

集群平均分槽

[root@mgm ~]# redis-trib.rb rebalance 192.168.44.150:6379
>>> Performing Cluster Check (using node 192.168.44.150:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4
Moving 1332 slots from 192.168.44.152:6379 to 192.168.44.156:6379
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1332 slots from 192.168.44.151:6379 to 192.168.44.156:6379
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################
Moving 1332 slots from 192.168.44.154:6379 to 192.168.44.156:6379
####################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################

查看集群信息

[root@mgm ~]# redis-trib.rb info 192.168.44.150:6379
192.168.44.156:6379 (98ece26b...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

把集群中的主机恢复为独立的数据库服务器

	在数据库服务器本机执行如下操作
	停止redis服务
	注释掉配置文件中的集群功能
	清空数据库目录
	启动服务
	连接服务查看不到集群信息 也没有数据

host150做操作

在数据库服务器本机执行如下操作

停止redis服务

[root@host150 ~]# redis-cli -h 192.168.44.150 shutdown
[root@host150 ~]# ss -utnlp | grep redis

注释掉配置文件中的集群功能

[root@host150 ~]# vim /etc/redis/6379.conf
[root@host150 ~]# sed -n '815p;823p;829p' /etc/redis/6379.conf
#cluster-enabled yes
#cluster-config-file nodes-6379.conf
#cluster-node-timeout 5000

清空数据库目录

[root@host150 ~]# ls /var/lib/redis/6379/
dump.rdb  nodes-6379.conf
[root@host150 ~]# rm -rf /var/lib/redis/6379/*
[root@host150 ~]# ls /var/lib/redis/6379/

启动服务

[root@host150 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host150 ~]# ss -utnlp | grep redis
tcp    LISTEN     0      128    192.168.44.150:6379                  *:*                   users:(("redis-server",pid=18107,fd=6))

连接服务查看不到集群信息 也没有数据

[root@host150 ~]# redis-cli -h 192.168.44.150
192.168.44.150:6379> ping
PONG
192.168.44.150:6379> cluster info
ERR This instance has cluster support disabled	#没有启用集群功能
192.168.44.150:6379>

在管理主机查看集群信息看不到host150主机

[root@mgm ~]# redis-trib.rb info 192.168.44.151:6379
192.168.44.151:6379 (6e5fca15...) -> 0 keys | 4096 slots | 1 slaves.
192.168.44.154:6379 (1cab1f6f...) -> 0 keys | 4096 slots | 0 slaves.
192.168.44.152:6379 (fbe02f38...) -> 1 keys | 4096 slots | 1 slaves.
192.168.44.156:6379 (98ece26b...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154055.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!