redis一主两从部署
mkdir /redis
vim docker-compose.yml
version: '3'
services:
master:
image: redis:5.0.9-alpine3.11
container_name: redis-master
network_mode: host
restart: always
volumes:
- /redis/node-1/data:/data #目录没有创建就好了,/data下存放的是dump.rdb文件方便后面做数据持久化
- /redis/node-1/conf/redis.conf:/etc/redis/redis.conf #配置文件是从其他redis主机拷贝过来的
command: redis-server /etc/redis/redis.conf
slave1:
image: redis:5.0.9-alpine3.11
container_name: redis-slave-1
network_mode: host
restart: always
volumes:
- /redis/node-2/data:/data
- /redis/node-2/conf/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
depends_on:
- master
slave2:
image: redis:5.0.9-alpine3.11
container_name: redis-slave-2
network_mode: host
restart: always
volumes:
- /redis/node-3/data:/data
- /redis/node-3/conf/redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
depends_on:
- master
主节点配置文件编辑
#bind 127.0.0.1
requirepass 密码
允许远程登录和设置密码
两台从节点配置文件编辑
#bind 127.0.0.1
port 6380
requirepass 密码
slaveof ip 端口 #填写master节点的ip+端口
masterauth 密码 #如果master设置了密码这里也需要填写master的密码否则无法连接
docker-compose up -d启动集群
测试
[root@pc-test redis]# docker exec -it redis-master /bin/sh
/data # redis-cli -c
127.0.0.1:6379> auth 密码
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.184,port=6380,state=online,offset=126,lag=0
slave1:ip=192.168.1.184,port=6381,state=online,offset=126,lag=1
master_replid:fb584d49c371d8dfb49794398fdc69190e660fdb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126
哨兵搭建
mkdir redis-sentinel
vim docker-compose.yml
version: '2'
services:
sentinel1:
image: redis:5.0.9-alpine3.11 ## 镜像
container_name: redis-sentinel-1
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
network_mode: host
restart: always
volumes:
- /redis/node-1/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis:5.0.9-alpine3.11 ## 镜像
container_name: redis-sentinel-2
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
network_mode: host
restart: always
volumes:
- /redis/node-2/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis:5.0.9-alpine3.11 ## 镜像
container_name: redis-sentinel-3
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
network_mode: host
restart: always
volumes:
- /redis/node-3/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf
配置文件设置
vim /redis/node-1/conf/sentinel.conf
sentinel monitor myredis ip 端口 1
sentinel auth-pass myredis 密码
cp /redis/node-1/conf/sentinel.conf /redis/node-2/conf/sentinel.conf
cp /redis/node-1/conf/sentinel.conf /redis/node-3/conf/sentinel.conf
docker-compose up -d启动
测试
docker stop redis-master
192.168.1.184:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.184,port=6381,state=online,offset=88456,lag=0
master_replid:fa561c3a547f3e3ad994fb933222529295e82210
master_replid2:12478db5509bec9883cd021de59e230b2372080d
master_repl_offset:88456
second_repl_offset:1592
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:88456