redis高可用

主从的原理就是 从节点向[主节点]发送一个syn请求 主节点收到请求 在后台创建一个rdb快照文件发送给从节点 ,再在暂存空间记录创建到发送完成这段时间的所有写命令,等rdb发送完成再发送写命令 从节点加载rdb和写命令完成主从同步 

哨兵就是通过ping 如果通不了了 就会告知其他的哨兵 都发现ping不了了则标记下线 选举出新的主节点 !!

直接开整

首先最简单的

直接配置不用docker的操作
master192.168.85.128
slave1192.168.85.129
slave2192.168.85.130
1.安装redis
1
# yum install redis -y
2.更改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# ls /etc/redis*
/etc/redis.conf /etc/redis-sentinel.conf
这是redis的配置 这是哨兵的配置文件

# cat /etc/redis.conf|grep -v "^#"|grep -v "^$"
bind 0.0.0.0

protected-mode no #关闭保护模式 这个很重要 不关闭不会同步

东西很多咱们只看需要修改的


# ls /etc/redis*
/etc/redis.conf /etc/redis-sentinel.conf
这是redis的配置 这是哨兵的配置文件

# cat /etc/redis.conf|grep -v "^#"|grep -v "^$"

bind 0.0.0.0

protected-mode no #关闭保护模式 这个很重要 不关闭不会同步

slaveof 192.168.85.128 6379 #从的配置文件里写上主的ip加端口
3.启动redis查看是否同步了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# systemctl start redis
# redis-cli -h 192.168.85.130 info replication
# Replication

role:master

connected_slaves:2

slave0:ip=192.168.85.128,port=6379,state=online,offset=1615599,lag=1

slave1:ip=192.168.85.129,port=6379,state=online,offset=1615742,lag=1

master_repl_offset:1615742

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:567167

repl_backlog_histlen:1048576
4.配置哨兵监控redis
1
2
3
4
5
6
7
主从都一样配置 
# cat /etc/redis-sentinel.conf |grep -v "^#"|grep -v "^$"
protected-mode no #一定要no
dir "/tmp" #定义的这个目录要让哨兵有权限写进入 w写权限一定要
sentinel monitor mymaster 192.168.85.128 6379 2 #主ip 从要有两个要不然就没意义了
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 30000
5.启动哨兵  测试是否成功
1
2
3
4
5
6
7
8
# systemctl start redis-sentinel 
# systemctl stop redis #主节点的
[root@localhost redis]# redis-cli -h 192.168.85.130 info replication
# Replication role:master connected_slaves:1 slave0:ip=192.168.85.129,port=6379,state=online,offset=1710180,lag=0 #128ip没了说明成功了 master_repl_offset:1710323
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:661748
repl_backlog_histlen:1048576
6.附加 redis sub pub发布/订阅模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
SUBSCRIBE nb # 订阅者订阅nb这个信息

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "nb"

3) (integer)

PUBLISH nb "nihao"

(integer) 1

127.0.0.1:6379>

SUBSCRIBE nb

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "nb"

3) (integer) 1

1) "message"

2) "nb"

3) "nihao"
用docker-compose方式实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# tree
.

├── docker-compose.yml

├── docker-compose.yml_back

├── master

│   └── redis.conf

├── sentinel.conf

└── slave

└── redis.conf

2 directories, 5 files
1.编写docker-compose.yml

注意点:最好固定在一个ip上  command 使用&&可能会造成服务阻塞 用& exec代替

               定义网络是networks 需要顶格写 

               写完用docker-compose config 检验代码是否正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# cat docker-compose.yml 

services:

redis-master:

image: 192.168.85.128/nb/redis:latest

container_name: redis-master

networks:

redis-network:

ipv4_address: 172.23.0.2

command: ["bash", "-c", "redis-server /etc/redis/redis.conf & exec redis-server /etc/redis/sentinel.conf --sentinel"]

volumes:

- ./master/redis.conf:/etc/redis/redis.conf

- ./sentinel.conf:/etc/redis/sentinel.conf

restart: always

ports:

- "6380:6379"

- "26380:26379"

redis-slave1:

image: 192.168.85.128/nb/redis:latest

container_name: redis-slave1

networks:

redis-network:

ipv4_address: 172.23.0.3

command: ["bash", "-c", "redis-server /etc/redis/redis.conf & exec redis-server /etc/redis/sentinel.conf --sentinel"]

volumes:

- ./slave/redis.conf:/etc/redis/redis.conf

- ./sentinel.conf:/etc/redis/sentinel.conf

ports:

- "6381:6379"

- "26381:26379"

depends_on: #运行在master之后

- redis-master

redis-slave2:

image: 192.168.85.128/nb/redis:latest

container_name: redis-slave2

networks:

redis-network:

ipv4_address: 172.23.0.4

command: ["bash", "-c", "redis-server /etc/redis/redis.conf & exec redis-server /etc/redis/sentinel.conf --sentinel"]

volumes:

- ./slave/redis.conf:/etc/redis/redis.conf

- ./sentinel.conf:/etc/redis/sentinel.conf

ports:

- "6382:6379"

- "26382:26379"

depends_on:

- redis-master

networks:

redis-network:

driver: bridge

ipam:

driver: default

config:

- subnet: 172.23.0.0/24
2.编写 master和slave文件 和哨兵
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# cat master/redis.conf 

bind 0.0.0.0

protected-mode no

port 6379

appendonly yes

bind 0.0.0.0

protected-mode no

port 6379

appendonly yes

slaveof 172.23.0.2 6379

port 26379

sentinel monitor mymaster 172.23.0.2 6379 2

sentinel down-after-milliseconds mymaster 5000

sentinel failover-timeout mymaster 10000

sentinel parallel-syncs mymaster 1
3.测试停止redis-master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# redis-cli -p 6381 info replication
# Replication

role:master

connected_slaves:1

slave0:ip=172.23.0.4,port=6379,state=online,offset=78826,lag=0

master_failover_state:no-failover

master_replid:9f5b5a6d7cc115b658406a112116f5f4c7ee4b8f

master_replid2:f83e52939d733f9789838c7af50db30bbd041e71

master_repl_offset:78826

second_repl_offset:8631

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:834

repl_backlog_histlen:77993

redis高可用
https://www.tiantian123.asia/2025/05/26/redis高可用/
作者
lht
发布于
2025年5月26日
许可协议