第八章、Redis Cluster 集群模式

Redis Cluster 集群模式

Redis 的 Cluster 集群模式, 启动还挺简单,主要有两步

1、配置文件

2、启动验证

集群规划

根据官方推荐,集群部署至少要 3 台以上的master节点,最好使用 3 主 3 从六个节点的模式。

节点                                            服务器IP                           配置文件            监听端口

cluster-master-1      192.168.3.71 /etc/redis/redis.conf 6379

cluster-master-2      192.168.3.72 /etc/redis/redis.conf 6379

cluster-master-3      192.168.3.73 /etc/redis/redis.conf 6379

cluster-slave-1      192.168.3.71 /etc/redis/redis6380.conf 6380

cluster-slave-2      192.168.3.72 /etc/redis/redis6380.conf 6380

cluster-slave-3      192.168.3.73 /etc/redis/redis6380.conf 6380

配置文件设置:

三台服务器创建一下数据存放目录:

mkdir -pv /redis/{6379,6380}

其中6379目录相当于主节点存放目录;6380目录相当于从节点存放目录

配置文件内容如下(记得复制6份并替换端口号和工作目录)

#94行左右 保护模式
protected-mode no
#257行左右 后台运行
daemonize yes
#290行左右  设置进程ID和日志位置,根据你启用的节点监听端口来命名保持一致
pidfile /var/run/redis_6380.pid
logfile /var/log/redis/redis_6380.log
#455行左右  修改工作目录,根据你启用的节点监听端口来命名保持一致
dir /redis/6380
# 900行左右设置认证密码(可选)
requirepass longtao
#1255行左右  启用AOF模式
appendonly yes
#1380行开始,后面需要开启这三行参数
# 启用集群模式
cluster-enabled yes 
# 根据你启用的节点监听端口来命名保持一致,这个是用来保存其他节点的名称,状态等信息的
cluster-config-file nodes_6380.conf 
# 超时时间
cluster-node-timeout 5000

启动集群

每台服务器上启动 redis 节点

redis-server redis6379.conf
redis-server redis6380.conf

每台Redis 启动结果.png

然后启动集群

# -a 后面跟着是认证密码,根据需要设置

# --cluster-replicas 1 命令的意思是创建master的时候同时创建一个slave

redis-cli -a longtao --cluster create 192.168.3.71:6379 192.168.3.72:6379 192.168.3.73:6379 192.168.3.71:6380 192.168.3.72:6380 192.168.3.73:6380 --cluster-replicas 1

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> 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.3.72:6380 to 192.168.3.71:6379

Adding replica 192.168.3.73:6380 to 192.168.3.72:6379

Adding replica 192.168.3.71:6380 to 192.168.3.73:6379

M: 234bc8723b957d3d4a91677da0f64600a7761f7f 192.168.3.71:6379

   slots:[0-5460] (5461 slots) master

M: 359adb45a35ba6ccc5a76ff303c2b2ed6a92345b 192.168.3.72:6379

   slots:[5461-10922] (5462 slots) master

M: 8807ca47a84e1518b0cc61cb10f7df1cf826f104 192.168.3.73:6379

   slots:[10923-16383] (5461 slots) master

S: 48fd8baec6d1776de52983d2546d62b83daea52f 192.168.3.71:6380

   replicates 8807ca47a84e1518b0cc61cb10f7df1cf826f104

S: c42d808d7cbb389e5e9298a99e50aa261944c9b6 192.168.3.72:6380

   replicates 234bc8723b957d3d4a91677da0f64600a7761f7f

S: 4f57540795a2d4648de0dc0d769fcba2ed30ffc3 192.168.3.73:6380

   replicates 359adb45a35ba6ccc5a76ff303c2b2ed6a92345b

Can I set the above configuration? (type 'yes' to accept): yes

# 这里有个要手动输入 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.3.71:6379)

M: 234bc8723b957d3d4a91677da0f64600a7761f7f 192.168.3.71:6379

   slots:[0-5460] (5461 slots) master

   1 additional replica(s)

S: 4f57540795a2d4648de0dc0d769fcba2ed30ffc3 127.0.0.1:6380

   slots: (0 slots) slave

   replicates 359adb45a35ba6ccc5a76ff303c2b2ed6a92345b

M: 8807ca47a84e1518b0cc61cb10f7df1cf826f104 192.168.3.73:6379

   slots:[10923-16383] (5461 slots) master

S: 4f57540795a2d4648de0dc0d769fcba2ed30ffc3 192.168.3.73:6380

   slots: (0 slots) slave

   replicates 359adb45a35ba6ccc5a76ff303c2b2ed6a92345b

M: 359adb45a35ba6ccc5a76ff303c2b2ed6a92345b 192.168.3.72:6379

   slots:[5461-10922] (5462 slots) master

   2 additional replica(s)

S: c42d808d7cbb389e5e9298a99e50aa261944c9b6 192.168.3.72:6380

   slots: (0 slots) slave

   replicates 234bc8723b957d3d4a91677da0f64600a7761f7f

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

如图:

Redis-cluster运行结果.png

数据验证

# 注意 集群模式下要带参数 -c,表示集群,否则不能正常存取数据!!!

#  如果设置过认证密码需要登录时加入密码: -a longtao 

# redis-cli -h 192.168.3.73 -p 6379 -a longtao -c

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# 设置键值对

192.168.3.73:6379> set long tao

-> Redirected to slot [5598] located at 192.168.3.72:6379

OK

# 这可以看到集群的特点:把数据存到计算得出的 slot,这里还自动跳到了 192.168.3.72

192.168.3.72:6379> get long

"tao"

192.168.3.72:6379>

测试结果如图:

cluster-测试成功结果.png

换成其他节点,也测试成功

[root@CentOS7 redis]# redis-cli -h 192.168.3.71 -p 6379 -a longtao -c

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.3.71:6379> get long

-> Redirected to slot [5598] located at 192.168.3.72:6379

"tao"

192.168.3.72:6379>

测试结果如图:

P107 2021-09-06_131838.png

本文内容参考自 知乎,作者为 猴哥一一,原文网址:https://zhuanlan.zhihu.com/p/145186839

版权声明:
作者:WaterBear
链接:https://l-t.top/1971.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>