Redis有三種集群模式
* 主從模式
* Sentinel模式
* Cluster模式
參考:Redis集群詳解
1.首先需要docker 、docker-compose 環境
2.下載相關配置文件
Gitee地址: https://gitee.com/lifeToSpring/redis-cluster
目錄結構:
redis.sh
redis-server /config/nodes-${PORT}.conf
3.修改對應nodes-${PORT}.conf的配置文件(參考):
# bind 127.0.0.1 //加上注釋#
protected-mode no //關閉保護模式
port 6391 //綁定自定義端口
# daemonize yes //禁止redis后台運行
pidfile /var/run/redis_6391.pid
cluster-enabled yes //開啟集群 把注釋#去掉
cluster-config-file nodes_6391.conf //集群的配置 配置文件首次啟動自動生成
appendonly yes //開啟aof
cluster-announce-ip 10.xx.xx.xx //要宣布的IP地址。nat模式要指定宿主機IP
cluster-announce-port 6391 //要宣布的數據端口。
cluster-announce-bus-port 16391 //要宣布的集群總線端口
4.1 節點192.168.255.225:
docker-compose.yml
services:
redis-master1:
image: redis:5.0 # 基礎鏡像
container_name: node1 # 容器名稱
working_dir: /config # 切換工作目錄
environment: # 環境變量
- PORT=6391 # 會使用config/nodes-${PORT}.conf這個配置文件
ports: # 映射端口,對外提供服務
- 6391:6391 # redis的服務端口
- 16391:16391 # redis集群監控端口
stdin_open: true # 標准輸入打開
tty: true # 后台運行不退出
network_mode: host # 使用host模式
privileged: true # 擁有容器內命令執行的權限
volumes:
- /mydata/redis-cluster/config:/config #配置文件目錄映射到宿主機
entrypoint: # 設置服務默認的啟動程序
- /bin/bash
- redis.sh
redis-master2:
image: redis:5.0
working_dir: /config
container_name: node2
environment:
- PORT=6392
ports:
- 6392:6392
- 16392:16392
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-master3:
image: redis:5.0
container_name: node3
working_dir: /config
environment:
- PORT=6393
ports:
- 6393:6393
- 16393:16393
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
啟動redis實例
docker-compose -f docker-compose.yml up
4.2 節點192.168.255.226:
docker-compose.yml
version: "3"
services:
redis-slave1:
image: redis:5.0
container_name: node4
working_dir: /config
environment:
- PORT=6394
ports:
- 6394:6394
- 16394:16394
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-slave2:
image: redis:5.0
working_dir: /config
container_name: node5
environment:
- PORT=6395
ports:
- 6395:6395
- 16395:16395
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
redis-slave3:
image: redis:5.0
container_name: node6
working_dir: /config
environment:
- PORT=6396
ports:
- 6396:6396
- 16396:16396
stdin_open: true
network_mode: host
tty: true
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
啟動redis實例
docker-compose -f docker-compose.yml up
5 使用docker鏡像 zvelo/redis-trib 構建redis集群
docker run --rm -it zvelo/redis-trib create --replicas 1 192.168.255.225:6391 192.168.255.225:6392 192.168.255.225:6393 192.168.255.226:6394 192.168.255.226:6395 192.168.255.226:6396