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

Docker构建Redis集群

Redis基础镜像

在目录/data下创建redis_base目录。

<code>mkdir redis_base
<span class="hljs-built_in">cd</span> redis_base
vim Dockerfile</code>

Redis基础镜像的Dockerfile如下:

<code>FROM ubuntu
MAINTAINER chen <span class="hljs-string">"383936915@qq.com"</span>
ENV REFRESHED_AT <span class="hljs-number">2017</span><span class="hljs-subst">-</span><span class="hljs-number">09</span><span class="hljs-subst">-</span><span class="hljs-number">16</span>
RUN apt<span class="hljs-attribute">-get</span> <span class="hljs-attribute">-yyq</span> update <span class="hljs-subst">&amp;&amp;</span> apt<span class="hljs-attribute">-get</span> <span class="hljs-attribute">-yqq</span> install redis<span class="hljs-attribute">-server</span> redis<span class="hljs-attribute">-tools</span>
VOLUME <span class="hljs-preprocessor">[</span> <span class="hljs-string">"/var/lib/redis"</span>,<span class="hljs-string">"/var/log/redis/"</span><span class="hljs-preprocessor">]</span><span class="hljs-markup">
EXPOSE 6379
CMD </span><span class="hljs-preprocessor">[</span><span class="hljs-preprocessor">]</span><span class="hljs-markup"></span></code>

进入/data/redis_base目录,执行命令:

<code>docker build <span class="hljs-attribute">-t</span> chen/redis_base <span class="hljs-built_in">.</span></code>

构建完成后,查看镜像:

<code>docker images </code>

可以看到Redis的镜像已经构建完成。

Redis Master节点

在目录/data下创建redis_primary目录。

<code>mkdir redis_primary
<span class="hljs-built_in">cd</span> redis_primary
vim Dockerfile</code>

Redis Master节点镜像的Dockerfile如下:

<code><span class="hljs-keyword">FROM</span> chen/redis_base
MAINTAINER chen <span class="hljs-string">"383936915@qq.com"</span>
ENV REFRESHED_AT <span class="hljs-number">2017</span>-<span class="hljs-number">09</span>-<span class="hljs-number">22</span>
ENTRYPOINT [<span class="hljs-string">"redis-server"</span>,<span class="hljs-string">"--logfile /var/log/redis/redis-server.log"</span>]</code>

进入/data/redis_primary目录,执行命令:

<code>docker build <span class="hljs-attribute">-t</span> chen/redis_primary <span class="hljs-built_in">.</span></code>

构建完成后,查看镜像:

<code>docker images </code>

可以看到Redis Master的镜像已经构建完成。

创建Docker NetWork

创建express network,Redis集群的节点都运行在express中。
执行以下命令创建express network:

<code>docker network <span class="hljs-operator"><span class="hljs-keyword">create</span> express </span></code>

这样我们就创建了一个express network,执行以下命令可以查看express的信息:

<code>docker network inspect express</code>

Redis从节点

步骤跟主节点一致,主要是Dockfile不一样,Dockerfile如下:

<code><span class="hljs-keyword">FROM</span> chen/redis_replica
MAINTAINER chen <span class="hljs-string">"383936915@qq.com"</span>
ENV REFRESHED_AT <span class="hljs-number">2017</span>-<span class="hljs-number">09</span>-<span class="hljs-number">22</span>
ENTRYPOINT [<span class="hljs-string">"redis-server"</span>,<span class="hljs-string">"--logfile /var/log/redis/redis-replica.log"</span>,<span class="hljs-string">"--slaveof redis_primary 6379"</span>]</code>

构建/data/redis_replica目录下执行以下命令:

<code>docker build <span class="hljs-attribute">-t</span> chen/redis_replica <span class="hljs-built_in">.</span></code>

这样一个Redis从节点的镜像就构建好了。

启动Redis主从节点

我们需要先启动主节点,并运行在express network中,执行以下命令:

