Redis 고가용성 및 클러스터링

Redis 고가용성 및 클러스터링

Redis는 단일 시스템 데이터 지속성을 달성할 수 있지만 RDB나 AOF는
단일 가동 중지 지점 문제를 해결할 수 없습니다. 즉, 단일 Redis 서버 자체에 시스템 오류, 하드웨어 오류 등이 발생하면
직접적으로 데이터 손실이 발생하므로 단일점 문제를 해결하려면 다른 기술을 사용해야 합니다.

1. Redis 마스터-슬레이브

Redis 데이터의 호스트 간 백업을 실현할 수 있는 활성/대기 모드. 단말은 고가용성 로드의 VIP에 접속한 후,
로드 서버에서 설정한 Redis 백엔드 실제 서버에 접속하는데, 이 모드는 프로그램에서 Redis 서버의 실제 IP 주소를 구성할 필요가 없습니다. Redis 서버의 주소는 나중에 변경되므로 Redis
서버만 변경하면 됩니다.

[외부 링크 사진 전송에 실패했습니다. 소스 사이트에 리칭 방지 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다(img-lr6g1d3s-1693219416526)(png/Redis-Master-Slave.png)]

환경

주인 IP 역할
redis-서버-노드1 192.168.131.148 주인
redis-node2 192.168.131.149 노예

1.1 Redis 마스터-슬레이브 복제 기본 구성

또한 Redis 슬레이브는 지속성을 활성화하고 마스터와 동일한 연결 비밀번호를 설정해야 합니다. 왜냐하면 슬레이브가
나중에 마스터로 승격될 수 있고 슬레이브 측이 마스터 동기화로 전환된 후에 모든 이전 데이터가 손실되기 때문입니다.
슬레이브가 마스터의 슬레이브가 되면 Redis 슬레이브 서비스는 현재 Redis 서버의 모든 데이터를 삭제 하고
마스터의 데이터를 자체 메모리로 가져오지만,
동기화 관계를 끊은 후에도 현재 동기화된 데이터는

1.1.1 명령줄을 통한 구성

192.168.131.149:6379> REPLICAOF 192.168.131.148 6379
OK
192.168.131.149:6379> CONFIG SET masterauth stevenux
OK
(1.71s)

1.1.2 동기화 로그 보기

노예

[root@redis-node2 ~]# tail -f /var/log/redis.log
7160:S 11 Feb 2020 11:35:55.542 * Connecting to MASTER 192.168.131.148:6379
7160:S 11 Feb 2020 11:35:55.542 * MASTER <-> REPLICA sync started
7160:S 11 Feb 2020 11:35:55.542 * Non blocking connect for SYNC fired the event.
7160:S 11 Feb 2020 11:35:55.543 * Master replied to PING, replication can continue...
7160:S 11 Feb 2020 11:35:55.544 * Partial resynchronization not possible (no cached master)
7160:S 11 Feb 2020 11:35:55.546 * Full resync from master: 0a3e5fcc5e891bc3bfae8c390529f1702f3d8068:546   # 首次连接全量复制
7160:S 11 Feb 2020 11:35:55.629 * MASTER <-> REPLICA sync: receiving 176 bytes from master
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Flushing old data
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Loading DB in memory
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Finished with success

주인

[root@redis-server-node1 ~]# tail -f /var/log/redis.log
2976:M 11 Feb 2020 11:32:03.391 * Synchronization with replica 192.168.131.149:6379 succeeded
2976:M 11 Feb 2020 11:32:04.541 # Connection with replica client id #86 lost.
2976:M 11 Feb 2020 11:35:55.546 * Replica 192.168.131.149:6379 asks for synchronization
2976:M 11 Feb 2020 11:35:55.546 * Full resync requested by replica 192.168.131.149:6379
2976:M 11 Feb 2020 11:35:55.546 * Starting BGSAVE for SYNC with target: disk
2976:M 11 Feb 2020 11:35:55.546 * Background saving started by pid 3185
3185:C 11 Feb 2020 11:35:55.549 * DB saved on disk
3185:C 11 Feb 2020 11:35:55.550 * RDB: 0 MB of memory used by copy-on-write
2976:M 11 Feb 2020 11:35:55.630 * Background saving terminated with success
2976:M 11 Feb 2020 11:35:55.630 * Synchronization with replica 192.168.131.149:6379 succeeded

1.1.3 마스터-슬레이브 상태 보기

마스터 상태

[root@redis-server-node1 redis]# redis-cli -h 192.168.131.148
192.168.131.148:6379> info replication
NOAUTH Authentication required.
192.168.131.148:6379> AUTH stevenux
OK
192.168.131.148:6379> info replication
# Replication
role:master
connected_slaves:1   # 连接的从库数
slave0:ip=192.168.131.149,port=6379,state=online,offset=0,lag=3 # 从库的具体情况
master_replid:0a3e5fcc5e891bc3bfae8c390529f1702f3d8068 # 主库复制ID
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154

노예 상태

192.168.131.149:6379> info replication
# Replication
role:slave
master_host:192.168.131.148
master_port:6379
master_link_status:up   # 主库在线
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:560
slave_priority:100
slave_read_only:1   # 从库只读
connected_slaves:0
master_replid:0a3e5fcc5e891bc3bfae8c390529f1702f3d8068  # 主库复制ID
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:560
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:547
repl_backlog_histlen:14

1.1.4 수정된 구성이 영구적으로 적용됩니다.

주인

[root@redis-server-node1 ~]# vim /apps/redis/etc/redis.conf
################################## SECURITY ###################################
requirepass stevenux
...

노예

[root@redis-node2 ~]# vim /apps/redis/etc/redis.conf
################################# REPLICATION #################################
replicaof 192.168.131.148 6379
masterauth stevenux
...

1.1.5 재시작 및 데이터 일관성 확인

주인

192.168.131.148:6379> SET keyone lisuo
OK
192.168.131.148:6379> GET keyone
"lisuo"
192.168.131.148:6379> LPUSH menu apple banana
(integer) 2
192.168.131.148:6379> LPUSH menu pine
(integer) 3
192.168.131.148:6379> LRANGE menu 0 -1
1) "pine"
2) "banana"
3) "apple"

노예

192.168.131.149:6379> KEYS *
1) "menu"
2) "keyone"
192.168.131.149:6379> LRANGE menu 0 -1
1) "pine"
2) "banana"
3) "apple"
192.168.131.149:6379> GET keyone
"lisuo"

슬레이브는 읽기 전용이지만 쓰기는 불가능합니다.

192.168.131.149:6379> SET KEY0 V0
(error) READONLY You can't write against a read only replica.

1.1.6 마스터 로그

데이터 변경 후 마스터 로그

[root@redis-server-node1 ~]# tail -f /var/log/redis.log
2976:M 11 Feb 2020 11:32:03.391 * Synchronization with replica 192.168.131.149:6379 succeeded
2976:M 11 Feb 2020 11:32:04.541 # Connection with replica client id #86 lost.
2976:M 11 Feb 2020 11:35:55.546 * Replica 192.168.131.149:6379 asks for synchronization
2976:M 11 Feb 2020 11:35:55.546 * Full resync requested by replica 192.168.131.149:6379
2976:M 11 Feb 2020 11:35:55.546 * Starting BGSAVE for SYNC with target: disk
2976:M 11 Feb 2020 11:35:55.546 * Background saving started by pid 3185
3185:C 11 Feb 2020 11:35:55.549 * DB saved on disk
3185:C 11 Feb 2020 11:35:55.550 * RDB: 0 MB of memory used by copy-on-write
2976:M 11 Feb 2020 11:35:55.630 * Background saving terminated with success
2976:M 11 Feb 2020 11:35:55.630 * Synchronization with replica 192.168.131.149:6379 succeeded
2976:M 11 Feb 2020 11:45:29.674 * Background saving started by pid 3344
3344:C 11 Feb 2020 11:45:29.675 * DB saved on disk
3344:C 11 Feb 2020 11:45:29.675 * RDB: 0 MB of memory used by copy-on-write
2976:M 11 Feb 2020 11:45:29.706 * Background saving terminated with success
2976:M 11 Feb 2020 12:00:30.031 * 1 changes in 900 seconds. Saving...    # 数据变动(有业务活动)
2976:M 11 Feb 2020 12:00:30.031 * Background saving started by pid 3490
3490:C 11 Feb 2020 12:00:30.032 * DB saved on disk   # (RDB保存完成)
3490:C 11 Feb 2020 12:00:30.032 * RDB: 0 MB of memory used by copy-on-write
2976:M 11 Feb 2020 12:00:30.132 * Background saving terminated with success

데이터 변경 후 슬레이브의 동기화 로그

[root@redis-node2 ~]# tail -f /var/log/redis.log
7160:S 11 Feb 2020 11:35:55.542 * Connecting to MASTER 192.168.131.148:6379
7160:S 11 Feb 2020 11:35:55.542 * MASTER <-> REPLICA sync started
7160:S 11 Feb 2020 11:35:55.542 * Non blocking connect for SYNC fired the event.
7160:S 11 Feb 2020 11:35:55.543 * Master replied to PING, replication can continue...
7160:S 11 Feb 2020 11:35:55.544 * Partial resynchronization not possible (no cached master)
7160:S 11 Feb 2020 11:35:55.546 * Full resync from master: 0a3e5fcc5e891bc3bfae8c390529f1702f3d8068:546  # 首次连接全量复制开始
7160:S 11 Feb 2020 11:35:55.629 * MASTER <-> REPLICA sync: receiving 176 bytes from master
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Flushing old data
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Loading DB in memory
7160:S 11 Feb 2020 11:35:55.630 * MASTER <-> REPLICA sync: Finished with success # 全量复制结束
7160:S 11 Feb 2020 11:50:56.009 * 1 changes in 900 seconds. Saving...
7160:S 11 Feb 2020 11:50:56.010 * Background saving started by pid 7194
7194:C 11 Feb 2020 11:50:56.010 * DB saved on disk
7194:C 11 Feb 2020 11:50:56.011 * RDB: 0 MB of memory used by copy-on-write
7160:S 11 Feb 2020 11:50:56.110 * Background saving terminated with success
7160:S 11 Feb 2020 12:05:57.044 * 1 changes in 900 seconds. Saving...  # 主从同步后数据化变动
7160:S 11 Feb 2020 12:05:57.045 * Background saving started by pid 7256
7256:C 11 Feb 2020 12:05:57.045 * DB saved on disk  # RDB文件保存到吸盘
7256:C 11 Feb 2020 12:05:57.046 * RDB: 0 MB of memory used by copy-on-write
7160:S 11 Feb 2020 12:05:57.146 * Background saving terminated with success

1.1.7 마스터-슬레이브 복제 프로세스

