docker搭建redis集群

Updated on with 11 views and 0 comments
  1. 下载 Redis 镜像
docker pull redis
  1. 创建存储目录和配置文件
mkdir -p /docker/redis/{7000,7001,7002,7003,7004,7005}/data
  1. 编辑配置文件redis.conf,使用时去除注释
vim redis.conf

port 7000					##节点端口
cluster-enabled yes				##cluster 集群模式
cluster-config-file nodes.conf			##集群配置名
cluster-node-timeout 5000			##超时时间
cluster-announce-ip 10.168.1.229		##物理机ip
cluster-announce-port 7000			##节点映射端口
cluster-announce-bus-port 17000			##节点总线端
masterauth password@123				##集群密码
requirepass password@123			##登录密码
appendonly yes					##持久化模式
protected-mode no
  1. 复制配置文件到各个文件夹/700,/7001/7002...,修改端口和ip
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.168.1.229	
cluster-announce-port 7001
cluster-announce-bus-port 17001
masterauth password@123
requirepass password@123
appendonly yes
protected-mode no
  1. docker创建6个redis容器
docker run -p 7000:7000 -p 17000:17000 --restart always --name redis-7000 --privileged=true -v /tools/redis/7000/redis7000.conf:/etc/redis/redis.conf -v /tools/redis/7000/data:/data -d redis redis-server /etc/redis/redis.conf

docker run -p 7001:7001 -p 17001:17001 --restart always --name redis-7001 --privileged=true -v /tools/redis/7001/redis7001.conf:/etc/redis/redis.conf -v /tools/redis/7001/data:/data -d redis redis-server /etc/redis/redis.conf
  1. 进入任意一个容器,创建集群(三主三从,允许挂掉2台)
docker exec -it redis-7000 /bin/bash

redis-cli --cluster create 10.168.1.229:7000 10.168.1.229:7001 10.168.1.229:7002 10.168.1.229:7003 10.168.1.229:7004 10.168.1.229:7005 -a password@123 --cluster-replicas 1
  1. 进入任意一个容器,查看集群状态
redis-cli --cluster check 10.168.1.229:7000 -a password@123
  1. 进入任意一个容器,测试集群效果
redis-cli -p 7000 -a password@123 -c

set test helloword
get test
-> Redirected to slot [6918] located at 10.168.1.229:7005
"helloword"

标题:docker搭建redis集群
作者:weiweihaha
地址:http://blog.lijida.cn/articles/2019/11/13/1573633798288.html