Docker容器中也可以部署Redis集群

前言:

一個Redis集群,至少需要6臺服務器,3主3從架構。

本文是基於已有的創建好了的redis的鏡像。

內容詳細說明了實現步驟。

主要環境要求:宿主機centos7,docker 1.12.5,Redis 3.0.7


Docker鏡像構建上下文環境,文件列表:

Dockerfile

createCluster.sh

supervisord.conf

redis-7001.conf

redis-7002.conf

redis-7003.conf

redis-7004.conf

redis-7005.conf

redis-7006.conf

== Dockerfile ==

 ############################################################
# 構建RedisClusters鏡像centos7+jdk-8u121-linux-x64+Redis3.0.7
############################################################
#設置基礎鏡像
FROM redisos/redis:3.0.7
#定義作者
MAINTAINER itjiangtan "[email protected]"
#安裝ruby以便執行redis集群創建命令
RUN yum -y install ruby
RUN yum -y install rubygems
#運行redis-trib.rb腳本d=創建集群還需要安裝redis的ruby包

RUN gem install redis --version 3.0.7
#通過supervisord啟動各個redis節點,需要安裝supervisor
RUN yum -y install python-setuptools
RUN easy_install supervisor
#redis集群最少需要6個節點,每個節點一個配置文件
RUN mkdir -p /7001 /7002 /7003 /7004 /7005 /7006
COPY ./redis-7001.conf /7001/
COPY ./redis-7002.conf /7002/
COPY ./redis-7003.conf /7003/
COPY ./redis-7004.conf /7004/
COPY ./redis-7005.conf /7005/
COPY ./redis-7006.conf /7006/
#日誌目錄放入卷中
VOLUME ["/var/log/redis"]
#添加啟動redis節點腳本-supervisord腳本
ADD ./supervisord.conf /etc/
#添加創建集群腳本
ADD ./createCluster.sh /createCluster.sh
RUN chmod +x /createCluster.sh
#暴露端口
EXPOSE 7001 7002 7003 7004 7005 7006
ENTRYPOINT ["/bin/sh","/createCluster.sh"]

說明:

FROM redisos/redis:3.0.7 ,該構建是基於一個已經部署了Redis的鏡像。部署Redis基礎鏡像比較簡單這裡不再贅述。

== redis.conf需要更改的配置 ==

#設置redis前臺運行,由supervisord管理

daemonize no

#每個節點的端口不能衝突

port 7006

#設置每個節點的日誌文件位置

logfile "/var/log/redis/redis-server-7006.log"

#開啟集群功能

cluster-enabled yes

#集群文件名字,每個節點也不能重複

cluster-config-file nodes-7006.conf

#節點心跳時間

cluster-node-timeout 5000

注意,6個節點對應6個配置文件。部署時要逐一配置(duan端口分別為7001,7002...7006)

== 創建集群的腳本 createCluster.sh ==

supervisord -c /etc/supervisord.conf

#sleep保證redis節點都啟動

sleep 5

STATUS=$(echo "cluster info" | /redis-3.0.7/src/redis-cli -p 7001 | grep "cluster_statek"| wc -l)

if [ $STATUS -eq 1 ];

then

#保持docker進程活躍,防止容器自動退出

tail -f /var/log/redis/redis-1.log

fi

#注意分隔符是tab

IP=$(cat /etc/hosts|grep $(hostname)|cut -f1 -d " ")

echo "yes" | ruby /redis-3.0.7/src/redis-trib.rb create --replicas 1 $IP:7001 $IP:7002 $IP:7003 $IP:7004 $IP:7005 $IP:7006

#保持docker進程活躍,防止容器自動退出

tail -f /var/log/redis/redis-1.log

supervisord 是python實現的進程管理應用,Dockerfile中安裝了相關命令,可再到Dockerfile中看一下.

== supervisord.conf Redis啟動配置 ==

[supervisord]

nodaemon=false

[program:redis-1]

command=/redis-3.0.7/src/redis-server /7001/redis-7001.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

[program:redis-2]

command=/redis-3.0.7/src/redis-server /7002/redis-7002.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

[program:redis-3]

command=/redis-3.0.7/src/redis-server /7003/redis-7003.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

[program:redis-4]

command=/redis-3.0.7/src/redis-server /7004/redis-7004.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

[program:redis-5]

command=/redis-3.0.7/src/redis-server /7005/redis-7005.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

[program:redis-6]

command=/redis-3.0.7/src/redis-server /7006/redis-7006.conf

stdout_logfile=/var/log/redis/%(program_name)s.log

stderr_logfile=/var/log/redis/%(program_name)s.log

autorestart=true

== 鏡像構建命令 ==

docker build -t redisos/rediscluster .

== 容器啟動命令 ==

docker run -it --name rediscluster -v /containers/rediscluster:/var/log/redis --privileged=true -p 7001:7001 -p 7002:7002 -p 7003:7003 -p 7004:7004 -p 7005:7005 -p 7006:7006 redisos/rediscluster

== 測試 ==

/to/redis/path/redis-cli -h redis_node_ip -p port -c

連接以後,執行cluster info命令,如果輸出"cluster_statek"就ok了。

另外:消息隊列集群也是可以打包成鏡像部署的。有興趣的同學可以下去多研究研究。


分享到:


相關文章: