Linux Redis集群安装手册

提示:搭建redis集群最少6个redis服务器

第一节工具原料

网址:
rubby
redis
edis-3.3.5.gem
版本:
ruby-2.6.5
redis-4.0.14
redis-3.3.5.gem
系统环境:

主机名 操作系统 ip地址 安装软件 redis端口 备注
nsql140 CentOS 7.4 192.168.122.140 ruby-2.6.5 redis-4.0.14 redis-3.3.5.gem 6379 6380 redis集群管理节点
nsql141 CentOS 7.4 192.168.122.141 redis-4.0.14 6379 6380
nsql142 CentOS 7.4 192.168.122.142 redis-4.0.14 6379 6380

实验前提(所有主机都要操作)

[root@nosql140 ~]# systemctl stop firewalld
[root@nosql140 ~]# systemctl disable firewalld
[root@nosql140 ~]# yum -y install iptables-services
[root@nosql140 ~]# service iptables -F
[root@nosql140 ~]# service iptables save
[root@nosql140 ~]# setenforce 0
[root@nosql140 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

redis安装与初始化

1.安装

[root@nosql140 iso]# yum -y install gcc-c++ make
[root@nosql140 iso]# tar -xvf redis-4.0.14.tar.gz 
[root@nosql140 iso]# cd redis-4.0.14
[root@nosql140 redis-4.0.14]# make 
[root@nosql140 redis-4.0.14]# mmake install

2.初始化
一台主机安装2个redis

#执行脚本命令,一路默认
[root@nosql140 redis-4.0.14]# ./utils/install_server.sh
....
Starting Redis server...
Installation successful!
[root@nosql140 ~]# vim /etc/redis/6379.conf
....
bind 192.168.122.140
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
....
[root@nosql140 ~]# /etc/init.d/redis_6379 restart
[root@nosql140 ~]# ss -tunlp | grep 6379
tcp    LISTEN     0      128    192.168.122.140:6379                  *:*                   users:(("redis-server",pid=819,fd=6))
tcp    LISTEN     0      128    192.168.122.140:16379                 *:*                   users:(("redis-server",pid=819,fd=8))
#写入6380就一路默认
[root@nosql140 redis-4.0.14]# ./utils/install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 6380
Please select the redis config file name [/etc/redis/6380.conf]
....
[root@nosql140 ~]# vim /etc/redis/6380.conf
....
bind 192.168.122.140
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
....
[root@nosql140 ~]# /etc/init.d/redis_6380 restart
[root@nosql140 ~]# ss -tunlp | grep 6380
tcp    LISTEN     0      128    192.168.122.140:6380                  *:*                   users:(("redis-server",pid=822,fd=6))
tcp    LISTEN     0      128    192.168.122.140:16380                 *:*                   users:(("redis-server",pid=822,fd=8))

3.管理命令

#启动 | 停止 | 查看状态 | 重启
/etc/init.d/redis_6379 start | stop | status | restart

第三节安装rubby

[root@nosql140 iso]# yum -y install openssl-devel
[root@nosql140 iso]# mkdir -p /usr/local/ruby2.6
[root@nosql140 iso]# tar -xvf ruby-2.6.5.tar.gz 
[root@nosql140 iso]# cd ruby-2.6.5
[root@nosql140 ruby-2.6.5]# ./configure -prefix=/usr/local/ruby2.6
[root@nosql140 ruby-2.6.5]# make && make install
[root@nosql140 ~]# vim /etc/profile
....
export RUBBY_HOME=/usr/local/ruby2.6/
export PATH=$RUBBY_HOME/bin:$PATH
[root@nosql140 ~]# source /etc/profile
[root@nosql140 ~]# ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
[root@nosql140 ~]# gem -v
3.0.3

第四节使用gem安装redis的接口

[root@nosql140 iso]# gem install ./redis-3.3.5.gem
Successfully installed redis-3.3.5
Parsing documentation for redis-3.3.5
Installing ri documentation for redis-3.3.5
Done installing documentation for redis after 0 seconds
1 gem installed

第五节redis-trib.rb创建集群

[root@nosql140 iso]# cd  redis-4.0.14
[root@nosql140 redis-4.0.14]# cp src/redis-trib.rb /usr/local/bin/
[root@nosql140 redis-4.0.14]# redis-trib.rb create --replicas 1 192.168.122.140:6379 192.168.122.141:6379 192.168.122.142:6379 192.168.122.140:6380 192.168.122.141:6380 192.168.122.142:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.122.140:6379
192.168.122.141:6379
192.168.122.142:6379
Adding replica 192.168.122.141:6380 to 192.168.122.140:6379
Adding replica 192.168.122.142:6380 to 192.168.122.141:6379
Adding replica 192.168.122.140:6380 to 192.168.122.142:6379
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:0-5460 (5461 slots) master
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:5461-10922 (5462 slots) master
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:10923-16383 (5461 slots) master
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
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.122.140:6379)
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