<code><span class="hljs-comment">docker</span> <span class="hljs-comment">run</span> <span class="hljs-literal">-</span><span class="hljs-comment">d</span> <span class="hljs-literal">-</span><span class="hljs-comment">h</span> <span class="hljs-comment">redis_primary</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">net</span> <span class="hljs-comment">express</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">name</span> <span class="hljs-comment">redis_primary</span> <span class="hljs-comment">chen/redis_primary</span> </code>

依次启动两个从节点,执行以下命令:

<code><span class="hljs-comment">docker</span> <span class="hljs-comment">run</span> <span class="hljs-literal">-</span><span class="hljs-comment">d</span> <span class="hljs-literal">-</span><span class="hljs-comment">h</span> <span class="hljs-comment">redis_replica1</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">name</span> <span class="hljs-comment">redis_replica1</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">net</span> <span class="hljs-comment">express</span> <span class="hljs-comment">chen/redis_replica</span> </code>
<code><span class="hljs-comment">docker</span> <span class="hljs-comment">run</span> <span class="hljs-literal">-</span><span class="hljs-comment">d</span> <span class="hljs-literal">-</span><span class="hljs-comment">h</span> <span class="hljs-comment">redis_replica2</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">name</span> <span class="hljs-comment">redis_replica2</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">net</span> <span class="hljs-comment">express</span> <span class="hljs-comment">chen/redis_replica</span> </code>

这样一个主节点和两个从节点都启动起来了。

查看Redis主从节点的日志

先查看Redis Master节点的日志,如下:

<code>docker run <span class="hljs-attribute">-ti</span> <span class="hljs-subst">--</span>rm <span class="hljs-subst">--</span>volumes<span class="hljs-attribute">-from</span> redis_replica1 ubuntu cat /<span class="hljs-built_in">var</span>/<span class="hljs-keyword">log</span>/redis/redis<span class="hljs-attribute">-replica</span><span class="hljs-built_in">.</span><span class="hljs-keyword">log</span></code>

可以看到主节点具体的日志输出:

<code>1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.929</span> * <span class="hljs-tag">The</span> <span class="hljs-tag">server</span> <span class="hljs-tag">is</span> <span class="hljs-tag">now</span> <span class="hljs-tag">ready</span> <span class="hljs-tag">to</span> <span class="hljs-tag">accept</span> <span class="hljs-tag">connections</span> <span class="hljs-tag">on</span> <span class="hljs-tag">port</span> 6379
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.934</span> * <span class="hljs-tag">Connecting</span> <span class="hljs-tag">to</span> <span class="hljs-tag">MASTER</span> <span class="hljs-tag">redis_primary</span><span class="hljs-pseudo">:6379</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.939</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span> <span class="hljs-tag">started</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Non</span> <span class="hljs-tag">blocking</span> <span class="hljs-tag">connect</span> <span class="hljs-tag">for</span> <span class="hljs-tag">SYNC</span> <span class="hljs-tag">fired</span> <span class="hljs-tag">the</span> <span class="hljs-tag">event</span>.
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Master</span> <span class="hljs-tag">replied</span> <span class="hljs-tag">to</span> <span class="hljs-tag">PING</span>, <span class="hljs-tag">replication</span> <span class="hljs-tag">can</span> <span class="hljs-tag">continue</span>...
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Partial</span> <span class="hljs-tag">resynchronization</span> <span class="hljs-tag">not</span> <span class="hljs-tag">possible</span> (<span class="hljs-tag">no</span> <span class="hljs-tag">cached</span> <span class="hljs-tag">master</span>)
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:15</span><span class="hljs-class">.941</span> * <span class="hljs-tag">Full</span> <span class="hljs-tag">resync</span> <span class="hljs-tag">from</span> <span class="hljs-tag">master</span>: 3<span class="hljs-tag">dc967ddf2be0a82b9c9af0027a478e058f523cf</span><span class="hljs-pseudo">:1</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:16</span><span class="hljs-class">.044</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">receiving</span> 18 <span class="hljs-tag">bytes</span> <span class="hljs-tag">from</span> <span class="hljs-tag">master</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:16</span><span class="hljs-class">.044</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Flushing</span> <span class="hljs-tag">old</span> <span class="hljs-tag">data</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:16</span><span class="hljs-class">.044</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Loading</span> <span class="hljs-tag">DB</span> <span class="hljs-tag">in</span> <span class="hljs-tag">memory</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:35</span><span class="hljs-pseudo">:16</span><span class="hljs-class">.044</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Finished</span> <span class="hljs-tag">with</span> <span class="hljs-tag">success</span></code>

从节点的具体日志输出,执行以下命令:

<code>docker run <span class="hljs-attribute">-ti</span> <span class="hljs-subst">--</span>rm <span class="hljs-subst">--</span>volumes<span class="hljs-attribute">-from</span> redis_replica1 ubuntu cat /<span class="hljs-built_in">var</span>/<span class="hljs-keyword">log</span>/redis/redis<span class="hljs-attribute">-replica</span><span class="hljs-built_in">.</span><span class="hljs-keyword">log</span></code>
<code>docker run <span class="hljs-attribute">-ti</span> <span class="hljs-subst">--</span>rm <span class="hljs-subst">--</span>volumes<span class="hljs-attribute">-from</span> redis_replica2 ubuntu cat /<span class="hljs-built_in">var</span>/<span class="hljs-keyword">log</span>/redis/redis<span class="hljs-attribute">-replica</span><span class="hljs-built_in">.</span><span class="hljs-keyword">log</span></code>

可以看到Redis从节点的日志输出:

<code>1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.937</span> * <span class="hljs-tag">Connecting</span> <span class="hljs-tag">to</span> <span class="hljs-tag">MASTER</span> <span class="hljs-tag">redis_primary</span><span class="hljs-pseudo">:6379</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.939</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span> <span class="hljs-tag">started</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Non</span> <span class="hljs-tag">blocking</span> <span class="hljs-tag">connect</span> <span class="hljs-tag">for</span> <span class="hljs-tag">SYNC</span> <span class="hljs-tag">fired</span> <span class="hljs-tag">the</span> <span class="hljs-tag">event</span>.
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Master</span> <span class="hljs-tag">replied</span> <span class="hljs-tag">to</span> <span class="hljs-tag">PING</span>, <span class="hljs-tag">replication</span> <span class="hljs-tag">can</span> <span class="hljs-tag">continue</span>...
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Partial</span> <span class="hljs-tag">resynchronization</span> <span class="hljs-tag">not</span> <span class="hljs-tag">possible</span> (<span class="hljs-tag">no</span> <span class="hljs-tag">cached</span> <span class="hljs-tag">master</span>)
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.939</span> * <span class="hljs-tag">Full</span> <span class="hljs-tag">resync</span> <span class="hljs-tag">from</span> <span class="hljs-tag">master</span>: 3<span class="hljs-tag">dc967ddf2be0a82b9c9af0027a478e058f523cf</span><span class="hljs-pseudo">:1135</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.965</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">receiving</span> 18 <span class="hljs-tag">bytes</span> <span class="hljs-tag">from</span> <span class="hljs-tag">master</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.966</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Flushing</span> <span class="hljs-tag">old</span> <span class="hljs-tag">data</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.966</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Loading</span> <span class="hljs-tag">DB</span> <span class="hljs-tag">in</span> <span class="hljs-tag">memory</span>
1<span class="hljs-pseudo">:S</span> 24 <span class="hljs-tag">Sep</span> 07<span class="hljs-pseudo">:48</span><span class="hljs-pseudo">:48</span><span class="hljs-class">.966</span> * <span class="hljs-tag">MASTER</span> &amp;<span class="hljs-tag">lt</span>;<span class="hljs-tag">-</span>&amp;<span class="hljs-tag">gt</span>; <span class="hljs-tag">SLAVE</span> <span class="hljs-tag">sync</span>: <span class="hljs-tag">Finished</span> <span class="hljs-tag">with</span> <span class="hljs-tag">success</span></code>

这样Redis集群就弄好啦:]

未经允许不得转载:冰点网络 » Docker构建Redis集群

分享到:更多 ()

评论 抢沙发

评论前必须登录!