docker-compose 部署方式
文件存放位置
docker-compose.yml
version: '3'
services:
node1:
image: redis
container_name: node1
restart: always
ports:
- 10403:10403
- 20403:20403
# 指定时区,保证容器内时间正确
environment:
TZ: "Asia/Shanghai"
sysctls:
# 必要的内核参数
net.core.somaxconn: '1024'
volumes:
- ./data/node1:/data
- ./conf/node1.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
node2:
image: redis
container_name: node2
restart: always
ports:
- 10404:10404
- 20404:20404
# 指定时区,保证容器内时间正确
environment:
TZ: "Asia/Shanghai"
sysctls:
# 必要的内核参数
net.core.somaxconn: '1024'
volumes:
- ./data/node2:/data
- ./conf/node2.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
node3:
image: redis
container_name: node3
restart: always
ports:
- 10405:10405
- 20405:20405
# 指定时区,保证容器内时间正确
environment:
TZ: "Asia/Shanghai"
sysctls:
# 必要的内核参数
net.core.somaxconn: '1024'
volumes:
- ./data/node3:/data
- ./conf/node3.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
node1.conf,其余两个套出来
# bind 127.0.0.1
# 关闭保护模式
protected-mode no
# 绑定自定义端口
port 10403
# 禁止redis后台运行
# daemonize yes
pidfile /var/run/redis_10403.pid
# 开启集群
cluster-enabled yes
# 集群的配置 配置文件首次启动自动生成
cluster-config-file nodes_10403.conf
# 开启aof
appendonly yes
# 要宣布的IP地址。nat模式要指定宿主机IP
cluster-announce-ip 192.168.2.126
# 要宣布的数据端口。
cluster-announce-port 10403
# 要宣布的集群总线端口,注意,这里端口必须是数据端口+10000
cluster-announce-bus-port 20403
执行命令
1. docker-compose up -d 启动容器
2. docker exec -it node1 /bin/bash 进入容器
3. redis-cli create --cluste ip:port ip:port ip:port(这里有几个node就填写几个,最少3个) --cluster-replicas 0(这里是分片,我启动的node少,就没要了) 初始化集群
不初始化,会报集群不能使用
4. redis-cli -h 127.0.0.1 -p 10403 -c 进入命令行,注意,-c是为了接下命令行是cluster命令模式,不然set get 不会自动跳转到对应的node上
5. cluster info 查看相关信息
6. cluster nodes 查看集群中节点信息