白云岛资源网 Design By www.pvray.com

前言

由于本人是个docker控,不喜欢安装各种环境,而且安装redis-trib也有点繁琐,索性用docker来做redis cluster。

本文用的是伪集群,真正的集群放到不同的机器即可。端口是7001-7006。

工作目录: /data/redis

创建文件夹

首先创建一堆对应端口的文件夹,下面是脚本

create.sh

for i in `seq 7001 7006`
do
 mkdir -p ${i}/data
done

添加执行权限并执行

chmod 777 create.sh
./create.sh

部署redis

本人不喜欢docker run,所以用了docker-compose。官方文档 https://docs.docker.com/compose/overview/

创建docker-compose.yml

用publicisworldwide/redis-cluster镜像的原因是人家已经把配置文件写好了,配置文件没有挂载是懒,别学我

docker-compose.yml

version: '3.4'

x-image:
 &default-image
 publicisworldwide/redis-cluster
x-restart:
 &default-restart
 always
x-netmode:
 &default-netmode
 host

services:
 redis1:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7001/data:/data
 environment:
 - REDIS_PORT=7001

 redis2:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7002/data:/data
 environment:
 - REDIS_PORT=7002

 redis3:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7003/data:/data
 environment:
 - REDIS_PORT=7003

 redis4:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7004/data:/data
 environment:
 - REDIS_PORT=7004

 redis5:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7005/data:/data
 environment:
 - REDIS_PORT=7005

 redis6:
 image: *default-image
 network_mode: *default-netmode
 restart: *default-restart
 volumes:
 - /data/redis/7006/data:/data
 environment:
 - REDIS_PORT=7006

启动所有redis

docker-compose up -d

如果报错,那就是版本问题。

查看docker版本

docker -v

在文档中可查看docker版本支持的docker-compose.yml版本,为了方便大家查看,我复制出来了。不过一般来说,docker升级比较快,功能迭代也很快,最好还是用最新版本。

Compose file format Docker Engine release 3.4 17.09.0+ 3.3 17.06.0+ 3.2 17.04.0+ 3.1 1.13.1+ 3.0 1.13.0+ 2.3 17.06.0+ 2.2 1.13.0+ 2.1 1.12.0+ 2.0 1.10.0+ 1.0 1.9.1.+

部署cluster

运行以下命令(inem0o/redis-trib没有pull会自动pull)

注意:加上-it,不然后续的确认没法继续

docker run --rm -it inem0o/redis-trib create --replicas 1 192.168.30.70:7001 192.168.30.70:7002 192.168.30.70:7003 192.168.30.70:7004 192.168.30.70:7005 192.168.30.70:7006

会出现

> Creating cluster
> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.30.70:7001
192.168.30.70:7002
192.168.30.70:7003
Adding replica 192.168.30.70:7004 to 192.168.30.70:7001
Adding replica 192.168.30.70:7005 to 192.168.30.70:7002
Adding replica 192.168.30.70:7006 to 192.168.30.70:7003
M: 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1 192.168.30.70:7001
  slots:0-5460 (5461 slots) master
M: bb8fda08e1dcd39e937443f81b5458e80f52d804 192.168.30.70:7002
  slots:5461-10922 (5462 slots) master
M: d907530ee9f6356e0e61a6c7f4d0cc1b22da1189 192.168.30.70:7003
  slots:10923-16383 (5461 slots) master
S: 52eee69afa751d71c84d5436d14b0e16a37536fa 192.168.30.70:7004
  replicates 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1
S: 701ed2fbb3df9fc63b083818620f5c020d05e323 192.168.30.70:7005
  replicates bb8fda08e1dcd39e937443f81b5458e80f52d804
S: a3548a9dffa225f05786ea2289db65f5f1c623be 192.168.30.70:7006
  replicates d907530ee9f6356e0e61a6c7f4d0cc1b22da1189
Can I set the above configuration"htmlcode">
Waiting for the cluster to join.....
> Performing Cluster Check (using node 192.168.30.70:7001)
M: 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1 192.168.30.70:7001
  slots:0-5460 (5461 slots) master
  1 additional replica(s)
M: d907530ee9f6356e0e61a6c7f4d0cc1b22da1189 192.168.30.70:7003@17003
  slots:10923-16383 (5461 slots) master
  1 additional replica(s)
S: a3548a9dffa225f05786ea2289db65f5f1c623be 192.168.30.70:7006@17006
  slots: (0 slots) slave
  replicates d907530ee9f6356e0e61a6c7f4d0cc1b22da1189
S: 701ed2fbb3df9fc63b083818620f5c020d05e323 192.168.30.70:7005@17005
  slots: (0 slots) slave
  replicates bb8fda08e1dcd39e937443f81b5458e80f52d804
S: 52eee69afa751d71c84d5436d14b0e16a37536fa 192.168.30.70:7004@17004
  slots: (0 slots) slave
  replicates 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1
M: bb8fda08e1dcd39e937443f81b5458e80f52d804 192.168.30.70:7002@17002
  slots:5461-10922 (5462 slots) master
  1 additional replica(s)
[OK] All nodes agree about slots configuration.
> Check for open slots...
> Check slots coverage...
[OK] All 16384 slots covered.

完成。

遇到的问题

创建集群的时候会遇到Waiting for the cluster to join....................一直没有成功

答:网络模式改成host,docker run 的方式加上--net host,docker-compose方式加上network_mode: host。

我原本是端口映射 7001:7000+17001:17000,但是不行,不知道什么原因。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com