Docker安装
- yum install -y yum-utils device-mapper-persistent-data lvm2
- yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- yum clean all
- yum makecache fast
- yum -y install docker-ce
- systemctl start docker
- docker version
Redis安装
- mkdir /docer/redis_docker
- mkdir /usr/local/redis_docker
- cd /usr/local/redis_docker/
- wget http://download.redis.io/releases/redis-4.0.1.tar.gz
- tar -xzf redis-4.0.1.tar.gz
- yum install -y gcc-c++
- cd redis-4.0.1/
- make
- vim redis.conf
bind 0.0.0.0
requirepass yourpwd
masterauth yourpwd
logfile "/usr/local/redis/logs/redis-server.log"
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000
- cd …
- vim Dockerfile
#指定镜像
FROM centos:7
#定义路径的环境变量
ENV REDIS_HOME /usr/local
#将Dockerfile同级目录下的redis-4.0.1.tar.gz复制到镜像的根目录
ADD redis-4.0.1.tar.gz /
#创建安装目录,根据环境变量信息,实际的创建目录为:/usr/local/redis
RUN mkdir -p $REDIS_HOME/redis
#将Dockerfile同级目录下redis-4.0.1中的redis配置文件拷贝到容器的/usr/local/redis目录中
ADD redis-4.0.1/redis.conf $REDIS_HOME/redis/
#更新镜像的yum
RUN yum -y update
#安装gcc相关编译工具
RUN yum install -y gcc make
#指定工作目录
WORKDIR /redis-4.0.1
#执行编译
RUN make
#移动编译后的redis-server到容器相关的目录
RUN mv /redis-4.0.1/src/redis-server $REDIS_HOME/redis/
#移动到上一级
WORKDIR /
#删除解压文件
RUN rm -rf /redis-4.0.1
#删除对于的工具
RUN yum remove -y gcc make
#添加数据卷
#/usr/local/redis/logs/redis-server.log
#此目录需要和redis.conf中logfile一致
VOLUME ["/usr/local/redis/logs"]
#暴露6379的端口
EXPOSE 6379
- docker build -t myDocker/redis-cluster .
- mkdir /usr/local/redis_docker/redis-node
- cd /usr/local/redis_docker/redis-node
- vim Dockerfile
FROM myDocker/redis-cluster:latest
ENTRYPOINT ["/usr/local/redis/redis-server", "/usr/local/redis/redis.conf"]
- docker build -t myDocker/redis-node .
- docker run -d --name redis-node-7000 -p 7000:6379 myDocker/redis-node
- docker run -d --name redis-node-7001 -p 7001:6379 myDocker/redis-node
- docker run -d --name redis-node-7002 -p 7002:6379 myDocker/redis-node
- docker run -d --name redis-node-7003 -p 7003:6379 myDocker/redis-node
- docker run -d --name redis-node-7004 -p 7004:6379 myDocker/redis-node
- docker run -d --name redis-node-7005 -p 7005:6379 myDocker/redis-node
- docker ps
- docker inspect 7000-7005的docker容器id,通过docker ps命令可以看到 |grep IPAddress
- ls -l redis-cli
- cp redis-cli /usr/local/bin
- redis-cli -p 7000
- redis-cli -p 7000 -a 你设置的密码
集群配置
- vim addslots.sh
#将0-5461的槽点配置在172.17.0.4:6379(宿主机127.0.0.1:7000)的redis上
n=0
for ((i=n;i<=5461;i++))
do
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7000 -a chhu2017 CLUSTER ADDSLOTS $i
done
#将5462-10922的槽点配置在172.17.0.5:6379(宿主机127.0.0.1:7001)的redis上
n=5462
for ((i=n;i<=10922;i++))
do
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7001 -a chhu2017 CLUSTER ADDSLOTS $i
done
#将10923-16383的槽点配置在172.17.0.6:6379(宿主机127.0.0.1:7002)的redis上
n=10923
for ((i=n;i<=16383;i++))
do
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7002 -a chhu2017 CLUSTER ADDSLOTS $i
done
- sh addslots.sh
- vi addSlaveNodes.sh
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7002 -a chhu2017 CLUSTER REPLICATE 172.17.0.6:6379的集群ID
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7003 -a chhu2017 CLUSTER REPLICATE 172.17.0.5:6379的集群ID
/usr/local/bin/redis-cli -h 127.0.0.1 -p 7004 -a chhu2017 CLUSTER REPLICATE 172.17.0.7:6379的集群ID
- sh addSlaveNodes.sh