草庐IT

【Redis】Cluster集群

Janzen_Q 2023-11-17 原文

一、Redis Cluster 工作原理

在引入哨兵机制后,解决了Redis主从架构Master故障时的主从切换问题,保证了Redis服务可用性。但依旧无法解决单机节点出现的写入性能瓶颈(网卡速率、单机内存容量、并发数量)

1、早期为解决单机性能瓶颈问题采用的解决方案:

1、客户端分片:由客户端程序进行读写key的redis节点判断和分配,并且由客户端自行处理读写请求分配、高可用管理及故障转移操作

2、proxy代理模式:引入第三方代理程序,客户端通过连接proxy代理服务器对数据进行读写,由proxy程序进行读写判断分配,并对集群节点进行管理。但导致proxy又出现单点故障风险,并增加了一层数据处理环节

redis3.0 之后官方推出了无中心化的Cluster集群机制,集群中的每个节点单独保持当前节点数据和集群状态信息,每个节点需要和其余全部节点保持连接。

2、Redis Cluster工作特点

1、Redis cluster集群中的各个节点之间(采用ping机制)进行互联

2、集群中的某个节点失效,是有整个集群中超过半数的节点监测失效(sdown),才会真正判定为节点不可用(odown)

3、客户端无需配置连接proxy即可直连redis,应用程序需配置集群的所有redis节点IP

4、redis cluster将集群中所有的节点平均映射到0-16383(16384个)slot槽位上,将数据库的读写全部映射到对应的节点上操作,因此每增加n个集群主节点就能对redis进行n倍扩展,每个主节点将分担16384/n个slot

5、redis cluster预先将16384个槽位分配至集群中的主节点,当接收到对redis的写入请求时,会通过 CRC16(key)%16384 计算出当前key的所属slot编号,从而将数据写入到对应的主节点上,从而解决单机产生的性能瓶颈

3、Redis Cluster 架构设计

Redis Cluster 基本架构

 Redis Cluster 主从架构

为解决Cluster集群中各Master节点的高可用性,可采用Cluster主从架构部署,将部分加入集群的节点设置为Slave角色,从而解决Master节点的高可用性

Cluster可自行实现类似哨兵机制的每组Master-Slave的主从切换,无需额外配置哨兵机制。并且支持每组节点的 1-n 模式(不支持1-n-n)

 Redis Cluster 部署架构设计

  

3、Redis Cluster局限性

1、大多数时候客户端连接性能会“降低”,由于cluster在读写数据时需要先进行solt计算并重定向访问到对应节点上,造成了数据处理“延迟性”。

2、部分命令无法跨节点使用:mget、keys、scan、flush、sinter 等

3、客户端维护复杂:SDK 和 应用自身消耗增加(更多的数据连接池)

4、不支持多数据库:cluster 模式下仅支持使用db0

5、复制仅支持一层:不支持级联复制

6、key事务、Lua 的支持有限:key操作必须在同一个节点上,事务和Lua无法跨节点使用

 

二、Redis Cluster 相关配置详解

################################ REDIS CLUSTER  ###############################

 

三、Redis Cluster部署

0、基础环境准备

所有redis节点采用相同硬件配置,相同密码,清空所有数据

  IP 角色 slot
node1 10.0.0.20 M1 0-5460
node2 10.0.0.21 M2 5461-10922
node3 10.0.0.22 M3 10923-16383
node4 10.0.0.23 集群扩容缩容备用(M4)  
node5 10.0.0.30 S1  
node6 10.0.0.31 S2  
node7 10.0.0.32 S3  
node8 10.0.0.33 集群扩容缩容备用(S4)  

 

 

 

 

 

 

 

初始化安装6台 redis服务,并启用Cluster模式

[root@Redis-Ubuntu1804-node1:~]# apt install redis -y
[root@Redis-Ubuntu1804-node1:~]# sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth redis' -e '/# requirepass/a requirepass redis' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file/a cluster-config-file nodes-6379.conf' -e '/# cluster-require-full-coverage yes/a cluster-require-full-coverage yes' /etc/redis/redis.conf
[root@Redis-Ubuntu1804-node1:~]# systemctl restart redis
[root@Redis-Ubuntu1804-node1:~]# ss -ntl
State             Recv-Q             Send-Q                          Local Address:Port                           Peer Address:Port             
LISTEN            0                  128                             127.0.0.53%lo:53                                  0.0.0.0:*                
LISTEN            0                  128                                   0.0.0.0:22                                  0.0.0.0:*                
LISTEN            0                  128                                 127.0.0.1:6010                                0.0.0.0:*                
LISTEN            0                  128                                   0.0.0.0:16379                               0.0.0.0:*                
LISTEN            0                  128                                   0.0.0.0:6379                                0.0.0.0:*                
LISTEN            0                  128                                      [::]:22                                     [::]:*                
LISTEN            0                  128                                     [::1]:6010                                   [::]:*                
[root@Redis-Ubuntu1804-node1:~]# ps -ef | grep redis
redis      2098      1  0 01:38 ?        00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root       2112   1236  0 01:39 pts/0    00:00:00 grep --color=auto redis
[root@Redis-Ubuntu1804-node1:~]# 

1、基本模式手动部署

基本步骤

1、各节点服务器上安装redis服务,启用Cluster配置

2、通过meet 将节点添加至集群中

3、为各Master节点分配槽位 0~16383 ,必须全部分配完成,集群才可用

4、指定各节点主从关系

1.1、查看当前节点状态信息

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster nodes
98bb2740b622645ff51fd07bc994bb22e94feaaa :6379@16379 myself,master - 0 0 0 connected
[root@Redis-Ubuntu1804-node1:~]# 

 

