Redis主从复制哨兵实现,并模拟master故障场景

Posted njsummer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis主从复制哨兵实现,并模拟master故障场景相关的知识,希望对你有一定的参考价值。

Redis是一个开源(BSD许可),内存中数据结构存储,用作数据库,缓存和消息代理。它支持数据结构:如字符串、哈希、列表、集、带范围查询的排序集、位图、超日志、带半径查询和流的地理空间索引。Redis 具有内置的复制、Lua 脚本、LRU 逐出、事务和不同级别的磁盘持久性,并通过 Redis Sentinel 和 Redis Cluster 的自动分区提供高可用性。

本文将实践redis的主从复制和哨兵。

1. 架构拓扑及主机

Redis主从复制、哨兵实现,并模拟master故障场景_脚本

IP

主机名

角色

软件

192.168.250.18

Redis-IP18

Redis 主

Redis6.2.6

192.168.250.28

Redis-IP28

Redis 从1

Redis6.2.6

192.168.250.38

Redis-IP38

Redis 从2

Redis6.2.6

环境准备:
时间同步,确保NTP或Chrony服务正常运行。
禁用SELinux和防火墙(或放通需要的端口)

2. 准备三台Redis主机

任务内容:初始化CentOS、修改规划好的主机名、通过脚本一键安装好Redis。

#### 只列出 IP18的操作过程,同样的操作步骤完成 IP28和IP38的Redis安装
[root@CentOS84-IP18 ]#hostnamectl set-hostname Redis-IP18
[root@CentOS84-IP18 ]#exit
[root@Redis-IP18 ]#cd /data/
[root@Redis-IP18 ]#
[root@Redis-IP18 ]#bash install_redis.sh
[root@Redis-IP18 ]#

[root@Redis-IP18 ]#rz
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring install_redis.sh...
100% 2 KB 2 KB/sec 00:00:01 0 Errors

[root@Redis-IP18 ]#ll /data/
total 4
-rw-r--r-- 1 root root 2854 Apr 26 19:01 install_redis.sh
# 验证安装
[root@Redis-IP18 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 511 [::1]:6379 [::]:*
[root@Redis-IP18 ]#

3. 配置Redis主从复制

3.1 概述

虽然Redis可以实现单机的数据持久化,但无论是RDB也好或者AOF也好,都解决不了单点宕机问题,即一旦单台 redis服务器本身出现系统故障、硬件故障等问题后,就会直接造成数据的丢失。此外,单机的性能也是有极限的,因此需要使用另外的技术,比如复制、哨兵、集群等来解决单点故障和性能扩展的问题。

Redis主从复制、哨兵实现,并模拟master故障场景_redis_02

默认所有的redis 安装完成后的状态都为master,需要将规划用来作为slave的redis主机,进行角色修改并指向master服务器的IP+PORT+Password。在从节点执行REPLICAOF MASTER_IP PORT 指令可以启用主从同步复制功能,早期版本使用 SLAVEOF 。

基本操作命令:

# 这样的操作命令
127.0.0.1:6379> REPLICAOF MASTER_IP PORT
127.0.0.1:6379> CONFIG SET masterauth <masterpass>

3.2 主从Redis中复制架构的配置

哨兵的前提是已经实现了一个redis的主从复制的运行环境,从而实现一个一主两从基于哨兵的高可用redis架构

注意: master 的配置文件中masterauth 和slave 都必须相同。所有主从节点的redis.conf中关健配置如下:

所有主从节点都要执行的配置

## 因为是编译安装的,redis.conf 文件的路径和 yum安装( /etc/redis.conf )的不同。
[root@Redis ]#vim /apps/redis/etc/redis.conf
bind 0.0.0.0 # 修改redis监听,下面的密码可以自行定义,但必须对应好
masterauth "123456"
requirepass "123456"

# yum 安装一般只需要修改上面的项,因本实践是编译安装,按照自定义的目录结构修改下面的几项。
pidfile "/apps/redis/run/redis_6379.pid"
logfile "/apps/redis/log/redis-6379.log"
dir "/apps/redis/data"

所有从节点都要执行的配置

# 将所有的从redis都指向主redis,同样也可以自行定义下从redis的优先级别  replica-priority 100  这个值可以修改下,实现从节点的优先级自定义
[root@Redis ]#echo "replicaof 192.168.250.18 6379" >> /etc/redis.conf

3.2.1 主redis-IP18节点的配置及验证复制状态

经验和技巧:在学习一个新的软件,我们曾经经常提醒一般都先yum安装,读懂其文件结构和关键性配置文件的内容,这样有利于编译自定义安装后借鉴,在官方提供的配置文件内做了很多注释性的解读,非常人性化,内容很多很长,我们仅通过命令提取出实际生效行。

# redis.conf 内实际上校的行  cat /apps/redis/etc/redis.conf 列出的文件内容过于冗长。特别要注意的是复制和借鉴的文件,要查看下权限,避免不必要的错误。

[root@Redis-IP18 ]#egrep -v "^#|^$" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile "/apps/redis/run/redis_6379.pid"
loglevel notice
logfile "/apps/redis/log/redis-6379.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "title listen-addr server-mode"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/apps/redis/data"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
supervised systemd
save 3600 1
save 300 100
save 60 10000
user default on sanitize-payload #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all
[root@Redis-IP18 ]#

##############################################################################
## 验证主从复制,主节点上的状态
[root@Redis-IP18 ]#redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.250.38,port=6379,state=online,offset=51770731,lag=1
slave1:ip=192.168.250.28,port=6379,state=online,offset=51770731,lag=1
master_failover_state:no-failover
master_replid:f14a4fed51b884834035a13d89829ff621bfeef7
master_replid2:0137cb1fa513d18c18a764ac95cbbc2916585710
master_repl_offset:51770874
second_repl_offset:51638431
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:50722299
repl_backlog_histlen:1048576
127.0.0.1:6379>

3.2.2 从redis-IP28节点的配置及验证复制状态

#### 过滤出有效的配置行
[root@Redis-IP28 ]#egrep -v "^#|^$" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile "/apps/redis/run/redis_6379.pid"
loglevel notice
logfile "/apps/redis/log/redis-6379.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "title listen-addr server-mode"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/apps/redis/data"
replicaof 192.168.250.18 6379
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass "123456"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4kb
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
supervised systemd
save 3600 1
save 300 100
save 60 10000
user default on #8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92 ~* &* +@all
[root@Redis-IP28 ]#

##############################################################################
## 验证主从复制,从节点上的状态
[root@Redis-IP28 ]#redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.250.18
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:51860994
slave_repl_offset:51860994
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:f14a4fed51b884834035a13d89829ff621bfeef7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:51860994
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:51729425
repl_backlog_histlen:131570
127.0.0.1:6379>

3.2.3 从redis-IP38节点的配置及验证复制状态

#### 过滤出有效配置行
[root@Redis-IP38 ]#egrep -v "^#|^$" /apps/redis/etc/redis.conf
bind 0.0.0.0 -::1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile "/apps/redis/run/redis_6379.pid"
loglevel notice
logfile "/apps/redis/log/redis-6379.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "title listen-addr server-mode"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/apps/redis/data"
masterauth "123456"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 80
acllog-max-len Redis——主从复制哨兵模式集群服务

redis高可用 哨兵(Sentinel),主从复制架构

Redis精华所在,一口气说完Redis的主从复制和哨兵模式

Redis群集

redis主从复制及哨兵机制

redis主从复制及哨兵机制