Redis는 전체 동기화와 증분 동기화로 구분되는 마스터-슬레이브 복제를 지원하며, 첫 번째 동기화는 전체 동기화이며, 마스터-슬레이브 동기화는 슬레이브 서버가
마스터 서버의 데이터를 백업하고 슬레이브 서버도 연결이 가능하도록 지원합니다. 슬레이브 서버, 즉 다른 Redis
서버가 가능 슬레이브 서버에서 데이터 동기화 수행 Redis의 마스터-슬레이브 동기화는 Non-Blocking이며 마스터가
서버로부터 sync(2.8 이전 버전 PSYNC) 명령을 받으면 백그라운드에서 BGSAVE 명령을 실행하고 새로 작성된 데이터를 쓰기 위해 하위 프로세스를 포크합니다.
데이터는 버퍼에 기록되고 bgsave가 실행된 후
생성된 RDB 파일이 클라이언트로 전송되고 클라이언트는 수신된 RDB 파일을 로드합니다. Redis 마스터는
모든 버퍼의 내용을 Redis 슬레이브에 전송하고 동기식 슬레이브 서버는
오프셋 위치(MySQL의 binlog 위치와 동일)를 마스터 서버에 전송합니다. 마스터
서버가 이 위치 이후의 데이터는 버퍼에 포함됩니다. 버퍼에 있는 데이터의 백로그는 Redis 슬레이브
서버로 전송되고, 슬레이브 서버는 압착된 데이터를 작성합니다. 마스터 서버가 메모리에 들어가 완전한 데이터 동기화가 완료되면
슬레이브 서버는 다시 동기화할 때 현재 오프셋 위치만 마스터 서버에 전송하면 됩니다. 그런 다음 마스터 서버는
다음 데이터를 슬레이브 서버에 전송합니다. 해당 위치를 찾아 메모리에 저장합니다.

Redis 전체 복제는 일반적으로 슬레이브의 첫 번째 초기화 단계에서 발생하며, 이때 슬레이브는 마스터의
모든 데이터를 복사 해야 합니다. 구체적인 마스터-슬레이브 단계는 다음과 같습니다.

  1. 슬레이브 서버는 마스터 서버에 연결하고 SYNC 명령을 보냅니다.
  2. 마스터 서버는 SYNC 이름을 수신한 후 BGSAVE 명령을 실행하여 RDB 스냅샷 파일을 생성하고 버퍼를 사용하여
    이후 실행되는 모든 쓰기 명령을 기록합니다.
  3. 마스터 서버 BGSAVE가 실행된 후 모든 슬레이브 서버에 스냅샷 파일을 전송하고 전송 중에
    실행된 쓰기 명령을 계속 기록합니다.
  4. 서버로부터 스냅샷 파일을 받은 후 기존 데이터를 모두 버리고 받은 스냅샷을 로드합니다.
  5. 마스터 서버 스냅샷이 전송된 후 버퍼에 있는 쓰기 명령을 슬레이브 서버로 전송하기 시작합니다.
  6. 슬레이브 서버는 스냅샷 로드를 마치고 명령 요청 수신을 시작하며 마스터 서버 버퍼에서 쓰기 명령을 실행합니다.
  7. 이후 동기화는 먼저 자체slave_repl_offset 위치를 전송하고 새로 추가된 데이터만 동기화되며 전체 동기화는 수행되지 않습니다.

주황색 프레임이 전체 동기화 과정, 녹색 프레임이 부분 동기화 과정입니다
.Slave-sync-process.png)]

1.1.8 마스터-슬레이브 동기화 최적화 구성

2.8 버전 이전에는 Redis에서는 증분식 부분 복제 기능을 제공하지 않았으나, 네트워크가 중단되거나 슬레이브 Redis가 재시작되면
마스터와 슬레이브 간에 전체 동기화가 발생하는데, 즉 부분 복제 기능이 추가된 것입니다. 버전 2.8.

repl-diskless-sync no
   # 是否使用无盘同步RDB文件,默认为no,no为不使用无盘,需要将RDB文件保存到磁盘后
   # 再发送给slave,yes为支持无盘,支持无盘就是RDB文件不需要保存至本地磁盘,而且直
   # 接通过socket文件发送给slave。

repl-diskless-sync-delay 5
   # Master准备好RDB文件后等等待传输时间

repl-ping-slave-period 10
   # slave端向server端发送ping的时间区间设置,默认为10秒

repl-timeout 60
   # 设置复制超时时间

repl-disable-tcp-nodelay no
   # 是否启用TCP_NODELAY,如设置成yes,则redis会合并小的TCP包从而节省带宽,但会增加
   # 同步延迟(40ms),造成master与slave数据不一致,假如设置成no,则redis master会
   # 立即发送同步数据,没有延迟,前者关注性能,后者关注redis服务中的数据一致性。

repl-backlog-size 1mb
   # master的写入数据缓冲区,用于记录自上一次同步后到下一次同步过程中间的写入命令,
   # 计算公式:repl-backlog-size = 允许从节点最大中断时长 * 主实例offset每秒写入量
   # 比如master每秒最大写入64mb,最大允许60秒,那么就要设置为64mb*60秒=3840MB(3.8G)
 
repl-backlog-ttl 3600
   # 如果一段时间后没有slave连接到master,则backlog size的内存将会被释放。如果值
   # 为0则表示永远不释放这部份内存。 slave-priority 100 #slave端的优先级设置,值是
   # 一个整数,数字越小表示优先级越高。当master故障时将会按照优先级来选择slave端进行
   # 恢复,如果值设置为0,则表示该slave永远不会被选择。
 
#min-slaves-to-write 1
   # 设置一个master端的可用slave至少多少

#min-slaves-max-lag 20
   # 设置所有slave延迟时间都大于多少秒时,master不接收写操作(拒绝写入)。

1.1.9 슬레이브에서 마스터로 전환

마스터의 스위치로 인해 master_replid가 변경되고 슬레이브 앞의 master_replid가
현재 마스터와 일치하지 않아 모든 슬레이브의 전체 동기화가 트리거됩니다. 슬레이브에서 마스터로 전환하는 것은 매우 간단합니다.
실행만 하면 됩니다 SLAVEOF no one.

현재 슬레이브 상황 보기:

192.168.131.149:6379> info replication
# Replication
role:slave
master_host:192.168.131.148
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
...

슬레이브 동기화를 중지하고 상태를 확인하십시오.

192.168.131.149:6379> SLAVEOF no one
OK
192.168.131.149:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:bfc98f9b16833d6c7c9638c34ef4a9be4ccc7999
master_replid2:0a3e5fcc5e891bc3bfae8c390529f1702f3d8068
master_repl_offset:13231
second_repl_offset:13232
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:547
repl_backlog_histlen:12685

지금 데이터를 쓸 수 있는지 테스트합니다.

192.168.131.149:6379> set keytest hello
OK
192.168.131.149:6379> get keytest
"hello"

연결이 끊어진 후의 마스터 로그

...
2976:M 11 Feb 2020 14:04:51.220 # Connection with replica 192.168.131.149:6379 lost

연결이 끊어진 후의 슬레이브 로그

...
7160:M 11 Feb 2020 14:04:51.202 # Connection with master lost.
7160:M 11 Feb 2020 14:04:51.202 * Caching the disconnected master state.
7160:M 11 Feb 2020 14:04:51.202 * Discarding previously cached master state.
7160:M 11 Feb 2020 14:04:51.202 * MASTER MODE enabled (user request from 'id=23 addr=192.168.131.149:33746 fd=8 name= age=5 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=34 qbuf-free=32734 obl=0 oll=0 omem=0 events=r cmd=slaveof')
7160:M 11 Feb 2020 14:05:48.724 * 1 changes in 900 seconds. Saving...
7160:M 11 Feb 2020 14:05:48.724 * Background saving started by pid 7514
7514:C 11 Feb 2020 14:05:48.728 * DB saved on disk
7514:C 11 Feb 2020 14:05:48.728 * RDB: 0 MB of memory used by copy-on-write
7160:M 11 Feb 2020 14:05:48.825 * Background saving terminated with success

1.2 문제 해결

1.2.1 슬레이브 측에서 마스터 인증 비밀번호가 올바르게 설정되지 않았습니다.

마스터 비밀번호는 stevenux이며 123456으로 설정되어 있습니다.

192.168.131.149:6379> REPLICAOF 192.168.131.148 6379
OK
192.168.131.149:6379> CONFIG SET masterauth 123456
OK
192.168.131.149:6379> info replication
# Replication
role:slave
master_host:192.168.131.148
master_port:6379
master_link_status:down  # 显示master未连接
master_last_io_seconds_ago:-1
master_sync_in_progress:0
...

노예의 로그

...
7160:S 11 Feb 2020 14:12:09.761 * Connecting to MASTER 192.168.131.148:6379
7160:S 11 Feb 2020 14:12:09.761 * MASTER <-> REPLICA sync started
7160:S 11 Feb 2020 14:12:09.763 * Non blocking connect for SYNC fired the event.
7160:S 11 Feb 2020 14:12:09.763 * Master replied to PING, replication can continue...
7160:S 11 Feb 2020 14:12:09.764 # Unable to AUTH to MASTER: -ERR invalid password

1.2.2 일관성 없는 Redis 버전

Redis Master는 버전 5.0.7을 사용하고 Redis Slave는 버전 3.2.12를 사용합니다
. 02-11-14-35-04.png)]

슬레이브 로그에는 DRB 형식이 호환되지 않는다는 메시지가 표시됩니다.
[외부 링크 이미지 전송에 실패했습니다. 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 이미지를 저장하고 직접 업로드하는 것이 좋습니다(img-g0DIl0I2-1693219416530) ( png/2020-02-11-14-34-24.png)]

1.2.3 원격으로 연결할 수 없습니다

보안 모드가 켜져 있으면 바인딩 주소나 비밀번호가 설정되지 않습니다.

protected-mode yes # 该选项开启安全模式
bind 0.0.0.0 # 表示可以从任意地址连接redis
requirepass www.suosuoli.cn # 设置密码

2. 레디스 클러스터

Redis의 마스터-슬레이브 아키텍처는 마스터와 슬레이브 역할 사이를 자동으로 전환할 수 없습니다. 즉, 마스터에 Redis 서비스
예외, 호스트 정전, 디스크 손상 등이 있는 경우 마스터를 사용할 수 없으며 Redis 고가용성은 자체 달성할 수 없습니다. - 전송 실패
(슬레이브에서 마스터로 승격) 시 슬레이브 Redis 서버로 전환하려면 수동으로 환경 구성을 변경해야 하며, 또한
Redis 서비스의 병렬 쓰기 성능은 수평 확장이 불가능합니다. 비즈니스 글쓰기 요구 사항을 충족할 수 없는 경우
두 가지 핵심 문제를 해결해야 합니다. 지금 바로:

  1. 마스터와 슬레이브 역할 간의 원활한 전환으로 비즈니스가 인식하지 못하고 비즈니스 사용에 영향을 미치지 않습니다.
  2. Redis 서버는 동적으로 수평으로 확장될 수 있으므로 여러 서버가 병렬로 쓰기를 수행하여 더 높은 동시성을 달성할 수 있습니다.