简单解释一下这个命令:调用 ruby 命令来进行创建集群:

--replicas 1 表示主从复制比例为 1:1,即一个主节点对应一个从节点;然后,默认给我们分配好了每个主节点和对应从节点服务,以及 solt 的大小,因为在 Redis 集群中有且仅有 16383 个 solt ,默认情况会给我们平均分配,当然你可以指定,后续的增减节点也可以重新分配。

第六节集群验证

随便登陆一台验证,这里以140的6379为例

#加参数 -c 可连接到集群,因为 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略,-p 参数为端口号 
[root@nosql140 ~]# redis-cli -c -h 192.168.122.140 -p 6379
#查看集群状态
192.168.122.140: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:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:66
cluster_stats_messages_pong_sent:67
cluster_stats_messages_sent:133
cluster_stats_messages_ping_received:62
cluster_stats_messages_pong_received:66
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:133
#查看集群节点信息
192.168.122.140:6379> cluster nodes
829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380@16380 slave 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 0 1570704238536 4 connected
d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380@16380 slave 457ad182d1f12a65cf0c0903ed129542d74d1034 0 1570704239000 5 connected
2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379@16379 master - 0 1570704237000 3 connected 10923-16383
457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379@16379 myself,master - 0 1570704237000 1 connected 0-5460
1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379@16379 master - 0 1570704237534 2 connected 5461-10922
ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380@16380 slave 1503ad1b97e6891b612146c48a1ad16dc626331e 0 1570704239538 6 connected

第七节管理集群

语法格式:

redis-trib.rb  选项  参数

选项:

  • add-node 添加master主机
  • check 检测集群
  • reshard 重新分片
  • add-node --slave 添加slave主机
  • del-node 删除主机

添加新节点

1.添加master节点
安装新redis节点(在140主机上)

[root@nosql140 ~]# cd /iso/redis-4.0.14/
[root@nosql140 redis-4.0.14]# ./utils/install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 6378
...
[root@nosql140 redis-4.0.14]# vim /etc/redis/6378.conf
bind 192.168.122.140
cluster-enabled yes
cluster-config-file nodes-6378.conf
cluster-node-timeout 15000
[root@nosql140 redis-4.0.14]# /etc/init.d/redis_6378 restart
[root@nosql140 redis-4.0.14]# ss -tunpl | grep 6378
tcp    LISTEN     0      128    192.168.122.140:16378                 *:*                   users:(("redis-server",pid=1193,fd=8))
tcp    LISTEN     0      128    192.168.122.140:6378                  *:*                   users:(("redis-server",pid=1193,fd=6))

2.添加节点到集群中

[root@nosql140 ~]# redis-trib.rb add-node 192.168.122.140:6378 192.168.122.140:6379
>>> Adding node 192.168.122.140:6378 to cluster 192.168.122.140:6379
....
>>> Send CLUSTER MEET to node 192.168.122.140:6378 to make it join the cluster.
[OK] New node added correctly.

3.查看状态

