www.icesr.com
IT运维工程师的摇篮

Docker下redis与springboot三部曲之二:安装redis主从和哨兵

在上一章《 Docker下redis与springboot三部曲之一:极速体验》我们快速体验了springboot访问单机版redis,今天的实战中我们在Docker下搭建redis主从和哨兵服务;

原文地址:http://blog.csdn.net/boling_cavalry/article/details/78995407

实战环境

  1. 本次实战的环境是Ubuntu16,安装的Docker版本是17.03.2-ce;
  2. 当前电脑创建文件夹/usr/local/work/share,后面会用来做共享文件夹;
  3. 本次实战要创建八个容器,redis相关的6个(1主2从3哨兵),再用一个redis客户端用于远程连接前面6个机器做验证,还有一个用来部署springboot的机器,如下表格所示:
容器名 ip 功能
blog_master_1 172.18.0.2 redis主
blog_slave_1 172.17.0.4 redis从一
blog_slave_2 172.18.0.7 redis从二
blog_sentinel_1 172.18.0.5 redis哨兵一
blog_sentinel_2 172.18.0.8 redis哨兵二
blog_sentinel_3 172.18.0.9 redis哨兵三
blog_client_1 172.18.0.3 redis客户端,远程连接主、从、哨兵验证工作情况
blog_java_1 172.18.0.6 springboot工程运行环境

制作哨兵镜像

redis主、从所用镜像同为官方redis:3,而哨兵的容器不能直接用这个镜像,需要在这个镜像的基础上做一定的加工方可使用;
制作哨兵镜像需要制作下面这三个文本文件:
1. sentinel.conf;
2. sentinel-entrypoint.sh;
3. Dockerfile;

下面依次说明这三个文件的内容和作用:

  • 本机创建一个文件夹,名为sentinel
  • sentinel文件夹下新增一个sentinel.conf文件,哨兵相关的配置都在这里,制作镜像时会用到,内容如下:
<code><span class="hljs-comment"># Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf</span>

port <span class="hljs-number">26379</span>

dir /tmp

sentinel monitor mymaster redis-master <span class="hljs-number">6379</span> <span class="hljs-variable">$SENTINEL_QUORUM</span>

sentinel down-after-milliseconds mymaster <span class="hljs-variable">$SENTINEL_DOWN_AFTER</span>

sentinel parallel-syncs mymaster <span class="hljs-number">1</span>

sentinel failover-timeout mymaster <span class="hljs-variable">$SENTINEL_FAILOVER</span></code>

上面的配置中,redis-master对应的是docker-compose.yml中redis主的link参数;

  • sentinel文件夹下新增一个sentinel-entrypoint.sh文件,容器启动的时候会执行该脚本,内容如下:
<code><span class="hljs-shebang">#!/bin/sh</span>
<span class="hljs-comment">#替换原有的配置参数</span>
sed -i <span class="hljs-string">"s//$SENTINEL_QUORUM/<span class="hljs-variable">$SENTINEL_QUORUM</span>/g"</span> /etc/redis/sentinel.conf
sed -i <span class="hljs-string">"s//$SENTINEL_DOWN_AFTER/<span class="hljs-variable">$SENTINEL_DOWN_AFTER</span>/g"</span> /etc/redis/sentinel.conf
sed -i <span class="hljs-string">"s//$SENTINEL_FAILOVER/<span class="hljs-variable">$SENTINEL_FAILOVER</span>/g"</span> /etc/redis/sentinel.conf
<span class="hljs-comment">#再执行另一个脚本</span>
<span class="hljs-keyword">exec</span> docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel</code>
  • sentinel文件夹下新增一个Dockerfile文件,内容如下:
<code><span class="hljs-preprocessor"># Docker image for redis sentinel</span>
<span class="hljs-preprocessor"># VERSION 0.0.1</span>
<span class="hljs-preprocessor"># Author: bolingcavalry</span>

<span class="hljs-preprocessor">#基础镜像使用官方redis:3</span>
FROM redis:<span class="hljs-number">3</span>

<span class="hljs-preprocessor">#作者</span>
MAINTAINER BolingCavalry &lt;zq2599@gmail.com&gt;
<span class="hljs-preprocessor">#对外暴露26379接口</span>
EXPOSE <span class="hljs-number">26379</span>
<span class="hljs-preprocessor">#替换原有的sentinel.conf文件</span>
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
<span class="hljs-preprocessor">#定义环境变量</span>
ENV<span class="hljs-constant"> SENTINEL_QUORUM </span><span class="hljs-number">2</span>
ENV<span class="hljs-constant"> SENTINEL_DOWN_AFTER </span><span class="hljs-number">30000</span>
ENV<span class="hljs-constant"> SENTINEL_FAILOVER </span><span class="hljs-number">180000</span>
<span class="hljs-preprocessor">#把容器启动时要执行的脚本复制到镜像中</span>
COPY sentinel-entrypoint.sh /usr/local/bin/
<span class="hljs-preprocessor">#给脚本添加可执行权限</span>
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
<span class="hljs-preprocessor">#定义启动容器时自动执行的脚本</span>
ENTRYPOINT [<span class="hljs-string">"sentinel-entrypoint.sh"</span>]</code>
  • 打开命令行,在sentinel文件夹下执行以下命令构建一个镜像:
<code>docker build <span class="hljs-attribute">-t</span> bolingcavalry/redis3sentinel:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span> <span class="hljs-built_in">.</span></code>

这样哨兵镜像就做好了;

通过docker-compose命令启动

哨兵镜像做好了,我们就可以启容器了,多个容器的启动还是用docker-compose比较方便,新建一个目录名叫blog,在blog目录下制作docker-compose.yml文件,内容如下:

<code>master:
  image: redis:3
  ports:
    -<span class="ruby"> <span class="hljs-string">"6379:6379"</span>
</span>slave:
  image: redis:3
  command: redis-server --slaveof redis-master 6379
  links:
    -<span class="ruby"> <span class="hljs-symbol">master:</span>redis-master
</span>sentinel:
  image: bolingcavalry/redis3sentinel:0.0.1
  environment:
    -<span class="ruby"> <span class="hljs-constant">SENTINEL_DOWN_AFTER</span>=<span class="hljs-number">5000</span>
</span>    -<span class="ruby"> <span class="hljs-constant">SENTINEL_FAILOVER</span>=<span class="hljs-number">5000</span>    
</span>  links:
    -<span class="ruby"> <span class="hljs-symbol">master:</span>redis-master
</span>    -<span class="ruby"> slave
</span>  ports:
    -<span class="ruby"> <span class="hljs-string">"26379:26379"</span>
</span>java:
  image: bolingcavalry/springbootrun:0.0.1
  links:
    -<span class="ruby"> <span class="hljs-symbol">sentinel:</span>redis-sentinel
</span>    -<span class="ruby"> <span class="hljs-symbol">master:</span>redis-master
</span>  volumes:
     -<span class="ruby"> /usr/local/work/<span class="hljs-symbol">share:</span>/usr/<span class="hljs-constant">Downloads</span>
</span>  ports:
    -<span class="ruby"> <span class="hljs-string">"8080:8080"</span>
</span>  tty:true
client:
  image: redis:3</code>

docker ps命令看到五个容器启动成功,如下图:
这里写图片描述

查看信息

  • 执行以下命令查看master的IP:
<code><span class="hljs-label">root@rabbitmq:</span>~# docker exec blog_jav<span class="hljs-built_in">a_1</span> cat /etc/hosts
<span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>       localhost
<span class="hljs-label">::</span><span class="hljs-number">1</span>     localhost ip6-localhost ip6-loopback
<span class="hljs-label">fe00::</span><span class="hljs-number">0</span> ip6-localnet
<span class="hljs-label">ff00::</span><span class="hljs-number">0</span> ip6-mcastprefix
<span class="hljs-label">ff02::</span><span class="hljs-number">1</span> ip6-allnodes
<span class="hljs-label">ff02::</span><span class="hljs-number">2</span> ip6-allrouters
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.2</span>      master_1 b3191e43f27e blog_master_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.2</span>      redis-master b3191e43f27e blog_master_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.5</span>      blog_sentinel_1 a85a4096837b
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.5</span>      redis-sentinel a85a4096837b blog_sentinel_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.5</span>      sentinel_1 a85a4096837b blog_sentinel_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.2</span>      blog_master_1 b3191e43f27e
<span class="hljs-number">172.17</span>.<span class="hljs-number">0.6</span>      <span class="hljs-number">337</span>f68cf3f25</code>