Redis 클러스터를 구현하는 방법에는 여러 가지가 있습니다.
클라이언트 조각화
프록시 샤딩
레디스 클러스터

2.1 Sentinel-Redis 센티넬 메커니즘

Sentinel 프로세스는 Redis 클러스터에서 마스터 마스터 서버의 작동 상태를 모니터링하는 데 사용되며, 마스터 마스터 서버에 장애가 발생하면 마스터와 슬레이브 서버 간을 전환하여 시스템
의 고가용성을 보장할 수 있습니다.
redis2.6+ 버전 중간.

Redis의 센티널 메커니즘은 버전 2.8부터 안정화되었습니다.
일반적으로 프로덕션 환경에서는 Redis 버전 2.8 이상을 사용하는 것이 좋습니다 .

Sentinel은 하나의 아키텍처에서 여러 Sentinel 프로세스를 실행할 수 있는 분산 시스템으로, 이러한
프로세스는 gossip 프로토콜을 사용하여 마스터 서버가 오프라인인지 여부에 대한 정보를 받고,
투표 프로토콜(Agreement Protocols)을 사용하여 자동 장애 조치를 수행할지 여부와 어떤 슬레이브를 수행할지 결정합니다. 새로운 마스터로 선정됩니다
. 각 센티널(Sentinel) 프로세스는 정기적으로 다른 센티널(Sentinel), 마스터, 슬레이브에게
메시지를 상대방이 "살아 있는지" 확인하고, 상대방이 지정된 구성 시간 내에 응답을 받지 못한 것으로 확인되면 (구성 가능),
상대방이 오프라인 상태라고 일시적으로 생각하는 것을 소위 "주관적 다운"이라고 합니다. 주관적
이란 각 구성원이 가지고 있고 같을 수도 있고 다를 수도 있는 고유한 의식입니다. 영어명: 주관적 다운(Subjective Down)이라고 함 SDOWN으로. 주관적인 가동 중지 시간이 있으면 객관적인 가동 중지 시간도 있어야 합니다
. "sentinel 그룹"에 속한 대부분의 Sentinel 프로세스가 Master 메인 서버에서 SDOWN 판단을 하고
SENTINEL is-master-down-by-addr 명령어를 통해 서로 통신을 하게 되면 결과적으로 Master Server는 오프라인 판단이 된다
.* *"Objectively Down"**.어떤 의식에 의거하지 않고 실제로 존재하는 모든 것을 객관적으로 이르는 말. 영어 명칭은
Objectively Down, 줄여서 ODOWN이다. 특정 투표 알고리즘을 통해 나머지 슬레이브 서버 노드 중 하나를
선택하여 마스터 서버 노드로 승격시킨 후 해당 구성이 자동으로 수정되고 장애 조치가 활성화됩니다.

Sentinel 메커니즘은 마스터와 슬레이브 역할의 전환 문제를 해결할 수 있습니다.

2.1.1 마스터-슬레이브 복제 수동 구성

환경

주인 IP 역할
redis-서버-노드1 192.168.131.148 주인
redis-node2 192.168.131.149 노예
redis-node3 192.168.131.150 노예

새 슬레이브 추가: 192.168.131.150 구성

192.168.131.150:6379> REPLICAOF 192.168.131.148 6379
OK
192.168.131.150:6379> CONFIG SET masterauth stevenux
OK
(2.27s)

증가 후 마스터의 상태

192.168.131.148:6379> auth stevenux
OK
192.168.131.148:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.131.149,port=6379,state=online,offset=13427,lag=1
slave1:ip=192.168.131.150,port=6379,state=online,offset=0,lag=0
master_replid:85f9d986a9f7e06d5bf273d8b1004d517e20a7ea
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:13427
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:13232
repl_backlog_histlen:196

새로 추가된 슬레이브 데이터 동기화 보기

[root@redis-node3 ~]# redis-cli -h 192.168.131.150
192.168.131.150:6379> KEYS *
1) "key3"
2) "keyone"
3) "zset1"
4) "menu"
5) "set1"
# 同步正常

2.1.1.1 장애 조치 중 Redis에 대한 프로그램 연결

다양한 Redis 클라이언트의 공식 문서
[외부 링크 이미지 전송 실패, 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 이미지를 저장하고 직접 업로드하는 것이 좋습니다 (img-2O6QOYEG-19416532)(png/2020-02-11 -16-22-44.png)]

Java 클라이언트는 Jedis를 통해 Redis에 연결하는데, Java 코드를 사용할 때 Jedis 객체를 생성하기만 하면
여러 개의 Jedis 연결 풀을 구축하여 Redis에 연결할 수 있으며 애플리케이션에서 연결 풀을 직접 호출하여 Redis에 연결할 수 있습니다.

고가용성을 보장하기 위해 Redis 서비스는 일반적으로 Sentinel 모드로 배포되며, Redis 서비스의 주요 서비스가
다운되면 다른 슬레이브 서비스가 중재되어 마스터 역할을 하게 됩니다. 현재 애플리케이션이
Jedis 연결 풀을 사용하고 마스터 서비스가 다운되더라도 애플리케이션은 여전히 ​​새로운 마스터 서비스에 연결할 수 없습니다. 이 문제를 해결하기 위해
Jedis는 해당 Sentinel 구현도 제공합니다. Redis Sentinel 마스터-슬레이브에서 사용할 수 있습니다. 전환 시
애플리케이션에 알리고 애플리케이션을 새로운 마스터 서비스에 연결합니다.


Redis Sentinel의 사용은 프로그램을 사용하여 Redis에 연결할 때 매우 간단합니다. JedisPool에 Sentinel 및 MasterName 매개 변수를 추가하기 만 하면 됩니다 . JRedis Sentinel의 하위 계층
은 Redis 구독을 기반으로 Redis 마스터-슬레이브 서비스의 전환 알림을 실현합니다. Reids 마스터-슬레이브 전환이 발생하면 Sentinel 알림이
Jedis에게 연결 전환을 적극적으로 알리기 위해 전송됩니다
. JedisSentinelPool은 연결 풀에서 연결 개체를 얻을 때마다 연결 개체를 감지합니다. 이 링크와 Sentinel의 마스터 연결 매개 변수가 서비스가 일관성이 없으면
연결이 닫힙니다. , 새로운 Jedis 연결 개체를 다시 얻습니다.

2.1.2 Sentinel 구성 파일 편집

Sentinel은 Redis 서버와 별도로 배포가 가능하며, 실제 사용 시에는 일반적으로 동일한 호스트에 구성됩니다.

마스터:192.168.131.148


슬레이브:192.168.131.149
슬레이브:192.168.131.150

2.1.3 센트리 시작

마스터:192.168.131.148

[root@redis-server-node1 ~]# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf
[root@redis-server-node1 ~]# ss -ntl
State      Recv-Q Send-Q            Local Address:Port             Peer Address:Port
LISTEN     0      100                   127.0.0.1:25                          *:*
LISTEN     0      128                   127.0.0.1:9000                        *:*
LISTEN     0      50                            *:3306                        *:*
LISTEN     0      511                           *:26379                       *:*
LISTEN     0      511                           *:6379                        *:*
LISTEN     0      128                           *:111                         *:*
LISTEN     0      128                           *:22                          *:*
LISTEN     0      100                       [::1]:25                       [::]:*
LISTEN     0      128                        [::]:111                      [::]:*
LISTEN     0      128                        [::]:22                       [::]:*

슬레이브:192.168.131.149

[root@redis-node2 etc]# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf
[root@redis-node2 etc]# ss -ntl
State      Recv-Q Send-Q              Local Address:Port         Peer Address:Port
LISTEN     0      100                       127.0.0.1:25                 *:*
LISTEN     0      511                 192.168.131.149:26379              *:*
LISTEN     0      511                               *:6379               *:*
LISTEN     0      128                               *:111                *:*
LISTEN     0      128                               *:80                 *:*
LISTEN     0      128                               *:22                 *:*
LISTEN     0      100                           [::1]:25              [::]:*
LISTEN     0      128                            [::]:111             [::]:*
LISTEN     0      128                            [::]:22              [::]:*

슬레이브:192.168.131.150

[root@redis-node3 etc]# /apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf
[root@redis-node3 etc]# ss -ntl
State      Recv-Q Send-Q              Local Address:Port             Peer Address:Port
LISTEN     0      100                     127.0.0.1:25                          *:*
LISTEN     0      511               192.168.131.150:26379                       *:*
LISTEN     0      511                             *:6379                        *:*
LISTEN     0      128                             *:111                         *:*
LISTEN     0      128                             *:80                          *:*
LISTEN     0      128                             *:22                          *:*
LISTEN     0      100                         [::1]:25                       [::]:*
LISTEN     0      128                          [::]:111                      [::]:*
LISTEN     0      128                          [::]:80                       [::]:*
LISTEN     0      128                          [::]:22                       [::]:*

센티넬 서비스 로그

[root@redis-server-node1 ~]# tail -f /apps/redis/logs/sentinel_26379.log
6752:X 11 Feb 2020 16:38:37.989 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=6752, just started
6752:X 11 Feb 2020 16:38:37.989 # Configuration loaded
6753:X 11 Feb 2020 16:38:37.990 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6753:X 11 Feb 2020 16:38:37.991 * Running mode=sentinel, port=26379.
6753:X 11 Feb 2020 16:38:37.991 # Sentinel ID is e67caf2137b93d25c9b3ae98f4da1b37722c33ae
6753:X 11 Feb 2020 16:38:37.992 # +monitor master mymaster 192.168.131.148 6379 quorum 2
6753:X 11 Feb 2020 16:38:37.992 * +slave slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.148 6379
6753:X 11 Feb 2020 16:38:37.993 * +slave slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6753:X 11 Feb 2020 16:38:48.759 * +sentinel sentinel e6a6c4e2aeac5e259d2fab347e14ebe8c260b875 192.168.131.149 26379 @ mymaster 192.168.131.148 6379
6753:X 11 Feb 2020 16:38:57.607 * +sentinel sentinel d1322d808ebd2abd6e65fad6f53599aa4eb11ddc 192.168.131.150 26379 @ mymaster 192.168.131.148 6379

2.1.4 장애 조치 테스트를 위해 Redis 마스터 중지

Redis 마스터 상태 보기

192.168.131.148:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.131.149,port=6379,state=online,offset=91470,lag=1
slave1:ip=192.168.131.150,port=6379,state=online,offset=91470,lag=1
master_replid:85f9d986a9f7e06d5bf273d8b1004d517e20a7ea
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:91615
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:13232
repl_backlog_histlen:78384

현재 센티넬 상태

sentinel 상태, 특히 마지막 줄에는 masterIP 수, 슬레이브 수,
sentinel 수 등이 포함되며 이는 모든 서버 수와 ​​일치해야 합니다.

[root@redis-server-node1 ~]# redis-cli -h 192.168.131.148 -p 26379
192.168.131.148:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.131.148:6379,slaves=2,sentinels=3
# 最后一行指明masterIP和Slave个数及Sentinel个数

