解压编译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
修改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
镜像制作
[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 ENV REDIS_HOME /usr/local 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指令
构建镜像
# 切换中国源 [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
镜像制作完成,制作中间可能会报: 错误,这时候需要在镜像配置中添加一句命令:
... RUN rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 RUN yum -y update # 更新yum源 RUN yum install -y gcc make # 安装编译需要的工具
查看镜像:
[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 基础镜像就制作完成了
Public key for glibc-headers-2.17-222.el7.x86_64.rpm is not installed
二、制作redis节点镜像
基于此前制作的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 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"]
构建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
查看镜像
[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
查看容器信息
[root@etcd3 redis]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 10c62aafa4da hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 3 seconds ago Up 2 seconds 0.0.0.0:6384->6379/tcp redis-6384 73e4f843d8cb hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 12 seconds ago Up 10 seconds 0.0.0.0:6383->6379/tcp redis-6383 d9a71dd3f969 hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 20 seconds ago Up 18 seconds 0.0.0.0:6382->6379/tcp redis-6382 396e174a1d92 hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 3 days ago Up 3 days 0.0.0.0:6381->6379/tcp redis-6381 df6ebce6f12a hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 3 days ago Up 3 days 0.0.0.0:6380->6379/tcp redis-6380 1673a7d859ea hakimdstx/nodes-redis:4.0.1 "/usr/local/redis/..." 3 days ago Up 3 days 0.0.0.0:6379->6379/tcp redis-6379
集群访问
客户端在初始化的时候只需要知道一个节点的地址即可,客户端会先尝试向这个节点执行命令,比如 ,如果key所在的slot刚好在该节点上,则能够直接执行成功。如果slot不在该节点,则节点会返回MOVED错误,同时把该slot对应的节点告诉客户端,客户端可以去该节点执行命令
192.168.10.52:6383> get hello (error) MOVED 866 172.17.0.2:6379 192.168.10.52:6379> set number 20004 (error) MOVED 7743 172.17.0.3:6379
另外,redis集群版只使用db0,select命令虽然能够支持select 0。其他的db都会返回错误。
192.168.10.52:6383> select 0 OK 192.168.10.52:6383> select 1 (error) ERR SELECT is not allowed in cluster mode