Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > Redis集群(cluster模式)搭建

Redis集群(cluster模式)搭建过程

作者:天空中那座城

文章介绍了Redis集群的概念、使用原因和搭建方法,Redis集群通过分区实现数据水平扩容,提供了一定的可用性,文章详细阐述了集群的连接方式,解释了如何分配节点,并提供了详细的集群搭建步骤,包括创建节点、清空数据、修改配置、启动节点、配置集群等

1、什么是集群

2、为什么使用

3、集群连接

4、redis cluster 如何分配这六个节点?

5、集群搭建

1) 通过redis中utils路径下 ./install_server.sh 执行文件创建6个不同的redis节点,端口号分别为6379、6380、6381、8362、6383、6384

2)如果各个节点原本有存储数据,则先将各个节点的数据文件清空。将6个文件夹内的文件全部删除。

清空数据目录。删除 Redis 数据目录下的所有文件。使用命令: rm -rf /path/to/redis/data/*

3)修改6个redis节点的配置文件

在 /etc/redis 文件中,找到每一个端口号对应的配置文件:

例如修改的内容如下:

bind 0.0.0.0
port 6380 # 设置成对应服务专属的端口号
daemonize yes
dbfilename "dump6380.rdb" # 设置成对应服务专属的名字
appendonly yes
cluster-enabled yes
cluster-config-file nodes-6380.conf # 设置成对应服务专属的

4)启动六个节点

[root@bogon src]# ./redis-server /etc/redis/6379.conf
[root@bogon src]# ./redis-server /etc/redis/6380.conf
[root@bogon src]# ./redis-server /etc/redis/6381.conf
[root@bogon src]# ./redis-server /etc/redis/6382.conf
[root@bogon src]# ./redis-server /etc/redis/6383.conf
[root@bogon src]# ./redis-server /etc/redis/6384.conf

5)查看各个节点是否启动成功

[root@bogon src]# ps -ef | grep redis
root        3889       1  0 09:56 ?        00:00:03 ./redis-server 0.0.0.0:6379 [cluster]
root        3895       1  0 09:56 ?        00:00:03 ./redis-server 0.0.0.0:6380 [cluster]
root        3901       1  0 09:57 ?        00:00:03 ./redis-server 0.0.0.0:6381 [cluster]
root        3907       1  0 09:57 ?        00:00:02 ./redis-server *:6382 [cluster]
root        3913       1  0 09:57 ?        00:00:02 ./redis-server 0.0.0.0:6383 [cluster]
root        3919       1  0 09:57 ?        00:00:02 ./redis-server 0.0.0.0:6384 [cluster]
root        4247    2418  0 10:22 pts/0    00:00:00 grep --color=auto redis

6)配置集群

命令格式: --cluster-replicas 1 表示为每个master创建一个slave节点

注意:这里的IP为每个节点所在机器的真实IP

[root@localhost src]# ./redis-cli --cluster create 192.168.177.128:6379 192.168.177.128:6380 192.168.177.128:6381 192.168.177.128:6382 192.168.177.128:6383 192.168.177.128:6384 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.177.128:6383 to 192.168.177.128:6379
Adding replica 192.168.177.128:6384 to 192.168.177.128:6380
Adding replica 192.168.177.128:6382 to 192.168.177.128:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379
   slots:[0-5460] (5461 slots) master
M: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380
   slots:[5461-10922] (5462 slots) master
M: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381
   slots:[10923-16383] (5461 slots) master
S: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382
   replicates ae77569d28f01657d9e3e04810e8562abdb3f5dd
S: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383
   replicates 5ccafb9ee2f223c987c740d3d8282f200e4892dd
S: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384
   replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764
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.177.128:6379)
M: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382
   slots: (0 slots) slave
   replicates ae77569d28f01657d9e3e04810e8562abdb3f5dd
S: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383
   slots: (0 slots) slave
   replicates 5ccafb9ee2f223c987c740d3d8282f200e4892dd
S: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384
   slots: (0 slots) slave
   replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7)查看主从关系

命令格式:redis-cli --cluster check 【本台redis自己的IP】:【本台redis自己的端口】

[root@localhost src]# ./redis-cli --cluster check 192.168.177.128:6379
192.168.177.128:6379 (ae77569d...) -> 0 keys | 5461 slots | 1 slaves.
192.168.177.128:6381 (dcd5d506...) -> 0 keys | 5461 slots | 1 slaves.
192.168.177.128:6380 (5ccafb9e...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.177.128:6379)
M: ae77569d28f01657d9e3e04810e8562abdb3f5dd 192.168.177.128:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: dcd5d5066cd9aa5e3f2830ce985395acb978d764 192.168.177.128:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 5ccafb9ee2f223c987c740d3d8282f200e4892dd 192.168.177.128:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 8849bb5437931a3fd8510ffa4c0f755f4e26d1eb 192.168.177.128:6382
   slots: (0 slots) slave
   replicates ae77569d28f01657d9e3e04810e8562abdb3f5dd
S: b00800f65775c52519bd6e9f398f916ffe46fed8 192.168.177.128:6383
   slots: (0 slots) slave
   replicates 5ccafb9ee2f223c987c740d3d8282f200e4892dd
S: 6e7a1ae63691267b000363f6528b620a656e03e7 192.168.177.128:6384
   slots: (0 slots) slave
   replicates dcd5d5066cd9aa5e3f2830ce985395acb978d764
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看下图即可发现,一主对一从:

8) 主节点数据写测试

加参数 -c ,防止路由失效

[root@bogon src]# ./redis-cli -p 6381 -c
127.0.0.1:6381> get name
-> Redirected to slot [5798] located at 192.168.109.149:6380
(nil)
192.168.109.149:6380> get name
(nil)
192.168.109.149:6380>

9)从节点读数据测试

[root@bogon src]# ./redis-cli -p 6379 -c
127.0.0.1:6379> set name zhangsan
-> Redirected to slot [5798] located at 192.168.109.149:6380
OK
192.168.109.149:6380> get name
"zhangsan"
192.168.109.149:6380>
[root@bogon src]# ./redis-cli -p 6383 -c
127.0.0.1:6383> get name
-> Redirected to slot [5798] located at 192.168.109.149:6380
"zhangsan"
192.168.109.149:6380>
[root@bogon src]# ./redis-cli -p 6383 -c
127.0.0.1:6383> readonly
OK
127.0.0.1:6383> get name
"zhangsan"
127.0.0.1:6383>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文