마스터 중지

[root@redis-server-node1 ~]# systemctl stop redis
[root@redis-server-node1 ~]# ss -ntl
State       Recv-Q Send-Q                     Local Address:Port                                    Peer Address:Port
LISTEN      0      100                            127.0.0.1:25                                                 *:*
LISTEN      0      128                            127.0.0.1:9000                                               *:*
LISTEN      0      50                                     *:3306                                               *:*
LISTEN      0      511                                    *:26379                                              *:*

2.1.5 장애 조치 중 Sentinel 로그 분석

단일 서버에서 마스터를 중지한 후 redis-node3(192.168.131.150)을 새 마스터로 선택합니다.

6011:X 11 Feb 2020 16:38:55.553 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
6011:X 11 Feb 2020 16:38:55.553 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=6011, just started
6011:X 11 Feb 2020 16:38:55.553 # Configuration loaded
6012:X 11 Feb 2020 16:38:55.555 * Increased maximum number of open files to 10032 (it was originally set to 1024).
6012:X 11 Feb 2020 16:38:55.555 * Running mode=sentinel, port=26379.

########################################## 当前Redis哨兵的运行ID
6012:X 11 Feb 2020 16:38:55.556 # Sentinel ID is d1322d808ebd2abd6e65fad6f53599aa4eb11ddc

########################################## 当前使用仲裁成员是2个
6012:X 11 Feb 2020 16:38:55.557 # +monitor master mymaster 192.168.131.148 6379 quorum 2
6012:X 11 Feb 2020 16:38:55.558 * +slave slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 16:38:55.559 * +slave slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 16:38:56.384 * +sentinel sentinel e67caf2137b93d25c9b3ae98f4da1b37722c33ae 192.168.131.148 26379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 16:38:56.891 * +sentinel sentinel e6a6c4e2aeac5e259d2fab347e14ebe8c260b875 192.168.131.149 26379 @ mymaster 192.168.131.148 6379

########################################## 主观认为宕机
6012:X 11 Feb 2020 17:12:59.241 # +sdown master mymaster 192.168.131.148 6379

########################################## 仲裁后确认为宕机
6012:X 11 Feb 2020 17:12:59.305 # +odown master mymaster 192.168.131.148 6379 #quorum 2/2
6012:X 11 Feb 2020 17:12:59.305 # +new-epoch 1

########################################## 尝试故障转移
6012:X 11 Feb 2020 17:12:59.305 # +try-failover master mymaster 192.168.131.148 6379

########################################## 投票选取Master
6012:X 11 Feb 2020 17:12:59.308 # +vote-for-leader d1322d808ebd2abd6e65fad6f53599aa4eb11ddc 1
6012:X 11 Feb 2020 17:12:59.310 # e67caf2137b93d25c9b3ae98f4da1b37722c33ae voted for d1322d808ebd2abd6e65fad6f53599aa4eb11ddc 1
6012:X 11 Feb 2020 17:12:59.310 # e6a6c4e2aeac5e259d2fab347e14ebe8c260b875 voted for d1322d808ebd2abd6e65fad6f53599aa4eb11ddc 1
6012:X 11 Feb 2020 17:12:59.385 # +elected-leader master mymaster 192.168.131.148 6379

########################################## 故障转移主机为192.168.131.148
6012:X 11 Feb 2020 17:12:59.385 # +failover-state-select-slave master mymaster 192.168.131.148 6379

########################################## 故障转移目标为192.168.131.150
6012:X 11 Feb 2020 17:12:59.476 # +selected-slave slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:12:59.476 * +failover-state-send-slaveof-noone slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:12:59.532 * +failover-state-wait-promotion slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:13:00.279 # +promoted-slave slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:13:00.279 # +failover-state-reconf-slaves master mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:13:00.350 * +slave-reconf-sent slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:13:01.377 * +slave-reconf-inprog slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:13:01.449 # -odown master mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:16:00.291 # +failover-end-for-timeout master mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:16:00.291 # +failover-end master mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:16:00.291 * +slave-reconf-sent-be slave 192.168.131.150:6379 192.168.131.150 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:16:00.291 * +slave-reconf-sent-be slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.148 6379
6012:X 11 Feb 2020 17:16:00.291 # +switch-master mymaster 192.168.131.148 6379 192.168.131.150 6379
6012:X 11 Feb 2020 17:16:00.291 * +slave slave 192.168.131.149:6379 192.168.131.149 6379 @ mymaster 192.168.131.150 6379
6012:X 11 Feb 2020 17:16:00.291 * +slave slave 192.168.131.148:6379 192.168.131.148 6379 @ mymaster 192.168.131.150 6379
6012:X 11 Feb 2020 17:16:30.315 # +sdown slave 192.168.131.148:6379 192.168.131.148 6379 @ mymaster 192.168.131.150 6379

2.1.6 장애 조치 후 구성 파일이 수정됩니다.

replicaof IP PORTredis.conf 줄의 마스터 IP는 장애 조치 후에 수정되고
sentinel.conf의 마스터 IP sentinel monitor IP도 수정됩니다.

redis-node2–Slave:192.168.131.149 원래 IP는
redis-server-node1–Master:192.168.131.148을 가리킵니다.

장애 조치 후에는 새로 선출된 마스터인 192.168.131.150이 되어야 합니다.

[root@redis-node2 etc]# vim /apps/redis/etc/redis.conf
################################# REPLICATION #################################
replicaof 192.168.131.150 6379
...

[root@redis-node2 etc]# vim /apps/redis/etc/sentinel.conf
# Default is 30 seconds.
sentinel monitor mymaster 192.168.131.150 6379 2
...

원래 마스터의 구성 파일을 수정하고 해당 마스터가 새 마스터를 가리키도록 합니다: 192.168.131.150
새 마스터의 복제 상태:

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.131.148,port=6379,state=online,offset=897322,lag=0
slave1:ip=192.168.131.149,port=6379,state=online,offset=897322,lag=1
master_replid:f7f6ecbb5964ee44160654fbf82fedac3303ba67
master_replid2:85f9d986a9f7e06d5bf273d8b1004d517e20a7ea
master_repl_offset:897322
second_repl_offset:449770
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:13722
repl_backlog_histlen:883601

2.2 레디스 클러스터

초기 Redis 분산 클러스터 배포 체계:

  1. 클라이언트 요청 샤딩: 클라이언트 프로그램은 키 쓰기 할당 및 쓰기 Redis 노드를 결정하지만 클라이언트는
    쓰기 할당, 고가용성 관리 및 장애 조치 등을 처리 해야 합니다.
  2. 프록시 방식: Redis 프록시는 타사 소프트웨어를 기반으로 구현됩니다. 클라이언트는 먼저 프록시 계층에 연결하고 프록시 계층은
    키 작성 및 배포를 실현합니다. 클라이언트에게는 상대적으로 간단하지만 상대적으로 번거로운 작업입니다. 클러스터 관리 노드를 늘리거나 줄일 수 있으며 프록시
    자체도 단일 지점이며 성능 병목 현상이 발생합니다.

Redis의 현재 솔루션:
Redis의 센티널 메커니즘에서는 Redis의 고가용성 문제를 해결할 수 있습니다. 즉, 마스터가 실패하면 슬레이브가
자동으로 마스터로 승격되어 Redis 서비스의 정상적인 사용을 보장할 수 있습니다.
그러나 Redis 독립형 쓰기 문제는 해결할 수 없습니다. 병목 현상 문제, 즉 단일 머신의 Redis 쓰기 성능은 단일 머신의 메모리 크기, 동시성 수, 속도 등의 요인에 의해 제한 됩니다
. 따라서 Redis는
Redis 버전 3.0 이후에 비중앙 아키텍처를 갖춘 Redis 클러스터 메커니즘을 공식적으로 출시했습니다. 비중앙 Redis에서는 클러스터에서 각 노드는 현재 노드 데이터와 전체 클러스터
상태를 저장하고, 각 노드는 다른 모든 노드와 연결되어 있으며, 특징은 다음과 같습니다.

1.所有 Redis 节点使用(PING 机制)互联
2.集群中某个节点的失效,是整个集群中超过半数的节点监测都失效才算真正的失效
3.客户端不需要 proxy 即可直接连接 redis,应用程序需要写全部的 redis 服务器 IP4.redis cluster 把所有的 redis node 映射到 0-16383 个槽位(slot)上,读写需要到指定的
redis node 上进 操作,因此有多少个 reids node 相当于 redis 并发扩展了多少倍。
5.Redis cluster 预先分配 16384(slot)槽位,当需要在 redis 集群中写入一个 key-value
的时候,会使用 CRC16(key) mod 16384 之后的值,决定将 key 写入值哪一个槽位从而决定写入哪
一个 Redis 节点上,从而有效解决 机瓶颈。

2.2.1 Redis 클러스터 아키텍처

2.2.1.1 Redis 클러스터 기본 아키텍처

세 개의 주요 노드가 A, B, C 세 개의 노드이고 해시 슬롯(해시 슬롯)을 사용하여 16384개의
슬롯을 할당하는 경우 세 노드의 슬롯 간격은 다음과 같습니다.

节点A覆盖 05460
节点B覆盖 546110922
节点C覆盖 1092316383

[외부 링크 사진 전송이 실패했습니다. 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다(img-DgXmcPbd-1693219416533)(png/Redis-Cluster-Master-no-slave.png )]

2.2.1.2 Redis 클러스터 마스터-슬레이브 아키텍처

Redis 클러스터 아키텍처는 동시성 문제를 해결하지만 새로운 문제가 발생합니다.
각 Redis 마스터의 고가용성을 어떻게 해결합니까 ?

이때 Redis 클러스터 마스터-슬레이브 아키텍처가 생방송실에 들어왔습니다! !

Redis 클러스터의 개략도는 다음과 같습니다.

[외부 링크 사진 전송이 실패했습니다. 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다(img-XUJeQDmF-1693219416534)(png/Redis-Cluster-Master-slave.png)]

2.2.2 Redis 클러스터 배포

환경 준비

주인 CPU 이름 역할
192.168.131.155 redis-서버-노드1 주인
192.168.131.156 redis-node2 주인
192.168.131.157 redis-node3 주인
192.168.131.158 redis-node4 노예
192.168.131.159 redis-node5 노예
192.168.131.154 redis-node6 노예
192.168.131.160 redis-node7 여분의
192.168.131.161 redis-node8 여분의

2.2.2.1 Redis 클러스터 마스터-슬레이브 아키텍처 생성을 위한 전제 조건

  1. 각 Redis 노드 노드는 동일한 하드웨어 구성, 동일한 비밀번호 및 동일한 Redis 버전을 사용합니다.
  2. 각 노드마다 활성화해야 하는 파라미터는 cluster-enabled yes클러스터를 활성화하며,
    활성화 후 [cluster]에 Redis 프로세스 정보가 표시됩니다.
   [root@redis-server-node5 ~]# ps -ef | grep redis
   redis      1788      1  0 15:12 ?        00:00:15 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]  # 集群运行模式
  1. cluster-config-file nodes-6380.conf이 옵션은 각 노드에 대해 클러스터에서 생성된 구성 파일을 구성하며,
    이는 수동 작업 없이 Redis Cluster 클러스터에서 자동으로 생성 및 유지 관리됩니다.
  2. 모든 Redis 서버에는 사전에 데이터가 없어야 합니다.
  3. 먼저 독립 실행형으로 시작

