05.Redis的Cluster集群实现

基于Redis 5 以上版本的 redis cluster 部署

  • 生产环境:6台服务器,分别是三组master/slave,适用于生产环境

在这里插入图片描述

1.环境准备

节点名称ip地址
node110.0.0.11
node210.0.0.12
node310.0.0.13
node410.0.0.14
node510.0.0.15
node610.0.0.16

2.相关配置

2.1.修改配置文件

root@redis1:~# vi /apps/redis/etc/redis.conf

bind 0.0.0.0 -::1   #本机ip
masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后 redis 进程会有cluster标识
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态数据文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可用

2.2.拷贝配置文件到其他5台机器

  • 我通过编译安装的6台redis,所以配置文件一样
rsync -a /apps/redis/etc/redis.conf 10.0.0.12:/apps/redis/etc/redis.conf
rsync -a /apps/redis/etc/redis.conf 10.0.0.13:/apps/redis/etc/redis.conf
rsync -a /apps/redis/etc/redis.conf 10.0.0.14:/apps/redis/etc/redis.conf
rsync -a /apps/redis/etc/redis.conf 10.0.0.15:/apps/redis/etc/redis.conf
rsync -a /apps/redis/etc/redis.conf 10.0.0.16:/apps/redis/etc/redis.conf

2.3.所有机器检查一下修改的配置文件是否修改正确

grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "bind" 
grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "masterauth" 
grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "requirepass" 
grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-enabled" 
grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-config-file" 
grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-require-full-coverage" 
  • 执行结果如下

root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "bind" 
bind 0.0.0.0 -::1
root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "masterauth" 
masterauth 123456
root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "requirepass" 
requirepass 123456
root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-enabled" 
cluster-enabled yes
root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-config-file"
cluster-config-file nodes-6379.conf
root@redis3:~# grep -Ev  "#|^$" /apps/redis/etc/redis.conf |grep "cluster-require-full-coverage" 
cluster-require-full-coverage no

2.4.6台启动redis服务,并查看进程

systemctl restart redis
ps aux |grep redis 

2.5.6台确认是否多一个cluster标签

root@redis1:~# ps aux |grep redis
redis       8882  0.0  0.7  57956  7444 ?        Ssl  15:34   0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root        8899  0.0  0.0   6432   656 pts/0    S+   15:34   0:00 grep --color=auto redis

2.6 检查是否配置成功

# 输入ping,如果每台redis都返回pong,说明配置成功
redis-cli -h 10.0.0.16 -p 6379 -a '123456'
redis-cli -h 10.0.0.15 -p 6379 -a '123456'
redis-cli -h 10.0.0.14 -p 6379 -a '123456'
redis-cli -h 10.0.0.13 -p 6379 -a '123456'
redis-cli -h 10.0.0.12 -p 6379 -a '123456'
redis-cli -h 10.0.0.11 -p 6379 -a '123456'

root@redis4:~# redis-cli -h 10.0.0.14 -p 6379 -a '123456'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.14:6379> ping
PONG
10.0.0.14:6379> 

3.创建集群

3.1创建集群

root@redis1:~# redis-cli -a 123456 --cluster create 10.0.0.11:6379 10.0.0.12:6379 10.0.0.13:6379 10.0.0.14:6379 10.0.0.15:6379 10.0.0.16:6379 --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 10.0.0.15:6379 to 10.0.0.11:6379
Adding replica 10.0.0.16:6379 to 10.0.0.12:6379
Adding replica 10.0.0.14:6379 to 10.0.0.13:6379
M: 849866f5d6959ef1c3f6d416f2f0db7345399f9c 10.0.0.11:6379
   slots:[0-5460] (5461 slots) master
M: d48cbb52775fd99f6c17a5e6d90fa21057c1edd6 10.0.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: 9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15 10.0.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: 4fd9cd7e5cb078962ae2edd03444d50ed76ba41d 10.0.0.14:6379
   replicates 9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15
S: fbb2a0af5cadb72899284feaa4e11cd1cd922029 10.0.0.15:6379
   replicates 849866f5d6959ef1c3f6d416f2f0db7345399f9c
S: 2a2c22e17408dee097ff99c285985f2bdeaf2e53 10.0.0.16:6379
   replicates d48cbb52775fd99f6c17a5e6d90fa21057c1edd6
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 10.0.0.11:6379)
M: 849866f5d6959ef1c3f6d416f2f0db7345399f9c 10.0.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 4fd9cd7e5cb078962ae2edd03444d50ed76ba41d 10.0.0.14:6379
   slots: (0 slots) slave
   replicates 9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15
M: 9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15 10.0.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: d48cbb52775fd99f6c17a5e6d90fa21057c1edd6 10.0.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: fbb2a0af5cadb72899284feaa4e11cd1cd922029 10.0.0.15:6379
   slots: (0 slots) slave
   replicates 849866f5d6959ef1c3f6d416f2f0db7345399f9c
S: 2a2c22e17408dee097ff99c285985f2bdeaf2e53 10.0.0.16:6379
   slots: (0 slots) slave
   replicates d48cbb52775fd99f6c17a5e6d90fa21057c1edd6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.``

3.2验证集群

3.2.1查看主从状态

  • 主节点:role:master
root@redis1:~# redis-cli -c -h 10.0.0.11 -p 6379 -a '123456'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.11:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:18573bbf4a6219fa15f28781c6f17c16f63f98c0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:8064
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:8064
  • 从节点:role:slave
root@redis4:~# redis-cli -c -h 10.0.0.14  -p 6379 -a '123456'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.14:6379> info replication
# Replication
role:slave
master_host:10.0.0.13
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_read_repl_offset:14
slave_repl_offset:14
master_link_down_since_seconds:-1
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:ecfb75ada6e667d04bf1662b116bde35341a97d3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3.2.2查看指定master节点的slave节点信息(查看对应关系)

root@redis1:~# redis-cli -c -h 10.0.0.11 -p 6379 -a '123456'
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.11:6379> cluster nodes
849866f5d6959ef1c3f6d416f2f0db7345399f9c 10.0.0.11:6379@16379 myself,master - 0 1670256170000 1 connected 0-5460
4fd9cd7e5cb078962ae2edd03444d50ed76ba41d 10.0.0.14:6379@16379 slave 9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15 0 1670256171000 3 connected
9c9bed986328aeb72f6e9ecabb6fe76d8d86ae15 10.0.0.13:6379@16379 master - 0 1670256169135 3 connected 10923-16383
d48cbb52775fd99f6c17a5e6d90fa21057c1edd6 10.0.0.12:6379@16379 master - 0 1670256171000 2 connected 5461-10922
fbb2a0af5cadb72899284feaa4e11cd1cd922029 10.0.0.15:6379@16379 slave 849866f5d6959ef1c3f6d416f2f0db7345399f9c 0 1670256172218 1 connected
2a2c22e17408dee097ff99c285985f2bdeaf2e53 10.0.0.16:6379@16379 slave d48cbb52775fd99f6c17a5e6d90fa21057c1edd6 0 1670256171198 2 connected
10.0.0.11:6379> 

3.2.3验证集群状态

root@redis2:~# redis-cli -h 10.0.0.11 -p 6379 -a 123456 CLUSTER INFO
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:709
cluster_stats_messages_pong_sent:777
cluster_stats_messages_sent:1486
cluster_stats_messages_ping_received:772
cluster_stats_messages_pong_received:709
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1486
total_cluster_links_buffer_limit_exceeded:0
  • 查看任意节点的集群状态
root@redis3:~# redis-cli -a 123456 --cluster info 10.0.0.12:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.12:6379 (d48cbb52...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.11:6379 (849866f5...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.13:6379 (9c9bed98...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值