千家信息网

Redis集群中新增节点(master、slave)(实验)

发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,新增master节点的配置文件:# vim /usr/local/redis-3.0.6-6379/redis.confdaemonize yes //开启后台运行pidfile /var/run/r
千家信息网最后更新 2025年11月09日Redis集群中新增节点(master、slave)(实验)

新增master节点的配置文件

# vim /usr/local/redis-3.0.6-6379/redis.conf

daemonize yes //开启后台运行

pidfile /var/run/redis_6379.pid //pid文件

port 6379 //端口

bind 192.168.2.100 //默认127.0.0.1,需要改为其他节点可访问的地址

logfile "/usr/local/redis-3.0.6-6379/redis_6379.log" //log文件路径

dir /usr/local/redis-3.0.6-6379/ //RDB文件路径

appendonly yes //开启AOF持久化

cluster-enabled yes //开启集群

cluster-config-file nodes-6379.conf //集群配置文件

cluster-node-timeout 15000 //请求超时,默认15秒


启动新增master节点

# redis-server redis.conf


安装ruby环境:redis-trib.rb命令,需要在ruby环境中执行

# yum -y install ruby ruby-devel rubygems rpm-build

# gem install redis

Successfully installed redis-3.2.1

Parsing documentation for redis-3.2.1

1 gem installed


可能会遇到的报错:

ERROR: Could not find a valid gem 'redis' (>= 0), here is why:

Unable to download data from https://rubygems.org/ - no such name (https://rubygems.org/latest_specs.4.8.gz)

手动下载:https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem

执行:# gem install -l ./redis-3.2.1.gem

把master节点加入集群中:

# redis-trib.rb add-node 192.168.2.202:6379 192.168.2.100:6379

//第一个ip:port 新节点

第二个ip:port 集群中的任意节点

查看集群所有节点信息:可以看到,新增节点已加入集群,但是没有分配hash slot

# redis-trib.rb check 192.168.2.202:6379

>>> Performing Cluster Check (using node 192.168.2.202:6379)

M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379

slots: (0 slots) master

0 additional replica(s)

... ...


给新增master节点分配slot:

# redis-trib.rb reshard 192.168.2.202:6379

>>> Performing Cluster Check (using node 192.168.2.202:6379)

M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379

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

//想要移动多少个hash slot?(分配给新节点)

What is the receiving node ID? 8326ff0be199fa0d4db74f0ebcc58f27e65991b4

//接受的节点ID是什么?(新加节点的ID)

Please enter all the source node IDs.

Type 'all' to use all the nodes as source nodes for the hash slots.

//如果从所有节点移动,输入all

Type 'done' once you entered all the source nodes IDs.

//输入指定节点ID,done结尾

Source node #1:all

... ...

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

//是否满意hash slot移动计划?

查看集群节点的信息:确认新增节点hash slot分配正确

# redis-trib.rb check 192.168.2.202:6379

>>> Performing Cluster Check (using node 192.168.2.202:6379)

M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379

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

0 additional replica(s)

... ...

启动slave节点:配置和上面一样

# redis-server /usr/local/redis-3.0.6-6380/redis.conf

将slave节点加入集群:

# redis-trib.rb add-node --slave 192.168.2.202:6380 192.168.2.100:6379

//加入--slave参数,表示加入的是slave

第一个IP是新增的slave

第二个IP是节点中的任意节点


查看集群信息:(可以看到新加slave属于192.168.2.202:6379的从节点)

# redis-trib.rb check 192.168.2.100:6379

>>> Performing Cluster Check (using node 192.168.2.100:6379)

M: 8326ff0be199fa0d4db74f0ebcc58f27e65991b4 192.168.2.202:6379

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

1 additional replica(s)

S: e4dc23dc67418bf66c6c63655110612cb9516aff 192.168.2.202:6380

slots: (0 slots) slave

replicates 8326ff0be199fa0d4db74f0ebcc58f27e65991b4

... ...

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.


0