2.2.2.2 Redis 클러스터 생성

Redis 3 및 Redis 4 버전:
클러스터 관리 도구 redis-trib.rb를 사용해야 합니다. 이 도구는 redis 클러스터를 관리하기 위해 공식적으로 시작되었습니다
. Redis의 소스 코드 src 디렉터리에 통합되어 있으며 다음을 기반으로 합니다. redis에서 제공하는 클러스터 명령 패키지로 간단하고 편리하며
실용적인 작업 도구입니다. redis-trib.rb는 redis 작성자가 ruby를 사용하여 개발한 것입니다.
centos 시스템 yum이 설치하는 Ruby는 다음과 같이 하위 버전이 있습니다.

~$ yum install ruby rubygems
~$ yum install redis

[외부 링크 사진 전송에 실패했습니다. 소스 사이트에 리칭 방지 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다. (img-3eUOgBxx-1693219416534)(png/2020-02-12-19-46-15 .png)]

컴파일하고 설치하려면 더 높은 버전의 Ruby를 다운로드하세요.

[root@redis-server-node1 redis4]# yum remove ruby rubygems  -y
[root@redis-server-node1 redis4]# wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
[root@redis-server-node1 src]# tar xf ruby-2.5.5.tar.gz
[root@redis-server-node1 src]# cd ruby-2.5.5
[root@redis-server-node1 ruby-2.5.5]# ./configure
[root@redis-server-node1 ruby-2.5.5]# make -j 2
[root@redis-server-node1 ruby-2.5.5]# make install
[root@redis-server-node1 ruby-2.5.5]# gem  install redis
[root@redis-server-node1 ruby-2.5.5]# gem install redis
Fetching: redis-4.1.3.gem (100%)
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 0 seconds
1 gem installed
[root@redis-server-node1 ruby-2.5.5]# find / -name redis-trib.rb
/usr/local/src/redis-4.0.14/src/redis-trib.rb
[root@redis-server-node1 ruby-2.5.5]# cp /usr/local/src/redis-4.0.14/src/redis-trib.rb /usr/bin/

#如果无法在线安装,可以下载reids模块安装包离线安装
#https://rubygems.org/gems/redis #先下载redis模块安装包
#gem install -l redis-3.3.0.gem #安装redis模块

설치 후 redis-trib.rb명령이 실행 가능한지 확인하십시오.

[root@redis-server-node1 ruby-2.5.5]# redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN
                  --replicas <arg>
  check           host:port
  info            host:port
  fix             host:port
                  --timeout <arg>
  reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port
                  --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
                  --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.

Redis에 비밀번호가 설정되어 있는 경우 Ruby 클라이언트 소스코드를 변경하고 설정된 비밀번호로 비밀번호를 수정해야 합니다.

[root@redis-server-node1 ruby-2.5.5]# vim /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb
# frozen_string_literal: true

require_relative "errors"
require "socket"
require "cgi"
class Redis
  class Client

    DEFAULTS = {
    
    
      :url => lambda {
    
     ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "stevenux",  # 原来值为nil
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :reconnect_delay => 0,
      :reconnect_delay_max => 0.5,
      :inherit_socket => false
    }
    ...

각 Redis 인스턴스를 먼저 시작하면 시작 후 Redis 6379 + 10000을 기반으로 추가 포트가 생성됩니다.

[root@redis-server-node1 ~]# systemctl start redis
[root@redis-server-node1 ~]# ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      100       127.0.0.1:25                            *:*
LISTEN      0      511               *:16379 # 集群主机间通信的端口   *:*
LISTEN      0      128       127.0.0.1:9000                          *:*
LISTEN      0      50                *:3306                          *:*
LISTEN      0      511               *:6379 # Redis端口              *:*
LISTEN      0      128               *:111                           *:*
LISTEN      0      128               *:22                            *:*
LISTEN      0      100           [::1]:25                         [::]:*
LISTEN      0      128            [::]:111                        [::]:*
LISTEN      0      128            [::]:22                         [::]:*

Redis3/4 버전은 Ruby 스크립트를 사용하여 클러스터를 생성합니다.

[root@redis-server-node1 ~]# redis-trib.rb create --replicas 1 192.168.100.10:6379 192.168.100.11:6379 192.168.100.12:6379 192.168.100.13:6379 192.168.100.14:6379 192.168.100.15:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.100.10:6379
192.168.100.11:6379
192.168.100.12:6379
Adding replica 192.168.100.14:6379 to 192.168.100.10:6379
Adding replica 192.168.100.15:6379 to 192.168.100.11:6379
Adding replica 192.168.100.13:6379 to 192.168.100.12:6379
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-5460 (5461 slots) master
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:5461-10922 (5462 slots) master
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:10923-16383 (5461 slots) master
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
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.100.10:6379)
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:5461-10922 (5462 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.

主从对应关系:主<-->192.168.100.10 <--> 192.168.100.14
192.168.100.11 <--> 192.168.100.15
192.168.100.12 <--> 192.168.100.13

Redis에 데이터가 남아 있으면 클러스터 생성이 실패하고 데이터가 지워집니다.

192.168.131.155>FLUSHDB
192.168.131.155>FLUSHALL
192.168.131.155>CLUSTER RESET # 重置集群

Redis3/4 클러스터 상태 보기:

[root@redis-server-node1 ~]# redis-trib.rb info 192.168.100.14:6379
192.168.100.12:6379 (6789c52c...) -> 0 keys | 5461 slots | 1 slaves.
192.168.100.11:6379 (9f26fd0a...) -> 0 keys | 5462 slots | 1 slaves.
192.168.100.10:6379 (90c32a43...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

Redis5 버전은 자체 redis-cli명령을 사용하여 클러스터를 생성합니다.

[root@redis-server-node1 ~]# redis-cli -a stevenux --cluster create 192.168.131.155:6379 192.168.131.156:6379 192.168.131.157:6379 192.168.131.158:6379 192.168.131.159:6379 192.168.131.154: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 192.168.131.159:6379 to 192.168.131.155:6379
Adding replica 192.168.131.154:6379 to 192.168.131.156:6379
Adding replica 192.168.131.158:6379 to 192.168.131.157:6379
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.155:6379
   slots:[0-5460] (5461 slots) master
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.156:6379
   slots:[5461-10922] (5462 slots) master
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.157:6379
   slots:[10923-16383] (5461 slots) master
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.158:6379
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.159:6379
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.154:6379
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
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.131.155:6379)
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.155:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.157:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.158:6379
   slots: (0 slots) slave
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.159:6379
   slots: (0 slots) slave
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.154:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.156:6379
   slots:[5461-10922] (5462 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.

主从对应关系:主<--># 192.168.131.155<-->192.168.131.159
# 192.168.131.156<-->192.168.131.154
# 192.168.131.157<-->192.168.131.158

没设置静态IP的后果,第二天IP全变了:主 <-->192.168.131.168 <--> 192.168.131.165
192.168.131.166 <--> 192.168.131.167
192.168.131.170 <--> 192.168.131.169

변화된 환경

주인 CPU 이름 역할
192.168.131.165 redis-node5 노예
192.168.131.166 redis-node3 주인
192.168.131.167 redis-node4 노예
192.168.131.168 redis-서버-노드1 주인
192.168.131.169 redis-node6 노예
192.168.131.170 redis-node2 주인
192.168.131.160 redis-node7 여분의
192.168.131.161 redis-node8 여분의

2.2.2.3 Redis 클러스터 상태 확인

마스터들이 적게 모으고 많이 남기는 걸 보니 속도도 빠르고 휴일도 10시가 넘으니 피드백 받기가 난리네요. 수도세, 전기세도 고려하고 있습니다. 영적미디어텍. 마스터인증 비밀번호는 이기
때문에 설정되지 않은 경우, 마스터가 설정된 적이 없지만 클러스터가 이미 실행 중이므로 클러스터가 이미 실행 중이
어야 합니다 각 슬레이브 콘솔은 config set을 사용하여 masterauth 비밀번호를 설정하거나,
가급적이면 다음에서 비밀번호를 설정한 후 각 redis 구성 파일 에 이를 기록합니다. 제어점을 구성 파일에 기록합니다.

이때 마스터-슬레이브 관계를 확인해보면 메인 라이브러리가 다운된 상태로 나오니 masterauth그냥
설정하세요 /2020-02-12-22-52-15.png)]

라이브러리에서 실행 중 CONFIG SET masterauth stevenux
[외부 링크 사진 전송에 실패했습니다. 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다. (img-CdbqYQ8F-1693219416535)(png/2020-02-12-22 -55-19.png )]

다른 두 개의 슬레이브 라이브러리는 별도로 설정됩니다.CONFIG SET masterauth stevenux

2.2.2.4 슬레이브에 연결된 마스터의 상태가 Up인지 확인

[외부 링크 사진 전송에 실패했습니다. 소스 사이트에 리칭 방지 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다. (img-06RXOaQV-1693219416536)(png/2020-02-13-13-42-03 .png)]

192.168.131.165:6379> info replication
# Replication
role:slave
master_host:192.168.131.168
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:922
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8792e94ec747252b1521d724319c63b8eff47c78
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:922
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:922

192.168.131.167:6379> info replication
# Replication
role:slave
master_host:192.168.131.166
master_port:6379
master_link_status:up    # master为166,已连接
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:22848
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7ccc9bf85b51d654c5ca00ccb77295341daa55d8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:22848
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:22848

192.168.131.169:6379> INFO replication
# Replication
role:slave
master_host:192.168.131.170
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:28
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2eb15a59d68d46b214b2a6ed0fa4819e13e12959
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byt

2.2.2.5 마스터와 슬레이브도 연결되어 있는지 확인

[root@redis-node3 ~]# redis-cli -h 192.168.131.166 -p 6379
192.168.131.166:6379> auth stevenux
OK
192.168.131.166:6379> ingo replication
(error) ERR unknown command `ingo`, with args beginning with: `replication`,
192.168.131.166:6379> info replication
# Replication
role:master
connected_slaves:1  # slave为167,已连接
slave0:ip=192.168.131.167,port=6379,state=online,offset=22750,lag=1
master_replid:7ccc9bf85b51d654c5ca00ccb77295341daa55d8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:22750
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:22750

192.168.131.168:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.131.165,port=6379,state=online,offset=740,lag=0
master_replid:8792e94ec747252b1521d724319c63b8eff47c78
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:740
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:740

192.168.131.170:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.131.169,port=6379,state=online,offset=616,lag=1
master_replid:2eb15a59d68d46b214b2a6ed0fa4819e13e12959
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:616
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:616

