docker搭建redis集群(最新修改)

--write by zhuwx 2019-11-23 23:39:55 +0800 CST

docker pull redis

# 在 /home 目录下创建redis-cluster-test 文件夹
➜ mkdir -p /home/redis-cluster-test
➜ cd /home/redis-cluster-test
➜ vim redis-cluster.conf

# 把下列信息写入redis-cluster.conf文件中
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

# 在 /home/redis-cluster-test 目录下生成conf和data目录,并生成配置文件
➜ for port in `seq 6380 6385`; do
mkdir -p ./${port}/conf && PORT=${port} envsubst < ./redis-cluster.conf > ./${port}/conf/redis.conf && mkdir -p ./${port}/data;
done

# tree命令查看目录(如果命令不存在,执行安装命令:yum -y install tree)
➜ tree
.
├── 6380
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6381
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6382
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6383
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6384
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6385
│ ├── conf
│ │ └── redis.conf
│ └── data
└── redis-cluster.conf

18 directories, 7 files

# 创建6个redis容器
➜ for port in `seq 6380 6385`; do
docker run --net=host --name=redis-${port} -v `pwd`/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -d redis:latest redis-server /usr/local/etc/redis/redis.conf;
done

# 本人使用Debian系统

➜ apt install redis

# 启动集群

redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 --cluster-replicas 1