假設(shè)當(dāng)前目錄為/data/db/redis
- 下載redis配置文件 http://download.redis.io/redis-stable/redis.conf
- 創(chuàng)建config目錄,將下載的redis.conf復(fù)制到config目錄,修改如下信息
#bind 127.0.0.1 #注釋掉地址綁定
port 700* #3份配置文件,從7001到7003
appendonly yes #允許持久化
cluster-enable yes #啟用集群
cluster-config-file nodes-700*.conf #存放節(jié)點(diǎn)配置信息的文件名,從7001到7003
cluster-node-timeout 15000 #節(jié)點(diǎn)失效檢測(cè)的超時(shí)時(shí)間
在config目錄下分別創(chuàng)建redis-7001.conf, redis-7002.conf, redis-7003.conf
- 創(chuàng)建7001, 7002, 7003目錄
- 創(chuàng)建docker-compose.yml文件
version: "3.8"
services:
redis-master1:
image: redis:latest
container_name: redis-7001
#stdin_open: true
tty: true
restart: always
network_mode: host #需要為host模式
privileged: true
volumes:
- /data/db/redis/config/redis-7001.conf:/usr/local/etc/redis/redis.conf
- ./7001:/data
command:
redis-server /usr/local/etc/redis/redis.conf
redis-master2:
image: redis:latest
container_name: redis-7002
tty: true
restart: always
network_mode: host
privileged: true
volumes:
- /data/db/redis/config/redis-7002.conf:/usr/local/etc/redis/redis.conf
- ./7002:/data
command:
redis-server /usr/local/etc/redis/redis.conf
redis-master3:
image: redis:latest
container_name: redis-7003
tty: true
restart: always
network_mode: host
privileged: true
volumes:
- /data/db/redis/config/redis-7003.conf:/usr/local/etc/redis/redis.conf
- ./7003:/data
command:
redis-server /usr/local/etc/redis/redis.conf
- 啟動(dòng)redis服務(wù)
docker-compose up -d
- 進(jìn)入其中一臺(tái)容器,進(jìn)行集群創(chuàng)建
docker exec -it redis-7001 bash
redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 --cluster-replicas 0
此時(shí)根據(jù)命令行提示輸入yes,即可創(chuàng)建集群成功
注:
官方備注,網(wǎng)絡(luò)模式需要為host

image.png