1.2、将所有节点加入到当前集群中 

Cluster节点操作相关命令

1、加入节点:将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。

1)CLUSTER MEET <ip:port>

2、移除节点:

1)CLUSTER FORGET <node_id>

2)、redis-trib.rb del-node <ip> <port> <node_id>

3、设置主从节点:

CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。  

4、节点数据备份到硬盘:

CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.20 6379
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.20 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.21 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.22 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.30 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.31 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster meet 10.0.0.32 6379
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster nodes
98bb2740b622645ff51fd07bc994bb22e94feaaa 10.0.0.20:6379@16379 myself,master - 0 1681926433000 1 connected
9fe09d53d884c6c99c926a0e7213cf5386987ebf 10.0.0.32:6379@16379 master - 0 1681926434205 5 connected
55fd3a5d21e026b6503a89f1d88996b2a67c314a 10.0.0.31:6379@16379 master - 0 1681926435212 4 connected
6717c6b5a6170963ac251a1f14b0c600e917aa26 10.0.0.30:6379@16379 master - 0 1681926433000 3 connected
d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed 10.0.0.21:6379@16379 master - 0 1681926434000 0 connected
bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2 10.0.0.22:6379@16379 master - 0 1681926433199 2 connected
[root@Redis-Ubuntu1804-node1:~]# 

 

1.3、查看当前集群信息

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:70
cluster_stats_messages_pong_sent:89
cluster_stats_messages_meet_sent:6
cluster_stats_messages_sent:165
cluster_stats_messages_ping_received:88
cluster_stats_messages_pong_received:76
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:165
[root@Redis-Ubuntu1804-node1:~]# 

 

1.4、为集群节点分配槽位

Cluster 槽位相关命令
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。

CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。

CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。

CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。

CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。

CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。

CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 

使用命令 redis-cli -h <目标节点IP> -p <目标节点端口> -a <目标节点密码> --no-auth-warning cluster addslots <要分配的槽位号> 进行槽位分配

[root@Client-Ubuntu-1804-250:~/script/redis]# cat add_slots.sh 
#!/bin/bash
#
#********************************************************************
#Author:                janzen
#Date:                  2023-04-20
#FileName:             add_slots.sh
#Description:          The test script
#Copyright (C):        2023 All rights reserved
#********************************************************************
host=$1
port=$2
start=$3
end=$4
passwd=redis

for slot in `seq $start $end`; do
    redis-cli -h $host -p $port -a $passwd --no-auth-warning cluster addslots $slot &&
    echo $slot add to node $host:$port
done
[root@Client-Ubuntu-1804-250:~/script/redis]# 
[root@Client-Ubuntu-1804-250:~/script/redis]# ./add_slots.sh 10.0.0.20 6379 0 5460 >> addslot.log
[root@Client-Ubuntu-1804-250:~/script/redis]# ./add_slots.sh 10.0.0.21 6379 5461 10922 >> addslot.log
[root@Client-Ubuntu-1804-250:~/script/redis]# ./add_slots.sh 10.0.0.22 6379 10923 16383 >> addslot.log

 

##查看当前集群信息,槽位已完成分配
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster nodes 98bb2740b622645ff51fd07bc994bb22e94feaaa 10.0.0.20:6379@16379 myself,master - 0 1681929286000 1 connected 0-5460 55fd3a5d21e026b6503a89f1d88996b2a67c314a 10.0.0.31:6379@16379 master - 0 1681929287000 4 connected 9fe09d53d884c6c99c926a0e7213cf5386987ebf 10.0.0.32:6379@16379 master - 0 1681929287000 5 connected 6717c6b5a6170963ac251a1f14b0c600e917aa26 10.0.0.30:6379@16379 master - 0 1681929288247 3 connected d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed 10.0.0.21:6379@16379 master - 0 1681929286000 0 connected 5461-10922 bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2 10.0.0.22:6379@16379 master - 0 1681929289256 2 connected 10923-16383 [root@Redis-Ubuntu1804-node1:~]#

1.5、创建 Master-Slave 主从关系

使用命令 redis-cli -h <从节点IP> -p <从节点端口> -a <从节点密码> --no-auth-warning cluster replicate <主节点node id> 

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.30 cluster replicate 98bb2740b622645ff51fd07bc994bb22e94feaaa
OK

##观察主节点上的replication信息
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.30,port=6379,state=online,offset=350,lag=1
master_replid:6a823070fcbc13b4f4eb7b5d13bf1d2f4625a1a2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:350
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:350
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.20 cluster nodes
98bb2740b622645ff51fd07bc994bb22e94feaaa 10.0.0.20:6379@16379 myself,master - 0 1681929989000 1 connected 0-5460
55fd3a5d21e026b6503a89f1d88996b2a67c314a 10.0.0.31:6379@16379 master - 0 1681929992372 4 connected
9fe09d53d884c6c99c926a0e7213cf5386987ebf 10.0.0.32:6379@16379 master - 0 1681929991000 5 connected
6717c6b5a6170963ac251a1f14b0c600e917aa26 10.0.0.30:6379@16379 slave 98bb2740b622645ff51fd07bc994bb22e94feaaa 0 1681929992000 3 connected
d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed 10.0.0.21:6379@16379 master - 0 1681929989351 0 connected 5461-10922
bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2 10.0.0.22:6379@16379 master - 0 1681929991364 2 connected 10923-16383

