创建Redis集群(重新修改)

创建Redis集群
为6台主机安装并运行redis服务
# yum -y install gcc gcc-c++
# tar -zxvf redis-4.0.8.tar.gz
# cd redis-4.0.8/
# make
# make install
# ./utils/install_server.sh
• 调整配置文件
# vim /etc/redis/redis.conf
bind IP地址 //只写物理接口IP地址
daemonize yes //守护进程方式运行
port xxxx //端口号不要使用默认的6379
cluster-enabled yes//启用集群
cluster-config-file nodes-xxxx.conf//指定集群信息文件
cluster-node-timeout 5000//请求超时 5 秒

代码如下
(以6355为例)
Hint: Its a good idea to run make test 😉

INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/root/soft/redis/redis-4.0.8/src”
[[email protected] redis-4.0.8]# ./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] 6355
Please select the redis config file name [/etc/redis/6355.conf]
Selected default – /etc/redis/6355.conf
Please select the redis log file name [/var/log/redis_6355.log]
Selected default – /var/log/redis_6355.log
Please select the data directory for this instance [/var/lib/redis/6355]
Selected default – /var/lib/redis/6355
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6355
Config file : /etc/redis/6355.conf
Log file : /var/log/redis_6355.log
Data dir : /var/lib/redis/6355
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6355.conf => /etc/init.d/redis_6355
Installing service…
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server…
Installation successful!
[[email protected] redis-4.0.8]#
[[email protected] redis-4.0.8]#
[[email protected] redis-4.0.8]#
[[email protected] redis-4.0.8]# /etc/init.d/redis_6355 stop
Stopping …
Redis stopped
[[email protected] redis-4.0.8]#
[[email protected] redis-4.0.8]# vim /etc/redis/6355.conf
[[email protected] redis-4.0.8]# /etc/init.d/redis_6355 start
Starting Redis server…
确认服务正常
[[email protected] redis-4.0.8]# netstat -antup | grep redis
tcp 0 0 192.168.4.55:6355 0.0.0.0:* LISTEN 5351/redis-server 1
tcp 0 0 192.168.4.55:16355 0.0.0.0:* LISTEN 5351/redis-server 1
[[email protected] redis-4.0.8]# /etc/init.d/redis_6355 status
Redis is running (5351)

###############################3
目前的配置文件如下
[[email protected] redis-4.0.8]# grep -v “^#” /etc/redis/6355.conf | grep -v “^$”
bind 192.168.4.55
protected-mode yes
port 6355
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6355.pid
loglevel notice
logfile /var/log/redis_6355.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis/6355
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
cluster-enabled yes
cluster-config-file nodes-6355.conf
cluster-node-timeout 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
################################################

查看集群信息
[[email protected] redis-4.0.8]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> CLUSTER info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
192.168.4.51:6351>
192.168.4.51:6351>
192.168.4.51:6351> cluster nodes
2fb277f09dee1ee10da756e2a41a7ee344c68c76 :[email protected] myself,master – 0 0 0 connected
192.168.4.51:6351>

创建集群
(1)在51上面安装ruby环境
[[email protected] redis-4.0.8]# yum install -y ruby rubygems
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
192.168.4.254_rhel7 | 4.1 kB 00:00:00
正在解决依赖关系
–> 正在检查事务
###########################################
已安装:
ruby.x86_64 0:2.0.0.648-30.el7 rubygems.noarch 0:2.0.14.1-30.el7

作为依赖被安装:
libyaml.x86_64 0:0.1.4-11.el7_0 ruby-irb.noarch 0:2.0.0.648-30.el7 ruby-libs.x86_64 0:2.0.0.648-30.el7
rubygem-bigdecimal.x86_64 0:1.2.0-30.el7 rubygem-io-console.x86_64 0:0.4.2-30.el7 rubygem-json.x86_64 0:1.7.7-30.el7
rubygem-psych.x86_64 0:2.0.0-30.el7 rubygem-rdoc.noarch 0:4.0.0-30.el7

完毕!

(2)安装ruby
[[email protected] redis-cluster]# ll
总用量 204
-rw-r–r–. 1 root root 73728 5月 22 10:56 redis-3.2.1.gem
-rw-r–r–. 1 root root 131500 5月 22 10:56 ruby-devel-2.0.0.648-30.el7.x86_64.rpm
[[email protected] redis-cluster]#
[[email protected] redis-cluster]#
[[email protected] redis-cluster]# yum -y install ruby-devel-2.0.0.648-30.el7.x86_64.rpm
已加载插件:langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
正在检查 ruby-devel-2.0.0.648-30.el7.x86_64.rpm: ruby-devel-2.0.0.648-30.el7.x86_64
ruby-devel-2.0.0.648-30.el7.x86_64.rpm 将被安装
正在解决依赖关系
–> 正在检查事务
—> 软件包 ruby-devel.x86_64.0.2.0.0.648-30.el7 将被 安装
–> 解决依赖关系完成

