Docker搭建Redis高可用集群(基于redis-sentinel)

时间:2022-11-26 12:00:20


一主二从

wget http://download.redis.io/redis-stable/redis.conf
# 主库
docker run --name redis-master -d -p 6300:6300 -v /D/Workspace/Docker/docker-install-redis-cluster/redis.conf:/etc/redis/redis.conf redis redis-server /etc/redis/redis.conf --requirepass 123456 --port 6300
docker exec -it redis-master bash
redis-cli -a 123456 -p 6300
config set masterauth 123456

#从库1
docker run --name redis-slave1 -d -p 6301:6301 redis redis-server --requirepass 123456 --port 6301
docker exec -it redis-slave1 bash
redis-cli -a 123456 -p 6301
slaveof 127.0.0.1 6300
config set masterauth 123456

#从库1
docker run --name redis-slave2 -d -p 6302:6302 redis redis-server --requirepass 123456 --port 6302
docker exec -it redis-slave2 bash
redis-cli -a 123456 -p 6302
slaveof 127.0.0.1 6300
config set masterauth 123456

配置哨兵

wget http://download.redis.io/redis-stable/sentinel.conf

配置sentinel.conf

sentinel monitor mymaster 127.0.0.1 6300 2
sentinel auth-pass mymaster 123456
daemonize yes

启动sentinel实例:

docker run -it --name redis-sentinel1 -v /D/Workspace/Docker/docker-install-redis-cluster/sentinel1.conf:/usr/local/etc/redis/sentinel.conf -d redis /bin/bash
docker exec -it redis-sentinel1 bash
redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run -it --name redis-sentinel2 -v /D/Workspace/Docker/docker-install-redis-cluster/sentinel2.conf:/usr/local/etc/redis/sentinel.conf -d redis /bin/bash
docker exec -it redis-sentinel2 bash
redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run -it --name redis-sentinel3 -v /D/Workspace/Docker/docker-install-redis-cluster/sentinel3.conf:/usr/local/etc/redis/sentinel.conf -d redis /bin/bash
docker exec -it redis-sentinel3 bash
redis-sentinel /usr/local/etc/redis/sentinel.conf

验证

# 主库
docker exec -it redis-master bash
redis-cli -a 123456 -p 6300 DEBUG sleep 60

# 从库
docker exec -it redis-slave1 bash
info Replication

docker exec -it redis-slave2 bash
info Replication