在上一章《 Docker下redis与springboot三部曲之一:极速体验》我们快速体验了springboot访问单机版redis,今天的实战中我们在Docker下搭建redis主从和哨兵服务;
原文地址:http://blog.csdn.net/boling_cavalry/article/details/78995407
redis主、从所用镜像同为官方redis:3,而哨兵的容器不能直接用这个镜像,需要在这个镜像的基础上做一定的加工方可使用; 制作哨兵镜像需要制作下面这三个文本文件:
下面依次说明这三个文件的内容和作用:
# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf
port 26379
dir /tmp
sentinel monitor mymaster redis-master 6379 $SENTINEL_QUORUM
sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster $SENTINEL_FAILOVER
上面的配置中,redis-master对应的是docker-compose.yml中redis主的link参数;
#!/bin/sh
#替换原有的配置参数
sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf
#再执行另一个脚本
exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel
# Docker image for redis sentinel
# VERSION 0.0.1
# Author: bolingcavalry
#基础镜像使用官方redis:3
FROM redis:3
#作者
MAINTAINER BolingCavalry <zq2599@gmail.com>
#对外暴露26379接口
EXPOSE 26379
#替换原有的sentinel.conf文件
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
#定义环境变量
ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 30000
ENV SENTINEL_FAILOVER 180000
#把容器启动时要执行的脚本复制到镜像中
COPY sentinel-entrypoint.sh /usr/local/bin/
#给脚本添加可执行权限
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
#定义启动容器时自动执行的脚本
ENTRYPOINT ["sentinel-entrypoint.sh"]
docker build -t bolingcavalry/redis3sentinel:0.0.1 .
这样哨兵镜像就做好了;
哨兵镜像做好了,我们就可以启容器了,多个容器的启动还是用docker-compose比较方便,新建一个目录名叫blog,在blog目录下制作docker-compose.yml文件,内容如下:
master:
image: redis:3
ports:
- "6379:6379"
slave:
image: redis:3
command: redis-server --slaveof redis-master 6379
links:
- master:redis-master
sentinel:
image: bolingcavalry/redis3sentinel:0.0.1
environment:
- SENTINEL_DOWN_AFTER=5000
- SENTINEL_FAILOVER=5000
links:
- master:redis-master
- slave
java:
image: bolingcavalry/springbootrun:0.0.1
links:
- sentinel:redis-sentinel
- master:redis-master
volumes:
- /usr/local/work/share:/usr/Downloads
ports:
- "8080:8080"
tty:true
client:
image: redis:3
用docker ps命令看到五个容器启动成功,如下图:
root@rabbitmq:~# docker exec blog_java_1 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 master_1 b3191e43f27e blog_master_1
172.17.0.2 redis-master b3191e43f27e blog_master_1
172.17.0.5 blog_sentinel_1 a85a4096837b
172.17.0.5 redis-sentinel a85a4096837b blog_sentinel_1
172.17.0.5 sentinel_1 a85a4096837b blog_sentinel_1
172.17.0.2 blog_master_1 b3191e43f27e
172.17.0.6 337f68cf3f25
可以看到master的ip是172.17.0.2;
root@rabbitmq:/usr/local/work/blog# docker exec blog_sentinel_1 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 blog_master_1 b3191e43f27e
172.17.0.2 master_1 b3191e43f27e blog_master_1
172.17.0.2 redis-master b3191e43f27e blog_master_1
172.17.0.4 blog_slave_1 f1cb5e268c14
172.17.0.4 slave f1cb5e268c14 blog_slave_1
172.17.0.4 slave_1 f1cb5e268c14 blog_slave_1
172.17.0.5 a85a4096837b
可以看到哨兵的ip是172.17.0.5(a85a4096837b是容器ID);
docker exec -it blog_client_1 /bin/bash
root@942deeb36265:/data# redis-cli -h 172.17.0.2 -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.17.0.4,port=6379,state=online,offset=99305,lag=0
master_repl_offset:99440
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:99439
可以看见只有一个slave:172.17.0.4
root@942deeb36265:/data# redis-cli -h 172.17.0.5 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.17.0.2:6379,slaves=2,sentinels=1
可以看见只有一个哨兵;
之前我们规划的是两个slave、三个哨兵,目前只有一个slave和一个哨兵,接下来我们试试docker-compose提供的横向扩容的命令,快速增加slave和哨兵的容器数;
执行以下命令,增加一个slave:
docker-compose scale slave=2
再执行命令docker ps看一下容器情况,如下:
root@rabbitmq:/usr/local/work/blog# docker-compose scale slave=2
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting blog_slave_1 ... done
Creating blog_slave_2 ... done
root@rabbitmq:/usr/local/work/blog# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56a10f1fc70b redis:3 "docker-entrypoint..." 11 seconds ago Up 10 seconds 6379/tcp blog_slave_2
337f68cf3f25 bolingcavalry/springbootrun:0.0.1 "/bin/bash" 29 minutes ago Up 29 minutes 0.0.0.0:8080->8080/tcp blog_java_1
a85a4096837b bolingcavalry/redis3sentinel:0.0.1 "sentinel-entrypoi..." 29 minutes ago Up 29 minutes 6379/tcp, 26379/tcp blog_sentinel_1
f1cb5e268c14 redis:3 "docker-entrypoint..." 29 minutes ago Up 29 minutes 6379/tcp blog_slave_1
942deeb36265 redis:3 "docker-entrypoint..." 29 minutes ago Up 29 minutes 6379/tcp blog_client_1
b3191e43f27e redis:3 "docker-entrypoint..." 29 minutes ago Up 29 minutes 6379/tcp blog_master_1
root@942deeb36265:/data# redis-cli -h 172.17.0.2 -p 6379 info Replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.17.0.4,port=6379,state=online,offset=127060,lag=1
slave1:ip=172.17.0.7,port=6379,state=online,offset=127060,lag=0
master_repl_offset:127195
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:127194
执行以下命令,增加两个哨兵:
docker-compose scale sentinel=3
再执行命令docker ps看一下容器情况,如下:
root@rabbitmq:/usr/local/work/blog# docker-compose scale sentinel=3
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting blog_sentinel_1 ... done
Creating blog_sentinel_2 ... done
Creating blog_sentinel_3 ... done
root@rabbitmq:/usr/local/work/blog# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
45ede729f216 bolingcavalry/redis3sentinel:0.0.1 "sentinel-entrypoi..." About a minute ago Up About a minute 6379/tcp, 26379/tcp blog_sentinel_3
f99d0501b137 bolingcavalry/redis3sentinel:0.0.1 "sentinel-entrypoi..." About a minute ago Up About a minute 6379/tcp, 26379/tcp blog_sentinel_2
abcc206ff31f redis:3 "docker-entrypoint..." About a minute ago Up About a minute 6379/tcp blog_slave_2
f7527e8d9b98 bolingcavalry/springbootrun:0.0.1 "/bin/bash" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp blog_java_1
f3b7e3e8e820 bolingcavalry/redis3sentinel:0.0.1 "sentinel-entrypoi..." About a minute ago Up About a minute 6379/tcp, 26379/tcp blog_sentinel_1
3a42ac696e7b redis:3 "docker-entrypoint..." About a minute ago Up About a minute 6379/tcp blog_slave_1
11c4bc32062d redis:3 "docker-entrypoint..." About a minute ago Up About a minute 6379/tcp blog_master_1
07a2be2bf114 redis:3 "docker-entrypoint..." About a minute ago Up About a minute 6379/tcp blog_client_1
root@07a2be2bf114:/data# redis-cli -h 172.17.0.5 -p 26379 info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.17.0.3:6379,slaves=2,sentinels=3
可以看到哨兵数量为3;
进入master容器写一条记录,再进入slave容器读这个记录,看看主从同步是否生效:
root@rabbitmq:/usr/local/work/blog# docker exec -it blog_master_1 /bin/bash
root@11c4bc32062d:/data# redis-cli
127.0.0.1:6379> set abc 123456
OK
127.0.0.1:6379> quit
root@11c4bc32062d:/data# exit
exit
root@rabbitmq:/usr/local/work/blog# docker exec -it blog_slave_2 /bin/bash
root@abcc206ff31f:/data# redis-cli
127.0.0.1:6379> get abc
"123456"
127.0.0.1:6379>
1:X 10 Jan 04:37:43.358 # +sdown master mymaster 172.17.0.2 6379
1:X 10 Jan 04:37:43.458 # +new-epoch 1
1:X 10 Jan 04:37:43.458 # +vote-for-leader dae600f31e27dc2b5eef79cdb6154f4365676a04 1
1:X 10 Jan 04:37:43.865 # +config-update-from sentinel dae600f31e27dc2b5eef79cdb6154f4365676a04 172.17.0.9 26379 @ mymaster 172.17.0.2 6379
1:X 10 Jan 04:37:43.865 # +switch-master mymaster 172.17.0.2 6379 172.17.0.4 6379
1:X 10 Jan 04:37:43.865 * +slave slave 172.17.0.7:6379 172.17.0.7 6379 @ mymaster 172.17.0.4 6379
1:X 10 Jan 04:37:43.866 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.4 6379
1:X 10 Jan 04:37:48.903 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.4 6379
可以看见发生了切换,redis集群的master由之前的 172.17.0.2 6379变成了172.17.0.4 6379;
root@c451d8ecb672:/data# redis-cli -h 172.17.0.4 -p 6379 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.17.0.7,port=6379,state=online,offset=22318,lag=0
master_repl_offset:22453
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:22452
root@c451d8ecb672:/data# redis-cli -h 172.17.0.7 -p 6379 info Replication
# Replication
role:slave
master_host:172.17.0.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:28570
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
以上就是在Docker下搭建redis主从和哨兵的整个实战过程,接下来的实战,我们会用springboot访问redis高可用环境,本章的环境在下一个章的实战中会继续用到;