1. 部署三主三从
- 编辑
redis.conf
文件,并复制5
份-
创建
6
个文件夹mkdir -p redis-cluster-mode/redis/ cd redis-cluster-mode/redis/ mkdir -p master1/conf/ master2/ master3/ slave1/ slave2/ slave3/
-
获取
redis.conf
配置文件cd master1/conf/ wget http://download.redis.io/redis-stable/redis.conf
-
修改配置文件
-
vim redis.conf
-
命令模式下
命令输入
那里/关键字
可以快速查找- 命令模式下
u
撤销 - 命令模式下
Ctrl + r
回复被撤销的操作
- 命令模式下
-
注释掉
bind 127.0.0.1
-
# requirepass foobared
撤销注释,并修改密码,6个redis
密码要一致 -
# masterauth <master-password>
撤销注释,并将<master-password>
改为设置的密码。开启验证 -
# cluster-enabled yes
撤销注释,开启集群模式 -
# cluster-config-file nodes-6379.conf
撤销注释,nodes-6379.conf
是集群信息
的记录文件
-
-
复制文件
# 返回到 master1 同级目录 cd ../../ # 复制 cp -rf master1/conf/ master2/ cp -rf master1/conf/ master3/ cp -rf master1/conf/ slave1/ cp -rf master1/conf/ slave2/ cp -rf master1/conf/ slave3/
-
- 编辑
docker-compose.yml
文件-
回到
reids/
同级目录cd ../
-
vim docker-compose.yml
version: "3.7" services: redis-master-1: image: redis volumes: - ./redis/master1/conf/redis.conf:/etc/redis/redis.conf - ./redis/master1/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6381:6379 command: ['redis-server', '/etc/redis/redis.conf'] redis-master-2: image: redis volumes: - ./redis/master2/conf/redis.conf:/etc/redis/redis.conf - ./redis/master2/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6382:6379 command: ['redis-server', '/etc/redis/redis.conf'] redis-master-3: image: redis volumes: - ./redis/master3/conf/redis.conf:/etc/redis/redis.conf - ./redis/master3/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6383:6379 command: ['redis-server', '/etc/redis/redis.conf'] redis-slave-1: image: redis volumes: - ./redis/slave1/conf/redis.conf:/etc/redis/redis.conf - ./redis/slave1/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6384:6379 command: ['redis-server', '/etc/redis/redis.conf'] redis-slave-2: image: redis volumes: - ./redis/slave2/conf/redis.conf:/etc/redis/redis.conf - ./redis/slave2/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6385:6379 command: ['redis-server', '/etc/redis/redis.conf'] redis-slave-3: image: redis volumes: - ./redis/slave3/conf/redis.conf:/etc/redis/redis.conf - ./redis/slave3/conf/nodes-6379.conf:/etc/redis/nodes-6379.conf ports: - 6386:6379 command: ['redis-server', '/etc/redis/redis.conf']
-
docker-compose up -d
-
查看容器是否启动成功
docker ps -a
-
查看
redis
容器的ip
# 这是查看的公式,将docker ps -a 查看到的 6 个的 <CONTAINER ID> 代进去 # 根据设置的端口号 6881 - 6886 来进行查看,方便后续排序,设置主从 docker inspect <CONTAINER ID> | grep IPA
-
进入其中一个容器
docker exec -it <CONTAINER ID> /bin/bash
-
开启
redis
redis-cli -a <密码>
-
将其中其他的
redis
加入集群cluster meet ip 6379
-
查看集群节点
cluster nodes # 退出 redis exit
-
配置
三主三从
redis-cli --cluster create <6个redis的 ip:端口> -a <密码> --cluster-replicas 1
-
网友评论