2.2.2.6 클러스터 상태 확인

[root@redis-node4 ~]# redis-cli -h 192.168.131.167 -p 6379
192.168.131.170:6379> 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 # 6个节点
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:24775
cluster_stats_messages_pong_sent:25321
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:50099
cluster_stats_messages_ping_received:25319
cluster_stats_messages_pong_received:24765
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:50086

2.2.2.7 클러스터 노드의 해당 관계 보기

192.168.131.170:6379> cluster nodes
f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.167:6379@16379 slave 55ac711af0b76fe380102a71c1a2f02657ec4009 0 1581581004450 4 connected
55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.166:6379@16379 master - 0 1581581003441 3 connected 10923-16383
686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.165:6379@16379 slave c50ffdba27c949e44fd108fbb43ea7f0b5155073 0 1581581002431 5 connected
827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379@16379 slave 25f50036c8e17175d1a4b7f2e9aca845af4114a6 0 1581581002000 6 connected
25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.156:6379@16379 myself,master - 0 1581581000000 2 connected 5461-10922
c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.168:6379@16379 master - 0 1581581001000 1 connected 0-5460

2.2.2.8 클러스터 데이터 쓰기 및 동기화 확인

각 마스터의 글쓰기:

[root@redis-node3 ~]# redis-cli -h 192.168.131.166
192.168.131.166:6379> set key1 v1 # 经过计算,当前key1的槽位不在本机,在192.168.131.170
(error) MOVED 9189 192.168.131.170:6379
192.168.131.166:6379> set key2 v2
(error) MOVED 4998 192.168.131.168:6379
192.168.131.166:6379> set key3 v3
(error) MOVED 935 192.168.131.168:6379
192.168.131.166:6379> set key4 v4   # 经过计算,当前key4的槽位在本机
OK
192.168.131.166:6379> get key4
(error) NOAUTH Authentication required.
192.168.131.166:6379> auth stevenux
OK
192.168.131.166:6379> get key4
"v4"

# 从库 Slave 可以看有哪些 key,但是无法读取 key 的内容:
# 从库查看数据,Slave为192.168.131.167
192.168.131.167:6379> keys *
1) "key4"
192.168.131.167:6379> get key4
(error) MOVED 13120 192.168.131.166:6379   # 提示只能在主库看内容

[root@redis-server-node1 ~]# redis-cli -h 192.168.131.168
192.168.131.168:6379> SET KEY1 V1
(error) NOAUTH Authentication required.
192.168.131.168:6379> auth stevenux
OK
192.168.131.168:6379> SET KEY1 V1
OK
192.168.131.168:6379> keys *
1) "KEY1"

# 从库查看数据,Slave为192.168.131.165
192.168.131.165:6379> keys *
1) "KEY1"
192.168.131.165:6379> get KEY1
(error) MOVED 5259 192.168.131.168:6379

[root@redis-node2 ~]# redis-cli -h 192.168.131.170
192.168.131.170:6379> SET KEY3 V7
(error) MOVED 13513 192.168.131.166:6379
192.168.131.170:6379> SET KEY2 V7
OK
192.168.131.170:6379> GET KEY2
(error) NOAUTH Authentication required.
192.168.131.170:6379> auth stevenux
OK
192.168.131.170:6379> GET KEY2
"V7"

# 从库查看数据,Slave为192.168.131.169
192.168.131.169:6379> keys *
1) "KEY2"
192.168.131.169:6379> get KEY2
(error) MOVED 9448 192.168.131.170:6379

2.2.2.9 클러스터 상태 확인 및 모니터링

# Redis4使用:
[root@s1 ~]# redis-trib.rb info  172.18.200.105:6379