可以看到master的ip是172.17.0.2

  • 执行以下命令查看哨兵的IP:
<code>root<span class="hljs-variable">@rabbitmq</span><span class="hljs-symbol">:/usr/local/work/blog</span><span class="hljs-comment"># docker exec blog_sentinel_1 cat /etc/hosts</span>
<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span>       localhost
<span class="hljs-symbol">:</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span>     localhost ip6-localhost ip6-loopback
<span class="hljs-symbol">fe00:</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span> ip6-localnet
<span class="hljs-symbol">ff00:</span><span class="hljs-symbol">:</span><span class="hljs-number">0</span> ip6-mcastprefix
<span class="hljs-symbol">ff02:</span><span class="hljs-symbol">:</span><span class="hljs-number">1</span> ip6-allnodes
<span class="hljs-symbol">ff02:</span><span class="hljs-symbol">:</span><span class="hljs-number">2</span> ip6-allrouters
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span>      blog_master_1 b3191e43f27e
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span>      master_1 b3191e43f27e blog_master_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span>      redis-master b3191e43f27e blog_master_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">4</span>      blog_slave_1 f1cb5e268c14
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">4</span>      slave f1cb5e268c14 blog_slave_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">4</span>      slave_1 f1cb5e268c14 blog_slave_1
<span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">5</span>      a85a4096837b</code>

可以看到哨兵的ip是172.17.0.5(a85a4096837b是容器ID);

  • 执行以下命令进入client容器:
<code>docker <span class="hljs-keyword">exec</span> -it blog_client_1 /bin/bash</code>
  • 在client容器中执行以下命令查看master的情况:
<code>root<span class="hljs-localvars">@942</span>deeb36265:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.2 -p 6379 info Replication</span>
<span class="hljs-preprocessor"># Replication</span>
<span class="hljs-label">role:</span>master
<span class="hljs-label">connected_slaves:</span><span class="hljs-number">1</span>
<span class="hljs-label">slave0:</span>ip=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.4</span>,port=<span class="hljs-number">6379</span>,state=online,offset=<span class="hljs-number">99305</span>,lag=<span class="hljs-number">0</span>
<span class="hljs-label">master_repl_offset:</span><span class="hljs-number">99440</span>
<span class="hljs-label">repl_backlog_active:</span><span class="hljs-number">1</span>
<span class="hljs-label">repl_backlog_size:</span><span class="hljs-number">1048576</span>
<span class="hljs-label">repl_backlog_first_byte_offset:</span><span class="hljs-number">2</span>
<span class="hljs-label">repl_backlog_histlen:</span><span class="hljs-number">99439</span></code>

可以看见只有一个slave:172.17.0.4

  • 在client容器中执行以下命令查看哨兵的情况:
<code>root<span class="hljs-localvars">@942</span>deeb36265:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.5 -p 26379 info Sentinel</span>
<span class="hljs-preprocessor"># Sentinel</span>
<span class="hljs-label">sentinel_masters:</span><span class="hljs-number">1</span>
<span class="hljs-label">sentinel_tilt:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_running_scripts:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_scripts_queue_length:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_simulate_failure_flags:</span><span class="hljs-number">0</span>
<span class="hljs-label">master0:</span>name=mymaster,status=ok,address=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.2</span>:<span class="hljs-number">6379</span>,slaves=<span class="hljs-number">2</span>,sentinels=<span class="hljs-number">1</span></code>

可以看见只有一个哨兵;

slave扩容

之前我们规划的是两个slave、三个哨兵,目前只有一个slave和一个哨兵,接下来我们试试docker-compose提供的横向扩容的命令,快速增加slave和哨兵的容器数;

执行以下命令,增加一个slave:

<code><span class="hljs-attribute">docker-compose scale slave</span>=<span class="hljs-string">2</span></code>

再执行命令docker ps看一下容器情况,如下:

