搭建Redis 集群,测试集群:对节点主机重新分片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Redis 集群,测试集群:对节点主机重新分片相关的知识,希望对你有一定的参考价值。
搭建Redis 集群
准备6台redis服务器,具体要求如下:
ip地址 端口 etho 日志文件名
192.168.4.51 6051 都可以接收连接请求 redis51.log
192.168.4.52 6052 都可以接收连接请求 redis52.log
192.168.4.53 6053 都可以接收连接请求 redis53.log
192.168.4.54 6054 都可以接收连接请求 redis54.log
192.168.4.55 6055 都可以接收连接请求 redis55.log
192.168.4.56 6056 都可以接收连接请求 redis56.log
环境准备
1 在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务,清。
生成密钥与传密钥
[[email protected] ~]# ssh-keygen
[[email protected] ~]# for i in 192.168.4.{51..56}
do
ssh-copy-id $i
done
在每一台主机上创建redis
[[email protected] ~]# tar -xf redis/redis-4.0.8.tar.gz -C .
[[email protected] ~]# cd redis-4.0.8/
[[email protected] redis-4.0.8]# yum -y install gcc gcc-c++
[[email protected] redis-4.0.8]# make && make install
[[email protected] redis-4.0.8]# ./utils/install_server.sh
[[email protected] redis-4.0.8]# ln -s /etc/init.d/redis_6379 /bin/
在这大家可以也写个脚本批量安装redis
1.1 清空数据(在51-56上做一样的操作)
[[email protected] phpredis-2.2.4]# redis-cli -h 192.168.4.51 -p 6051
192.168.4.51:6051> FLUSHALL
OK
192.168.4.51:6051> keys *
(empty list or set)
192.168.4.51:6051>
[[email protected] phpredis-2.2.4]# vim /etc/redis/6379.conf
70 bind 192.168.4.51 //物理接口ip地址
93 port 6051 //端口
daemonize yes //以守护方式运行
815 cluster-enabled yes //开启集群
823 cluster-config-file nodes-6051.conf // 集群的配置文件不要使用默认的名称
829 cluster-node-timeout 15000 // 请求超时 5 秒
[[email protected] phpredis-2.2.4]# redis_6379 stop
[[email protected] phpredis-2.2.4]# redis_6379 start
[[email protected] ~]# ss -antulp | grep redis
tcp LISTEN 0 128 192.168.4.51:6051 : users:(("redis-server",pid=24771,fd=6))
tcp LISTEN 0 128 192.168.4.51:16051 : users:(("redis-server",pid=24771,fd=8))
装包: 在执行创建集群命令的主机安装ruby软件包
安装解释ruby代码的软件包(在51上安装)
[[email protected] redis]# yum -y install ruby ruygems
[[email protected] redis]# rpm -q rubygems ruby
rubygems-2.0.14.1-30.el7.noarch
ruby-2.0.0.648-30.el7.x86_64
[[email protected] redis-cluster]#rpm -ivh --nodeps ruby-devel-2.0.0.648-30.el7.x86_64.rpm
安装ruby连接redis 接口程序 gem
[[email protected] redis-cluster]# gem install redis-3.2.1.gem
生成创建集群的脚本redis-trib.rb
[[email protected] redis]# tar -xf redis-4.0.8.tar.gz
[[email protected] redis]# cd redis-4.0.8/
[[email protected] redis-4.0.8]# ln -s /root/redis/redis-4.0.8/src/redis-trib.rb /sbin/
创建集群:
[[email protected] redis-4.0.8]# redis-trib.rb create --replicas 1
192.168.4.51:6051
192.168.4.52:6052
192.168.4.53:6053
192.168.4.54:6054
192.168.4.55:6055
192.168.4.56:6056
>> Creating cluster
>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.4.51:6051
192.168.4.52:6052
192.168.4.53:6053
Adding replica 192.168.4.55:6055 to 192.168.4.51:6051
Adding replica 192.168.4.56:6056 to 192.168.4.52:6052
Adding replica 192.168.4.54:6054 to 192.168.4.53:6053
M: c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01 192.168.4.51:6051
slots:0-5560 (5561 slots) master
M: 2c9ee556add7e5187621efd64e4a9fc54bc99489 192.168.4.52:6052
slots:5561-10922 (5562 slots) master
M: bcb32a6d6b9e988789e654c32856c8c478ef253e 192.168.4.53:6053
slots:10923-16383 (5561 slots) master
S: 799f99bc22d8c4a9e0fcdc98166f6e6f5e348df0 192.168.4.54:6054
replicates bcb32a6d6b9e988789e654c32856c8c478ef253e
S: 18b86bca8fa9c9d36f3cad927a1c75c825bee9a4 192.168.4.55:6055
replicates c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01
S: 6a7196a77555cdf14f1a665386ff493928b31d94 192.168.4.56:6056
replicates 2c9ee556add7e5187621efd64e4a9fc54bc99489
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:6051)
M: c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01 192.168.4.51:6051
slots:0-5560 (5561 slots) master
1 additional replica(s)
S: 6a7196a77555cdf14f1a665386ff493928b31d94 192.168.4.56:6056
slots: (0 slots) slave
replicates 2c9ee556add7e5187621efd64e4a9fc54bc99489
............................................
..............................................
replicates bcb32a6d6b9e988789e654c32856c8c478ef253e
[OK] All nodes agree about slots configuration.
>> Check for open slots...
>> Check slots coverage...
[OK] All 16384 slots covered.
[[email protected] redis-4.0.8]# cat /var/lib/redis/6379/nodes-6051.conf #集群配置文件
6a7196a77555cdf14f1a665386ff493928b31d94 192.168.4.56:[email protected] slave 2c9ee556add7e5187621efd64e4a9fc54bc99489 0 1530777397000 6 connected
。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。。。。。
每台 redis服务 在本机登录 查看集群信息
[[email protected] redis-4.0.8]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> 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:934
cluster_stats_messages_pong_sent:1137
cluster_stats_messages_sent:2071
cluster_stats_messages_ping_received:1132
cluster_stats_messages_pong_received:934
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2071
192.168.4.51:6051> CLUSTER NODES # 查看本机信息
6a7196a77555cdf14f1a665386ff493928b31d94 192.168.4.56:[email protected] slave 2c9ee556add7e5187621efd64e4a9fc54bc99489 0 1530778521000 6 connected
bcb32a6d6b9e988789e654c32856c8c478ef253e 192.168.4.53:[email protected] master - 0 1530778520185 3 connected 10923-16383
18b86bca8fa9c9d36f3cad927a1c75c825bee9a4 192.168.4.55:[email protected] slave c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01 0 1530778521000 5 connected
2c9ee556add7e5187621efd64e4a9fc54bc99489 192.168.4.52:[email protected] master - 0 1530778521187 2 connected 5561-10922
799f99bc22d8c4a9e0fcdc98166f6e6f5e348df0 192.168.4.54:[email protected] slave bcb32a6d6b9e988789e654c32856c8c478ef253e 0 1530778520000 4 connected
c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01 192.168.4.51:[email protected] myself,master - 0 1530778519000 1 connected 0-5560
测试集群:
连接master库 存储数据,对应的从库 会自动同步数据
[[email protected] ~]# redis-cli -c -h master库ip地址 -p 对应的端口号
Master::
[[email protected] redis-4.0.8]# redis-cli -c -h 192.168.4.51 -p 6051
192.168.4.51:6051> set age 10
192.168.4.51:6051> set name aa
192.168.4.52:6052> keys *
1) "name"
Slave:
[[email protected] phpredis-2.2.4]# redis-cli -c -h 192.168.4.52 -p 6052
192.168.4.52:6052> keys *
1)"name"
选项 参数
– add-node 添加新节点(default master)
– check 对节点主机做检查
– reshard 对节点主机重新分片
– add-node --slave 添加从节点主机
– del-node 删除节点主机
集群选举测试:停止某个主库的redis服务 对应的从会自动升级主库
[[email protected] redis-4.0.8]# redis-trib.rb check 192.168.4.51:6051
M: bcb32a6d6b9e988789e654c32856c8c478ef253e 192.168.4.53:6053
slots:10923-16383 (5561 slots) master
1 additional replica(s)
S: 18b86bca8fa9c9d36f3cad927a1c75c825bee9a4 192.168.4.54:6054
slots: (0 slots) slave
replicates c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01
......................
由此可以看出4.53是master ,4.55是slave,现在我们停止53
[[email protected] ~]# redis-cli -c -h 192.168.4.53 -p 6053 shutdown
[[email protected] redis-4.0.8]# redis-trib.rb check 192.168.4.51:6051
M: 799f99bc22d8c4a9e0fcdc98166f6e6f5e348df0 192.168.4.54:6054
slots:10923-16383 (5561 slots) master
对节点主机重新分片
[[email protected] ~]# redis-trib.rb reshard 192.168.4.51:6051
How many slots do you want to move (from 1 to 16384)? 5196
What is the receiving node ID? c5f725669ad74fbf48b907b06c3b0ff6ca5eeb01
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:799f99bc22d8c4a9e0fcdc98166f6e6f5e348df0
添加主节点主机:
redis-trib.rb add-node 添加的节点:端口号 集群主节点:端口号
[[email protected] ~]# redis-trib.rb add-node 192.168.4.48:6048 192.168.4.51:6051
添加从节点
redis-trib.rb add-node --slave 从节点 ip: 端口 192.168.4.51:6351
*如果不指定主节点的 id 的话,会把新节点 随机添加为 从节点 最少的主的从
[[email protected] ~]# redis-trib.rb add-node --slave 192.168.4.51:6051 192.168.4.51:6051
删除节点主机
删除从库
[[email protected] ~]# redis-trib.rb del-node 192.168.4.51:6051 676c81e55e491ea7d4f9eb9f53d5c72abc1b8513
>> Removing node 676c81e55e491ea7d4f9eb9f53d5c72abc1b8513 from cluster 192.168.4.51:6051
>> Sending CLUSTER FORGET messages to the cluster...
>> SHUTDOWN the node.
如果要想把删除的数据节点重新加入集群需要还原数据库
[[email protected] ~]# redis-cli -h 192.168.4.51 -p 6051
192.168.4.51:6051> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:3
。。。。。。。。
192.168.4.51:6051> CLUSTER RESET
OK
192.168.4.51:6051> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
这样才可以重新添加
删除主机master
[[email protected] ~]# redis-trib.rb reshard 192.168.4.51:6051
How many slots do you want to move (from 1 to 16384)? 2000 #增加的槽数
What is the receiving node ID? 18ad59ca4d756e61a5076ea2f6a93d567e521a5c #增加到哪个节点
Source node #1:6e8a84fdd95193fd53a9386f8b24c24c4e5122ff #重哪个节点上移出
Do you want to proceed with the proposed reshard plan (yes/no)?yes
[[email protected] ~]# redis-trib.rb del-node 192.168.4.48 6e8a84fdd95193fd53a9386f8b24c24c4e5122ff
>> Removing node 6e8a84fdd95193fd53a9386f8b24c24c4e5122ff from cluster 192.168.4.48
Invalid IP or Port (given as 192.168.4.48) - use IP:Port format
以上是关于搭建Redis 集群,测试集群:对节点主机重新分片的主要内容,如果未能解决你的问题,请参考以下文章
redis集群与分片-Redis Cluster集群的搭建与实践