# Redis5使用:
[root@redis-node3 ~]# redis-cli -a stevenux --cluster check 192.168.131.166:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.131.166:6379 (55ac711a...) -> 1 keys | 5461 slots | 1 slaves.
192.168.131.168:6379 (c50ffdba...) -> 1 keys | 5461 slots | 1 slaves.
192.168.131.170:6379 (25f50036...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.131.166:6379)
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.166:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.165:6379
   slots: (0 slots) slave
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.167:6379
   slots: (0 slots) slave
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.168:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[5461-10922] (5462 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.

2.2.3 Redis 클러스터 클러스터 노드 유지 관리

클러스터가 장기간 실행된 후에는
Redis 노드 노드 추가, 노드 축소, 노드 마이그레이션 등 하드웨어 장애, 네트워크 계획, 비즈니스 성장 등의 이유로 인해 기존 클러스터에 대한 해당 조정이 불가피합니다. 그리고 서버 교체. 노드를 추가
하고 노드를 삭제하려면 기존 슬롯 재할당 및 데이터 마이그레이션이 필요합니다. 노드를 추가하고 삭제하기 전에
충분한 준비가 필요한데, 이를 당연하게 여겨서는 안 됩니다.

2.2.3.1 동적으로 노드 추가


Redis 노드 노드를 추가하려면 이전 Redis 노드와 동일한 버전과 구성 이 필요하며 마스터와 슬레이브 각각 2개의 Redis 노드를 시작합니다 .실제
프로덕션 에서는 마스터와 슬레이브가 최종적으로 교체됩니다. 그리고 노예 도 마찬가지고, 노화의 정도도 마찬가지다.

예:
회사 비즈니스의 급속한 발전으로 인해 기존 3-마스터 및 3-슬레이브 Redis 클러스터 아키텍처는
기존 비즈니스의 동시 쓰기 요구 사항을 충족하지 못할 수 있으므로 회사는 긴급하게 두 대의 서버 192.168.131.163을 구입했습니다. 및 192.168.131.164
를 동적으로 클러스터에 추가하면 비즈니스 사용량 및 데이터 손실에 영향을 미치지 않으며 추가 프로세스는 다음과 같습니다.


Redis 노드의 구성 파일을 192.168.131.163 Redis 컴파일 및 설치 디렉터리에 동기화하고 구성 파일의 수신 IP 에 주의하세요 .

[root@redis-node3 ~]# scp /apps/redis/etc/redis.conf 192.168.131.163:/apps/redis/etc/
The authenticity of host '192.168.131.163 (192.168.131.163)' can't be established.
ECDSA key fingerprint is SHA256:Th4hwF8/bfgThJ5IrqAGh3GSsasTSmHmPfVNDI6EKss.
ECDSA key fingerprint is MD5:fb:cf:db:aa:73:87:4c:8f:4e:bb:c3:d8:45:14:20:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.131.163' (ECDSA) to the list of known hosts.
[email protected]'s password:
redis.conf                                               100% 5183     7.0MB/s   00:00

[root@redis-node7 ~]# ll /apps/redis/etc/
total 12
-rw-r--r-- 1 redis redis 5183 Feb 13 16:38 redis.conf

추가하려면 Redis를 시작하세요.

[root@redis-node7 ~]# systemctl start redis
[root@redis-node7 ~]# ss -ntl
State       Recv-Q Send-Q       Local Address:Port           Peer Address:Port
LISTEN      0      100              127.0.0.1:25                        *:*
LISTEN      0      511                      *:16379                     *:*
LISTEN      0      511                      *:6379                      *:*
클러스터에 노드 추가

현재 Redis 클러스터에 새 Redis 노드 192.168.131.163-redis-node7을 추가합니다.
Redis 4를 추가하는 방법:

[root@redis-server-node1 ~]# redis-trib.rb add-node 192.168.100.16:6379 192.168.100.10:6379
>>> Adding node 192.168.100.16:6379 to cluster 192.168.100.10:6379
>>> Performing Cluster Check (using node 192.168.100.10:6379)
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.100.16:6379 to make it join the cluster.
[OK] New node added correctly. # 添加成功

Redis 5를 추가하는 방법:

[root@redis-node3 ~]# redis-cli -a stevenux --cluster add-node  192.168.131.163:6379  192.168.131.166:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.131.163:6379 to cluster 192.168.131.166:6379
>>> Performing Cluster Check (using node 192.168.131.166:6379)
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.166:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.165:6379
   slots: (0 slots) slave
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.167:6379
   slots: (0 slots) slave
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.168:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[5461-10922] (5462 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.
>>> Send CLUSTER MEET to node 192.168.131.163:6379 to make it join the cluster.
[OK] New node added correctly.   # 添加成功
슬롯 재배포

호스트를 추가한 후에는 클러스터에 추가된 새 호스트를 다시 조각화해야 합니다. 그렇지 않으면 조각화 없이 데이터를 쓸 수 없습니다.
현재 상태 확인:

Redis4:

[root@s1 ~]# redis-trib.rb  check    192.168.131.165:6379 #当前状态
[root@redis-server-node1 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots: (0 slots) master  # 192.168.100.16的曹位为0
   0 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12: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@s1 ~]# redis-trib.rb  reshard  192.168.131.163:6379 #重新分片
[root@redis-server-node1 ~]# redis-trib.rb reshard 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots: (0 slots) master
   0 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12: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.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 69e8db533eb78a831013a398bf2ef3ac4d23c8fb
Source node #1:all
  Moving slot 12286 from 6789c52caa900c09dda2df57132e73646bcf3735
    Moving slot 12287 from 6789c52caa900c09dda2df57132e73646bcf3735
Do you want to proceed with the proposed reshard plan (yes/no)?
...
Moving slot 12286 from 192.168.100.12:6379 to 192.168.100.16:6379:
Moving slot 12287 from 192.168.100.12:6379 to 192.168.100.16:6379:
[root@redis-server-node1 ~]#
[root@redis-server-node1 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   0 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.   # 重新分配成功

Redis5:

새로 추가된 마스터: 192.168.131.163 할당되지 않은 슬롯
[외부 링크 사진 전송 실패, 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다 (img-2iK9GpKt-1693219416537)(png/2020- 02-13 -16-56-46.png)]

다음을 사용하여 redis-cli --cluster reshard NEWIP:PORT슬롯을 재할당합니다.

[root@redis-node3 ~]# redis-cli -a stevenux --cluster reshard 192.168.131.163:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.131.163:6379)
M: 8b12fd741381878f6fa871a1c2995fc2ea9b939f 192.168.131.163:6379
   slots: (0 slots) master
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.167:6379
   slots: (0 slots) slave
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.168:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.166:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.165:6379
   slots: (0 slots) slave
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[5461-10922] (5462 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.
How many slots do you want to move (from 1 to 16384)?4096 # 需要迁移的槽位
What is the receiving node ID? 8b12fd741381878f6fa871a1c2995fc2ea9b939f  # 接收曹位的主机ID(新增的节点ID)
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.  # 输入all直接从各个master均分曹位到新节点
  Type 'done' once you entered all the source nodes IDs.
Source node #1: c50ffdba27c949e44fd108fbb43ea7f0b5155073  # 迁移曹位的源节点1
Source node #2: 55ac711af0b76fe380102a71c1a2f02657ec4009  # 迁移曹位的源节点2
Source node #3: 25f50036c8e17175d1a4b7f2e9aca845af4114a6  # 迁移曹位的源节点3
Source node #4: done    # 完成
    Moving slot 5468 from 25f50036c8e17175d1a4b7f2e9aca845af4114a6
    Moving slot 5469 from 25f50036c8e17175d1a4b7f2e9aca845af4114a6
    ...
     Moving slot 11809 from 55ac711af0b76fe380102a71c1a2f02657ec4009
    Moving slot 11810 from 55ac711af0b76fe380102a71c1a2f02657ec4009
Do you want to proceed with the proposed reshard plan (yes/no)?yes

슬롯 마이그레이션이 완료되었는지 확인

Redis4:

[root@redis-server-node1 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   0 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

Redis5:


새 마스터에 대한 슬레이브 노드 추가

고가용성을 달성하기 위해 새로 추가된 마스터의 슬레이브 라이브러리로 다른 노드 슬레이브: 192.168.131.164를 추가합니다.

Redis4:

# 添加
root@redis-server-node1 ~]# redis-trib.rb add-node 192.168.100.17:6379 192.168.100.10:6379
>>> Adding node 192.168.100.17:6379 to cluster 192.168.100.10:6379
>>> Performing Cluster Check (using node 192.168.100.10:6379)
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.100.17:6379 to make it join the cluster.
[OK] New node added correctly.


# 检查集群
[root@redis-server-node8 ~]# redis-trib.rb check 192.168.100.17
Invalid IP or Port (given as 192.168.100.17) - use IP:Port format
[root@redis-server-node8 ~]# redis-trib.rb check 192.168.100.17:6379
>>> Performing Cluster Check (using node 192.168.100.17:6379)
M: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) master
   0 additional replica(s)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   0 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

# 设置192.168.100.16为192.168.100.17的主库
[root@redis-server-node8 ~]# redis-cli -h 192.168.100.17
192.168.100.17:6379> auth stevenux
OK
192.168.100.17:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:b44fe27c247dee3c68779e8855d21a7b04b7deda
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.100.17:6379> cluster replicate 69e8db533eb78a831013a398bf2ef3ac4d23c8fb  # 192.168.100.16的ID
OK
192.168.100.17:6379> config set masterauth stevenux
OK
192.168.100.17:6379> info replication
# Replication
role:slave
master_host:192.168.100.16   # 设置成功
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:67ac09c6185d2004f3b4fcebc5a352f61340ec36
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0

# 检查集群完整性
[root@redis-server-node7 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 69e8db533eb78a831013a398bf2ef3ac4d23c8fb
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

Redis5:

[root@redis-node3 ~]# redis-cli -a stevenux --cluster add-node 192.168.131.164:6379 192.168.131.165:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.131.164:6379 to cluster 192.168.131.165:6379
>>> Performing Cluster Check (using node 192.168.131.165:6379)
S: 686b5cf3ae7719e43882d53dbe0da4ae40b2134e 192.168.131.165:6379
   slots: (0 slots) slave
   replicates c50ffdba27c949e44fd108fbb43ea7f0b5155073
M: 8b12fd741381878f6fa871a1c2995fc2ea9b939f 192.168.131.163:6379
   slots: (0 slots) master
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f762795742a5d6a19bb2735f3eb3db4fb86e24c3 192.168.131.167:6379
   slots: (0 slots) slave
   replicates 55ac711af0b76fe380102a71c1a2f02657ec4009
S: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
M: c50ffdba27c949e44fd108fbb43ea7f0b5155073 192.168.131.168:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 55ac711af0b76fe380102a71c1a2f02657ec4009 192.168.131.166: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.
>>> Send CLUSTER MEET to node 192.168.131.164:6379 to make it join the cluster.
[OK] New node added correctly.
새 노드 상태를 슬레이브로 변경

마스터의 슬레이브로 수동으로 지정해야 합니다. 그렇지 않으면 기본 역할은
새 노드에 마스터로 로그인하고 명령을 사용하여 CLUSTER REPLICATE MasterID마스터의 슬레이브로 구성하는 것입니다.

192.168.131.164:6379> CLUSTER REPLICATE f98ad5793d9faefdce6dccd274c9afd9d82f07de
192.168.131.164:6379> CONFIG SET masterauth stevenux


# 设置192.168.100.16为192.168.100.17的主库
[root@redis-server-node8 ~]# redis-cli -h 192.168.100.17
192.168.100.17:6379> auth stevenux
OK
192.168.100.17:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:b44fe27c247dee3c68779e8855d21a7b04b7deda
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
192.168.100.17:6379> cluster replicate 69e8db533eb78a831013a398bf2ef3ac4d23c8fb  # 192.168.100.16的ID
OK
192.168.100.17:6379> config set masterauth stevenux
OK
192.168.100.17:6379> info replication
# Replication
role:slave
master_host:192.168.100.16   # 设置成功
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:67ac09c6185d2004f3b4fcebc5a352f61340ec36
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0
현재 클러스터 상태 확인

각 Mater에 슬레이브 노드가 있는지 확인하십시오.

Redis4:

[root@redis-server-node7 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 69e8db533eb78a831013a398bf2ef3ac4d23c8fb
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

Redis5:

[root@redis-node5 ~]# redis-cli -a stevenux --cluster check 192.168.131.170:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.131.170:6379 (25f50036...) -> 0 keys | 4096 slots | 1 slaves.
192.168.131.167:6379 (d81888e8...) -> 0 keys | 4096 slots | 1 slaves.
192.168.131.168:6379 (314e1ca8...) -> 0 keys | 4096 slots | 1 slaves.
192.168.131.169:6379 (827a9a79...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.131.170:6379)
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[0-4095] (4096 slots) master
   1 additional replica(s)
M: d81888e80018bb7ecdd7778373317ea591beb16a 192.168.131.167:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 27b4c0993bdba6460123655a5167ca32ad3c497d 192.168.131.163:6379
   slots: (0 slots) slave
   replicates d81888e80018bb7ecdd7778373317ea591beb16a
S: 3f3f142332722bda502d1019cbdce87dbcb89b70 192.168.131.164:6379
   slots: (0 slots) slave
   replicates 314e1ca8d842bfafb8b059cab891e662b5240b3b
S: 4a1e762f3d69113005617379cb4d290c9a792185 192.168.131.165:6379
   slots: (0 slots) slave
   replicates 827a9a793e893c92036736678eed55175d3dc3e5
M: 314e1ca8d842bfafb8b059cab891e662b5240b3b 192.168.131.168:6379
   slots:[8192-12287] (4096 slots) master
   1 additional replica(s)
M: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots:[4096-8191] (4096 slots) master
   1 additional replica(s)
S: 809c3debc42c25256001b0abcac46b23d64b0402 192.168.131.166:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

2.2.3.2 동적으로 노드 삭제

노드를 추가할 때 먼저 클러스터에 노드를 추가한 후 슬롯을 할당합니다.노드를 삭제하는 작업은 노드를 추가하는 작업의
반대입니다.삭제된 Redis 노드의 슬롯을 먼저 마이그레이션하는 것입니다. 클러스터의 다른 Redis 노드
노드 Redis 노드의 슬롯이 완전히 마이그레이션되지 않은 경우
노드 삭제 시 데이터가 있으므로 삭제할 수 없다는 메시지가 표시됩니다.

삭제할 마스터의 슬롯을 다른 마스터로 마이그레이션

마이그레이션할 Redis 마스터의 소스 서버는 데이터가 없는지 확인해야 합니다. 그렇지 않으면 마이그레이션이 오류를 보고하고 강제로 중단됩니다.

Redis4:

# 192.168.100.16 --> 192.168.100.10  移动4096
root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.16:6379
...
Moving slot 12283 from 192.168.100.16:6379 to 192.168.100.10:6379:
Moving slot 12284 from 192.168.100.16:6379 to 192.168.100.10:6379:
Moving slot 12285 from 192.168.100.16:6379 to 192.168.100.10:6379:
Moving slot 12286 from 192.168.100.16:6379 to 192.168.100.10:6379:
Moving slot 12287 from 192.168.100.16:6379 to 192.168.100.10:6379:

[root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.16:6379
>>> Performing Cluster Check (using node 192.168.100.16:6379)
M: 69e8db533eb78a831013a398bf2ef3ac4d23c8fb 192.168.100.16:6379
   slots: (0 slots) master  # 已经没有槽位
   0 additional replica(s)
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-6826,10923-12287 (8192 slots) master   # 8192个
   2 additional replica(s)
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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@s1 ~]# redis-trib.rb    reshard  192.168.7.101:6379
[root@s1 ~]# redis-trib.rb fix  192.168.7101:6379  # 如果迁移失败使用此命令修复集群

Redis5:

[root@redis-node5 ~]# redis-cli -a stevenux --cluster reshard 192.168.131.170:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.131.170:6379)
M: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 192.168.131.170:6379
   slots:[0-4095] (4096 slots) master
   1 additional replica(s)
M: d81888e80018bb7ecdd7778373317ea591beb16a 192.168.131.167:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 27b4c0993bdba6460123655a5167ca32ad3c497d 192.168.131.163:6379
   slots: (0 slots) slave
   replicates d81888e80018bb7ecdd7778373317ea591beb16a
S: 3f3f142332722bda502d1019cbdce87dbcb89b70 192.168.131.164:6379
   slots: (0 slots) slave
   replicates 314e1ca8d842bfafb8b059cab891e662b5240b3b
S: 4a1e762f3d69113005617379cb4d290c9a792185 192.168.131.165:6379
   slots: (0 slots) slave
   replicates 827a9a793e893c92036736678eed55175d3dc3e5
M: 314e1ca8d842bfafb8b059cab891e662b5240b3b 192.168.131.168:6379
   slots:[8192-12287] (4096 slots) master
   1 additional replica(s)
M: 827a9a793e893c92036736678eed55175d3dc3e5 192.168.131.169:6379
   slots:[4096-8191] (4096 slots) master
   1 additional replica(s)
S: 809c3debc42c25256001b0abcac46b23d64b0402 192.168.131.166:6379
   slots: (0 slots) slave
   replicates 25f50036c8e17175d1a4b7f2e9aca845af4114a6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? d81888e80018bb7ecdd7778373317ea591beb16a  # 167接收
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: 25f50036c8e17175d1a4b7f2e9aca845af4114a6 # 170为源
Source node #2: done
   Moving slot 4095 from 25f50036c8e17175d1a4b7f2e9aca845af4114a6
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 0 from 192.168.131.170:6379 to 192.168.131.167:6379
...
Moving slot 4095 from 192.168.131.170:6379 to 192.168.131.167:6379
클러스터에서 서버 삭제

슬롯이 마이그레이션되었으나 서버 IP 정보가 클러스터에 남아 있으므로 해당 IP
정보를 클러스터에서 삭제해야 합니다.

Redis3/4:

~$ redis-rtib.rb del-node 192.168.131.170:6379 25f50036c8e17175d1a4b7f2e9aca845af4114a6
[root@redis-server-node6 ~]# redis-trib.rb del-node 192.168.100.16:6379 69e8db533eb78a831013a398bf2ef3ac4d23c8fb
>>> Removing node 69e8db533eb78a831013a398bf2ef3ac4d23c8fb from cluster 192.168.100.16:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

Redis5:

~$ redis-cli -a stevenux --cluster del-node 192.168.131.170:6379 25f50036c8e17175d1a4b7f2e9aca845af4114a6
노드가 삭제되었는지 확인

Redis4:
192.168.100.16이 클러스터에서 삭제되었는지 확인

[root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.16:6379
[ERR] Sorry, can't connect to node 192.168.100.16:6379
[root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.16:6379
[ERR] Sorry, can't connect to node 192.168.100.16:6379
[root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.15:6379
>>> Performing Cluster Check (using node 192.168.100.15:6379)
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-6826,10923-12287 (8192 slots) master
   2 additional replica(s)  # 192.168.100.17跑到了192.168.100.10的直播间!!!
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

Redis5:
192.168.131.170이 클러스터에서 삭제되었는지 확인

~$ redis-cli -a stevenux --cluster check 192.168.131.163:6379

참고: 192.168.131.170이 삭제되면 이전 슬레이브는 자동으로
Redis 클러스터에 있는 다른 마스터의 슬레이브가 되며, 필요하지 않은 경우 이 노드를 삭제할 수도 있습니다.

192.168.131.170을 삭제한 후 클러스터 확인
~$ redis-cli -a stevenux --cluster check 192.168.131.163:6379
192.168.100.16을 삭제한 후 클러스터 확인
[root@redis-server-node6 ~]# redis-trib.rb check 192.168.100.15:6379
>>> Performing Cluster Check (using node 192.168.100.15:6379)
S: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots: (0 slots) slave
   replicates 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-6826,10923-12287 (8192 slots) master
   2 additional replica(s)  # 192.168.100.17跑到了192.168.100.10的直播间!!!
M: 9f26fd0ac2b4694708c76edeb71d3c9ed02bbae4 192.168.100.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.

2.2.3.3 마스터 다운타임 시뮬레이션


현재 아키텍처는 3개의 마스터와 3개의 슬레이브로 구성되어 있으며, 서로 호스트 간 마스터-슬레이브 모드 로 마스터가 다운된 후 자동으로 슬레이브로 전환되는지 테스트됩니다 .

테스트 마스터: 192.168.100.11 데이터 쓰기
[root@redis-server-node2 ~]# redis-cli -h 192.168.100.11
192.168.100.11:6379> AUTH stevenux
OK
192.168.100.11:6379> SET master immaster
(error) MOVED 5565 192.168.100.10:6379
192.168.100.11:6379> SET master1 immaster
(error) MOVED 170 192.168.100.10:6379
192.168.100.11:6379> SET master2 immaster
(error) MOVED 12489 192.168.100.12:6379
192.168.100.11:6379> SET master3 immaster  # master3
OK
192.168.100.11:6379> SET master6 immaster
(error) MOVED 12365 192.168.100.12:6379
192.168.100.11:6379> SET master5 immaster
(error) MOVED 46 192.168.100.10:6379
192.168.100.11:6379> SET master4 immaster
(error) MOVED 4111 192.168.100.10:6379
192.168.100.11:6379> SET master7 immaster  # master7
OK

테스트 슬레이브: 192.168.100.15 데이터 동기화
[root@redis-server-node6 ~]# redis-cli -h 192.168.100.15
192.168.100.15:6379> AUTH stevenux
OK
192.168.100.15:6379> KEYS *
1) "master7"
2) "master3"
마스터 중지 및 장애 조치 확인

Redis 마스터 서비스가 중지된 후 해당 슬레이브가 마스터로 선택되어 데이터 읽기
및 쓰기 작업을 계속 처리합니다.

[root@redis-server-node2 ~]# systemctl stop redis

슬레이브 로그 확인
[root@redis-server-node6 ~]# tail -f -n30 /apps/redis/logs/redis.log
...
1692:S 13 Feb 23:41:25.587 * MASTER <-> SLAVE sync started
1692:S 13 Feb 23:41:25.587 # Error condition on socket for SYNC: Connection refused
1692:S 13 Feb 23:41:26.091 # Starting a failover election for epoch 10.
1692:S 13 Feb 23:41:26.093 # Currently unable to failover: Waiting for votes, but majority still not reached.
1692:S 13 Feb 23:41:26.093 # Failover election won: I'm the new master. # 选为master
1692:S 13 Feb 23:41:26.093 # configEpoch set to 10 after successful failover
1692:M 13 Feb 23:41:26.093 # Setting secondary replication ID to bbc2adff0a23a7b90be69e27353b95c9c5d6954c, valid up to offset: 113. New replication ID is 7d75d5faf1386624d99d9e9f1e59524698ab3b6f
1692:M 13 Feb 23:41:26.093 * Discarding previously cached master state.
1692:M 13 Feb 23:41:26.093 # Cluster state changed: ok  # 集群状态更新
1692:M 13 Feb 23:41:31.353 * Slave 192.168.100.14:6379 asks for synchronization
...

[root@redis-server-node2 ~]# redis-trib.rb check 192.168.100.15:6379
>>> Performing Cluster Check (using node 192.168.100.15:6379)
M: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379  # 已经提升为主库
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
S: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots: (0 slots) slave
   replicates 6789c52caa900c09dda2df57132e73646bcf3735
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-6826,10923-12287 (8192 slots) master
   1 additional replica(s)
M: 6789c52caa900c09dda2df57132e73646bcf3735 192.168.100.12:6379
   slots:12288-16383 (4096 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.
`

2.2.3.4 기존 Redis 데이터 가져오기

데이터를 가져오려면 Redis 클러스터가 가져온 데이터와 동일한 키 이름을 가질 수 없어야 합니다. 그렇지 않으면
가져오기가 실패하거나 중단됩니다. 예:
회사에서 Redis 클러스터를 배포한 후 이전 데이터를 Redis 클러스터 클러스터로 가져와야 하지만 Redis 클러스터에서 사용하는 조각에 키를 저장하는 메커니즘으로 인해 기존 AOF 파일 또는 RDB 스냅샷을
사용합니다.
요구 사항을 충족할 수 없으므로 사용해야 합니다. 클러스터 데이터 가져오기 명령이 완료되었습니다.

데이터를 가져오기 전에 클러스터의 각 노드와 소스 Redis 서버를 포함한 각 Redis 서버의 비밀번호를 닫아
인증으로 인한 환경 불일치를 방지하고 가져오기가 불가능하도록 매개변수를 추가할 수 있습니다. –cluster-replace는
Redis 클러스터의 기존 키를 강제로 교체합니다.

[root@redis-server-node1 ~]# redis-trib.rb import --from 192.168.100.16:6379 --replace 192.168.100.12:6379
root@redis-server-node1 ~]# redis-trib.rb import --from 192.168.100.16:6379 --replace 192.168.100.10:6379
>>> Importing data from 192.168.100.16:6379 to cluster
>>> Performing Cluster Check (using node 192.168.100.10:6379)
M: 90c32a437cff64d61aa433b46d3143c77bc66eec 192.168.100.10:6379
   slots:0-6826,10923-12287 (8192 slots) master
   1 additional replica(s)
M: 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2 192.168.100.15:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
S: b39722ddcd69ef8ac2fdd792fa120c0732565cb2 192.168.100.17:6379
   slots: (0 slots) slave
   replicates 90c32a437cff64d61aa433b46d3143c77bc66eec
M: 9884dd51c7bd74c4abb2b9dc5ac0409488f2c0ae 192.168.100.13:6379
   slots:12288-16383 (4096 slots) master
   0 additional replica(s)
S: 8c495ba64297d26f30101d4d63e9898239d1160f 192.168.100.14:6379
   slots: (0 slots) slave
   replicates 79a9a7ec7b4963dc44e9a8ff6e8ad6b233e242c2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Connecting to the source Redis instance
*** Importing 5 keys from DB 0
Migrating fourth to 192.168.100.10:6379: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)
Migrating kssdf to 192.168.100.15:6379: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)
Migrating second to 192.168.100.10:6379: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)
Migrating ksdf to 192.168.100.10:6379: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)
Migrating key1 to 192.168.100.15:6379: ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)

# 该错误可参考这篇博文:https://blog.csdn.net/m0_37128231/article/details/80755478

2.2.4 Redis 확장 클러스터를 위한 타사 솔루션

Redis와 함께 제공되는 공식 Redis 클러스터 외에도
참고할 수 있는 오픈 소스 클러스터 솔루션이 몇 가지 있지만 대부분의 회사에서는 이를 사용하지 않습니다.

2.2.4.1 코드

Codis는 분산형 Redis 솔루션으로 상위 응용프로그램의 경우
Codis Proxy로 연결하는 것과 원본 Redis Server로 연결하는 것 사이에 큰 차이가 없습니다.(따라서 명령어 목록은 지원하지 않습니다.) 상위 응용프로그램은
스탠드 Redis. Codis의 하위 계층은 요청 전달, 논스톱 데이터 마이그레이션 및 기타 작업을 처리하며 그 뒤의 모든 것은 전면 클라이언트에 투명합니다. 그 뒤의 연결은 무제한의 Redis 서비스
라고 간단히 생각할 수 있습니다.
메모리.

codis-proxy는 redis와 동일하므로 codis-proxy에 접속하는 것과 redis에 접속하는 것에 차이가 없습니다.
codis-proxy는 Stateless이므로 기록 저장 여부에 대해 책임을 지지 않습니다. 데이터는 Zookeeper에 기록되며, 즉, codis
프록시는 Zookeeper에게 키의 레코드 위치를 쿼리하고 프록시는 처리를 위해 요청을 그룹에 전달합니다. 그룹은
마스터와 하나 이상의 슬레이브로 구성됩니다. 기본적으로 1024개의 슬롯이 있습니다.
Redis 클러스터에는 16384개의 슬롯이 있습니다. 기본적으로 서로 다른 슬롯의 내용을 저장하며 서로 다른 그룹에 넣습니다.

Github 프로젝트 주소

[외부 링크 사진 전송에 실패했습니다. 소스 사이트에 안티 리칭 메커니즘이 있을 수 있습니다. 사진을 저장하고 직접 업로드하는 것이 좋습니다. (img-Fu3CSzdn-1693219416538)(png/2020-02-14-00-19-58 .png)]

2.2.4.2 트웬프록시

샤딩은 Twemproxy 양방향 프록시 클라이언트에 의해 실현됩니다. 즉, 사용자 대신 데이터가 샤딩되어 다른 백엔드 서버에 읽고 쓰여집니다.
또한 memcached를 지원하고 프록시에 대한 알고리즘을 구성할 수 있습니다. 단점은 twemproxy가 병목 현상이 발생하여
데이터 마이그레이션을 지원하지 않습니다.

GitHub 프로젝트 주소

추천

출처blog.csdn.net/wang11876/article/details/132545061