Docker下Redis主从模式的搭建

Docker下Redis主从模式的搭建

一,准备

三台装有dockerServer的服务器(ip:ip1, ip2, ip3),Redis镜像

ip1:Redis主机,Redis哨兵1

ip2:Redis从机,Redis哨兵2

ip3:Redis哨兵3

集群名称:mymaster

主从端口:6379

哨兵端口:26379

密码:admin

服务器选择挂载目录:/Data/redis

二,搭建

2.1 搭建主机

在部署Redis主机的服务器/Data目录下,创建目录redis,然后在/Data/redis目录下创建logs和data两个文件夹;然后将配置文件移到/Data/redis目录下,并重新命名:redis.conf

redis.conf:

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
logfile "/etc/redis/redis-6379.log"
databases 16
always-show-logo yes
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
masterauth "admin"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
slaveof ip1 6379
slave-announce-ip ip1
slave-announce-port 6379
requirepass "admin"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

运行主机容器:

docker run –d --privileged=true –net host –v /Data/redis/redis.conf:/etc/redis/redis.conf  –v /Data/redis/logs/:/etc/redis/  -v /Data/redis/data:/data –name redis-6379 10.175.187.54:5000/redis  redis-server /etc/redis/redis.conf

2.2 搭建从机

在部署Redis从机的服务器Data目录下,创建目录redis,然后在/Data/redis目录下创建logs和data两个文件夹;然后将配置文件移到/Data/redis目录下,并重新命名:redis.conf

redis.conf

bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
databases 16
always-show-logo yes
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
masterauth "admin"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass "admin"
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
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
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

slave-announce-ip ip2
slave-announce-port 6379
slaveof ip1 6379

运行从机容器:

docker run –d --privileged=true –net host –v /Data/redis/redis.conf:/etc/redis/redis.conf  –v /Data/redis/logs/:/etc/redis/  -v /Data/redis/data:/data –name redis-6379 10.175.187.54:5000/redis  redis-server /etc/redis/redis.conf

2.3 搭建哨兵

在三台安装了DockerServer的服务器,在/Data/redis目录下创建/logs文件夹,然后将配置文件移到/Data/redis目录下,并重新命名:sentinel.conf

sentinel.conf

logfile "/etc/redis/redis-sentinel.log"

port 26379
daemonize no
protected-mode no
sentinel monitor mymaster ip1/ip2/ip3 6379 2
sentinel down-after-milliseconds mymaster 5000
#sentinel deny-scripts-reconfig yes
sentinel failover-timeout mymaster 60000
# Generated by CONFIG REWRITE
dir "/data"
sentinel auth-pass mymaster admin
sentinel config-epoch mymaster 8
sentinel leader-epoch mymaster 8
sentinel current-epoch 8

分别在三台服务器运行Redis哨兵容器命令:

docker run -privileged=true –net host –v /Data/redis/redis.conf:/sentinel.conf –v /Data/redis/:/etc/redis/ --name redis-sentinel 10.175.187.54:5000/redis redis-sentinel /sentinel.conf

部署完成

注意:给配置文件赋予读写可执行权限

chmod -R 777 xxx.conf

三,测试

打开两个控制台窗口,分别执行命令:

redis-cli –h ip1 –a admin
和
redis-cli –h ip2 –a admin

进入redis后,再执行命令:

info replication

查看情况,确认无误后,关闭Redis主机:

docker stop redis-6379

然后到从机redis查看情况:

info replication

观察从机是否变成主机。

再重新启动原Redis主机:

docker start redis-6379

观察此时原主机是否变成了从机。

此时可以关闭原从机(此时的主机),

docker stop redis-6379

观察原主机(此时的从机)是否会变成主机

info replication
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值