Docker-Compose搭建Redis集群的实现教程

本站所有内容来自互联网收集,仅供学习和交流,请勿用于商业用途。如有侵权、不妥之处,请第一时间联系我们删除!Q群:迪思分享
免费资源网 – https://freexyz.cn/

目录1. 集群配置2.编写redis.conf3. 在每个redis-*文件夹下创建redis.conf文件,并写入如下内容:4.编写docker-compose.yml文件5.开启集群6.测试6.1 查看节点属性6.2 查看节点信息6.3 插入一个值7.添加密码:7.1 编辑redis.conf文件7.2 编辑docker-compose.yml文件7.3 连接集群

1. 集群配置

3主 3从由于仅用于测试,故我这里只用1台服务器进行模拟

redis列表

Docker-Compose搭建Redis集群的实现教程插图

2.编写redis.conf

在server上创建一个目录用于存放redis集群部署文件。这里我放的路径为/root/redis-cluster

在/opt/docker/redis-cluster目录下创建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夹

mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6}

#创建持久化目录
mkdir -p /opt/docker/redis-cluster/redis-1/data
mkdir -p /opt/docker/redis-cluster/redis-2/data
mkdir -p /opt/docker/redis-cluster/redis-3/data
mkdir -p /opt/docker/redis-cluster/redis-4/data
mkdir -p /opt/docker/redis-cluster/redis-5/data
mkdir -p /opt/docker/redis-cluster/redis-6/data

3. 在每个redis-*文件夹下创建redis.conf文件,并写入如下内容:

cluster-enabled yes # 开启集群
cluster-config-file nodes.conf # 集群配置文件
cluster-node-timeout 5000 # 集群节点多少时间未响应视为该节点丢失
appendonly yes
port 6379 # redis监听端口
masterauth passwd123 #设置master节点密码
requirepass passwd123 #设置密码

注意:port值不能都为6379,根据上面redis列表设置的端口号,依次给redis-1 ~ redis-6设置6379~6384端口号

4.编写docker-compose.yml文件

这里直接使用daocloud提供的redis镜像地址daocloud.io/library/redis:6.0.4

在/root/redis-cluster文件夹下创建docker-compose.yml文件。

docker-compose.yml文件内容如下:

version: 3.1
services:
# redis1配置
redis1:
image: daocloud.io/library/redis:6.0.4
container_name: redis-1
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-1/data:/data
– /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
# redis2配置
redis2:
image: daocloud.io/library/redis:6.0.4
container_name: redis-2
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-2/data:/data
– /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
# redis3配置
redis3:
image: daocloud.io/library/redis:6.0.4
container_name: redis-3
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-3/data:/data
– /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
# redis4配置
redis4:
image: daocloud.io/library/redis:6.0.4
container_name: redis-4
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-4/data:/data
– /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
# redis5配置
redis5:
image: daocloud.io/library/redis:6.0.4
container_name: redis-5
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-5/data:/data
– /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]
# redis6配置
redis6:
image: daocloud.io/library/redis:6.0.4
container_name: redis-6
restart: always
network_mode: “host”
volumes:
– /opt/docker/redis-cluster/redis-6/data:/data
– /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf
command: [“redis-server”, “/usr/local/etc/redis/redis.conf”]

启动容器,执行命令:

#启动容器
docker-compose -f xxx.yaml up -d

Docker-Compose搭建Redis集群的实现教程插图1

5.开启集群

随便找一个容器进入,这里我选择redis-1进入。 在进入容器后,输入如下命令开启集群:

redis-cli –cluster create 192.168.66.180:6379

192.168.66.180:6380

192.168.66.180:6381

192.168.66.180:6382

192.168.66.180:6383

192.168.66.180:6384

–cluster-replicas 1

Docker-Compose搭建Redis集群的实现教程插图2

出现下图即为成功。

Docker-Compose搭建Redis集群的实现教程插图3

6.测试

使用redis-cli命令,连接集群中任意节点。(随便找一台能ping通集群所在IP的电脑,这里我用的本机进行测试)

6.1 查看节点属性

redis-cli -c -h 192.168.66.180 -p 6379
192.168.66.180:6379


© 版权声明
THE END
★喜欢这篇文章吗?喜欢的话,麻烦动动手指支持一下!★
点赞11 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容