redis集群创建以及使用
#centos8 redis版本5.0.3
1、RDB和AOF的优缺点
-
-
比如: 可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。
-
RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘工/0操作。 RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快
RDB持久化机制的缺点
-
如果想要在redis故障时,尽可能少的丢失数据,那么RDB没有AOF好。一般来说,RDB数据快照文件,都是每隔5分钟,或者更长时间生成一次,这个时候就得接受一旦redis进程宕机,那么会丢失最近5分钟的数据
-
RDB每次在fork子进程来执行RDB快照数据文件生成的时候,如果数据文件特别大,可能会导致对客户端提供的服务暂停数毫秒,或者甚至数秒,所以一般不要间隔太长时间进行持久化操作。
AOF持久化机制优点
-
AOF可以更好的保护数据不丢失,一般AOF会每隔1秒,通过一个后台线程执行一次fsync操作,最多丢失1秒钟的数据
-
AOF日志文件以append-only模式写入,所以没有任何磁盘寻址的开销,写入性能非常高,而且文件不容易破损,即使文件尾部破损,也很容易修复
-
AOF日志文件即使过大的时候,出现后台重写操作,也不会影响客户端的读写。因为在rewrite log的时候,会对其中的指导进行压缩,创建出一份需要恢复数据的最小日志出来。再创建新日志文件的时候,老的日志文件还是照常写入。 当新的merge后的日志文件ready的时候,再交换新老日志文件即可。
-
AOF日志文件的命令通过非常可读的方式进行记录,这个特性非常适合做灾难性的误删除的紧急恢复。比如某人不小心用flushall命令清空了所有数据,只要这个时候后台rewrite还没有发生,那么就可以立即拷贝AOF文件, 将最后一条flushall命令给删了,然后再将该AOF文件放回去,就可以通过恢复机制,自动恢复所有数据
AOF持久化机制的缺点
-
对于同一份数据来说,AOF日志文件通常比RDB数据快照文件更大
-
AOF开启后,支持的写QPS会比RDB支持的写QPS低,因为AOF一般会配置成每秒fsync一次日志文件,当然,每秒一次fsync,性能也还是很高的
-
以前AOF发生过bug,就是通过AOF记录的日志,进行数据恢复的时候,没有恢复一模一样的数据出来。所以说,类似AOF这种较为复杂的基于命令日志/merge/回放的方式,比基于RDB每次持久化一份完整的数据快照文件的方式,更加脆弱一些,容易有bug。 不过AOF就是为了避免rewrite过程导致的bug,因此每次rewrite并不是基于旧的指令日志进行merge的,而是基于当时内存中的数据进行指令的重新构建,这样健壮性会好很多。
RDB和AOF到底该如何选择
-
不要仅仅使用RDB,因为那样会导致你丢失很多数据
-
也不要仅仅使用AOF,因为那样有两个问题,第一,你通过AOF做冷备,没有RDB做冷备,来的恢复速度更快; 第二,RDB每次简单粗暴生成数据快照,更加健壮,可以避免AOF这种复杂的备份和恢复机制的bug
-
综合使用AOF和RDB两种持久化机制,用AOF来保证数据不丢失,作为数据恢复的第一选择; 用RDB来做不同程度的冷备,在AOF文件都丢失或损坏不可用的时候,还可以使用RDB来进行快速的数据恢复
2、master和slave同步过程
@1主从复制的大体过程
1.从服务发送一个sync同步命令给主服务要求全量同步 2.主服务接收到从服务的sync同步命令时,会fork一个子进程后台执行bgsave命令(非阻塞)快照保存,生成RDB文件,并将 RDB文件发送给从服务 3.从服务再将接收到的RDB文件载入自己的redis内存 4.待从服务将RDB载入完成后,主服务再将缓冲区所有写命令发送给从服务 5.从服务在将主服务所有的写命令载入内存从而实现数据的完整同步 6.从服务下次在需要同步数据时只需要发送自己的offset位置(相当于mysql binlog的位置)即可,只同步新增加的数据,再不需要全量同步
@2实现主从复制的具体步骤 master:10.0.0.81 salve:10.0.0.82
命令行配置
当前的主机默认为master模式 需要转换为slave并且指向master
[root@centos8 ~]# redis-cli 127.0.0.1:6379> replicaof 10.0.0.81 6379 OK 127.0.0.1:6379> config set masterauth 123456 OK
如果直接更改配置文件会把slave之前的数据清空 并把master数据导入 想保留数据就不要当作slave
当前slave状态
127.0.0.1:6379> info replication # Replication role:slave master_host:10.0.0.81 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:0 master_link_down_since_seconds:1603454411 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:2e44afde1a2781b97d89e26868e030c1e56b373a 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
然后再保存到redis.conf
[root@centos8 ~]# vim /etc/redis.conf #找到这两行并更改 replicaof 10.0.0.81 6379 masterauth 123456
重启验证
# Replication role:slave master_host:10.0.0.81 master_port:6379 master_link_status:up master_last_io_seconds_ago:7 master_sync_in_progress:0 slave_repl_offset:28 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:16d067d080521c1632586e1aed1ec02d2510d54a master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:28
检查是否同步成功
master
127.0.0.1:6379> set k1 v1 OK
slave
127.0.0.1:6379> get k1 "v1"
3、哨兵的使用和实现机制
哨兵:用于检查master的工作状态,一旦master有问题,就会选举出一个slave来做master,并通知其他的slave
通知客户端主从变化,有问题的master一旦恢复,就成为新master的slave
*****master 的配置文件中masterauth 和slave 都必须相同 *****
所有的master和slave都要配置
[root@centos8 ~]# vim /etc/redis.conf bind 0.0.0.0 masterauth "123456" #连接master密码 requirepass "123456" #执行redis-cli的密码
前提:已经实现主从复制
所有从节点设置👇
[root@centos8 ~]# vim /etc/redis.conf replicaof 10.0.0.81 6379
所有主从节点全部重启服务
[root@master ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=10.0.0.82,port=6379,state=online,offset=140,lag=0 slave1:ip=10.0.0.83,port=6379,state=online,offset=140,lag=1 master_replid:b00a1b0724ec707c1c556f8c4d53e6d5974ec153 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:140 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:140
[root@s1 ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> info replication # Replication role:slave master_host:10.0.0.81 master_port:6379 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:812 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:b00a1b0724ec707c1c556f8c4d53e6d5974ec153 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:812 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:812
[root@s2 ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> 127.0.0.1:6379> info replication # Replication role:slave master_host:10.0.0.81 master_port:6379 master_link_status:up master_last_io_seconds_ago:5 master_sync_in_progress:0 slave_repl_offset:742 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:b00a1b0724ec707c1c556f8c4d53e6d5974ec153 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:742 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:43 repl_backlog_histlen:700
编辑三个哨兵的配置文件 mymaster是集群名称
[root@master~]# vim /etc/redis-sentinel.conf bind 0.0.0.0 port 26379 daemonize yes pidfile "/var/run/redis-sentinel.pid" logfile "/var/log/sentinel_26379.log" dir /tmp #工作目录 sentinel monitor mymaster 10.0.0.81 6379 2 #2为法定人数限制(quorum),即有几个sentinel认为master down了就进行故障转移,一般此值是所有sentinel节点(一般总数是>=3的 奇数, 如:3,5,7等)的一半以上的整数值,比如,总数是3,即3/2=1.5,取整为2,是master的ODOWN客观下线的依据 sentinel auth-pass mymaster 123456 #集群中master的密码 sentinel down-after-milliseconds mymaster 30000 #(SDOWN)判断mymaster集群中所有节点的主观下线的时间,单位:毫秒,建议3000 sentinel parallel-syncs mymaster 1 #发生故障转移后,同时向新master同步数据的slave数量,数字越小总同步时间越长,但可以减轻新master的负载压力 sentinel failover-timeout mymaster 180000 #所有slaves指向新的master所需的超时时间,单位:毫秒 sentinel deny-scripts-reconfig yes #禁止修改脚本
启动哨兵 并保证三个哨兵有不同的myid
[root@master ~]# vim /etc/redis-sentinel.conf sentinel myid 1dd214fe6c2a849b87be718ac1e4f47ef364e2ca [root@s1 ~]# vim /etc/redis-sentinel.conf sentinel myid 9d120419260d474ad6af599965e53582dad5df7a [root@s2 ~]#vim /etc/redis-sentinel.conf sentinel myid 61e5db6ecb3af5bd90d09d46ac72e7dfa995d7dc
[root@centos8 ~]# systemctl enable --now redis-sentinel Created symlink /etc/systemd/system/multi-user.target.wants/redis-sentinel.service → /usr/lib/systemd/system/redis-sentinel.service. [root@centos8 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:26379 0.0.0.0:* LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
[root@s1 ~]# systemctl enable --now redis-sentinel [root@s1 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:26379 0.0.0.0:* LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
[root@s2 ~]# systemctl enable --now redis-sentinel [root@s2 ~]# ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:26379 0.0.0.0:* LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:*
查看master日志
[root@centos8 ~]# tail -f /var/log/redis/sentinel.log 1963:X 24 Oct 2020 16:09:49.555 # Configuration loaded 1963:X 24 Oct 2020 16:09:49.555 * supervised by systemd, will signal readiness 1963:X 24 Oct 2020 16:09:49.556 * Running mode=sentinel, port=26379. 1963:X 24 Oct 2020 16:09:49.557 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1963:X 24 Oct 2020 16:09:49.557 # Sentinel ID is e199d03e614204a9331a4d21c9672201977ac6f3 1963:X 24 Oct 2020 16:09:49.557 # +monitor master mymaster 10.0.0.81 6379 quorum 2 1963:X 24 Oct 2020 16:09:49.558 * +slave slave 10.0.0.83:6379 10.0.0.83 6379 @ mymaster 10.0.0.81 6379 1963:X 24 Oct 2020 16:09:49.559 * +slave slave 10.0.0.82:6379 10.0.0.82 6379 @ mymaster 10.0.0.81 6379 1963:X 24 Oct 2020 16:09:50.258 * +sentinel sentinel 61e5db6ecb3af5bd90d09d46ac72e7dfa995d7dc 10.0.0.83 26379 @ mymaster 10.0.0.81 6379 1963:X 24 Oct 2020 16:09:50.657 * +sentinel sentinel 9d120419260d474ad6af599965e53582dad5df7a 10.0.0.82 26379 @ mymaster 10.0.0.81 6379
4、redis cluster集群创建和使用
手动部署:
首先下载安装redis 然后所有主机更改配置文件 IP:81;82;83;84;85;86
[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 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster meet 10.0.0.82 6379 OK [root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster meet 10.0.0.83 6379 OK [root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster meet 10.0.0.84 6379 OK [root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster meet 10.0.0.85 6379 OK [root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster meet 10.0.0.86 6379 OK
查看连接通讯
[root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster nodes a5d8fa21d91c6c7a0b2b5353111dcf788ca7fa64 10.0.0.85:6379@16379 master - 0 1603532658000 4 connected 2969effeb1d8a712cbbd60a2feb19b80f5ceb777 10.0.0.82:6379@16379 master - 0 1603532660280 1 connected 80ce0afb05ac422e460c8385f31988d865591b92 10.0.0.81:6379@16379 myself,master - 0 1603532658000 2 connected 709aac4c760f9c71445d9fe5854ecdaabb617015 10.0.0.84:6379@16379 master - 0 1603532660000 3 connected e93774b3d4be4027bc6740da6eba6becee93d2eb 10.0.0.83:6379@16379 master - 0 1603532659000 0 connected 6ac36545bfb94f845c055f227f6c8a56ce881eaf 10.0.0.86:6379@16379 master - 0 1603532661289 5 connected
状态
[root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster info cluster_state:fail cluster_slots_assigned:0 cluster_slots_ok:0 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:0 cluster_current_epoch:5 cluster_my_epoch:2 cluster_stats_messages_ping_sent:151 cluster_stats_messages_pong_sent:172 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:328 cluster_stats_messages_ping_received:172 cluster_stats_messages_pong_received:156 cluster_stats_messages_received:328
没有槽位没法添加数据 写个添加槽位的通用脚本并运行 给三个master分配槽位(尽量平均分)
[root@centos8 ~]# vim addslot.sh #!bin/bash host=$1 port=$2 start=$3 end=$4 pass=123456 for slot in `seq ${start} ${end}`;do echo slot:$slot redis-cli -h ${host} -p $port -a ${pass} --no-auth-warning cluster addslots ${slot} done
[root@centos8 ~]# bash addslot.sh 10.0.0.81 6379 0 5461 #端口号 槽位从?到? [root@centos8 ~]# bash addslot.sh 10.0.0.82 6379 5462 10922 [root@centos8 ~]# bash addslot.sh 10.0.0.81 6379 0 5461
查看当前的分配过的信息
[root@centos8 ~]# redis-cli -a 123456 --no-auth-warning cluster nodes a5d8fa21d91c6c7a0b2b5353111dcf788ca7fa64 10.0.0.85:6379@16379 master - 0 1603537597000 4 connected 2969effeb1d8a712cbbd60a2feb19b80f5ceb777 10.0.0.82:6379@16379 master - 0 1603537597000 1 connected 5462-10922 80ce0afb05ac422e460c8385f31988d865591b92 10.0.0.81:6379@16379 myself,master - 0 1603537596000 2 connected 0-5461 709aac4c760f9c71445d9fe5854ecdaabb617015 10.0.0.84:6379@16379 master - 0 1603537596000 3 connected e93774b3d4be4027bc6740da6eba6becee93d2eb 10.0.0.83:6379@16379 master - 0 1603537597248 0 connected 10923-16383 6ac36545bfb94f845c055f227f6c8a56ce881eaf 10.0.0.86:6379@16379 master - 0 1603537598255 5 connected
[root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning 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:5 cluster_my_epoch:2 cluster_stats_messages_ping_sent:5194 cluster_stats_messages_pong_sent:5397 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:10596 cluster_stats_messages_ping_received:5397 cluster_stats_messages_pong_received:5199 cluster_stats_messages_received:10596
配置主从关系 #字符串是id
[root@centos8 ~]# redis-cli -h 10.0.0.84 -a 123456 --no-auth-warning cluster replicate 80ce0afb05ac422e460c8385f31988d865591b92 OK [root@centos8 ~]# redis-cli -h 10.0.0.85 -a 123456 --no-auth-warning cluster replicate 2969effeb1d8a712cbbd60a2feb19b80f5ceb777 OK [root@centos8 ~]# redis-cli -h 10.0.0.86 -a 123456 --no-auth-warning cluster replicate e93774b3d4be4027bc6740da6eba6becee93d2eb OK
配好好查看
[root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster nodes a5d8fa21d91c6c7a0b2b5353111dcf788ca7fa64 10.0.0.85:6379@16379 slave 2969effeb1d8a712cbbd60a2feb19b80f5ceb777 0 1603538261774 4 connected 2969effeb1d8a712cbbd60a2feb19b80f5ceb777 10.0.0.82:6379@16379 master - 0 1603538261000 1 connected 5462-10922 80ce0afb05ac422e460c8385f31988d865591b92 10.0.0.81:6379@16379 myself,master - 0 1603538260000 2 connected 0-5461 709aac4c760f9c71445d9fe5854ecdaabb617015 10.0.0.84:6379@16379 slave 80ce0afb05ac422e460c8385f31988d865591b92 0 1603538261000 3 connected e93774b3d4be4027bc6740da6eba6becee93d2eb 10.0.0.83:6379@16379 master - 0 1603538260769 0 connected 10923-16383 6ac36545bfb94f845c055f227f6c8a56ce881eaf 10.0.0.86:6379@16379 slave e93774b3d4be4027bc6740da6eba6becee93d2eb 0 1603538259758 5 connected [root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning 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 cluster_size:3 cluster_current_epoch:5 cluster_my_epoch:2 cluster_stats_messages_ping_sent:5649 cluster_stats_messages_pong_sent:5856 cluster_stats_messages_meet_sent:5 cluster_stats_messages_sent:11510 cluster_stats_messages_ping_received:5856 cluster_stats_messages_pong_received:5654 cluster_stats_messages_received:11510
slave
[root@centos8 ~]# redis-cli -h 10.0.0.84 -a 123456 --no-auth-warning info replication # Replication role:slave master_host:10.0.0.81 master_port:6379 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_repl_offset:476 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:d8d7534bf84e44f977724f9c28a60fd07ab8befc master_replid2:0000000000000000000000000000000000000000 master_repl_offset:476 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:476
查看主从关系以及槽位
[root@centos8 ~]# redis-cli -h 10.0.0.81 -a 123456 --no-auth-warning cluster slots 1) 1) (integer) 5462 2) (integer) 10922 3) 1) "10.0.0.82" 2) (integer) 6379 3) "2969effeb1d8a712cbbd60a2feb19b80f5ceb777" 4) 1) "10.0.0.85" 2) (integer) 6379 3) "a5d8fa21d91c6c7a0b2b5353111dcf788ca7fa64" 2) 1) (integer) 0 2) (integer) 5461 3) 1) "10.0.0.81" 2) (integer) 6379 3) "80ce0afb05ac422e460c8385f31988d865591b92" 4) 1) "10.0.0.84" 2) (integer) 6379 3) "709aac4c760f9c71445d9fe5854ecdaabb617015" 3) 1) (integer) 10923 2) (integer) 16383 3) 1) "10.0.0.83" 2) (integer) 6379 3) "e93774b3d4be4027bc6740da6eba6becee93d2eb" 4) 1) "10.0.0.86" 2) (integer) 6379 3) "6ac36545bfb94f845c055f227f6c8a56ce881eaf"
验证cluster写入输出 -c用集群的方式连接
[root@centos8 ~]# redis-cli -c -h 10.0.0.81 -a 123456 --no-auth-warning set name zhangty OK [root@centos8 ~]# redis-cli -c -h 10.0.0.81 -a 123456 --no-auth-warning get name "zhangty" [root@centos8 ~]# redis-cli -h 10.0.0.83 -a 123456 --no-auth-warning get name (error) MOVED 5798 10.0.0.82:6379 [root@centos8 ~]# redis-cli -c -h 10.0.0.83 -a 123456 --no-auth-warning get name "zhangty" [root@centos8 ~]# redis-cli -c -h 10.0.0.84 -a 123456 --no-auth-warning get name "zhangty" [root@centos8 ~]# redis-cli -c -h 10.0.0.85 -a 123456 --no-auth-warning get name "zhangty"