1、拉取redis镜像,我的版本是5.0.5
docker pull redis
redis运行后查看版本命令,fdaac4274b6e是容器id
docker exec -it fdaac4274b6e redis-server -v
2、在目录/opt/redis-cluster下新增6个文件夹,分别是6318、6319、6328、6329、6338、6339,在每个问价夹下新建conf和data文件夹
3、新建一个文件redis.conf,复制到每个conf文件夹中,其中端口修改成每个文件夹对应端口
port 6318
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirePass redis_test
4、进入/opt/redis-cluster目录,新建文件docker-compose-redis.yaml文件
docker-compose-redis.yaml内容如下
version: "3.4"
x-image:
&default-image
redis:latest
x-restart:
&default-restart
always
x-command:
&default-command
redis-server /etc/redis/redis.conf
x-netmode:
&default-netmode
host
services:
redis1:
image: *default-image
restart: *default-restart
container_name: redis5-m1
command: *default-command
volumes:
- ./6319/data:/data
- ./6319/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6319:6319' #服务端口
- '16319:16319' #集群端口
redis2:
image: *default-image
restart: *default-restart
container_name: redis5-m2
command: *default-command
volumes:
- ./6329/data:/data
- ./6329/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6329:6329'
- '16329:16329'
redis3:
image: *default-image
restart: *default-restart
container_name: redis5-m3
command: *default-command
volumes:
- ./6339/data:/data
- ./6339/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6339:6339'
- '16339:16339'
redis4:
image: *default-image
restart: *default-restart
container_name: redis5-s1
command: *default-command
volumes:
- ./6318/data:/data
- ./6318/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6318:6318'
- '16318:16318'
redis5:
image: *default-image
restart: *default-restart
container_name: redis5-s2
command: *default-command
volumes:
- ./6328/data:/data
- ./6328/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6328:6328'
- '16328:16328'
redis6:
image: *default-image
restart: *default-restart
container_name: redis5-s3
command: *default-command
volumes:
- ./6338/data:/data
- ./6338/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6338:6338'
- '16338:16338'
4、运行 docker-compose-redis.yaml
docker-compose -f docker-compose-redis.yaml up -d
运行后结果
docker pull goodsmileduck/redis-cli
查看help命令
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster help
6、集群命令
--a redis_test redis_test是redis密码
--cluster-replicas 1表示1主1从
docker run --rm -it goodsmileduck/redis-cli redis-cli -a redis_test --cluster-replicas 1 --cluster create 172.18.171.42:6319 172.18.171.42:6329 172.18.171.42:6339 172.18.171.42:6318 172.18.171.42:6328 172.18.171.42:6338
执行结果:
docker run --rm -it goodsmileduck/redis-cli redis-cli -c -h 172.18.171.42 -p 6319 -a redis_test
执行命令cluster nodes,查看结果
8、测试集群情况,在6319端口上写入一个h的key,数据被存在到了6339中,访问的节点也变成了6339,则说明集群成功
1、在redis-cluster文件夹中新增两个文件夹6348、6349(参考前面),并复制redis.conf
复制docker-compose-redis.yaml并重命名为docker-compose-redis2.yaml
修改内容为:
version: "3.4"
x-image:
&default-image
redis:latest
x-restart:
&default-restart
always
x-command:
&default-command
redis-server /etc/redis/redis.conf
x-netmode:
&default-netmode
host
services:
redis7:
image: *default-image
restart: *default-restart
container_name: redis5-m4
command: *default-command
volumes:
- ./6349/data:/data
- ./6349/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6349:6349' #服务端口
- '16349:16349' #集群端口
redis8:
image: *default-image
restart: *default-restart
container_name: redis5-s4
command: *default-command
volumes:
- ./6348/data:/data
- ./6348/conf/redis.conf:/etc/redis/redis.conf
network_mode: *default-netmode
ports:
- '6348:6348'
- '16348:16348'
2、执行docker-compose命令
docker-compose -f docker-compose-redis2.yaml up -d
172.18.171.42:6349 是新节点ip和端口
172.18.171.42:6339 是原集群中的ip和端口,可以是集群中任意一个节点iph和端口
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster add-node 172.18.171.42:6349 172.18.171.42:6339 -a redis_test
查看集群中的信息,已经多了红色标记的节点,但是没有哈希槽
4、分配哈希槽
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster reshard 172.18.171.42:6349 -a redis_test
即将给该节点分配的哈希槽数据、以及该节点的id
输入原主节点的id,可以输入多主节点id
(1)将所有节点用作哈希槽的源节点。:all
(2)在指定的节点拿出指定数量的哈希槽分配到目标节点:done
执行完成后,查看集群节点信息
5、新增从节点
172.18.171.42:6348 是新增节点的信息
172.18.171.42:6339 是集群中的任意一个节点
3b752995bef67d32b30c0f0c63c5aa4172f7f811 是主节点id
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster add-node 172.18.171.42:6348 172.18.171.42:6339 --cluster-slave --cluster-master-id 3b752995bef67d32b30c0f0c63c5aa4172f7f811 -a redis_test
查看集群情况
1、删除从节点
删除节点用del-node命令。此命令需要制定删除节点的ip和端口,以及节点的id。
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster del-node 172.18.171.42:6348 6c0262d81dc83c809d8588d6f0c8821b91bd833c -a redis_test
2、删除主节点
(1)将主节点的哈希槽分配到其他主节点上
将6349上的1365个槽分配到6319上
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster reshard 172.18.171.42:6349 --cluster-from 3b752995bef67d32b30c0f0c63c5aa4172f7f811 --cluster-to 6202eefd595279cf589bdec9e2fec274f63caaad --cluster-slots 1365 -a redis_test
将6349上的1366个槽分配到6329上
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster reshard 172.18.171.42:6349 --cluster-from 3b752995bef67d32b30c0f0c63c5aa4172f7f811 --cluster-to 33e1c446095012af2a43c18715f4cd5db9280679 --cluster-slots 1366 -a redis_test
将6349上的1365个槽分配到6339上
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster reshard 172.18.171.42:6349 --cluster-from 3b752995bef67d32b30c0f0c63c5aa4172f7f811 --cluster-to 65681aec21243032ef9977a1f5bc139564c584e2 --cluster-slots 1365 -a redis_test
三个都分配后,6349集群中没有了槽
(2)使用del-node命令来删除主节点
docker run --rm -it goodsmileduck/redis-cli redis-cli --cluster del-node 172.18.171.42:6349 3b752995bef67d32b30c0f0c63c5aa4172f7f811 -a redis_test