<code>root@rabbitmq:/usr/local/work/blog<span class="hljs-comment"># docker-compose scale slave=2</span>
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting blog_slave_1 <span class="hljs-keyword">...</span> done
Creating blog_slave_2 <span class="hljs-keyword">...</span> done
root@rabbitmq:/usr/local/work/blog<span class="hljs-comment"># docker ps</span>
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                NAMES
56a10f1fc70b        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   <span class="hljs-number">11</span> seconds ago      Up <span class="hljs-number">10</span> seconds       <span class="hljs-number">6379</span>/tcp                             blog_slave_2
337f68cf3f25        bolingcavalry/springbootrun:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>    <span class="hljs-string">"/bin/bash"</span>              <span class="hljs-number">29</span> minutes ago      Up <span class="hljs-number">29</span> minutes       <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">8080</span>-&gt;<span class="hljs-number">8080</span>/tcp               blog_java_1
a85a4096837b        bolingcavalry/redis3sentinel:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>   <span class="hljs-string">"sentinel-entrypoi..."</span>   <span class="hljs-number">29</span> minutes ago      Up <span class="hljs-number">29</span> minutes       <span class="hljs-number">6379</span>/tcp, <span class="hljs-number">26379</span>/tcp   blog_sentinel_1
f1cb5e268c14        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   <span class="hljs-number">29</span> minutes ago      Up <span class="hljs-number">29</span> minutes       <span class="hljs-number">6379</span>/tcp                             blog_slave_1
942deeb36265        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   <span class="hljs-number">29</span> minutes ago      Up <span class="hljs-number">29</span> minutes       <span class="hljs-number">6379</span>/tcp                             blog_client_1
b3191e43f27e        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   <span class="hljs-number">29</span> minutes ago      Up <span class="hljs-number">29</span> minutes       <span class="hljs-number">6379</span>/tcp               blog_master_1</code>
  • 在client容器上执行命令看看主从的信息:
<code>root<span class="hljs-localvars">@942</span>deeb36265:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.2 -p 6379 info Replication</span>
<span class="hljs-preprocessor"># Replication</span>
<span class="hljs-label">role:</span>master
<span class="hljs-label">connected_slaves:</span><span class="hljs-number">2</span>
<span class="hljs-label">slave0:</span>ip=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.4</span>,port=<span class="hljs-number">6379</span>,state=online,offset=<span class="hljs-number">127060</span>,lag=<span class="hljs-number">1</span>
<span class="hljs-label">slave1:</span>ip=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.7</span>,port=<span class="hljs-number">6379</span>,state=online,offset=<span class="hljs-number">127060</span>,lag=<span class="hljs-number">0</span>
<span class="hljs-label">master_repl_offset:</span><span class="hljs-number">127195</span>
<span class="hljs-label">repl_backlog_active:</span><span class="hljs-number">1</span>
<span class="hljs-label">repl_backlog_size:</span><span class="hljs-number">1048576</span>
<span class="hljs-label">repl_backlog_first_byte_offset:</span><span class="hljs-number">2</span>
<span class="hljs-label">repl_backlog_histlen:</span><span class="hljs-number">127194</span></code>

哨兵扩容

执行以下命令,增加两个哨兵:

<code><span class="hljs-attribute">docker-compose scale sentinel</span>=<span class="hljs-string">3</span></code>

再执行命令docker ps看一下容器情况,如下:

