redis集群搭建

1、RDB和AOF的优缺点

      RDB模式优点:

     (1)RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave或者save命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析。

    (2)RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘工/0操作。

   (3)RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快。

    RDB 模式缺点:

    (1)不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据

    (2)当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或者秒,取决于磁盘IO性能

    (3)在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端﹔如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然 AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失。

    AOF模式优点:

    (1) 数据安全性相对较高,根据所使用的fsync策略(fsync是同步内存中redis所有已经修改的文件到存储设备),默认是appendfsync everysec,即每秒执行一次 fsync,在这种配置下,Redis 仍然可以保持良好的性能,并且就算发生故障停机,也最多只会丢失一秒钟的数据

    (2)  由于该机制对日志文件的写入操作采用的是append模式,因此在写入过程中不需要seek, 即使出现宕机现象,也不会破坏日志文件中已经存在的内容。然而如果本次操作只是写入了一半数据就出现了系统崩溃问题,不用担心,在Redis下一次启动之前,可以通过 redis-check-aof 工具来解决数据一致性的问题

   (3) Redis可以在 AOF文件体积变得过大时,自动地在后台对AOF进行重写,重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合。整个重写操作是绝对安全的,因为Redis在 创建新 AOF文件的过程中,append模式不断的将修改数据追加到现有的 AOF文件里面,即使重写过程中发生停机,现有的 AOF文件也不会丢失。而一旦新AOF文件创建完毕,Redis就会从旧AOF文件切换到新AOF文件,并开始对新AOF文件进行追加操作。

   (4)  AOF包含一个格式清晰、易于理解的日志文件用于记录所有的修改操作。事实上,也可以通过该文件完成数据的重建。AOF文件有序地保存了对数据库执行的所有写入操作,这些写 入操作以Redis协议的格式保存,因此 AOF文件的内容非常容易被人读懂,对文件进行分析也很轻松。导出(export)AOF文件也非常简单:举个例子,如果你不小心执行了FLUSHALL命令,但只要AOF文件未被重写,那么只要停止服务器,移除 AOF文件末尾的FLUSHAL命令,并重启Redis ,就可以将数据集恢复到FLUSHALL执行之前的状态。

    AOF 模式缺点:

    (1) 即使有些操作是重复的也会全部记录,AOF 的文件大小要大于 RDB 格式的文件

    (2)AOF在恢复大数据集时的速度比 RDB 的恢复速度要慢

    (3)根据fsync策略不同,AOF速度可能会慢于RDB

   (4) bug 出现的可能性更多

 

2、master和slave同步过程

(1)从服务器连接主服务器,发送PSYNC命令     

(2)主服务器接收到PSYNC命令后,开始执行BGSAVE命令生成RDB快照文件并使用缓冲区记录此后执行的所有写命令

(3)主服务器BGSAVE执行完后,向所有从服务器发送RDB快照文件,并在发送期间继续记录被执行的写命令
(4)从服务器收到快照文件后丢弃所有旧数据,载入收到的快照至内存
(5)主服务器快照发送完毕后,开始向从服务器发送缓冲区中的写命令
(6)从服务器完成对快照的载入,开始接收命令请求,并执行来自主服务器缓冲区的写命令
(7)后期同步会先发送自己slave_repl_offset位置,只同步新增加的数据,不再全量同步

 

3、哨兵的使用和实现机制    

      哨兵(Sentinel) 是一个分布式系统,可以在一个架构中运行多个哨兵(sentinel) 进程,这些进程使用流言协议(gossip protocols)来接收关于Master主服务器是否下线的信息,并使用投票协议(AgreementProtocols)来决定是否执行自动故障迁移,以及选择哪个Slave作为新的Master。每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)、Master、Slave定时发送消息,以确认对方是否”活”着,如果发现对方在指定配置时间(此项可配置)内未得到回应,则暂时认为对方已离线,也就是所谓的”主观认为宕机” 。

     有主观宕机,对应的有客观宕机。当“哨兵群”中的多数Sentinel进程在对Master主服务器做出SDOWN的判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判断,这种方式就是“客观宕机”(客观:是不依赖于某种意识而已经实际存在的一切事物)。通过一定的vote算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改相关配置,并开启故障转移(failover)。

 

4、redis cluster集群创建和使用

(1)创建 redis cluster集群的环境准备

1. 每个redis 节点采用相同的硬件配置、相同的密码、相同的redis版本
2. 所有redis服务器必须没有任何数据
3. 准备六台主机,地址如下:

10.0.0.8
10.0.0.18
10.0.0.28
10.0.0.38
10.0.0.48
10.0.0.58

(2)启用 redis cluster 配置

