Docker搭建Redis一主二从三哨兵,并根据配置文件启动

在一台Centos虚拟机上,通过Docker容器实现Redis哨兵模式的搭建,配置Redis主从复制+自动化主从切换。

架构:一个主服务器、两个从服务器、三个哨兵服务器

环境:Centos7+Docker

 

1.安装Docker以后,拉起Redis镜像

 

1
docker pull  redis:6.0.12 

  

2.创建Redis启动的配置文件,并修改配置

 

1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir docker             //创建文件夹
 
touch  redis1.conf   //创建Redis1的配置文件
 
touch  redis2.conf   //创建Redis2的配置文件
 
touch  redis3.conf   //创建Redis3的配置文件
 
touch  sentinel1.conf   //创建Sentinel1的配置文件
 
touch  sentinel2.conf   //创建Sentinel2的配置文件
 
touch  sentinel3.conf   //创建Sentinel3的配置文件

  

  

 

redis1.conf配置如下:

 

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
bind 172.17.0.2   //容器redis1的实际IP地址,redis2.conf 和redis3.conf的配置除了IP地址和端口,其余部分都相同。
 
port 6379
 
logfile "redis1.log"
 
dir "/data"
 
protected-mode no
 
tcp-backlog 4096
 
timeout 300
 
tcp-keepalive 60
 
maxclients 10000
 
maxmemory-policy volatile-lru
 
slowlog-max-len 512
 
client-output-buffer-limit normal 0 0 0
 
client-output-buffer-limit slave 0 0 0
 
save 3600 1
 
repl-backlog-size 100mb
 
stop-writes-on-bgsave-error no

redis2.conf配置:

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
bind 172.17.0.3
 
port 6380
 
logfile "redis2.log"
 
dir "/data"
 
dbfilename "dump2.rdb"
 
appendfilename "appendonly2.aof"
 
protected-mode no
 
tcp-backlog 4096
 
timeout 300
 
tcp-keepalive 60
 
maxclients 10000
 
maxmemory-policy volatile-lru
 
slowlog-max-len 512
 
client-output-buffer-limit normal 0 0 0
 
client-output-buffer-limit slave 0 0 0
 
save 3600 1
 
repl-backlog-size 100mb
 
stop-writes-on-bgsave-error no
 
replicaof 172.17.0.2 6379   //注意配置连接的主节点
 
replica-read-only no 

redis3.conf配置如下:

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
bind 172.17.0.4
 
port 6381
 
logfile "redis3.log"
 
dir "/data"
 
dbfilename "dump3.rdb"
 
appendfilename "appendonly3.aof"
 
protected-mode no
 
tcp-backlog 4096
 
timeout 300
 
tcp-keepalive 60
 
maxclients 10000
 
maxmemory-policy volatile-lru
 
slowlog-max-len 512
 
client-output-buffer-limit normal 0 0 0
 
client-output-buffer-limit slave 0 0 0
 
save 3600 1
 
repl-backlog-size 100mb
 
stop-writes-on-bgsave-error no
 
replicaof 172.17.0.2 6379     //注意配置连接的主节点
 
replica-read-only no

  

sentinel1.conf配置如下:

 

1
2
3
4
5
6
7
8
9
port 26379    //sentinel2.conf和sentinel3.conf 只需修改端口即可
 
dir /data
 
logfile "sentinel.log"
 
daemonize yes
 
sentinel monitor redismaster 172.17.0.2  6379 2

  

3.启动容器redis1、redis2、redis3

 

1
2
3
4
5
docker run -d -p 6379:6379 -p 26379:26379 -v  ~/docker/redis1.conf:/data/redis.conf -v ~/docker/sentinel1.conf:/data/sentinel.conf --name redis1 redis:6.0.12 redis-server redis1.conf
 
docker run -d -p 6380:6380 -p 26380:26380 -v  ~/docker/redis2.conf:/data/redis2.conf -v ~/docker/sentinel2.conf:/data/sentinel2.conf --name redis2 redis:6.0.12 redis-server redis2.conf
 
docker run -d -p 6381:6381 -p 26381:26381 -v  ~/docker/redis3.conf:/data/redis3.conf -v ~/docker/sentinel3.conf:/data/sentinel3.conf --name redis3 redis:6.0.12 redis-server redis3.conf

    

4.启动成功后,docker  ps  会看到一下界面:

 

 

5.设置容器内Redis服务自启动

 

1
2
3
4
5
docker update --restart=always redis1    //已经启动的容器,设置开机自启动
 
docker update --restart=always redis2    //已经启动的容器,设置开机自启动
 
docker update --restart=always redis3   //已经启动的容器,设置开机自启动

  

 6.分别依次进入redis1、redis2、redis3容器内

 

1
2
3
4
5
docker exec -it redis1 bash
 
docker exec -it redis2 bash
 
docker exec -it redis3 bash

  

进入容器内,执行  ls  命令 ,会看到映射的配置文件

 

 

 连接Redis客户端,查看主从情况: (可以看到redis1是Master ,并且有两个从节点)

 

 

 

 

 

7.分别进入redis1、redis2、redis3容器内,启动sentinel哨兵:

 

1
2
3
4
5
docker exec  -it redis1  bash         //进入redis1容器内
 
redis-sentinel    sentinel1.conf     //启动哨兵命令
 
redis-cli   -h  172.17.0.2 -p  26379  //连接哨兵客户端
1
2
3
4
5
docker exec  -it redis2  bash       //进入redis2容器内
 
redis-sentinel    sentinel2.conf     //启动哨兵命令
 
redis-cli   -h  172.17.0.3 -p  26380  //连接哨兵客户端
1
2
3
4
5
docker exec  -it redis3  bash       //进入redis3容器内
 
redis-sentinel    sentinel3.conf     //启动哨兵命令
 
redis-cli   -h  172.17.0.4 -p  26381 //连接哨兵客户端

 

 

 

 

 

 

 

 

 

8.都启动成功之后,开启另一个终端窗口,验证哨兵自动主从切换

 

1
docker  stop   redis1  //关闭主节点的容器

 

 

 可以看到主节点已经变成了172.17.0.4;主从切换成功。

 

posted @   Active_Sentinel  阅读(370)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示