依赖关系解决

==========================================================================================================================
Package 架构 版本 源 大小
==========================================================================================================================
正在安装:
ruby-devel x86_64 2.0.0.648-30.el7 /ruby-devel-2.0.0.648-30.el7.x86_64 337 k

事务概要
==========================================================================================================================
安装 1 软件包

总计:337 k
安装大小:337 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : ruby-devel-2.0.0.648-30.el7.x86_64 1/1
验证中 : ruby-devel-2.0.0.648-30.el7.x86_64 1/1

已安装:
ruby-devel.x86_64 0:2.0.0.648-30.el7

完毕!
[[email protected] redis-cluster]#
[[email protected] redis-cluster]# gem install redis-3.2.1.gem
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
1 gem installed
[[email protected] redis-cluster]#

创建集群
回到redis的安装包,其内有命令文件
[[email protected] src]# pwd
/root/soft/redis/redis-4.0.8/src

添加节点
# ./redis-trib.rb create –replicas 1 host:port host:port ……
–replicas 1 ,自动为每一个master节点分配一个slave节点
备注:–replicas 1 中 “1”的意思是每个主有1个从

[[email protected] src]# ./redis-trib.rb create –replicas 1 192.168.4.51:6351 192.168.4.52:6352 192.168.4.53:6353 192.168.4.54:6354 192.168.4.55:6355 192.168.4.56:6356
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes…
Using 3 masters:
192.168.4.51:6351
192.168.4.52:6352
192.168.4.53:6353
Adding replica 192.168.4.55:6355 to 192.168.4.51:6351
Adding replica 192.168.4.56:6356 to 192.168.4.52:6352
Adding replica 192.168.4.54:6354 to 192.168.4.53:6353
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
M: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
replicates c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
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.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots: (0 slots) slave
replicates c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots:5461-10922 (5462 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.

任何一台主机都能查看集群信息,例如在52上面
[[email protected] redis-4.0.8]# redis-cli -h 192.168.4.52 -p 6352
192.168.4.52:6352> 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:2
cluster_stats_messages_ping_sent:412
cluster_stats_messages_pong_sent:401
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:817
cluster_stats_messages_ping_received:398
cluster_stats_messages_pong_received:416
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:817
192.168.4.52:6352>
192.168.4.52:6352> CLUSTER nodes
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:[email protected] master – 0 1537930177000 3 connected 10923-16383
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:[email protected] master – 0 1537930178000 1 connected 0-5460
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:[email protected] myself,master – 0 1537930178000 2 connected 5461-10922
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:[email protected] slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537930176467 4 connected
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:[email protected] slave c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 0 1537930176000 6 connected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:[email protected] slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537930177000 5 connected

测试一下集群
(1)写入测试,可以看到,不同的数据分布在不同的节点
[[email protected] src]# redis-cli -c -h 192.168.4.51 -p 6351
192.168.4.51:6351> set school tarena
-> Redirected to slot [8455] located at 192.168.4.52:6352
OK
192.168.4.52:6352> set class linux
OK
192.168.4.52:6352> set pay 26800
-> Redirected to slot [4013] located at 192.168.4.51:6351
OK
192.168.4.51:6351> exit
(2)读测试,取不同的值,会去对应的主机去取
[[email protected] src]# redis-cli -c -h 192.168.4.55 -p 6355
192.168.4.55:6355> get name
-> Redirected to slot [5798] located at 192.168.4.52:6352
(nil)
192.168.4.52:6352> keys *
1) “school”
2) “class”
192.168.4.52:6352> get pay
-> Redirected to slot [4013] located at 192.168.4.51:6351
“26800”
192.168.4.51:6351>
(检查从节点)(看ID可以发现,56是52的从)
登陆52的从56去看一看,可以发现,值是一样的
192.168.4.51:6351> cluster nodes
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:[email protected] slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537931339356 4 connected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:[email protected] slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537931339000 5 connected
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:[email protected] myself,master – 0 1537931339000 1 connected 0-5460
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:[email protected] slave c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 0 1537931340000 6 connected
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:[email protected] master – 0 1537931340000 3 connected 10923-16383
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:[email protected] master – 0 1537931338000 2 connected 5461-10922
192.168.4.51:6351> exit
[[email protected] src]# redis-cli -c -h 192.168.4.56 -p 6356
192.168.4.56:6356> keys *
1) “class”
2) “school”