[root@centos8 ~]#dnf -y install redis
[root@centos8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
[root@centos8 ~]#systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@centos8 ~]#ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@centos8 ~]#ps -ef|grep redis
redis 22165 1 0 16:14 ? 00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root 22185 1631 0 16:19 pts/1 00:00:00 grep --color=auto redis

(3)创建集群

[root@centos8 ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58: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.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: 9777324df35dfe14af39cff8f0fc798e7e03dbbb 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
M: 27ef14bf4c4e773d88c31890cbf13c9469581d1e 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
M: 23e9b2a124d28c489d016e67a527014022bdaa2d 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
S: 12c036e4fe63af2bc9da6068f20cc6f72b328cd5 10.0.0.38:6379
replicates 9777324df35dfe14af39cff8f0fc798e7e03dbbb
S: bbe10278b3a43343a8483dba31785d55e6eefbb9 10.0.0.48:6379
replicates 27ef14bf4c4e773d88c31890cbf13c9469581d1e
S: eaddd605c135c591abc646609403091ebeae154c 10.0.0.58:6379
replicates 23e9b2a124d28c489d016e67a527014022bdaa2d
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.8:6379)
M: 9777324df35dfe14af39cff8f0fc798e7e03dbbb 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 12c036e4fe63af2bc9da6068f20cc6f72b328cd5 10.0.0.38:6379
slots: (0 slots) slave
replicates 9777324df35dfe14af39cff8f0fc798e7e03dbbb
S: eaddd605c135c591abc646609403091ebeae154c 10.0.0.58:6379
slots: (0 slots) slave
replicates 23e9b2a124d28c489d016e67a527014022bdaa2d
M: 23e9b2a124d28c489d016e67a527014022bdaa2d 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 27ef14bf4c4e773d88c31890cbf13c9469581d1e 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: bbe10278b3a43343a8483dba31785d55e6eefbb9 10.0.0.48:6379
slots: (0 slots) slave
replicates 27ef14bf4c4e773d88c31890cbf13c9469581d1e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

(4)查看主从状态

[root@centos8 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.38,port=6379,state=online,offset=112,lag=0
master_replid:55949c22523f9ca6a82d5ee6bf70026327413f13
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:112
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:112

(5)验证集群状态

[root@centos8 ~]#redis-cli -a 123456 CLUSTER INFO
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:474
cluster_stats_messages_pong_sent:450
cluster_stats_messages_sent:924
cluster_stats_messages_ping_received:445
cluster_stats_messages_pong_received:474
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:924
[root@centos8 ~]#redis-cli -a 123456 --cluster info 10.0.0.38:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379 (9777324d...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.18:6379 (27ef14bf...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.28:6379 (23e9b2a1...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

(6)查看集群node对应关系

[root@centos8 ~]#redis-cli -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
12c036e4fe63af2bc9da6068f20cc6f72b328cd5 10.0.0.38:6379@16379 slave 9777324df35dfe14af39cff8f0fc798e7e03dbbb 0 1603528526000 4 connected
eaddd605c135c591abc646609403091ebeae154c 10.0.0.58:6379@16379 slave 23e9b2a124d28c489d016e67a527014022bdaa2d 0 1603528527464 6 connected
23e9b2a124d28c489d016e67a527014022bdaa2d 10.0.0.28:6379@16379 master - 0 1603528528000 3 connected 10923-16383
27ef14bf4c4e773d88c31890cbf13c9469581d1e 10.0.0.18:6379@16379 master - 0 1603528527000 2 connected 5461-10922
bbe10278b3a43343a8483dba31785d55e6eefbb9 10.0.0.48:6379@16379 slave 27ef14bf4c4e773d88c31890cbf13c9469581d1e 0 1603528528470 5 connected
9777324df35dfe14af39cff8f0fc798e7e03dbbb 10.0.0.8:6379@16379 myself,master - 0 1603528524000 1 connected 0-5460
[root@centos8 ~]#redis-cli -a 123456 --cluster check 10.0.0.38:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379 (9777324d...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.18:6379 (27ef14bf...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.28:6379 (23e9b2a1...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.38:6379)
S: 12c036e4fe63af2bc9da6068f20cc6f72b328cd5 10.0.0.38:6379
slots: (0 slots) slave
replicates 9777324df35dfe14af39cff8f0fc798e7e03dbbb
M: 9777324df35dfe14af39cff8f0fc798e7e03dbbb 10.0.0.8:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: eaddd605c135c591abc646609403091ebeae154c 10.0.0.58:6379
slots: (0 slots) slave
replicates 23e9b2a124d28c489d016e67a527014022bdaa2d
M: 27ef14bf4c4e773d88c31890cbf13c9469581d1e 10.0.0.18:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 23e9b2a124d28c489d016e67a527014022bdaa2d 10.0.0.28:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: bbe10278b3a43343a8483dba31785d55e6eefbb9 10.0.0.48:6379
slots: (0 slots) slave
replicates 27ef14bf4c4e773d88c31890cbf13c9469581d1e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

posted @ 2020-10-24 20:39  Zintent  阅读(121)  评论(0)    收藏  举报