Redis集群官方介绍:http://www.redis.cn/topics/cluster-tutorial.html
基于Docker搭建Redis集群
环境:6个节点,三主三从
制作Redis镜像:docker build -f dockerfile -t jiang/redis-cluster:4.0.1 .
dockerfile文件:
FROM redis:4.0.1
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir /usr/local/etc/redis && chown redis:redis /usr/local/etc/redis
ENV REDIS_C=/usr/local/etc/redis
RUN echo "port 6379" > $REDIS_C/redis.conf
RUN echo "cluster-enabled yes" >> $REDIS_C/redis.conf
RUN echo "cluster-config-file nodes.conf" >> $REDIS_C/redis.conf
RUN echo "cluster-node-timeout 5000" >> $REDIS_C/redis.conf
RUN echo "appendonly yes" >> $REDIS_C/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
制作Ruby镜像:docker build -f dockerfile2 -t jiang/my_ruby:2.4 .
dockerfile2文件:
FROM ruby:2.4
RUN gem install redis
ADD ./redis-4.0.1.tgz /usr/local
RUN cd /usr/local && ln -sf redis-4.0.1 redis
分别启动6个节点实例:
docker run --name redis7000 -p 7000:6379 -d jiang/redis-cluster:4.0.1
docker run --name redis7001 -p 7001:6379 -d jiang/redis-cluster:4.0.1
docker run --name redis7002 -p 7002:6379 -d jiang/redis-cluster:4.0.1
docker run --name redis7003 -p 7003:6379 -d jiang/redis-cluster:4.0.1
docker run --name redis7004 -p 7004:6379 -d jiang/redis-cluster:4.0.1
docker run --name redis7005 -p 7005:6379 -d jiang/redis-cluster:4.0.1
查看6个节点的容器IP
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7000
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7001
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7002
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7003
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7004
docker inspect --format='{{.NetworkSettings.IPAddress}}' redis7005
启动Ruby容器,进入/bin/bash,创建redis集群
docker run -it jiang/my_ruby:2.4 /bin/bash
# /usr/local/redis/src/redis-trib.rb create --replicas 1 172.17.0.2:6379 172.17.0.3:6379 172.17.0.4:6379 172.17.0.5:6379 172.17.0.6:6379 172.17.0.7:6379
出现下面显示说明集群创建成功:
[OK] All nodes agree about slots configuration.
[OK] All 16384 slots covered.
宿主机上利用redis客户端简单测试:
./redis-cli -c -p 7000
cluster info查看集群信息:
cluster nodes查看集群中各节点信息