##观察从节点上的replication信息
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.30 info Replication
# Replication
role:slave
master_host:10.0.0.20
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:462
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6a823070fcbc13b4f4eb7b5d13bf1d2f4625a1a2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:462
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:462
[root@Redis-Ubuntu1804-node1:~]# 

 

 继续完成主从组配置

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.31 cluster replicate d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.32 cluster replicate bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2
OK

##观察集群节点状态 [root@Redis
-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.30 cluster nodes 9fe09d53d884c6c99c926a0e7213cf5386987ebf 10.0.0.32:6379@16379 slave bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2 0 1681930655876 5 connected 98bb2740b622645ff51fd07bc994bb22e94feaaa 10.0.0.20:6379@16379 master - 0 1681930654870 1 connected 0-5460 55fd3a5d21e026b6503a89f1d88996b2a67c314a 10.0.0.31:6379@16379 slave d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed 0 1681930656886 4 connected bdec3c92bcaa00c8ad1850cd477eb0a8f8a28aa2 10.0.0.22:6379@16379 master - 0 1681930655000 2 connected 10923-16383 6717c6b5a6170963ac251a1f14b0c600e917aa26 10.0.0.30:6379@16379 myself,slave 98bb2740b622645ff51fd07bc994bb22e94feaaa 0 1681930654000 3 connected d9c17ded7fb139f9eb7fd41e476bb8e17c8b9bed 10.0.0.21:6379@16379 master - 0 1681930653863 0 connected 5461-10922

  

1.6、验证Cluster集群访问

[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.20 set node1 10.0.0.20
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.21 set node2 10.0.0.21
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.22 set node3 10.0.0.22
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.30 set node5 10.0.0.24
OK
[root@Redis-Ubuntu1804-node1:~]# 
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.31 get node1
"10.0.0.20"
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.31 get node2
"10.0.0.21"
[root@Redis-Ubuntu1804-node1:~]# redis-cli -c -a redis -h 10.0.0.21 get node6
"10.0.0.31"
[root@Redis-Ubuntu1804-node1:~]# 

 

 

2、基于 Redis 5 Cluster集群部署

--cluster 常用命令

[root@Redis-Ubuntu1804-node3:~]# redis-cli -a redis --cluster help
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN    ##创建集群
                 --cluster-replicas <arg>    ##指定每个集群的slave节点数,通常为1
  check          host:port    ##检查集群信息
                 --cluster-search-multiple-owners
  info           host:port    ##查看集群节点基本信息
  fix            host:port    ##修复集群
                 --cluster-search-multiple-owners
  reshard        host:port    ##在线热机迁移指定的slots数据
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port    ##平衡集群在线节点的slots数量
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port    ##添加新的节点到集群中
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id    ##删除集群中的节点
  call           host:port command arg arg .. arg    ##在集群上的所有节点执行命令
  set-timeout    host:port milliseconds    ##设置节点超时时间
  import         host:port    ##导入外部redis数据到当前集群
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

 

2.0、基本环境准备

[root@Redis-Ubuntu1804-node4:~]# redis-cli -a redis --version
redis-cli 5.0.14
[root@Redis-Ubuntu1804-node4:~]# redis-cli -a redis --no-auth-warning cluster nodes
e603091426e2ce29629ca51fb5e7dafb2f0c9524 :6379@16379 myself,master - 0 0 0 connected
[root@Redis-Ubuntu1804-node4:~]# 

 

2.1、创建Cluster集群

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --cluster create 10.0.0.20:6379 10.0.0.21:6379 10.0.0.22:6379 10.0.0.30:6379 10.0.0.31:6379 10.0.0.32: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.31:6379 to 10.0.0.20:6379
Adding replica 10.0.0.32:6379 to 10.0.0.21:6379
Adding replica 10.0.0.30:6379 to 10.0.0.22:6379
M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379
   slots:[0-5460] (5461 slots) master
M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379
   slots:[5461-10922] (5462 slots) master
M: 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379
   slots:[10923-16383] (5461 slots) master
S: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379
   replicates 07a36af61eb1f887419f2266b2360b4f795bb7a3
S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379
   replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518
S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379
   replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f
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.20:6379)
M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379
   slots: (0 slots) slave
   replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f
S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379
   slots: (0 slots) slave
   replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518
S: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379
   slots: (0 slots) slave
   replicates 07a36af61eb1f887419f2266b2360b4f795bb7a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@Redis-Ubuntu1804-node1:~]# 

 

