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

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

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

环境:Centos7+Docker

 

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

 

docker pull  redis:6.0.12  

  

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

 

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配置如下:

 

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配置:

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配置如下:

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配置如下:

 

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

 

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服务自启动

 

docker update --restart=always redis1    //已经启动的容器,设置开机自启动

docker update --restart=always redis2    //已经启动的容器,设置开机自启动

docker update --restart=always redis3   //已经启动的容器,设置开机自启动

  

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

 

docker exec -it redis1 bash

docker exec -it redis2 bash

docker exec -it redis3 bash

  

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

 

 

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

 

 

 

 

 

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

 

docker exec  -it redis1  bash         //进入redis1容器内

redis-sentinel    sentinel1.conf     //启动哨兵命令

redis-cli   -h  172.17.0.2 -p  26379  //连接哨兵客户端
docker exec  -it redis2  bash       //进入redis2容器内

redis-sentinel    sentinel2.conf     //启动哨兵命令

redis-cli   -h  172.17.0.3 -p  26380  //连接哨兵客户端
docker exec  -it redis3  bash       //进入redis3容器内

redis-sentinel    sentinel3.conf     //启动哨兵命令

redis-cli   -h  172.17.0.4 -p  26381 //连接哨兵客户端

 

 

 

 

 

 

 

 

 

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

 

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

 

 

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

 

posted @ 2022-01-25 20:12  Active_Sentinel  阅读(322)  评论(0编辑  收藏  举报