工作原理
案例1:部署redis集群
1.1 问题
具体要求如下:
部署管理主机
创建集群
查看集群信息
访问集群
1.2 方案
搭建redis集群,拓扑规划如图-1所示:
IP,端口规划如表-1所示:
步骤一:配置管理主机mgm57
- 部署ruby脚本运行环境
[root@mgm57 ~]# yum -y install rubygems
[root@mgm57 ~]# gem install redis-3.2.1.gem
- 创建管理集群脚本
[root@mgm57 ~]#mkdir /root/bin //创建命令检索目录
[root@mgm57 ~]#tar -zxf redis-4.0.8.tar.gz
[root@mgm57 ~]#cd redis-4.0.8/src/
[root@mgm57 ~]#cp redis-trib.rb /root/bin/ //创建管理集群脚本
[root@mgm57 ~]#chmod +x /root/bin/redis-trib.rb
[root@mgm57 ~]#redis-trib.rb help //查看命令帮助
步骤二:创建集群
- 启动服务器192.168.4.51的集群功能
[root@redisA ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisA ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.51 //修改ip
port 6351 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.conf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisA ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@redisA ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.51 -p 6351 shutdown
:wq
[root@redisA ~]# /etc/init.d/redis_6379 start
- 启动服务器192.168.4.52的集群功能
[root@redisB ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisB ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.52 //修改ip
port 6352 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.conf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisB ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@redisB ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.52 -p 6352 shutdown
:wq
[root@redisB ~]# /etc/init.d/redis_6379 start
- 启动服务器192.168.4.53的集群功能
[root@redisC ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisC ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.53 //修改ip
port 6353 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.conf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisC ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@redisC ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.53 -p 6353 shutdown
:wq
[root@redisC ~]# /etc/init.d/redis_6379 start
- 启动服务器192.168.4.54的集群功能
[root@redisD ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisD ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.54 //修改ip
port 6354 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.Donf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisD ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@redisD ~]# vim +43 /etc/init.d/redis_6379
$DLIEXED -h 192.168.4.54 -p 6354 shutdown
:wq
[root@redisD ~]# /etD/init.d/redis_6379 start
- 启动服务器192.168.4.55的集群功能
[root@redisE ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisE ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.55 //修改ip
port 6355 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.conf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisE ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@redisE ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.55 -p 6355 shutdown
:wq
[root@redisE ~]# /etc/init.d/redis_6379 start
- 启动服务器192.168.4.56的集群功能
[root@redisF ~]# /etc/init.d/redis_6379 stop //停止redis服务
[root@redisF ~]# vim /etc/redis/6379.conf //修改配置文件
bind 192.168.4.56 //修改ip
port 6356 //修改端口(可选配置)
cluster-enabled yes //启用集群功能
cluster-config-file nodes-6379.conf //存储集群信息的配置文件
cluster-node-timeout 5000 //集群节点通信超时时间
:wq
[root@redisF ~]# rm -rf /var/lib/redis/6379/* //清空数据
[root@rediseF ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.56 -p 6356 shutdown
:wq
[root@redisF ~]# /etc/init.d/redis_6379 start
- 在管理主机mgm57,创建集群
[root@mgm57 ~]# 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
>>> 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: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
slots:0-5460 (5461 slots) master
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
replicates 324e05df3f143ef97e50d09be0328a695e655986
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: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
slots: (0 slots) slave
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
slots: (0 slots) slave
replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
slots: (0 slots) slave
replicates d9f8fe6d6d9dd391be8e7904501db1535e4d17cb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. //提示16384个槽分配完毕
[root@mgm57 ~]#
步骤三:查看集群信息
- 在管理主机查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.51:6351 //查看集群信息
- 在管理主机检测集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.51:6351 //检测集群
- 在任意一台redis服务器本机,查看集群信息
[root@redisA ~]# redis-cli -h 192.168.4.51 -p 6351
192.168.4.51:6351> cluster info //查看集群信息
192.168.4.51:6351> cluster nodes //查看集群节点信息
步骤四:访问集群
- 在客户端连接集群中的任意一台服务器存取数据
[root@client50 ~]# redis-cli -c -h 192.168.4.51 -p 6351 //连接服务器51
192.168.4.51:6351> set x 100 //存储
-> Redirected to slot [16287] located at 192.168.4.53:6353 //提示存储在53主机
OK
192.168.4.53:6353> keys *
1) "x"
192.168.4.53:6353> set y 200
OK
192.168.4.53:6353> keys *
1) "y"
2) "x"
192.168.4.53:6353> set z 300 //存储
-> Redirected to slot [8157] located at 192.168.4.52:6352 //提示存储在52主机
OK
192.168.4.52:6352> keys * //在52主机查看数据 只有变量z
1) "z"
192.168.4.52:6352> get x
-> Redirected to slot [16287] located at 192.168.4.53:6353 //连接53主机获取数据
"100"
192.168.4.53:6353> keys *
1) "y"
2) "x"
192.168.4.53:6353> get z
-> Redirected to slot [8157] located at 192.168.4.52:6352
"300"
192.168.4.52:6352> set i 400
-> Redirected to slot [15759] located at 192.168.4.53:6353
OK
192.168.4.53:6353> set j 500
-> Redirected to slot [3564] located at 192.168.4.51:6351
OK
案例2:添加服务器
2.1 问题
部署新redis服务器
添加master角色主机到集群里
添加slave角色主机到集群里
步骤一:部署新redis服务器 ip为192.168.4.58
- 装包,初始化,启用集群功能,重启服务
]#yum -y install gcc
]#tar -zxf redis-4.0.8.tar.gz
]#cd redis-4.0.8/
]#make
]#make install
]#./utils/install_server.sh
]# /etc/init.d/redis_6379 stop
vim /etc/redis/6379.conf
bind 192.168.4.58
port 6358
cluster-enabled yes //启用集群
cluster-config-file nodes-6379.conf //存储集群信息文件
cluster-node-timeout 5000
:wq
]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.58 -p 6358 shutdown
:wq
]# /etc/init.d/redis_6379 start
步骤二:添加master角色主机到集群里
- 在管理主机,添加master角色主机
[root@mgm57 ~]# redis-trib.rb add-node 192.168.4.58:6358 192.168.4.53:6353 //执行添加命令
- 在管理主机,查看集群新消息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.53:6353 //查看集群信息
- 在管理主机,检测集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.53:6353 //检测集群
- 在管理主机,重新分配hash槽
[root@mgm57 ~]# redis-trib.rb reshard 192.168.4.53:6353
How many slots do you want to move (from 1 to 16384)?4096 //拿出4096个hash 槽给主机192.168.4.58
What is the receiving node ID? c5e0da48f335c46a2ec199faa99b830f537dd8a0 //主机192.168.4.58的id值
Source node #1:all //从当前所有master服务器获取hash槽
Do you want to proceed with the proposed reshard plan (yes/no)?yes //同意以上配置
...
Moving slot 12283 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12284 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12285 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12286 from 192.168.4.53:6353 to 192.168.4.58:6358:
Moving slot 12287 from 192.168.4.53:6353 to 192.168.4.58:6358:
- 在管理主机,查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.53:6353
步骤三:添加slave角色主机到集群里
- 部署新的redis服务器 192.168.4.59
]#yum -y install gcc
]#tar -zxf redis-4.0.8.tar.gz
]#cd redis-4.0.8/
]#make
]#make install
]#./utils/install_server.sh
]# /etc/init.d/redis_6379 stop
vim /etc/redis/6379.conf
bind 192.168.4.59
port 6359
cluster-enabled yes //启用集群
cluster-config-file nodes-6379.conf //存储集群信息文件
cluster-node-timeout 5000
:wq
]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.59 -p 6359 shutdown
:wq
]# /etc/init.d/redis_6379 start
- 在管理主机,添加slave角色主机
[root@mgm57 ~]# 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)
S: d9f8fe6d6d9dd391be8e7904501db1535e4d17cb 192.168.4.51:6351
slots: (0 slots) slave
replicates 2d343a9df48f6f6e207949e980ef498466a44dad
S: 894dd0008053f6fb65e9e4a36b755d9351607500 192.168.4.56:6356
slots: (0 slots) slave
replicates 324e05df3f143ef97e50d09be0328a695e655986
M: 2d343a9df48f6f6e207949e980ef498466a44dad 192.168.4.55:6355
slots:1365-5460 (4096 slots) master
1 additional replica(s)
M: 9e44139cffb8ebd7ed746aabbf4bcea9bf207645 192.168.4.53:6353
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: d9634ba0aa5c1a07193da4a013da6051c1515922 192.168.4.54:6354
slots: (0 slots) slave
replicates 9e44139cffb8ebd7ed746aabbf4bcea9bf207645
M: 324e05df3f143ef97e50d09be0328a695e655986 192.168.4.52:6352
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: 4fe1fa467ad237802021f5aac5f1d5b3e0db47ef 192.168.4.58:6358
slots:0-1364,5461-6826,10923-12287 (4096 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.
Automatically selected master 192.168.4.58:6358
>>> 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.58:6358. //提示添加完成
[OK] New node added correctly.
- 在管理主机,查看集群新消息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.51:6351 //查看信息
- 在管理主机,检测集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.53:6353 //检测集群
[root@mgm57 ~]# redis-trib.rb check 192.168.4.51:6351
- 在客户端,访问从服务器59,查看数据
[root@host50 ~]# redis-cli -c -h 192.168.4.59 -p 6359
192.168.4.59:6359> keys * //自动同步主服务器58的数据
1) "name"
2) "name2"
3) "age"
4) "y"
5) "shcool5"
案例3:移除服务器
3.1 问题
把slave服务器移除集群
把master服务器移除集群
步骤一:把slave服务器移除集群
- 在管理主机,移除slave服务器,从服务器没有槽,直接移除即可。
[root@mgm57 ~]# redis-trib.rb del-node 192.168.4.51:6351 f6649ea99b2f01faca26217691222c17a3854381 //执行移除命令
- 在管理主机,查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.51:6351
步骤二:把master服务器移除集群
- 在管理主机,先删除master服务器占用的hash槽
[root@mgm57 ~]# redis-trib.rb reshard 192.168.4.53:6353
How many slots do you want to move (from 1 to 16384)?4096 //移除4096个数槽
What is the receiving node ID? bc5c4e082a5a3391b634cf433a6486c867cfc44b
//要移动给谁的id即目标主机(这里可以随机写一个master的ID)
Source node #1: c5e0da48f335c46a2ec199faa99b830f537dd8a0
//从谁那移动即源主机(这里写4.58的ID)
Source node #2:done //设置完毕
...
Moving slot 12282 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Moving slot 12283 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Moving slot 12284 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Moving slot 12285 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Moving slot 12286 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Moving slot 12287 from c5e0da48f335c46a2ec199faa99b830f537dd8a0
Do you want to proceed with the proposed reshard plan (yes/no)?yes //提交
...
Moving slot 12282 from 192.168.4.58:6358 to 192.168.4.53:6353:
Moving slot 12283 from 192.168.4.58:6358 to 192.168.4.53:6353:
Moving slot 12284 from 192.168.4.58:6358 to 192.168.4.53:6353:
Moving slot 12285 from 192.168.4.58:6358 to 192.168.4.53:6353:
Moving slot 12286 from 192.168.4.58:6358 to 192.168.4.53:6353:
Moving slot 12287 from 192.168.4.58:6358 to 192.168.4.53:6353:
- 在管理主机,查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.51:6351
- 在管理主机,移除master主机
[root@mgm57 ~]# redis-trib.rb del-node 192.168.4.53:6353 c5e0da48f335c46a2ec199faa99b830f537dd8a0 //删除谁+删除的id
- 在管理主机,查看集群信息
[root@mgm57 ~]# redis-trib.rb info 192.168.4.51:6351