Redis集群部署及命令
一、简介
redis集群是一个无中心的分布式Redis存储架构,可以在多个节点之间进行数据共享,解决了Redis高可用、可扩展等问题。
redis集群提供了以下两个好处:
- 将数据自动切分(split)到多个节点
- 当集群中的某一个节点故障时,redis还可以继续处理客户端的请求。
集群中的主从复制
集群中的每个节点都有1个至N个复制品,其中一个为主节点,其余的为从节点,如果主节点下线了,集群就会把这个主节点的一个从节点设置为新的主节点,继续工作。这样集群就不会因为一个主节点的下线而无法正常工作。
二、搭建集群
1.准备
安装Redis集群前,需要先安装Redis。。过程见:https://www.cnblogs.com/expiator/p/9915774.html
注意,Redis3.0以上版本才可以搭建集群。
2.修改redis.conf配置
[root@localhost ~]# vim /usr/local/redis/etc/redis.conf
修改如下,cluster是指集群,并过配置支持集群,并指定配置文件,如下:
daemonize yes
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
3.准备开启多个节点
要让集群正常运作至少需要三个主节点,实际生产环境中需要每个节点一台主机。
由于电脑内存有限,这里采用伪集群。就简单在一台主机上创建6个redis节点来演示集群配置。
我们要创建的6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下:
192.168.213.11:7000
192.168.213.11:7001
192.168.213.11:7002
192.168.213.11:7003
192.168.213.11:7004
192.168.213.11:7005
首先我们创建6个以端口为名称的文件夹(由于每个redis节点启动的时候,都会在当前文件夹下创建快照文件,所以我们需要创建每个节点的启动目录)
4.创建各节点文件夹:
[root@localhost ~]# cd /usr/local/redis-3.0.0/
[root@localhost redis-3.0.0]#
[root@localhost redis-3.0.0]# mkdir 7000
[root@localhost redis-3.0.0]# mkdir 7001
[root@localhost redis-3.0.0]# mkdir 7002
[root@localhost redis-3.0.0]# mkdir 7003
[root@localhost redis-3.0.0]# mkdir 7004
[root@localhost redis-3.0.0]# mkdir 7005
5.复制配置文件到各个节点:
[root@localhost redis-3.0.0]#
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7000
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7001
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7002
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7003
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7004
[root@localhost redis-3.0.0]# cp /usr/local/redis/etc/redis.conf 7005
6.修改各个节点的redis.conf
daemonize yes
port 6379 #只有端口不同,其他相同
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
具体操作如下:
[root@localhost redis-3.0.0]#
[root@localhost redis-3.0.0]# vim 7000/redis.conf
[root@localhost redis-3.0.0]# vim 7001/redis.conf
[root@localhost redis-3.0.0]# vim 7002/redis.conf
[root@localhost redis-3.0.0]# vim 7003/redis.conf
[root@localhost redis-3.0.0]# vim 7004/redis.conf
[root@localhost redis-3.0.0]# vim 7005/redis.conf
[root@localhost redis-3.0.0]#
7.启动各个Redis节点:
[root@localhost redis-3.0.0]# cd 7000
[root@localhost 7000]# redis-server redis.conf
[root@localhost 7000]# cd ../7001
[root@localhost 7001]# redis-server redis.conf
[root@localhost 7001]# cd ../7002
[root@localhost 7002]# redis-server redis.conf
[root@localhost 7002]# cd ../7003
[root@localhost 7003]# redis-server redis.conf
[root@localhost 7003]# cd ../7004
[root@localhost 7004]# redis-server redis.conf
[root@localhost 7004]# cd ../7005
[root@localhost 7005]# redis-server redis.conf
8.查看Redis进程是否启动:
[root@localhost 7005]#
[root@localhost 7005]# ps -ef|grep -i redis
root 3630 1 0 10:15 ? 00:00:00 redis-server 0.0.0.0:7000 [cluster]
root 3683 1 0 10:19 ? 00:00:00 redis-server 0.0.0.0:7001 [cluster]
root 3699 1 0 10:19 ? 00:00:00 redis-server 0.0.0.0:7002 [cluster]
root 3715 1 0 10:19 ? 00:00:00 redis-server 0.0.0.0:7003 [cluster]
root 3731 1 0 10:19 ? 00:00:00 redis-server 0.0.0.0:7004 [cluster]
root 3747 1 0 10:19 ? 00:00:00 redis-server 0.0.0.0:7005 [cluster]
root 3778 3192 0 10:20 pts/0 00:00:00 grep --color=auto -i redis
9.安装Ruby
创建Redis集群要执行的ruby的脚本,需要ruby的环境
[root@localhost 7005]#
[root@localhost 7005]# cd ..
[root@localhost redis-3.0.0]# yum install ruby Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
... ... Dependency Installed:
ruby-irb.noarch 0:2.0.0.648-33.el7_4 ruby-libs.x86_64 0:2.0.0.648-33.el7_4
rubygem-bigdecimal.x86_64 0:1.2.0-33.el7_4 rubygem-io-console.x86_64 0:0.4.2-33.el7_4
rubygem-json.x86_64 0:1.7.7-33.el7_4 rubygem-psych.x86_64 0:2.0.0-33.el7_4
rubygem-rdoc.noarch 0:4.0.0-33.el7_4 rubygems.noarch 0:2.0.14.1-33.el7_4 Complete!
10.安装rubygems
RubyGems是Ruby的一个包管理器,提供了分发Ruby程序和库的标准格式“gem”,旨在方便地管理gem安装的工具
[root@localhost redis-3.0.0]# yum install rubygems
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
Package rubygems-2.0.14.1-33.el7_4.noarch already installed and latest version
Nothing to do
11.使用gem命令安装redis接口
[root@localhost redis-3.0.0]# gem install redis
Fetching: redis-4.0.3.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
结果报错,Ruby版本太低,需要升级到2.2.2以上。
12.安装rvm
rvm,全名Ruby Version Manager,是ruby的版本管理器。
[root@localhost redis-3.0.0]# curl -L get.rvm.io | bash -s stable
... ...
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete: [root@localhost redis-3.0.0]# source /usr/local/rvm/scripts/rvm
13.查看ruby可用版本
[root@localhost redis-3.0.0]# rvm list known
#MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]
ruby-head
...
14.安装满足要求的ruby版本
之前已经提示了要安装2.2.2以上的版本,这里我们选择2.3.0。
[root@localhost redis-3.0.0]# rvm install 2.3.0
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/centos/7/x86_64/ruby-2.3.0.tar.bz2
... ...
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.3.0 - #validate archive
ruby-2.3.0 - #extract
ruby-2.3.0 - #validate binary
ruby-2.3.0 - #setup
ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0@global
ruby-2.3.0 - #importing gemset /usr/local/rvm/gemsets/global.gems................................|
ruby-2.3.0 - #generating global wrappers.......
ruby-2.3.0 - #gemset created /usr/local/rvm/gems/ruby-2.3.0
ruby-2.3.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.0 - #generating default wrappers.......
15.安装gem redis接口
先卸载老版本的ruby,并使2.3.0版本生效。接着就可以安装gem的redis接口
[root@localhost redis-3.0.0]# rvm use 2.3.0
Using /usr/local/rvm/gems/ruby-2.3.0
[root@localhost redis-3.0.0]# rvm remove 1.8.7
ruby-1.8.7-head - #already gone
Using /usr/local/rvm/gems/ruby-2.3.0
[root@localhost redis-3.0.0]# ruby --version
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[root@localhost redis-3.0.0]# gem install redis
Fetching: redis-4.0.3.gem (100%)
Successfully installed redis-4.0.3
Parsing documentation for redis-4.0.3
Installing ri documentation for redis-4.0.3
Done installing documentation for redis after 1 seconds
1 gem installed
16.安装rubygems
[root@localhost redis-3.0.0]# yum install -y rubygems
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.sjtu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
Package rubygems-2.0.14.1-33.el7_4.noarch already installed and latest version
Nothing to do
三、Redis集群命令
1.创建Redis集群
以下命令可以创建集群:
redis-trib.rb create --replicas 1 192.168.213.11:7000 192.168.213.11:7001 192.168.213.11:7002 192.168.213.11:7003 192.168.213.11:7004 192.168.213.11:7005
但是,需要将redis-trib.rb复制到/usr/local/bin目录下。
还得在redis-3.0.0/src目录下启动,不然会报错redis-trib.rb: command not found...
[root@localhost redis-3.0.0]# redis-trib.rb create --replicas 1 192.168.213.11:7000 192.168.213.11:7001 192.168.213.11:7002 192.168.213.11:7003 192.168.213.11:7004 192.168.213.11:7005
bash: redis-trib.rb: command not found...
[root@localhost redis-3.0.0]# cp redis-trib.rb /usr/local/bin
cp: cannot stat ‘redis-trib.rb’: No such file or directory
[root@localhost redis-3.0.0]# cd src
[root@localhost src]# cp redis-trib.rb /usr/local/bin
创建集群后,会有三台Master主机,三台Slave从机。在启动过程,输入yes同意配置即可。
[root@localhost src]# redis-trib.rb create --replicas 1 192.168.213.11:7000 192.168.213.11:7001 192.168.213.11:7002 192.168.213.11:7003 192.168.213.11:7004 192.168.213.11:7005 >>> Creating cluster
Connecting to node 192.168.213.11:7000: OK
Connecting to node 192.168.213.11:7001: OK
Connecting to node 192.168.213.11:7002: OK
Connecting to node 192.168.213.11:7003: OK
Connecting to node 192.168.213.11:7004: OK
Connecting to node 192.168.213.11:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.213.11:7000
192.168.213.11:7001
192.168.213.11:7002
Adding replica 192.168.213.11:7003 to 192.168.213.11:7000
Adding replica 192.168.213.11:7004 to 192.168.213.11:7001
Adding replica 192.168.213.11:7005 to 192.168.213.11:7002
M: d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000
slots:0-5460 (5461 slots) master
M: 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001
slots:5461-10922 (5462 slots) master
M: 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002
slots:10923-16383 (5461 slots) master
S: d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003
replicates d8c56bba1dcda8e9ca32debd562eb950e55fc151
S: d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004
replicates 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
S: a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005
replicates 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 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.213.11:7000)
M: d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000
slots:0-5460 (5461 slots) master
M: 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001
slots:5461-10922 (5462 slots) master
M: 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002
slots:10923-16383 (5461 slots) master
M: d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003
slots: (0 slots) master
replicates d8c56bba1dcda8e9ca32debd562eb950e55fc151
M: d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004
slots: (0 slots) master
replicates 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
M: a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005
slots: (0 slots) master
replicates 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2.登录集群客户端
-c标识以集群方式登录,-p是指端口。
启动成功后,cluster info查看集群信息
[root@localhost src]# redis-cli -h 192.168.213.11 -c -p 7002
192.168.213.11:7002> 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:3
cluster_stats_messages_sent:282
cluster_stats_messages_received:282
192.168.213.11:7002>
无中心结构
无中心结构,连接任一个节点即访问到整个集群,每个节点都会维护集群状态,会根据哈希算法把数据归到某一个节点。
如下所示,通过7000主节点登录集群,写入数据时,"book"存储在7000主节点,而"myname"存储在7001主节点。
[root@bogon 7006]# redis-cli -h 192.168.213.11 -c -p 7000
192.168.213.11:7000> set book java
OK
192.168.213.11:7000> get book
"java" 192.168.213.11:7000> set myname lin
-> Redirected to slot [12807] located at 192.168.213.11:7002
OK
192.168.213.11:7002> get myname
"lin"
3. 查看集群中的所有节点:
[root@bogon redis-3.0.0]# redis-cli -c -p 7000 cluster nodes
d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000 myself,master - 0 0 1 connected 0-5460
d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003 slave d8c56bba1dcda8e9ca32debd562eb950e55fc151 0 1542248014060 4 connected
d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004 slave 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 0 1542248015066 5 connected
3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001 master - 0 1542248013054 2 connected 5461-10922
4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002 master - 0 1542248013054 3 connected 10923-16383
a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005 slave 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 0 1542248016072 6 connected
4.查看集群中的主节点:
redis-cli -c -p 7000 cluster nodes | grep master
如果想查找从节点则用grep slave匹配。
[root@bogon redis-3.0.0]# redis-cli -c -p 7000 cluster nodes | grep master
d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000 myself,master - 0 0 1 connected 0-5460
3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001 master - 0 1542247883232 2 connected 5461-10922
4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002 master - 0 1542247884237 3 connected 10923-16383
5.哈希槽(hash slot)
Redis 集群的数据分片(Redis Cluster data sharding)形式为哈希槽 。
Redis 集群有 16384 个哈希槽。 每一个 Redis 集群中的节点都承担一个哈希槽的子集。
观察我们查询出来的主节点信息,发现7000节点的哈希槽为0-5460,7001节点的哈希槽为5461-10922,7002节点的哈希槽为10923-16383。
哈希槽让在集群中添加和移除节点非常容易。例如,如果我想添加一个新节点 D,我需要从节点 A,B, C 移动一些哈希槽到节点 D。同样地,如果我想从集群中移除节点 A,我只需要移动 A 的哈希槽到 B 和 C。 当节点 A 变成空的以后,我就可以从集群中彻底删除它。 因为从一个节点向另一个节点移动哈希槽并不需要停止操作,所以添加和移除节点,或者改变节点持有 的哈希槽百分比,都不需要任何停机时间(downtime)。
6.添加新节点
新建7006文件夹,复制配置文件,并修改端口为7006,然后启动服务。
[root@bogon redis-3.0.0]# mkdir 7006
[root@bogon redis-3.0.0]# cp 7000/redis.conf 7006
[root@bogon 7006]# vim redis.conf
[root@bogon 7006]# redis-server redis.conf
将7006节点添加到集群中,如下:
[root@bogon 7006]# redis-trib.rb add-node 192.168.213.11:7006 192.168.213.11:7000
>>> Adding node 192.168.213.11:7006 to cluster 192.168.213.11:7000
Connecting to node 192.168.213.11:7000: OK
Connecting to node 192.168.213.11:7003: OK
Connecting to node 192.168.213.11:7004: OK
Connecting to node 192.168.213.11:7001: OK
Connecting to node 192.168.213.11:7002: OK
Connecting to node 192.168.213.11:7005: OK
>>> Performing Cluster Check (using node 192.168.213.11:7000)
M: d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003
slots: (0 slots) slave
replicates d8c56bba1dcda8e9ca32debd562eb950e55fc151
S: d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004
slots: (0 slots) slave
replicates 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
M: 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005
slots: (0 slots) slave
replicates 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.213.11:7006: OK
>>> Send CLUSTER MEET to node 192.168.213.11:7006 to make it join the cluster.
[OK] New node added correctly.
查看集群节点,发现新添加的7006节点:
[root@bogon 7006]# redis-cli -c -p 7000 cluster nodes
d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000 myself,master - 0 0 1 connected 0-5460
8bf3734539e9cce486af8ba28419c9f2f71eff1a 192.168.213.11:7006 master - 0 1542248732783 0 connected
d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003 slave d8c56bba1dcda8e9ca32debd562eb950e55fc151 0 1542248728758 4 connected
d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004 slave 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 0 1542248734793 5 connected
3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001 master - 0 1542248730771 2 connected 5461-10922
4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002 master - 0 1542248732279 3 connected 10923-16383
a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005 slave 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 0 1542248733789 6 connected
新节点添加后,以下可以有两种方式处理节点:
6.1:第一种情况:将新节点变成真正的主节点:
使用redis-trib程序,将集群中的某些哈希槽移动到新节点里面,这个新节点就成为真正的主节点了。
[root@bogon 7006]# redis-trib.rb reshard 192.168.213.11:7000
Connecting to node 192.168.213.11:7000: OK
Connecting to node 192.168.213.11:7006: OK
Connecting to node 192.168.213.11:7003: OK
Connecting to node 192.168.213.11:7004: OK
Connecting to node 192.168.213.11:7001: OK
Connecting to node 192.168.213.11:7002: OK
Connecting to node 192.168.213.11:7005: OK
>>> Performing Cluster Check (using node 192.168.213.11:7000)
M: d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8bf3734539e9cce486af8ba28419c9f2f71eff1a 192.168.213.11:7006
slots: (0 slots) master
replicates 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
S: d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003
slots: (0 slots) slave
replicates d8c56bba1dcda8e9ca32debd562eb950e55fc151
S: d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004
slots: (0 slots) slave
replicates 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
M: 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001
slots:5461-10922 (5462 slots) master
2 additional replica(s)
M: 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005
slots: (0 slots) slave
replicates 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e
[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
还需要指定把这些哈希槽转移到哪个节点上。输入新增的节点的ID即可。
观察刚才查询出来的集群信息,可以得到各个节点的节点ID。
8bf3734539e9cce486af8ba28419c9f2f71eff1a 192.168.213.11:7006 master - 0 1542248732783 0 connected
新节点7006的节点ID为8bf3734539e9cce486af8ba28419c9f2f71eff1a。如下:
What is the receiving node ID?
8bf3734539e9cce486af8ba28419c9f2f71eff1a
注意,哈希槽只能移动到其他主节点里面,如果移动到其他从节点里会报错,节点ID多了空格也会报错:
*** The specified node is not known or not a master, please retry.
然后需要我们指定转移哪几个几点的哈希槽。
输入all 表示从所有的主节点中随机转移,凑够1000个哈希槽。
也可以输入一个或多个节点ID,之后再输入done,表示从指定的节点中获取1000个哈希槽。
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
然后再输入yes,redis集群就开始分配哈希槽了。
Do you want to proceed with the proposed reshard plan (yes/no)? yes
至此,一个新的主节点就添加完成了,执行命令查看现在的集群中节点的状态。
删除主节点7006
如果删除的节点是主节点,需要先移除哈希槽。。
这里我们删除192.168.213.11:7006节点,这个节点有1000个哈希槽。
首先要把节点中的哈希槽转移到其他节点中,执行下面的命令,对集群重新分片:
redis-trib.rb reshard 192.168.33.130:7000
系统会提示我们要移动多少哈希槽,这里移动1000个,因为192.168.33.130:7006节点有1000个哈希槽。
然后选择使用哪个节点ID接收哈希槽,这里选择7000节点。也可以是其他节点
How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? d8c56bba1dcda8e9ca32debd562eb950e55fc151
然后选择移出哪个节点ID的哈希槽,这里选择7006节点。然后再输入done。
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:8bf3734539e9cce486af8ba28419c9f2f71eff1a
Source node #2:done
最后一步,使用下面的命令把这个节点删除:
[root@bogon 7006]# redis-trib.rb del-node 192.168.213.11:7000 8bf3734539e9cce486af8ba28419c9f2f71eff1a >>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[root@bogon 7006]#
使用以下命令查看7006节点是否删除成功:
[root@bogon 7006]# redis-cli -c -p 7000 cluster nodes
6.2 :第二种情况:将新增节点变成某个主节点的从节点:
这个新节点192.168.213.11:7006添加到集群中后,刚才演示的是第一种情况,让新节点变成主节点。
现在我们要演示第二种情况。让新节点7006成为7001的从节点。
再次新增节点7006。
[root@bogon 7006]# redis-trib.rb add-node 192.168.213.11:7006 192.168.213.11:7000
如果报错:Node is not empty. Either the node already knows other nodes
可以先删除备份信息和集群配置信息再重新添加节点。详情见 : https://blog.csdn.net/vtopqx/article/details/50235737
让新节点7006成为7001的从节点,只需要执行下面的命令就可以了,命令后面的节点ID就是7001的节点ID。(注意,这个从节点哈希槽必须为空,如果不为空,则需要转移掉哈希槽使之为空)。
[root@bogon 7006]# redis-cli -c -p 7006 cluster replicate 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
OK
查看7006节点是否已经成为7001的从节点,如下:
[root@bogon 7006]# redis-cli -p 7000 cluster nodes | grep slave | grep 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6
8bf3734539e9cce486af8ba28419c9f2f71eff1a 192.168.213.11:7006 slave 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 0 1542250097872 2 connected
d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004 slave 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 0 1542250095861 5 connected
删除从节点7006:
由于是从节点,所以直接删除就可以了。如果是主节点,需要将节点移出哈希槽。再执行del-node命令。
[root@bogon redis-3.0.0]# redis-trib.rb del-node 192.168.213.11:7006 8bf3734539e9cce486af8ba28419c9f2f71eff1a
>>> Removing node 8bf3734539e9cce486af8ba28419c9f2f71eff1a from cluster 192.168.213.11:7006
Connecting to node 192.168.213.11:7006: OK
Connecting to node 192.168.213.11:7005: OK
Connecting to node 192.168.213.11:7004: OK
Connecting to node 192.168.213.11:7002: OK
Connecting to node 192.168.213.11:7003: OK
Connecting to node 192.168.213.11:7001: OK
Connecting to node 192.168.213.11:7000: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
查看集群节点信息:
[root@bogon redis-3.0.0]# redis-cli -c -p 7000 cluster nodes
d8c56bba1dcda8e9ca32debd562eb950e55fc151 192.168.213.11:7000 myself,master - 0 0 1 connected 0-5460
d59c43901cef4b2afc43c9e8c9e1a6a1f6a90352 192.168.213.11:7003 slave d8c56bba1dcda8e9ca32debd562eb950e55fc151 0 1542272986495 4 connected
d45f935cb18f51c697adc4a920eb93158d0cb7d1 192.168.213.11:7004 slave 3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 0 1542272989516 5 connected
3de3f15f10a85a1b71cdd8b75d7a9f863b95a5d6 192.168.213.11:7001 master - 0 1542272988511 2 connected 5461-10922
4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 192.168.213.11:7002 master - 0 1542272985489 3 connected 10923-16383
a9d7d7b3fd3a61c7f836211c207d3a1d25c3b573 192.168.213.11:7005 slave 4449758829ef5d90c23ad87ccb7dc0ae8bbc750e 0 1542272987504 6 connected
可以发现7006节点已经删除了。
参考资料:
Redis集群部署及命令的更多相关文章
- Redis集群部署文档(Ubuntu15.10系统)
Redis集群部署文档(Ubuntu15.10系统)(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如 ...
- Redis集群部署3.0
我用的Mac的终端 ------------------------- 1.Redis简介 centos(5.4) Redis是一个key-value存储系统.和Memcached类似,但是解决了断 ...
- Redis集群部署与维护
Redis集群部署与维护 目录: 一. 集群架构 二. 集群部署 1. 创建redis-cluster目录 2. 编译redis 3. 编辑redis配置文件 4. 配置redis集群 5. redi ...
- 二进制redis集群部署
二进制redis集群部署 〇.前言 无聊想学罢了 准备环境: 三台centos7 1C1GB即可 三个路相连的地址 主机 IP 节点-角色-实例(端口) redis1 172.16.106.128 M ...
- Redis集群部署-windows
Redis集群部署-windows 前言 为了能体验一下部署Redis集群是一种怎么样的体验,所一边做一边写了这篇记录. 1.准备 从这里下载windows服务端 https://github.com ...
- redis集群部署那点事
[CentOS]make cc Command not found,make: *** [adlist.o] Error 127” 参考:https://blog.csdn.net/wzygis/ar ...
- Redis 集群部署
一.下载所需软件包 redis wget http://download.redis.io/releases/redis-4.0.6.tar.gz ruby wget https://cache.ru ...
- Docker网络讲解 及实验redis集群部署
理解docker0 准备工作:清空所有的容器,清空所有的镜像 docker rm -f $(docker ps -a -q) # 删除所有容器 docker rmi -f $(docker image ...
- Docker | redis集群部署实战
前面已经简单熟悉过redis的下载安装使用,今天接着部署redis集群(cluster),简单体会一下redis集群的高可用特性. 环境准备 Redis是C语言开发,安装Redis需要先将Redis的 ...
随机推荐
- HDFS 原理解析
源自https://www.cnblogs.com/duanxz/p/3874009.html Namenode是整个文件系统的管理节点.它维护着整个文件系统的文件目录树,文件/目录的元信息和每个文件 ...
- stage的划分
stage的划分是以shuffle操作作为边界的,遇到一个宽依赖就分一个stage 一个Job会被拆分为多组Task,每组任务被称为一个Stage就像Map Stage, Reduce Stage.S ...
- 设置SVN服务器
1 下载svn服务器,参考地址 http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html 下载地址: http://subv ...
- Appium -选择、操作元素3
UI Automator API定位 id .class name.acessibility id.xpath底层都是通过UI Automator API定位,UI Automator测试框架提供了一 ...
- vue.js 作一个用户表添加页面----初级
使用vue.js 制作一个用户表添加页面,实际上是把原来需要使用js写的部分,改写成vue.js的格式 首先,想象一下,先做思考,我们要添加用户表,设涉及到哪些数据,一个是用户id,一个是用户名,一个 ...
- 关于AB包的释放与 Resources.UnloadUnusedAssets的关系
Resources.UnloadUnusedAssets 并不能释放AB包中东西,只能释放从AB包中加载出来的资源,也可以释放场景中的资源,其它不是从AB包加载来的资源. AB加载后,整个包都加载到内 ...
- mysql 的sleep线程过多处理方法
php程序不要使用长连接:java程序调整连接池 什么是长连接? 其实长连接是相对于通常的短连接而说的,也就是长时间保持客户端与服务端的连接状态. 通常的短连接操作步骤是: 连接->数据传输-& ...
- java由字符型强制转化为整型例题
此Java程序依次输出参数,参数类型为字符型,要求更改程序,使得字符型强制转化为整形,并将这些整数相加,最后输出总和. 原程序: package demo; public class CommandP ...
- mysql 授权命令
MySQL 数据库赋予用户权限操作表 MySQL清空数据库的操作:truncate table tablename; MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库 ...
- JS 打印图片
在使用window.print()进行打印时,打印的内容可能会包含图片内容,此时的图片内容不能设置为背景图片,否则将无法再打印页面显示. <!doctype html> <html& ...