docker搭建redis集群(哨兵模式)

一、拉取镜像
docker pull redis 
二、起启动三个redis

配置文件

#端口
port 6379
#密码
requirepass 123456
##持久化模式         
appendonly yes 

创建目录


image.png

image.png

image.png
docker run --privileged=true -d \
-p 6391:6379 \
--name redis-1 \
-v /home/redis-s/redis1/redis.conf:/etc/redis/redis.conf \
-v /home/redis-s/redis1/data/:/data redis  \
redis-server /etc/redis/redis.conf --appendonly yes

docker run --privileged=true -d \
-p 6392:6379 \
--name redis-2 \
-v /home/redis-s/redis2/redis.conf:/etc/redis/redis.conf \
-v /home/redis-s/redis2/data/:/data redis  \
redis-server /etc/redis/redis.conf --appendonly yes

docker run --privileged=true -d \
-p 6393:6379 \
--name redis-3 \
-v /home/redis-s/redis3/redis.conf:/etc/redis/redis.conf \
-v /home/redis-s/redis3/data/:/data redis  \
redis-server /etc/redis/redis.conf --appendonly yes
三、设置主服务器(主不用设置,只设置从)

1主 2、3从
1、容器内部加载主节点

//进入容器
docker exec -it 容器id/容器名称 /bin/bash
//进入redis
redis-cli
//查看节点信息
info
//绑定主节点
# 从机需要设置该参数 (redis2.conf和redis3.conf需要将masterip设置为#主机的公网或者局域网ip,port为上面设置的6390), 注意该参数在早期版本为#slaveof
#replicaof  
SLAVEOF ip port

2、配置文件记载主节点
配置文件增加

##主节点认证密码
masterauth 123456  
#主节点                  
slaveof 192.168.2.134 6391
#保护模式
protected-mode no
#slave向master通告自己的固定ip 
slave-announce-ip 192.168.2.134
#slave向master通告自己的固定端口
slave-announce-port 6392
四、配置哨兵文件sentinel.conf
wget http://download.redis.io/redis-stable/sentinel.conf
cp sentinel.conf redis1/sentinel.conf
cp sentinel.conf redis2/sentinel.conf
cp sentinel.conf redis3/sentinel.conf
#redis1
#端口
port 26391
daemonize no
dir "/data"
#主节点
sentinel monitor mymaster 192.168.2.134 6391 2
sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 5000

sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 2
#docker无权限手动配置 报告自己的ip
sentinel announce-ip 192.168.2.134
sentinel announce-port 26391

--------------------------------------------------------------
#redis2
#端口
port 26392
daemonize no
dir "/data"
#主节点
sentinel monitor mymaster 192.168.2.134 6391 2
sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 5000

sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 2
#docker无权限手动配置 报告自己的ip
sentinel announce-ip 192.168.2.134
sentinel announce-port 26392

--------------------------------------------------------------

#redis3
port 26393
daemonize no
dir "/data"
#主节点
sentinel monitor mymaster 192.168.2.134 6391 2
sentinel auth-pass mymaster 123456

sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 5000

sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 2
#docker无权限手动配置 报告自己的ip
sentinel announce-ip 192.168.2.134
sentinel announce-port 26393

启动三个哨兵

docker run --privileged=true -d \
--name sentinel1  \
-v /home/redis-s/redis1/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
--net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run --privileged=true -d \
--name sentinel2  \
-v /home/redis-s/redis2/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
--net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf

docker run --privileged=true -d \
--name sentinel3  \
-v /home/redis-s/redis3/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
--net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf
image.png

docker 启动无法切换报错无权限

image.png

#哨兵增加配置
sentinel announce-ip 192.168.1.220
sentinel announce-port 26393


#slave向master通告自己的固定ip
slave-announce-ip 192.168.2.134
#slave向master通告自己的固定端口
slave-announce-port 6392
#redis.conf增加配置

你可能感兴趣的:(docker搭建redis集群(哨兵模式))