[root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6378
>>> Performing Cluster Check (using node 192.168.122.140:6378)
M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
   slots: (0 slots) master    #6378发现没有hash槽
   0 additional replica(s)
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

4手动对集群进行分片迁移
提示:总共16384,总共有4个master,即每个master=16384/4=4096

[root@nosql140 ~]# redis-trib.rb reshard 192.168.122.140:6378
>>> Performing Cluster Check (using node 192.168.122.140:6378)
M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
   slots: (0 slots) master
   0 additional replica(s)
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
[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)? 4096
What is the receiving node ID? cd989d61fad9ff2f0596b051f6a566fd2d541e3c
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
....
Do you want to proceed with the proposed reshard plan (yes/no)? yes
#查看状态,全均分hash槽
[root@nosql140 iso]# redis-trib.rb check 192.168.122.140:6378
>>> Performing Cluster Check (using node 192.168.122.140:6378)
M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到此添加redis的master节点成功。

添加slave节点
1.安装新redis6378(在141主机上)

[root@nosql141 ~]# /iso/redis-4.0.14/utils/install_server.sh 
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 6378
....
Installation successful!

bind 192.168.122.141
cluster-enabled yes
cluster-config-file nodes-6378.conf
cluster-node-timeout 15000
[root@nosql141 ~]# /etc/init.d/redis_6378 restart
[root@nosql141 ~]# ss -tunlp | grep 6378
tcp    LISTEN     0      128    192.168.122.141:16378                 *:*                   users:(("redis-server",pid=1238,fd=8))
tcp    LISTEN     0      128    192.168.122.141:6378                  *:*                   users:(("redis-server",pid=1238,fd=6))

2.添加从节点到集群中(随机添加)

[root@nosql140 iso]# redis-trib.rb add-node --slave 192.168.122.141:6378 192.168.122.140:6379
....
>>> Configure node as replica of 192.168.122.140:6378.
[OK] New node added correctly
[root@nosql140 iso]# redis-cli -c -h 192.168.122.140 -p 6379
192.168.122.140:6379> cluster nodes
ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380@16380 slave 1503ad1b97e6891b612146c48a1ad16dc626331e 0 1570760432983 8 connected
829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380@16380 slave 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 0 1570760435000 4 connected
716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378@16378 slave cd989d61fad9ff2f0596b051f6a566fd2d541e3c 0 1570760437000 9 connected
457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379@16379 myself,master - 0 1570760436000 1 connected 1365-5460
1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379@16379 master - 0 1570760436992 8 connected 6827-10922
2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379@16379 master - 0 1570760437000 3 connected 12288-16383
cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378@16378 master - 0 1570760437995 9 connected 0-1364 5461-6826 10923-12287
d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380@16380 slave 457ad182d1f12a65cf0c0903ed129542d74d1034 0 1570760438998 5 connected

删除节点

1.删除master节点
提示:删除master节点,先删除主机占用的hash槽
简单计算下
4096/3=1365
4096=1366+1365+1365
140:6379hash槽=4096+1366=5462
141:6379hash槽=4096+1365=5461
141:6379hash槽=4096+1365=5461
16384=5462+5461+5461
删除主机占用的hash槽

#确定master节点
[root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
>>> Performing Cluster Check (using node 192.168.122.140:6379)
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
   slots: (0 slots) slave
   replicates cd989d61fad9ff2f0596b051f6a566fd2d541e3c
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@nosql140 ~]# redis-trib.rb reshard 192.168.122.140:6379
How many slots do you want to move (from 1 to 16384)? 1366
What is the receiving node ID? 457ad182d1f12a65cf0c0903ed129542d74d1034
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:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
[root@nosql140 ~]# redis-trib.rb reshard 192.168.122.141:6379
....
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? 1503ad1b97e6891b612146c48a1ad16dc626331e
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:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
[root@nosql140 ~]# redis-trib.rb reshard 192.168.122.142:6379
....
How many slots do you want to move (from 1 to 16384)? 1365
What is the receiving node ID? 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
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:cd989d61fad9ff2f0596b051f6a566fd2d541e3c
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes

2.查看状态

[root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
>>> Performing Cluster Check (using node 192.168.122.140:6379)
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:2730-6826,10923-12287 (5462 slots) master
   2 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:0-1364,6827-10922 (5461 slots) master
   1 additional replica(s)
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:1365-2729,12288-16383 (5461 slots) master
   1 additional replica(s)
M: cd989d61fad9ff2f0596b051f6a566fd2d541e3c 192.168.122.140:6378
   slots: (0 slots) master    #无占用hash槽
   0 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
[OK] All nodes agree about slots configuration.

3.删除节点(删除之后redis服务自动关闭)

[root@nosql140 ~]# redis-trib.rb del-node 192.168.122.140:6378 cd989d61fad9ff2f0596b051f6a566fd2d541e3c
>>> Removing node cd989d61fad9ff2f0596b051f6a566fd2d541e3c from cluster 192.168.122.140:6378
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
>>> Performing Cluster Check (using node 192.168.122.140:6379)
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:2730-6826,10923-12287 (5462 slots) master
   2 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
S: 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 192.168.122.141:6378
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:0-1364,6827-10922 (5461 slots) master
   1 additional replica(s)
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:1365-2729,12288-16383 (5461 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看状态3台master,到此删除master节点就成功了。

删除slave节点
提示:移除从节点,从节点主机没有槽位范围,直接移除即可

[root@nosql140 ~]# redis-trib.rb del-node 192.168.122.141:6378 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78
>>> Removing node 716ff9d5388d59b8e8e57f1f4b79642a2c8a2f78 from cluster 192.168.122.141:6378
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@nosql140 ~]# redis-trib.rb check 192.168.122.140:6379
>>> Performing Cluster Check (using node 192.168.122.140:6379)
M: 457ad182d1f12a65cf0c0903ed129542d74d1034 192.168.122.140:6379
   slots:2730-6826,10923-12287 (5462 slots) master
   1 additional replica(s)
S: ecf4bcfb6175f7e75dfe3693f3107b585bd86c4e 192.168.122.142:6380
   slots: (0 slots) slave
   replicates 1503ad1b97e6891b612146c48a1ad16dc626331e
S: 829204c1049823bcaf3a55c6892bae18931cf918 192.168.122.140:6380
   slots: (0 slots) slave
   replicates 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b
M: 1503ad1b97e6891b612146c48a1ad16dc626331e 192.168.122.141:6379
   slots:0-1364,6827-10922 (5461 slots) master
   1 additional replica(s)
M: 2536392d9bdbe7b01a5a409d7fb680cb23e2cc1b 192.168.122.142:6379
   slots:1365-2729,12288-16383 (5461 slots) master
   1 additional replica(s)
S: d306596bfd3f8263f37230abcb265aa65e6867ae 192.168.122.141:6380
   slots: (0 slots) slave
   replicates 457ad182d1f12a65cf0c0903ed129542d74d1034
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看状态,3台slave无发现140:6378redis,删除slave节点成功。

常见错误

错误1

现象:

[root@nosql140 iso]# redis-trib.rb reshard 192.168.122.141:6379
>>> Check for open slots...
[WARNING] Node 192.168.122.141:6379 has slots in migrating state (5798).
[WARNING] Node 192.168.122.140:6378 has slots in importing state (5798).
[WARNING] The following slots are open: 5798
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** Please fix your cluster problems before resharding

原因:

    错误背景描述
    redis版本:4.0.14
    ruby gem reids 版本:4.0.0

    ruby gem安装的redis库,版本不能使用最新的4.0,否则redis-trib.rb reshard 192.168.122.140:6378 重新分片时会报错误

解决办法:
a. 卸载最新redis库,gem uninstall redis
b. 安装3.x版本,gem install redis -v 3.3.5 测试3.2.1到3.3.5都可以,4.x以上的分片报错
c. 登入两个提示错误的节点,执行清除命令即可

[root@nosql140 iso]# redis-cli -h 192.168.122.141 -p 6379
192.168.122.141:6379> cluster setslot 5798 stable
OK
[root@nosql140 iso]# redis-cli -h 192.168.122.140 -p 6378
192.168.122.140:6378> cluster setslot 5798 stable
OK

错误2:安装错误

现象:
zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
解决办法:办法解决

参考文章

结束语:
更多精彩内容持续更新中,关注微信公众号,有你更精彩。

微信公众号.jpg

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容