docker搭建redis主从哨兵集群的实现步骤

1  获取redis镜像

docker pull redis:6.2.7

2  创建redis主从、docker-compose文件

cd /opt/docker/redis
vi docker-compose.yml

docker-compose.ym

version: '3'
services:
master:
image: redis:6.2.7
container_name: redis-master
command: redis-server /etc/redis/redis.conf --requirepass 123456 --masterauth 123456
volumes:
- /opt/docker/redis/data/redis_data1:/data
- /opt/docker/redis/conf/redis1.conf:/etc/redis/redis.conf
network_mode: "host"
slave1:
image: redis:6.2.7
container_name: redis-slave-1
volumes:
- /opt/docker/redis/data/redis_data2:/data
- /opt/docker/redis/conf/redis2.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
depends_on:
- master
network_mode: "host"
slave2:
image: redis:6.2.7 
container_name: redis-slave-2
volumes:
- /opt/docker/redis/data/redis_data3:/data
- /opt/docker/redis/conf/redis3.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
depends_on:
- master
network_mode: "host"
sentinel1:
image: redis:6.2.7
container_name: redis-sentinel-1
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /opt/docker/redis/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
network_mode: "host"
depends_on:
- master
- slave1
- slave2
sentinel2:
image: redis:6.2.7
container_name: redis-sentinel-2
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /opt/docker/redis/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
network_mode: "host"
depends_on:
- master
- slave1
- slave2
sentinel3:
image: redis:6.2.7
container_name: redis-sentinel-3
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /opt/docker/redis/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
network_mode: "host"
depends_on:
- master
- slave1
- slave2


3   redis配置、哨兵配置

查看配置文件

cd /opt/docker/redis
tree ./

结构如下


1.png

redis1.conf

#redis1.conf
bind 0.0.0.0
port 6379
protected-mode no
slave-read-only no

redis2.conf

#redis2.conf
bind 0.0.0.0
port 6380
protected-mode no
slave-read-only no

redis3.conf

#redis3.conf
bind 0.0.0.0
port 6381
protected-mode no
slave-read-only no

sentinel1.conf

#sentinel1.conf
port 26379
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
sentinel deny-scripts-reconfig yes

sentinel1.conf

#sentinel2.conf
port 26380
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
sentinel deny-scripts-reconfig yes

sentinel1.conf

#sentinel3.conf
port 26381
dir /tmp
sentinel monitor mymaster 192.168.18.131 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
sentinel deny-scripts-reconfig yes

4  docker-compose

启动

docker-compose up
docker-compose up -d

查看启动情况

2.png

查看集群情况

docker exec -it 主节点容器id或者容器名称 bash
redis-cli -p 6379
info replication

3.png

原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/4140.html

发表评论

登录后才能评论