首先选择redis版本,目前最新是5.0,
5.0版本集群的创建工具是c语言写的,直接用redis-cli命令,不依赖于ruby,
如果是4.0版本,创建集群需要用到ruby脚本:redis-trib.rb
一、首先获取redis docker官方镜像
$ docker pull redis:5.0
或者
$ docker pull redis:4.0(大概25Mb)
4.0版本还需要获取 ruby 镜像备用
$ docker pull ruby(大概200Mb)
二、准备配置文件
1、创建redis配置文件(redis-cluster.tmpl)
我在路径/home下创建一个文件夹redis-cluster,在路径/home/redis-cluster下创建一个文件redis-cluster.tmpl,并把以下内容复制过去。(注:路径可自定义,我用的是/home/redis-cluster)
port ${PORT} protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 39.10X.XX.XX //自己服务器IP cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} appendonly yes
2、在/home/redis-cluster下生成conf和data目标,并生成配置信息
$ for port in `seq 7000 7005`; do \ mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; \ done
注意这个里面用到一个命令:envsubst,一般linux系统都自带这个命令。如果没有的话,就换个linux系统去执行吧。
三、创建redis docker容器
1、创建自定义network
$ docker network create redis-net
2、创建6个redis容器
注意下面的挂载路径自己根据实际情况调整。
$ for port in `seq 7000 7005`; do \ docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \ -v /home/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \ -v /home/redis-cluster/${port}/data:/data \ --restart always --name redis-${port} --net redis-net \ --sysctl net.core.somaxconn=1024 redis:5.0 redis-server /usr/local/etc/redis/redis.conf; \ done
至此,通过命令docker ps可查看刚刚生成的6个容器信息。
四、组成集群
4.0以前的版本通过ruby来实现集群,
先获得对应版本的 src/redis-trib.rb 文件,可以从这里下载源码包http://download.redis.io/releases/
解压后得到redis-trib.rb 文件。然后挂载到ruby的docker容器上执行,例如:
$ echo yes | docker run -i --rm --net redis-net \ -v /vagrant_data:/data \ ruby sh -c '\ gem install redis \ && ruby /data/redis-trib.rb create --replicas 1 \ '"$(for port in `seq 7000 7005`; do \ echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port} ' ' ; \ done)"
如果打印类似于如下的信息,则代表创建成功。
Successfully installed redis-4.1.0 1 gem installed >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 172.18.0.2:7000 172.18.0.3:7001 172.18.0.4:7002 Adding replica 172.18.0.6:7004 to 172.18.0.2:7000 Adding replica 172.18.0.7:7005 to 172.18.0.3:7001 Adding replica 172.18.0.5:7003 to 172.18.0.4:7002 M: 69d4fd6c9e88c9a134b2d714887646100c5d8f8b 172.18.0.2:7000 slots:0-5460 (5461 slots) master M: 0e9b44b7a5fb691ebaf8c4dc102806d5a2c63a26 172.18.0.3:7001 slots:5461-10922 (5462 slots) master M: 2ed13921839e9a287f05dfa0ad3c6bf1cf2bdbf0 172.18.0.4:7002 slots:10923-16383 (5461 slots) master S: d1b2d4c56db4818d236dd750de0fd28b59f9dcf8 172.18.0.5:7003 replicates 2ed13921839e9a287f05dfa0ad3c6bf1cf2bdbf0 S: a92038b327517afec4f777984440254b4a56ae44 172.18.0.6:7004 replicates 69d4fd6c9e88c9a134b2d714887646100c5d8f8b S: e4faabf2248e60d79f3d4848ac0a1d466b201222 172.18.0.7:7005 replicates 0e9b44b7a5fb691ebaf8c4dc102806d5a2c63a26 Can I set the above configuration? (type 'yes' to accept): >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.. >>> Performing Cluster Check (using node 172.18.0.2:7000) M: 69d4fd6c9e88c9a134b2d714887646100c5d8f8b 172.18.0.2:7000 slots:0-5460 (5461 slots) master 1 additional replica(s) M: 2ed13921839e9a287f05dfa0ad3c6bf1cf2bdbf0 192.168.33.11:7002 slots:10923-16383 (5461 slots) master 1 additional replica(s) S: a92038b327517afec4f777984440254b4a56ae44 192.168.33.11:7004 slots: (0 slots) slave replicates 69d4fd6c9e88c9a134b2d714887646100c5d8f8b M: 0e9b44b7a5fb691ebaf8c4dc102806d5a2c63a26 192.168.33.11:7001 slots:5461-10922 (5462 slots) master 1 additional replica(s) S: e4faabf2248e60d79f3d4848ac0a1d466b201222 192.168.33.11:7005 slots: (0 slots) slave replicates 0e9b44b7a5fb691ebaf8c4dc102806d5a2c63a26 S: d1b2d4c56db4818d236dd750de0fd28b59f9dcf8 192.168.33.11:7003 slots: (0 slots) slave replicates 2ed13921839e9a287f05dfa0ad3c6bf1cf2bdbf0 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
5.0版本类似,但是不用 ruby镜像,直接登录其中一个redis容器内,
1)首先确定好生成容器的ip
通过下面命令得到redis容器的ip地址:
$ for port in `seq 7000 7005`; do \ echo -n "$(docker inspect --format '{{ (index .NetworkSettings.Networks "redis-net").IPAddress }}' "redis-${port}")":${port} ' ' ; \ done
2)然后登陆其中一个redis容器
$docker exec -it redis-7000 bash
3)在容器中执行
$ redis-cli --cluster create 上面获得的ip地址 --cluster-replicas 1
五、测试
使用Redis客户端Redis-cli.exe来查看数据记录数,以及集群相关信息
命令 redis-cli –c –h ”地址” –p "端口号" ; c 表示集群
登录进去之后可以执行: cluster info,查看集群信息。