首先,我们需要理论知识:Redis Cluster是Redis的分布式解决方案,它解决了redis单机中心化的问题,分布式数据库——首要解决把整个数据集按照分区规则映射到多个节点的问题。
这边就需要知道分区规则——哈希分区规则。Redis Cluster 采用哈希分区规则中的虚拟槽分区。所有的键根据哈希函数映射到0 ~ 16383,计算公式:slot = CRC16(key)&16383。每一个节点负责维护一部分槽以及槽所映射的键值数据。
一、创建redis docker基础镜像
1.下载redis安装包,使用版本为:4.0.1
[root@etcd1 tmp]# mkdir docker_redis_cluster [root@etcd1 tmp]# cd docker_redis_cluster/ [root@etcd2 docker_redis_cluster]# wget http://download.redis.io/releases/redis-4.0.1.tar.gz
2.解压编译redis
[root@etcd1 docker_redis_cluster]# tar zxvf redis-4.0.1.tar.gz [root@etcd1 docker_redis_cluster]# cd redis-4.0.1/ [root@etcd1 redis-4.0.1]# make
3.修改redis配置
[root@etcd3 redis-4.0.1]# vi /tmp/docker_redis_cluster/redis-4.0.1/redis.conf
修改bind ip地址
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the # internet, binding to all the interfaces is dangerous and will expose the # instance to everybody on the internet. So by default we uncomment the # following bind directive, that will force Redis to listen only into # the IPv4 lookback interface address (this means Redis will be able to # accept connections only from clients running into the same computer it # is running). # # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES # JUST COMMENT THE FOLLOWING LINE. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #bind 127.0.0.1 bind 0.0.0.0
将守护进程yes改成no
# By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize no
将密码项注释去掉,添加新密码
# Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # # requirepass foobared
修改为
# Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. # requirepass 123456
因为配置了密码,所以,配置中另外一处主从连接也需要配置密码
# If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # # masterauth <master-password>
修改为
# If the master is password protected (using the "requirepass" configuration # directive below) it is possible to tell the slave to authenticate before # starting the replication synchronization process, otherwise the master will # refuse the slave request. # # masterauth <master-password> masterauth 123456
设置日志路径
# Specify the log file name. Also the empty string can be used to force # Redis to log on the standard output. Note that if you use standard # output for logging but daemonize, logs will be sent to /dev/null logfile "/var/log/redis/redis-server.log"
配置集群相关信息,去掉配置项前面的注释
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are # started as cluster nodes can. In order to start a Redis instance as a # cluster node enable the cluster support uncommenting the following: # cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not # intended to be edited by hand. It is created and updated by Redis nodes. # Every Redis Cluster node requires a different cluster configuration file. # Make sure that instances running in the same system do not have # overlapping cluster configuration file names. # cluster-config-file nodes-6379.conf # Cluster node timeout is the amount of milliseconds a node must be unreachable # for it to be considered in failure state. # Most other internal time limits are multiple of the node timeout. # cluster-node-timeout 15000
4.镜像制作
[root@etcd3 docker_redis_cluster]# cd /tmp/docker_redis_cluster [root@etcd3 docker_redis_cluster]# vi Dockerfile # Redis # Version 4.0.1 FROM Centos:7<br> ENV REDIS_HOME /usr/local<br> ADD redis-4.0.1.tar.gz / # 本地的redis源码包复制到镜像的根路径下,ADD命令会在复制过后自动解包。被复制的对象必须处于Dockerfile同一路径,且ADD后面必须使用相对路径 RUN mkdir -p $REDIS_HOME/redis # 创建安装目录 ADD redis-4.0.1/redis.conf $REDIS_HOME/redis/ # 将一开始编译产生并修改后的配置复制到安装目录 RUN yum -y update # 更新yum源 RUN yum install -y gcc make # 安装编译需要的工具 WORKDIR /redis-4.0.1 RUN make RUN mv /redis-4.0.1/src/redis-server $REDIS_HOME/redis/ # 编译后,容器中只需要可执行文件redis-server WORKDIR / RUN rm -rf /redis-4.0.1 # 删除解压文件 RUN yum remove -y gcc make # 安装编译完成之后,可以删除多余的gcc跟make VOLUME ["/var/log/redis"] # 添加数据卷 EXPOSE 6379 # 暴露6379端口,也可以暴露多个端口,这里不需要如此
PS.当前镜像非可执行镜像,所以没有包含ENTRYPOINT和CMD指令
5.构建镜像
# 切换中国源 [root@etcd3 docker_redis_cluster]# vi /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"] } # 编译 [root@etcd3 docker_redis_cluster]# docker build -t hakimdstx/cluster-redis . ... Complete! ---> 546cb1d34f35 Removing intermediate container 6b6556c5f28d Step 14/15 : VOLUME /var/log/redis ---> Running in 05a6642e4046 ---> e7e2fb8676b2 Removing intermediate container 05a6642e4046 Step 15/15 : EXPOSE 6379 ---> Running in 5d7abe1709e2 ---> 2d1322475f79 Removing intermediate container 5d7abe1709e2 Successfully built 2d1322475f79
镜像制作完成,制作中间可能会报: Public key for glibc-headers-2.17-222.el7.x86_64.rpm is not installed 错误,这时候需要在镜像配置中添加一句命令:
... RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 RUN yum -y update # 更新yum源 RUN yum install -y gcc make # 安装编译需要的工具
6.查看镜像:
[root@etcd3 docker_redis_cluster]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hakimdstx/cluster-redis 4.0.1 1fca5a08a4c7 14 seconds ago 435 MB centos 7 49f7960eb7e4 2 days ago 200 MB
以上,redis 基础镜像就制作完成了
二、制作redis节点镜像
1.基于此前制作的redis基础镜像创建一个redis节点镜像
[root@etcd3 tmp]# mkdir docker_redis_nodes [root@etcd3 tmp]# cd docker_redis_nodes [root@etcd3 docker_redis_nodes]# vi Dockerfile # Redis Node # Version 4.0.1<br> FROM hakimdstx/cluster-redis:4.0.1 # MAINTAINER_INFO MAINTAINER hakim 1194842583@qq.com ENTRYPOINT ["/usr/local/redis/redis-server", "/usr/local/redis/redis.conf"]
2.构建redis节点镜像
[root@etcd3 docker_redis_nodes]# docker build -t hakimdstx/nodes-redis:4.0.1 . Sending build context to Docker daemon 2.048 kB Step 1/3 : FROM hakimdstx/cluster-redis:4.0.1 ---> 1fca5a08a4c7 Step 2/3 : MAINTAINER hakim 1194842583@qq.com ---> Running in cc6e07eb2c36 ---> 55769d3bfacb Removing intermediate container cc6e07eb2c36 Step 3/3 : ENTRYPOINT /usr/local/redis/redis-server /usr/local/redis/redis.conf ---> Running in f5dedf88f6f6 ---> da64da483559 Removing intermediate container f5dedf88f6f6 Successfully built da64da483559
3.查看镜像
[root@etcd3 docker_redis_nodes]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hakimdstx/nodes-redis 4.0.1 da64da483559 51 seconds ago 435 MB hakimdstx/cluster-redis 4.0.1 1fca5a08a4c7 9 minutes ago 435 MB centos 7 49f7960eb7e4 2 days ago 200 MB