redis-cli命令
• 查看命令帮助
– redis-cli -h
• 常用选项
– -h IP地址
– -p 端口
– -c 集群模式redis-trib.rb脚本
• 语法格式
– redis-trib.rb 选项 参数
• 选项
– add-node 添加master主机
– check 检测集群
– reshard 重新分片
– add-node –slave 添加slave主机
– del-node 删除主机

选举master主机
• 停止master 主机的 Redis服务
– master宕机后对应的slave自动被选举为master
– 原master启动后 会自动配置为当前master的slave
• 查看集群主机信息
– redis-cli -h master_ip -p master_port
代码如下
先把52主机停止服务
[[email protected] redis-4.0.8]# redis-cli -h 192.168.4.52 -p 6352 shutdown
[[email protected] redis-4.0.8]# netstat -antup | grep redis
在登陆52的从56,可以看到52已经挂了,56升为新主
[[email protected] src]# redis-cli -c -h 192.168.4.56 -p 6356
192.168.4.56:6356> keys *
1) “class”
2) “school”
192.168.4.56:6356> CLUSTER nodes
2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:[email protected] master – 0 1537931513000 1 connected 0-5460
c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:[email protected] master – 0 1537931513000 3 connected 10923-16383
4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:[email protected] slave c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 0 1537931514357 4 connected
c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:[email protected] master,fail – 1537931492216 1537931491000 2 disconnected
dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:[email protected] slave 2fb277f09dee1ee10da756e2a41a7ee344c68c76 0 1537931513350 1 connected
d80244332c144307c2ecec60897632d81f299921 192.168.4.56:[email protected] myself,master – 0 1537931513000 7 connected 5461-10922

修复节点
当节点修复之后,会自动变为从库(有个主机是在后面的实验加上去的,可忽略)
如下所示可以看到,52已经变成了50的从库了
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

添加master主机
• 添加master主机
– 添加时不指定主机角色,默认新主机被选为master
– # ./redis-trib.rb add-node 新主机Ip:端口 管理主机:端口
[[email protected] src]# ./redis-trib.rb add-node 192.168.4.50:6350 192.168.4.51:6351
>>> Adding node 192.168.4.50:6350 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
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.4.50:6350 to make it join the cluster.
[OK] New node added correctly.

检测集群主机
– ./redis-trib.rb check 192.168.4.51:6351
– 主机角色为master
– 无槽位数量
代码如下:可以看到虽然有4个主,但是新添加的50是没有槽位数的
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
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.

新添加的主机由于没有分配槽位数,所以需要重新分片
在管理节点上重新分片,本次将每个节点均摊4096
[[email protected] src]# ./redis-trib.rb reshard 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
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.
How many slots do you want to move (from 1 to 16384)? 4096
//问你要挪多少槽位出来,16384/4刚好等于4096
What is the receiving node ID? 4003e6be771338a6e9ed6725d0f0ba8996f72b27
//哪个节点用来接收挪出来的槽位,注意要用ID
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 4096 slots.
Source nodes:
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
Resharding plan:
Moving slot 5461 from d80244332c144307c2ecec60897632d81f299921
Moving slot 5462 from d80244332c144307c2ecec60897632d81f299921
Moving slot 5463 from d80244332c144307c2ecec60897632d81f299921
#################//然后它就自动开始挪了//##################################
Moving slot 12285 from 192.168.4.53:6353 to 192.168.4.50:6350:
Moving slot 12286 from 192.168.4.53:6353 to 192.168.4.50:6350:
Moving slot 12287 from 192.168.4.53:6353 to 192.168.4.50:6350:
[[email protected] src]#
搞完收工,测试一下吧
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
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主机
部署一台新redis服务器
– 装包
– 初始化
– 启用集群配置
– 运行服务
• 添加slave主机
# ./redis-trib.rb add-node –slave [ –master-id id值 ] ip地址:端口 192.168.4.51:6351
如果不指定主节点的id 的话,会把新节点 随机添加为 从节点 最少的主的从
新建了一台主机59,然后我们就把这台主机添加为从
[[email protected] redis-4.0.8]# netstat -antup | grep redis
tcp 0 0 192.168.4.59:6359 0.0.0.0:* LISTEN 2985/redis-server 1
tcp 0 0 192.168.4.59:16359 0.0.0.0:* LISTEN 2985/redis-server 1

然后在51上面把59添加为从库
[[email protected] src]# ./redis-trib.rb add-node –slave 192.168.4.59:6359 192.168.4.51:6351
>>> Adding node 192.168.4.59:6359 to cluster 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
0 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
Automatically selected master 192.168.4.56:6356
>>> Send CLUSTER MEET to node 192.168.4.59:6359 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.4.56:6356.
[OK] New node added correctly.

查看一下,因为刚才没指定是谁的从库,所以它自动把59添加为56的从。因为刚才56没有从
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

移除master主机
配置步骤
– 重新分片释放占用的hash槽
– 移除master主机
# redis-trib.rb reshard 192.168.4.51:6351
# redis-trib.rb del-node 192.168.4.51:6351 maste主机id值

重新分片释放占用的hash槽
– 指定移出hash槽个数
– 指定接收hash槽主机ID
– 指定移出hash槽主机ID
本次实验,我们把50主移除掉
[[email protected] src]# ./redis-trib.rb reshard 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 4003e6be771338a6e9ed6725d0f0ba8996f72b27
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[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
//问你要移除多少个槽位,因为50有4096个,所以我们输入4096
What is the receiving node ID? 2fb277f09dee1ee10da756e2a41a7ee344c68c76
//问你哪个来接受,就先指定主51吧,注意都是写ID值
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:4003e6be771338a6e9ed6725d0f0ba8996f72b27
Source node #2:done
//问你从哪里移除,注意我们这一次是删除主50,所以就把50的ID写上去
Ready to move 4096 slots.
Source nodes:
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
1 additional replica(s)
Destination node:
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:1365-5460 (4096 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
Moving slot 1 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
Moving slot 2 from 4003e6be771338a6e9ed6725d0f0ba8996f72b27
########################然后他就开始移除了,要等会###############################
Moving slot 12285 from 192.168.4.50:6350 to 192.168.4.51:6351:
Moving slot 12286 from 192.168.4.50:6350 to 192.168.4.51:6351:
Moving slot 12287 from 192.168.4.50:6350 to 192.168.4.51:6351:
//搞完收工,查看确认一下
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
2 additional replica(s)
M: 4003e6be771338a6e9ed6725d0f0ba8996f72b27 192.168.4.50:6350
slots: (0 slots) master
0 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
已经移除完毕,现在删除主50
[[email protected] src]# ./redis-trib.rb del-node 192.168.4.51:6351 4003e6be771338a6e9ed6725d0f0ba8996f72b27
>>> Removing node 4003e6be771338a6e9ed6725d0f0ba8996f72b27 from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
2 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: c11d8dbd324bf4d733e230d1916bf2a27d3da6ad 192.168.4.52:6352
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
[[email protected] src]#

现在删除从库52,删除从非常简单,直接删除
[[email protected] src]# ./redis-trib.rb del-node 192.168.4.51:6351 c11d8dbd324bf4d733e230d1916bf2a27d3da6ad
>>> Removing node c11d8dbd324bf4d733e230d1916bf2a27d3da6ad from cluster 192.168.4.51:6351
>>> Sending CLUSTER FORGET messages to the cluster…
>>> SHUTDOWN the node.
[[email protected] src]# ./redis-trib.rb check 192.168.4.51:6351
>>> Performing Cluster Check (using node 192.168.4.51:6351)
M: 2fb277f09dee1ee10da756e2a41a7ee344c68c76 192.168.4.51:6351
slots:0-6826,10923-12287 (8192 slots) master
1 additional replica(s)
S: 4e5f0e5cbadae2b271cf7ae213ac8f8a55244aea 192.168.4.54:6354
slots: (0 slots) slave
replicates c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd
S: dd4fcfcf781362e23f820a8922310fe767ba9ce1 192.168.4.55:6355
slots: (0 slots) slave
replicates 2fb277f09dee1ee10da756e2a41a7ee344c68c76
M: d80244332c144307c2ecec60897632d81f299921 192.168.4.56:6356
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: c4e47ae971b7ef0c5a0facc0bc8e129ad08a22dd 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 2db7b61254b2b76315daec5f0d81d43ad625ce5e 192.168.4.59:6359
slots: (0 slots) slave
replicates d80244332c144307c2ecec60897632d81f299921
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

redis-trib.rb脚本选项总结
• redis-trib.rb 常用选项
create 创建集群
check 检查集群
reshard 重新分片
del-node 删除主机
add-node –slave 添加slave主机
add-node 添加master主机

此条目发表在reids分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注