2.2、查看集群状态

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379@16379 master - 0 1681998239529 3 connected 10923-16383
a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379@16379 master - 0 1681998238520 2 connected 5461-10922
6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379@16379 slave a4b387d82ac06e5cbf212e48c781e1c27b50320f 0 1681998237000 6 connected
60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379@16379 slave 6b142fe3438c3ea488c34366e5ea5a298f89f518 0 1681998238000 5 connected
5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379@16379 slave 07a36af61eb1f887419f2266b2360b4f795bb7a3 0 1681998237510 4 connected
6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379@16379 myself,master - 0 1681998236000 1 connected 0-5460
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis 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:290
cluster_stats_messages_pong_sent:282
cluster_stats_messages_sent:572
cluster_stats_messages_ping_received:277
cluster_stats_messages_pong_received:290
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:572
[root@Redis-Ubuntu1804-node1:~]# 
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning --cluster info 10.0.0.20:6379
10.0.0.20:6379 (6b142fe3...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.22:6379 (07a36af6...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.21:6379 (a4b387d8...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@Redis-Ubuntu1804-node1:~]# 
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning --cluster check 10.0.0.20:6379
10.0.0.20:6379 (6b142fe3...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.22:6379 (07a36af6...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.21:6379 (a4b387d8...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.20:6379)
M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379
   slots: (0 slots) slave
   replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f
S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379
   slots: (0 slots) slave
   replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518
S: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379
   slots: (0 slots) slave
   replicates 07a36af61eb1f887419f2266b2360b4f795bb7a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

2.3、验证集群写入

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster nodes
07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379@16379 master - 0 1681998730094 3 connected 10923-16383
a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379@16379 master - 0 1681998729087 2 connected 5461-10922
6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379@16379 slave a4b387d82ac06e5cbf212e48c781e1c27b50320f 0 1681998728000 6 connected
60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379@16379 slave 6b142fe3438c3ea488c34366e5ea5a298f89f518 0 1681998728079 5 connected
5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379@16379 slave 07a36af61eb1f887419f2266b2360b4f795bb7a3 0 1681998729000 4 connected
6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379@16379 myself,master - 0 1681998726000 1 connected 0-5460
#写入 key1:value1
###经过slot计算 key1 不属于当前节点

[root@Redis
-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning set key1 value1 (error) MOVED 9189 10.0.0.21:6379
##计算 key 值所属的slot号 [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster keyslot key1 (integer) 9189
##结合 nodes 信息,切换到对应节点上进行写入 [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.21 --no-auth-warning set key1 value1 OK
##get命令也只可查看当前节点上的数据 [root@Redis
-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning get key1 (error) MOVED 9189 10.0.0.21:6379 [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.21 --no-auth-warning get key1 "value1"
##可以用 -c 参数通过连接到集群进行数据操作,就无需反复切换节点(key1、key4 不在当前节点所属的slot范围,出现错误提示)
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster keyslot key1
(integer) 9189
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster keyslot key2
(integer) 4998
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster keyslot key3
(integer) 935
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning cluster keyslot key4
(integer) 13120
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -h 10.0.0.21 --no-auth-warning set key1 value1
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning set key1 value1
(error) MOVED 9189 10.0.0.21:6379
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning set key2 value2
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning set key3 value3
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis --no-auth-warning set key4 value4
(error) MOVED 13120 10.0.0.22:6379
[root@Redis-Ubuntu1804-node1:~]#

##使用 -c 参数后执行成功
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning set key1 value1
OK
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning set key4 value4
OK
[root@Redis-Ubuntu1804-node1:~]#

 

2.4、模拟故障验证集群故障切换

启用脚本模拟客户端数据读写

故障前集群信息状态

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning cluster nodes
07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379@16379 master - 0 1681999895495 3 connected 10923-16383
a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379@16379 master - 0 1681999896502 2 connected 5461-10922
6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379@16379 slave a4b387d82ac06e5cbf212e48c781e1c27b50320f 0 1681999894488 6 connected
60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379@16379 slave 6b142fe3438c3ea488c34366e5ea5a298f89f518 0 1681999895000 5 connected
5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379@16379 slave 07a36af61eb1f887419f2266b2360b4f795bb7a3 0 1681999892000 4 connected
6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379@16379 myself,master - 0 1681999893000 1 connected 0-5460
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster info 10.0.0.20:6379
10.0.0.20:6379 (6b142fe3...) -> 97 keys | 5461 slots | 1 slaves.
10.0.0.22:6379 (07a36af6...) -> 100 keys | 5461 slots | 1 slaves.
10.0.0.21:6379 (a4b387d8...) -> 92 keys | 5462 slots | 1 slaves.
[OK] 289 keys in 3 masters.
0.02 keys per slot on average.
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster check 10.0.0.20:6379
10.0.0.20:6379 (6b142fe3...) -> 99 keys | 5461 slots | 1 slaves.
10.0.0.22:6379 (07a36af6...) -> 103 keys | 5461 slots | 1 slaves.
10.0.0.21:6379 (a4b387d8...) -> 94 keys | 5462 slots | 1 slaves.
[OK] 296 keys in 3 masters.
0.02 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.20:6379)
M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379
   slots: (0 slots) slave
   replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f
S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379
   slots: (0 slots) slave
   replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518
S: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379
   slots: (0 slots) slave
   replicates 07a36af61eb1f887419f2266b2360b4f795bb7a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@Redis-Ubuntu1804-node1:~]# 

 

停用 10.0.0.22 节点

[root@Redis-Ubuntu1804-node3:~]# systemctl stop redis_6379.service 
[root@Redis-Ubuntu1804-node3:~]# 

 

观察故障期间集群状态及日志

##10.0.0.22节点标记 faild,并提升它的从节点 10.0.0.30 为master
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster check 10.0.0.20:6379 Could not connect to Redis at 10.0.0.22:6379: Connection refused *** WARNING: 10.0.0.30:6379 claims to be slave of unknown node ID 07a36af61eb1f887419f2266b2360b4f795bb7a3. 10.0.0.20:6379 (6b142fe3...) -> 131 keys | 5461 slots | 1 slaves. 10.0.0.21:6379 (a4b387d8...) -> 126 keys | 5462 slots | 1 slaves. [OK] 257 keys in 2 masters. 0.02 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.20:6379) M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379 slots: (0 slots) slave replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379 slots: (0 slots) slave replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518 S: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379 slots: (0 slots) slave replicates 07a36af61eb1f887419f2266b2360b4f795bb7a3 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [ERR] Not all 16384 slots are covered by nodes. [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster info 10.0.0.20:6379 Could not connect to Redis at 10.0.0.22:6379: Connection refused *** WARNING: 10.0.0.30:6379 claims to be slave of unknown node ID 07a36af61eb1f887419f2266b2360b4f795bb7a3. 10.0.0.20:6379 (6b142fe3...) -> 131 keys | 5461 slots | 1 slaves. 10.0.0.21:6379 (a4b387d8...) -> 126 keys | 5462 slots | 1 slaves. [OK] 257 keys in 2 masters. 0.02 keys per slot on average. [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning cluster nodes 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379@16379 master,fail - 1682000024732 1682000022415 3 disconnected a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379@16379 master - 0 1682000042000 2 connected 5461-10922 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379@16379 slave a4b387d82ac06e5cbf212e48c781e1c27b50320f 0 1682000040567 6 connected 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379@16379 slave 6b142fe3438c3ea488c34366e5ea5a298f89f518 0 1682000042582 5 connected 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379@16379 master - 0 1682000041575 7 connected 10923-16383 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379@16379 myself,master - 0 1682000041000 1 connected 0-5460 [root@Redis-Ubuntu1804-node1:~]#
17519:M 20 Apr 2023 22:14:01.600 * Marking node 07a36af61eb1f887419f2266b2360b4f795bb7a3 as failing (quorum reached).
17519:M 20 Apr 2023 22:14:01.600 # Cluster state changed: fail
17519:M 20 Apr 2023 22:14:02.167 # Failover auth granted to 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 for epoch 7
17519:M 20 Apr 2023 22:14:02.177 # Cluster state changed: ok

客户端日志出现了约4s 的服务不可用

 

恢复 10.0.0.22 节点

 

观察集群节点信息及日志

##10.0.0.22节点被添加为slave节点,并将master指向10.0.0.30
[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster info 10.0.0.20:6379 10.0.0.20:6379 (6b142fe3...) -> 150 keys | 5461 slots | 1 slaves. 10.0.0.21:6379 (a4b387d8...) -> 141 keys | 5462 slots | 1 slaves. 10.0.0.30:6379 (5c9789e1...) -> 154 keys | 5461 slots | 1 slaves. [OK] 445 keys in 3 masters. 0.03 keys per slot on average. [root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis -c --no-auth-warning --cluster check 10.0.0.20:6379 10.0.0.20:6379 (6b142fe3...) -> 150 keys | 5461 slots | 1 slaves. 10.0.0.21:6379 (a4b387d8...) -> 141 keys | 5462 slots | 1 slaves. 10.0.0.30:6379 (5c9789e1...) -> 154 keys | 5461 slots | 1 slaves. [OK] 445 keys in 3 masters. 0.03 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.20:6379) M: 6b142fe3438c3ea488c34366e5ea5a298f89f518 10.0.0.20:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 07a36af61eb1f887419f2266b2360b4f795bb7a3 10.0.0.22:6379 slots: (0 slots) slave replicates 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 M: a4b387d82ac06e5cbf212e48c781e1c27b50320f 10.0.0.21:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 6a9dd64b6bf1a7eb336ec9d320d86ae7450d5e2b 10.0.0.32:6379 slots: (0 slots) slave replicates a4b387d82ac06e5cbf212e48c781e1c27b50320f S: 60aec73a6b0d3929cda3a284bb513898d2e73657 10.0.0.31:6379 slots: (0 slots) slave replicates 6b142fe3438c3ea488c34366e5ea5a298f89f518 M: 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 10.0.0.30:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@Redis-Ubuntu1804-node1:~]#
##日志记录清除 10.0.0.22 节点上的faild 标记,并作为slave节点启动
17519:M 20 Apr 2023 22:14:01.600 * Marking node 07a36af61eb1f887419f2266b2360b4f795bb7a3 as failing (quorum reached).
17519:M 20 Apr 2023 22:14:01.600 # Cluster state changed: fail
17519:M 20 Apr 2023 22:14:02.167 # Failover auth granted to 5c9789e105f8d8a0f1dc5ac2a6d51831482fbd34 for epoch 7
17519:M 20 Apr 2023 22:14:02.177 # Cluster state changed: ok
17519:M 20 Apr 2023 22:17:23.792 * Clear FAIL state for node 07a36af61eb1f887419f2266b2360b4f795bb7a3: master without slots is reachable again.

 

3、基于Redis 3/4 Cluster集群部署

3.0、基本环境准备

[root@Redis-Ubuntu1804-node1:~]# redis-cli -a redis cluster nodes
1c2fee65ee50cdf66c862d04710672286b305f55 :6379@16379 myself,master - 0 0 0 connected
[root@Redis-Ubuntu1804-node1:~]# redis-cli --version
redis-cli 4.0.9
[root@Redis-Ubuntu1804-node1:~]# 

 

3.1、准备 redis-trib.rb 工具

查找 redis-trib.rb 工具路径,将二进制文件复制到 /usr/bin 目录下

[root@Redis-Ubuntu1804-node1:~]# find / -name redis-trib.rb
/usr/share/doc/redis-tools/examples/redis-trib.rb
[root@Redis-Ubuntu1804-node1:~]# cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/bin/

 

安装ruby运行环境

[root@Redis-Ubuntu1804-node1:~]# redis-trib.rb 
/usr/bin/env: ‘ruby’: No such file or directory
[root@Redis-Ubuntu1804-node1:~]# ruby

Command 'ruby' not found, but can be installed with:

snap install ruby  # version 3.2.2, or
apt  install ruby

See 'snap info ruby' for additional versions.

[root@Redis-Ubuntu1804-node1:~]# apt-cache madison ruby
      ruby |    1:2.5.1 | http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 Packages
[root@Redis-Ubuntu1804-node1:~]# apt install ruby -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.15.0-151 linux-headers-4.15.0-151-generic linux-image-4.15.0-151-generic linux-modules-4.15.0-151-generic
  linux-modules-extra-4.15.0-151-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  fonts-lato javascript-common libjs-jquery libruby2.5 rake ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit
  ruby2.5 rubygems-integration unzip zip
Suggested packages:
  apache2 | lighttpd | httpd ri ruby-dev bundler
The following NEW packages will be installed:
  fonts-lato javascript-common libjs-jquery libruby2.5 rake ruby ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert
  ruby-test-unit ruby2.5 rubygems-integration unzip zip
0 upgraded, 15 newly installed, 0 to remove and 62 not upgraded.
Need to get 6499 kB of archives.
After this operation, 29.0 MB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 fonts-lato all 2.0-2 [2698 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 javascript-common all 11 [6066 B]
Get:3 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 libjs-jquery all 3.2.1-1 [152 kB]
Get:4 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 rubygems-integration all 1.11 [4994 B]
Get:5 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 ruby2.5 amd64 2.5.1-1ubuntu1.13 [48.6 kB]
Get:6 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby amd64 1:2.5.1 [5712 B]
Get:7 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 rake all 12.3.1-1ubuntu0.1 [44.9 kB]
Get:8 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-did-you-mean all 1.2.0-2 [9700 B]
Get:9 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-minitest all 5.10.3-1 [38.6 kB]
Get:10 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-net-telnet all 0.1.1-2 [12.6 kB]
Get:11 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-power-assert all 0.3.0-1 [7952 B]
Get:12 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-test-unit all 3.2.5-1 [61.1 kB]
Get:13 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libruby2.5 amd64 2.5.1-1ubuntu1.13 [3074 kB]                              
Get:14 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 unzip amd64 6.0-21ubuntu1.2 [168 kB]                                      
Get:15 http://cn.archive.ubuntu.com/ubuntu bionic/main amd64 zip amd64 3.0-11build1 [167 kB]                                                   
Fetched 6499 kB in 9s (722 kB/s)                                                                                                               
Selecting previously unselected package fonts-lato.
(Reading database ... 144903 files and directories currently installed.)
Preparing to unpack .../00-fonts-lato_2.0-2_all.deb ...
Unpacking fonts-lato (2.0-2) ...
Selecting previously unselected package javascript-common.
Preparing to unpack .../01-javascript-common_11_all.deb ...
Unpacking javascript-common (11) ...
Selecting previously unselected package libjs-jquery.
Preparing to unpack .../02-libjs-jquery_3.2.1-1_all.deb ...
Unpacking libjs-jquery (3.2.1-1) ...
Selecting previously unselected package rubygems-integration.
Preparing to unpack .../03-rubygems-integration_1.11_all.deb ...
Unpacking rubygems-integration (1.11) ...
Selecting previously unselected package ruby2.5.
Preparing to unpack .../04-ruby2.5_2.5.1-1ubuntu1.13_amd64.deb ...
Unpacking ruby2.5 (2.5.1-1ubuntu1.13) ...
Selecting previously unselected package ruby.
Preparing to unpack .../05-ruby_1%3a2.5.1_amd64.deb ...
Unpacking ruby (1:2.5.1) ...
Selecting previously unselected package rake.
Preparing to unpack .../06-rake_12.3.1-1ubuntu0.1_all.deb ...
Unpacking rake (12.3.1-1ubuntu0.1) ...
Selecting previously unselected package ruby-did-you-mean.
Preparing to unpack .../07-ruby-did-you-mean_1.2.0-2_all.deb ...
Unpacking ruby-did-you-mean (1.2.0-2) ...
Selecting previously unselected package ruby-minitest.
Preparing to unpack .../08-ruby-minitest_5.10.3-1_all.deb ...
Unpacking ruby-minitest (5.10.3-1) ...
Selecting previously unselected package ruby-net-telnet.
Preparing to unpack .../09-ruby-net-telnet_0.1.1-2_all.deb ...
Unpacking ruby-net-telnet (0.1.1-2) ...
Selecting previously unselected package ruby-power-assert.
Preparing to unpack .../10-ruby-power-assert_0.3.0-1_all.deb ...
Unpacking ruby-power-assert (0.3.0-1) ...
Selecting previously unselected package ruby-test-unit.
Preparing to unpack .../11-ruby-test-unit_3.2.5-1_all.deb ...
Unpacking ruby-test-unit (3.2.5-1) ...
Selecting previously unselected package libruby2.5:amd64.
Preparing to unpack .../12-libruby2.5_2.5.1-1ubuntu1.13_amd64.deb ...
Unpacking libruby2.5:amd64 (2.5.1-1ubuntu1.13) ...
Selecting previously unselected package unzip.
Preparing to unpack .../13-unzip_6.0-21ubuntu1.2_amd64.deb ...
Unpacking unzip (6.0-21ubuntu1.2) ...
Selecting previously unselected package zip.
Preparing to unpack .../14-zip_3.0-11build1_amd64.deb ...
Unpacking zip (3.0-11build1) ...
Setting up libjs-jquery (3.2.1-1) ...
Setting up unzip (6.0-21ubuntu1.2) ...
Setting up zip (3.0-11build1) ...
Setting up fonts-lato (2.0-2) ...
Setting up ruby-did-you-mean (1.2.0-2) ...
Setting up ruby-net-telnet (0.1.1-2) ...
Setting up rubygems-integration (1.11) ...
Setting up javascript-common (11) ...
Setting up ruby-minitest (5.10.3-1) ...
Setting up ruby-power-assert (0.3.0-1) ...
Setting up ruby2.5 (2.5.1-1ubuntu1.13) ...
Setting up ruby (1:2.5.1) ...
Setting up ruby-test-unit (3.2.5-1) ...
Setting up rake (12.3.1-1ubuntu0.1) ...
Setting up libruby2.5:amd64 (2.5.1-1ubuntu1.13) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

解决 redis-rtib.rb 执行报错

[root@Redis-Ubuntu1804-node1:~]# redis-trib.rb 
Traceback (most recent call last):
    2: from /usr/bin/redis-trib.rb:25:in `<main>'
    1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- redis (LoadError)

 

##使用 gem 工具安装 ruby 的redis模块
[root@Redis-Ubuntu1804-node1:~]# gem install redis
Fetching: redis-5.0.6.gem (100%)
Successfully installed redis-5.0.6
Parsing documentation for redis-5.0.6
Installing ri documentation for redis-5.0.6
Done installing documentation for redis after 0 seconds
1 gem installed

##离线模式安装 gem
[root@Redis-Ubuntu1804-node1:~]# wget https://rubygems.org/downloads/redis-4.0.3.gem
[root@Redis-Ubuntu1804-node1:~]# gem install redis-4.0.3.gem

redis-trib.rb 常用命令

[root@Redis-Ubuntu1804-node1:~]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN  #创建集群
                  --replicas <arg>  #指定每个master节点的副本数,即每组主从的slave节点数,通常为1
  check           host:port  #检查集群信息
  info            host:port  #查看集群主机信息
  fix             host:port  #修复集群
                  --timeout <arg>
  reshard         host:port  #在线热机迁移指定的slots数据
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port  #平衡集群在线节点的slots数量
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port  #添加新的节点到集群中
                  --slave
                  --master-id <arg>
  del-node        host:port node_id  #删除集群中的指定节点
  set-timeout     host:port milliseconds  #设置节点超时时间
  call            host:port command arg arg .. arg  #在集群上所有节点上执行命令
  import          host:port  #导入外部redis服务器数据到当前集群
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

 

3.2、修改redis-trib.rb 添加连接redis密码

[root@Redis-Ubuntu1804-node1:~]# sed -i 's/@r = Redis.new.*/@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60, :password => "'redis'")/' /usr/bin/redis-trib.rb
[root@Redis-Ubuntu1804-node1:~]# vim /usr/bin/redis-trib.rb def connect(o={}) return if @r print "Connecting to node #{self}: " if $verbose STDOUT.flush begin @r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60, :password => 'redis') @r.ping rescue xputs "[ERR] Sorry, can't connect to node #{self}" exit 1 if o[:abort] @r = nil end xputs "OK" if $verbose end
[root@Redis-Ubuntu1804-node1:~]# redis-trib.rb info 10.0.0.20:6379
10.0.0.20:6379 (1c2fee65...) -> 0 keys | 0 slots | 0 slaves.
[OK] 0 keys in 1 masters.
0.00 keys per slot on average.
[root@Redis-Ubuntu1804-node1:~]# 

 

3.3、创建Cluster集群

[root@Redis-Ubuntu1804-node1:~]# redis-trib.rb create --replicas 1 10.0.0.20:6379 10.0.0.21:6379 10.0.0.22:6379 10.0.0.30:6379 10.0.0.31:6379 10.0.0.32:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.20:6379
10.0.0.21:6379
10.0.0.22:6379
Adding replica 10.0.0.31:6379 to 10.0.0.20:6379
Adding replica 10.0.0.32:6379 to 10.0.0.21:6379
Adding replica 10.0.0.30:6379 to 10.0.0.22:6379
M: 1c2fee65ee50cdf66c862d04710672286b305f55 10.0.0.20:6379
   slots:0-5460 (5461 slots) master
M: 337bf2a15dd9b0ae8caaee08c5ec41beb754b78e 10.0.0.21:6379
   slots:5461-10922 (5462 slots) master
M: 709b09b96f19209e7f0a1e3cfc5c035678491e4b 10.0.0.22:6379
   slots:10923-16383 (5461 slots) master
S: f9e73307b3146b25b51cec9a861463a616ed322b 10.0.0.30:6379
   replicates 709b09b96f19209e7f0a1e3cfc5c035678491e4b
S: c567eea1d3fda7b37c42b741bc32d14385bbe872 10.0.0.31:6379
   replicates 1c2fee65ee50cdf66c862d04710672286b305f55
S: 0428f9a71c369004c0ca01c0a403f9c2ee5fa41c 10.0.0.32:6379
   replicates 337bf2a15dd9b0ae8caaee08c5ec41beb754b78e
Can I set the above configuration? (type '%3            

有关【Redis】Cluster集群的更多相关文章

  1. 【详解】Docker安装Elasticsearch7.16.1集群 - 2

    开门见山|拉取镜像dockerpullelasticsearch:7.16.1|配置存放的目录#存放配置文件的文件夹mkdir-p/opt/docker/elasticsearch/node-1/config#存放数据的文件夹mkdir-p/opt/docker/elasticsearch/node-1/data#存放运行日志的文件夹mkdir-p/opt/docker/elasticsearch/node-1/log#存放IK分词插件的文件夹mkdir-p/opt/docker/elasticsearch/node-1/plugins若你使用了moba,直接右键新建即可如上图所示依次类推创建

  2. 关于ES集群信息的一些查看 - 2

    文章目录查看ES信息查看节点信息查看分片信息实际场景下ES分片及副本数量应该怎么分关于ES的灵活使用查看ES信息查看版本kibana:GET/查看节点信息GET/_cat/nodes?v解释:ip:集群中节点的ip地址;heap.percent:堆内存的占用百分比;ram.percent:总内存的占用百分比,其实这个不是很准确,因为buff/cache和available也被当作使用内存;cpu:cpu占用百分比;load_1m:1分钟内cpu负载;load_5m:5分钟内cpu负载;load_15m:15分钟内cpu负载;node.role:上图的dilmrt代表全部权限master:*代表

  3. linux查看es节点使用情况,elasticsearch(es) 如何查看当前集群中哪个节点是主节点(master) - 2

    elasticsearch查看当前集群中的master节点是哪个需要使用_cat监控命令,具体如下。查看方法es主节点确定命令,以kibana上查看示例如下:GET_cat/nodesv返回结果示例如下:ipheap.percentram.percentcpuload_1mload_5mload_15mnode.rolemastername172.16.16.188529952.591.701.45mdi-elastic3172.16.16.187329950.990.991.19mdi-elastic2172.16.16.231699940.871.001.03mdi-elastic4172

  4. org.elasticsearch.cluster.block.clusterblockexception: blocked by: [service_unavailable/1/state not - 2

    文章目录一.搭建集群时出现错误错误日志elasticsearch.logorg.elasticsearch.cluster.block.clusterblockexception:blockedby:[service_unavailable/1/statenotrecovered/initialized];原因:解决方案:一.搭建集群时出现错误错误日志elasticsearch.logorg.elasticsearch.cluster.block.clusterblockexception:blockedby:[service_unavailable/1/statenotrecovered/i

  5. kubernetes集群划分节点 - 2

    Kubernetes(K8s)是一个用于管理容器化应用程序的开源平台,可以帮助开发人员更轻松地部署、管理和扩展应用程序。在Kubernetes中,集群划分是一种重要的概念,可以帮助我们更好地组织和管理集群中的节点和资源。本文将介绍如何使用Kubernetes对集群进行划分,并提供详细的操作示例,希望能够帮助读者更好地了解和使用Kubernetes平台。Node划分Node划分是将集群中的节点按照一定的规则进行划分。在Kubernetes中,可以使用NodeSelector和Affinity机制来实现Node划分。NodeSelectorNodeSelector是一种将Pod调度到符合特定节点标

  6. 五-1、elasticsearch集群搭建(ES集群搭建) - 2

    目录一、下载Elasticsearch1.选择你要下载的Elasticsearch版本二、采用通用搭建集群的方法三、配置三台es1.上传压缩包到任意一台虚拟机中2.解压并修改配置文件(配置单台es)3.配置三台es集群4.设置后台启动和开机自启(可选)一、下载Elasticsearch1.选择你要下载的Elasticsearch版本es下载地址这里我下载的是二、采用通用搭建集群的方法集群搭建方法三、配置三台es1.上传压缩包到任意一台虚拟机中上传方式有两种第一种:使用xftp上传直接拖动过去就可以了。第二种:使用lrzsz先安装yum-yinstalllrzsz切换到要上传的位置cd/opt/

  7. idea连接远程k8s集群使用kubernetes-client - 2

    文章目录一.k8s集群修改config1.1备份当前k8s集群配置文件1.2删除当前k8s集群的apiserver的cert和key1.3生成新的apiserver的cert和key1.4刷新admin.conf1.5重启apiserver1.6刷新.kube/config二.安装kubectl2.1下载kubectl2.2配置kubectl三.使用kubernetes-client操作k8s集群3.1依赖3.2注意(可忽略)3.3创建StatefulSet3.4运行shell命令3.5删除StatefulSet3.6线上运行注意一.k8s集群修改config因为默认的是内网IP,复制出来后,

  8. ElasticSearch(十一)【集群搭建】 - 2

    十一、ES集群的相关概念上一篇文章《ElasticSearch-聚合查询》集群(cluster)一个集群就是由一个或多个节点组织在一起,它们共同持有你整个的数据,并一起提供索引和搜整合应用索功能。一个集群由一个唯一的名字标识,这个名字默认就是elasticsearch。这个名字是重要的,因为一个节点只能通过指定某个集群的名字,来加入这个集群节点(node)一个节点是集群中的一个服务器,作为集群的一部分,它存储你的数据,参与集群的索引节点和搜索功能。和集群类似,一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点索引(Index)一组

  9. docker搭建ElasticSearch集群 - 2

    一、安装ElasticSearch使用docker直接获取es镜像,执行命令dockerpullelasticsearch:7.7.0执行完成后,执行dockerimages即可看到上一步拉取的镜像。二、创建数据挂在目录,以及配置ElasticSearch集群配置文件,调高JVM线程数限制数量1.创建数据文件挂载目录,然后直接关闭防火墙mkdir-p/home/soft/ESmkdir-p/home/soft/ES/configcd/home/soft/ES创建挂载目录mkdirdata1data2data3进入config文件里面创建es配置文件cdES/config/查询防火墙状态syst

  10. javascript - 谷歌地图集群自定义图像 - 2

    我想用自定义图像更改谷歌地图聚类。但是,它不会改变我提供的任何内容。这个initMap函数是https://developers.google.com/maps/documentation/javascript/marker-clustering然后我尝试用来自谷歌的一些随机图像来更改集群图像。但是,它不呈现任何内容。集群不支持自定义集群镜像??functioninitMap(){varmap=newgoogle.maps.Map(document.getElementById('map'),{zoom:3,center:{lat:-28.024,lng:140.887}});//Cr

随机推荐