1、下载Redis的镜像
docker pull redis:latest
2、Redis配置文件
port <port>
daemonize no
dir "/data"
logfile "redis.log"
dbfilename "dump.rdb"
cluster-enabled yes
cluster-config-file nodes-<port>.conf
cluster-require-full-coverage no
3、运行6个Redis容器
#redis集群总线端口为redis客户端端口加上10000
docker run -d -p 6391:6391 -p 16391:16391 -v /root/redis/c1/redis.conf:/etc/redis/redis.conf -v /root/redis/c1/data:/data:rw --name redis-6391 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6392:6392 -p 16392:16392 -v /root/redis/c2/redis.conf:/etc/redis/redis.conf -v /root/redis/c2/data:/data:rw --name redis-6392 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6393:6393 -p 16393:16393 -v /root/redis/c3/redis.conf:/etc/redis/redis.conf -v /root/redis/c3/data:/data:rw --name redis-6393 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6394:6394 -p 16394:16394 -v /root/redis/c4/redis.conf:/etc/redis/redis.conf -v /root/redis/c4/data:/data:rw --name redis-6394 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6395:6395 -p 16395:16395 -v /root/redis/c5/redis.conf:/etc/redis/redis.conf -v /root/redis/c5/data:/data:rw --name redis-6395 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6396:6396 -p 16396:16396 -v /root/redis/c6/redis.conf:/etc/redis/redis.conf -v /root/redis/c6/data:/data:rw --name redis-6396 4760dc956b2d redis-server /etc/redis/redis.conf
4、创建一个ruby镜像,用于运行ruby-trib.rb
#ruby 环境
docker run -d -it --name redis-trib 2ff4e698f315
#安装vim(可选)
apt-get update
apt-get install vim
#安装redis(为了获取redis-trib.rb文件)
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make
#安装ruby的redis客户端
wget http://rubygems.org/downloads/redis-3.3.0.gem
gem install -l redis-3.3.0.gem
gem list -- check reids gem
#执行redis-trib命令
./redis-trib.rb create --replicas 1 <ip>:6391 <ip>:6392 <ip>:6393 <ip>:6394 <ip>:6395 <ip>:6396
这样,一个3主3从的Reids集群就搭建好了
5、增加需要扩容的节点
docker run -d -p 6397:6397 -p 16397:16397 -v /root/redis/c7/redis.conf:/etc/redis/redis.conf -v /root/redis/c7/data:/data:rw --name redis-6397 4760dc956b2d redis-server /etc/redis/redis.conf
docker run -d -p 6398:6398 -p 16398:16398 -v /root/redis/c8/redis.conf:/etc/redis/redis.conf -v /root/redis/c8/data:/data:rw --name redis-6398 4760dc956b2d redis-server /etc/redis/redis.conf
6、执行meet操作
cluster meet <ip> 6397
cluster meet <ip> 6398
7、设置新节点的组从关系
让6398成为6397的slave
cluster replicate a7230d1af480c6c5f229afb0b5690a4cfb08c802
8、使用redis-trib分配slots
1、src/redis-trib.rb reshard <ip>:6391 //任意选择一个节点,连上集群即可
2、移动4096个槽
3、目标是新的主节点,即6397节点的id
4、source是all
到这里,集群扩容做好了
9、slots迁出
src/redis-trib.rb reshard --from <迁出节点id> --to <迁入节点id>
--slots 1366 <ip>:6391 //迁出1366个slot,选择6391节点连接
10、forget缩容节点
先下从节点,再下主节点
src/redis-trib.rb del-node <ip>:6391 <要下线从节点的nodeid>
src/redis-trib.rb del-node <ip>:6391 <要下线主节点的nodeid>
到这里,集群缩容也做好了
网友评论