<code>root@rabbitmq:/usr/local/work/blog<span class="hljs-comment"># docker-compose scale sentinel=3</span>
WARNING: The scale command is deprecated. Use the up command with the --scale flag instead.
Starting blog_sentinel_1 <span class="hljs-keyword">...</span> done
Creating blog_sentinel_2 <span class="hljs-keyword">...</span> done
Creating blog_sentinel_3 <span class="hljs-keyword">...</span> done
root@rabbitmq:/usr/local/work/blog<span class="hljs-comment"># docker ps</span>
CONTAINER ID        IMAGE                                COMMAND                  CREATED              STATUS              PORTS                    NAMES
45ede729f216        bolingcavalry/redis3sentinel:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>   <span class="hljs-string">"sentinel-entrypoi..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp, <span class="hljs-number">26379</span>/tcp      blog_sentinel_3
f99d0501b137        bolingcavalry/redis3sentinel:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>   <span class="hljs-string">"sentinel-entrypoi..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp, <span class="hljs-number">26379</span>/tcp      blog_sentinel_2
abcc206ff31f        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp                 blog_slave_2
f7527e8d9b98        bolingcavalry/springbootrun:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>    <span class="hljs-string">"/bin/bash"</span>              About a minute ago   Up About a minute   <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">8080</span>-&gt;<span class="hljs-number">8080</span>/tcp   blog_java_1
f3b7e3e8e820        bolingcavalry/redis3sentinel:<span class="hljs-number">0.0</span><span class="hljs-number">.1</span>   <span class="hljs-string">"sentinel-entrypoi..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp, <span class="hljs-number">26379</span>/tcp      blog_sentinel_1
3a42ac696e7b        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp                 blog_slave_1
11c4bc32062d        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp                 blog_master_1
07a2be2bf114        redis:<span class="hljs-number">3</span>                              <span class="hljs-string">"docker-entrypoint..."</span>   About a minute ago   Up About a minute   <span class="hljs-number">6379</span>/tcp                 blog_client_1</code>
  • 在client容器上执行命令redis-cli -h 172.17.0.5 -p 26379 info Sentinel看看哨兵的信息:
<code>root<span class="hljs-localvars">@07</span>a2be2bf114:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.5 -p 26379 info Sentinel</span>
<span class="hljs-preprocessor"># Sentinel</span>
<span class="hljs-label">sentinel_masters:</span><span class="hljs-number">1</span>
<span class="hljs-label">sentinel_tilt:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_running_scripts:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_scripts_queue_length:</span><span class="hljs-number">0</span>
<span class="hljs-label">sentinel_simulate_failure_flags:</span><span class="hljs-number">0</span>
<span class="hljs-label">master0:</span>name=mymaster,status=ok,address=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.3</span>:<span class="hljs-number">6379</span>,slaves=<span class="hljs-number">2</span>,sentinels=<span class="hljs-number">3</span></code>

可以看到哨兵数量为3;

验证扩容结果

进入master容器写一条记录,再进入slave容器读这个记录,看看主从同步是否生效:

<code>root<span class="hljs-variable">@rabbitmq</span><span class="hljs-symbol">:/usr/local/work/blog</span><span class="hljs-comment"># docker exec -it blog_master_1 /bin/bash</span>
root<span class="hljs-variable">@11c4bc32062d</span><span class="hljs-symbol">:/data</span><span class="hljs-comment"># redis-cli</span>
<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span>&gt; set abc <span class="hljs-number">123456</span>
<span class="hljs-constant">OK</span>
<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span>&gt; quit
root<span class="hljs-variable">@11c4bc32062d</span><span class="hljs-symbol">:/data</span><span class="hljs-comment"># exit</span>
exit
root<span class="hljs-variable">@rabbitmq</span><span class="hljs-symbol">:/usr/local/work/blog</span><span class="hljs-comment"># docker exec -it blog_slave_2 /bin/bash</span>
root<span class="hljs-variable">@abcc206ff31f</span><span class="hljs-symbol">:/data</span><span class="hljs-comment"># redis-cli</span>
<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span>&gt; get abc
<span class="hljs-string">"123456"</span>
<span class="hljs-number">127.0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span>&gt;</code>

验证高可用

  • 执行命令docker stop blog_master_1将master停掉
  • 执行命令docker logs -f blog_sentinel_1看见哨兵容器的日志如下:
<code><span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.358</span> <span class="hljs-comment"># +sdown master mymaster 172.17.0.2 6379</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.458</span> <span class="hljs-comment"># +new-epoch 1</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.458</span> <span class="hljs-comment"># +vote-for-leader dae600f31e27dc2b5eef79cdb6154f4365676a04 1</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.865</span> <span class="hljs-comment"># +config-update-from sentinel dae600f31e27dc2b5eef79cdb6154f4365676a04 172.17.0.9 26379 @ mymaster 172.17.0.2 6379</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.865</span> <span class="hljs-comment"># +switch-master mymaster 172.17.0.2 6379 172.17.0.4 6379</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.865</span> * +slave slave <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">7</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span> <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">7</span> <span class="hljs-number">6379</span> @ mymaster <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">4</span> <span class="hljs-number">6379</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">43.866</span> * +slave slave <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span><span class="hljs-symbol">:</span><span class="hljs-number">6379</span> <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">2</span> <span class="hljs-number">6379</span> @ mymaster <span class="hljs-number">172.17</span>.<span class="hljs-number">0</span>.<span class="hljs-number">4</span> <span class="hljs-number">6379</span>
<span class="hljs-number">1</span><span class="hljs-symbol">:X</span> <span class="hljs-number">10</span> <span class="hljs-constant">Jan</span> <span class="hljs-number">04</span><span class="hljs-symbol">:</span><span class="hljs-number">37</span><span class="hljs-symbol">:</span><span class="hljs-number">48.903</span> <span class="hljs-comment"># +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.4 6379</span></code>

可以看见发生了切换,redis集群的master由之前的 172.17.0.2 6379变成了172.17.0.4 6379;

  • 进入blog_client_1容器,执行命令redis-cli -h 172.17.0.4 -p 6379 info Replication 看看最新的master信息如下:
<code>root@c451d8ecb672:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.4 -p 6379 info Replication </span>
<span class="hljs-preprocessor"># Replication</span>
<span class="hljs-label">role:</span>master
<span class="hljs-label">connected_slaves:</span><span class="hljs-number">1</span>
<span class="hljs-label">slave0:</span>ip=<span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.7</span>,port=<span class="hljs-number">6379</span>,state=online,offset=<span class="hljs-number">22318</span>,lag=<span class="hljs-number">0</span>
<span class="hljs-label">master_repl_offset:</span><span class="hljs-number">22453</span>
<span class="hljs-label">repl_backlog_active:</span><span class="hljs-number">1</span>
<span class="hljs-label">repl_backlog_size:</span><span class="hljs-number">1048576</span>
<span class="hljs-label">repl_backlog_first_byte_offset:</span><span class="hljs-number">2</span>
<span class="hljs-label">repl_backlog_histlen:</span><span class="hljs-number">22452</span></code>
  • 进入blog_client_1容器,执行命令redis-cli -h 172.17.0.7 -p 6379 info Replication 看看最另一个slave信息如下,role依然是slave:
<code>root@c451d8ecb672:/data<span class="hljs-preprocessor"># redis-cli -h 172.17.0.7 -p 6379 info Replication </span>
<span class="hljs-preprocessor"># Replication</span>
<span class="hljs-label">role:</span>slave
<span class="hljs-label">master_host:</span><span class="hljs-number">172.17</span><span class="hljs-number">.0</span><span class="hljs-number">.4</span>
<span class="hljs-label">master_port:</span><span class="hljs-number">6379</span>
<span class="hljs-label">master_link_status:</span>up
<span class="hljs-label">master_last_io_seconds_ago:</span><span class="hljs-number">1</span>
<span class="hljs-label">master_sync_in_progress:</span><span class="hljs-number">0</span>
<span class="hljs-label">slave_repl_offset:</span><span class="hljs-number">28570</span>
<span class="hljs-label">slave_priority:</span><span class="hljs-number">100</span>
<span class="hljs-label">slave_read_only:</span><span class="hljs-number">1</span>
<span class="hljs-label">connected_slaves:</span><span class="hljs-number">0</span>
<span class="hljs-label">master_repl_offset:</span><span class="hljs-number">0</span>
<span class="hljs-label">repl_backlog_active:</span><span class="hljs-number">0</span>
<span class="hljs-label">repl_backlog_size:</span><span class="hljs-number">1048576</span>
<span class="hljs-label">repl_backlog_first_byte_offset:</span><span class="hljs-number">0</span>
<span class="hljs-label">repl_backlog_histlen:</span><span class="hljs-number">0</span></code>

以上就是在Docker下搭建redis主从和哨兵的整个实战过程,接下来的实战,我们会用springboot访问redis高可用环境,本章的环境在下一个章的实战中会继续用到;

未经允许不得转载:冰点网络 » Docker下redis与springboot三部曲之二:安装redis主从和哨兵

分享到:更多 ()

评论 抢沙发

评论前必须登录!