最近面试被问到redis集群内容 因此整理下相关部署内容。文章以图为主,看着一点都不费劲,看完都不需要找翻其他文档。你就放心整吧,一整一个不吱声
环境准备
随便一台电脑, 安装一下docker
集群部署
- 执行脚本命令,生成redis.conf 配置文件
for port in $(seq 6380 6385);
do
mkdir -p /Users/candice/DevOps/docker/redis-cluster/node-${port}/conf
touch /Users/candice/DevOps/docker/redis-cluster/node-${port}/conf/redis.conf
cat << EOF > /Users/candice/DevOps/docker/redis-cluster/node-${port}/conf/redis.conf
port ${port}
requirepass 1234
masterauth 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.10.10.139
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
EOF
done
效果展示
- 编写redis-cluster.yml 准备启动redis
version: '3.7'
services:
node-redis-1:
image: redis:latest
container_name: node-redis-1
ports:
- "6380:6380"
- "16380:16380"
volumes:
# 目录记得修改, 别照搬照抄啊
# 第一步生成的redis.conf 挂载到容器内
- ./node-6380/conf/redis.conf:/etc/redis/redis.conf
# data数据挂载,rdb aof数据文件映射到宿主机
- /Users/yangjing/DevOps/data-volume/node-redis-1:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-2:
image: redis:latest
container_name: node-redis-2
ports:
- "6381:6381"
- "16381:16381"
volumes:
- ./node-6381/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-2:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-3:
image: redis:latest
container_name: node-redis-3
ports:
- "6382:6382"
- "16382:16382"
volumes:
- ./node-6382/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-3:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-4:
image: redis:latest
container_name: node-redis-4
ports:
- "6383:6383"
- "16383:16383"
volumes:
- ./node-6383/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-4:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-5:
image: redis:latest
container_name: node-redis-5
ports:
- "6384:6384"
- "16384:16384"
volumes:
- ./node-6384/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-5:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-6:
image: redis:latest
container_name: node-redis-6
ports:
- "6385:6385"
- "16385:16385"
volumes:
- ./node-6385/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-6:/data
command: redis-server /etc/redis/redis.conf
privileged: true
效果展示
- 随便找一个节点 进到docker内部 进行配置
// 密码 端口地址记得按自己配置的改
redis-cli -a 1234 --cluster create 10.10.10.139:6380 10.10.10.139:6381 10.10.10.139:6382 10.10.10.139:6383 10.10.10.139:6384 10.10.10.139:6385 --cluster-replicas 1
- 初始集群已配置完,看看效果
- 写写数据 试一下
集群扩容
现在是三主三从,如果想再加一主一从 怎么搞
- 再搞两个redis.conf 步骤跟之前一样, 记得改port,我是加了个6386、6387
- 部署下新的两个节点
version: '3.7'
services:
node-redis-7:
image: redis:latest
container_name: node-redis-7
ports:
- "6386:6386"
- "16386:16386"
volumes:
- ./node-6386/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-7:/data
command: redis-server /etc/redis/redis.conf
privileged: true
node-redis-8:
image: redis:latest
container_name: node-redis-8
ports:
- "6387:6387"
- "16387:16387"
volumes:
- ./node-6387/conf/redis.conf:/etc/redis/redis.conf
- /Users/yangjing/DevOps/data-volume/node-redis-8:/data
command: redis-server /etc/redis/redis.conf
privileged: true
-
先挂新的master节点
重新分配master 哈希数据槽
-
看看效果
把6387从节点挂上去
集群缩容
先删从节点 再处理master
结尾
文章写的挺累的,有哪里不明确或者有问题 随时留言哦, 我们共同探讨 共同学习~