一.搭建redis集群
1.拉取redis镜像
docker pull redis
2.创建网络
docker network create consul_consul
3.创建redis数据文件存储路径
mkdir -p /root/distributed/redis/data/master
mkdir -p /root/distributed/redis/data/slave1
mkdir -p /root/distributed/redis/data/slave2
4.创建redis compose文件
mkdir -p /root/distributed/redis
cd /root/distributed/redis
touch docker-compose.yml
5.配置docker-compsoe.yml文件
version: '3'
networks:
consul_consul:
external: true
services:
redis-master:
image: redis:latest
container_name: redis-master
restart: always
environment:
TZ: Asia/Shanghai
networks:
- consul_consul
ports:
- "6379:6379"
volumes:
- ./data/master:/data
command: redis-server --port 6379 --requirepass g3nt00567 --appendonly yes
redis-slave1:
image: redis:latest
command: redis-server --slaveof redis-master 6379 --port 6380 --requirepass g3nt00567 --masterauth g3nt00567 --appendonly yes
restart: always
networks:
- consul_consul
ports:
- "6380:6380"
environment:
TZ: Asia/Shanghai
volumes:
- ./data/slave1:/data
#ilinks:
# - redis-master
redis-slave2:
image: redis:latest
command: redis-server --slaveof redis-master 6379 --port 6381 --requirepass g3nt00567 --masterauth g3nt00567 --appendonly yes
restart: always
networks:
- consul_consul
ports:
- "6381:6381"
environment:
TZ: Asia/Shanghai
volumes:
- ./data/slave2:/data
#ilinks:
# - redis-master
二.搭建sentinel哨兵
1.建立sentinel文件夹
mkdir -p /root/distributed/redis/sentinel
cd /root/distributed/redis/sentinel
2.创建sentienl.conf文件
touch sentinel1.conf
3.配置sentienl1.conf
port 26379
dir "/tmp"
# 自定义集群名,其中 192.168.8.188 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel myid 41e24abf1a9b8c509d636678170251febf44036a
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.48.9 6381 2
sentinel auth-pass mymaster g3nt00567
sentinel config-epoch mymaster 1
sentinel leader-epoch mymaster 1
# Generated by CONFIG REWRITE
sentinel known-replica mymaster 192.168.48.7 6379
sentinel known-replica mymaster 192.168.48.8 6380
sentinel known-sentinel mymaster 192.168.48.11 26379 e480267c29f2c27a10a1fe8587a458b58719d719
sentinel known-sentinel mymaster 192.168.48.10 26379 34bac34cbac31e673b53dba146d50ea48b03334e
sentinel current-epoch 1
4.复制sentinel1.conf文件
cp /root/distributed/redis/sentinel1.conf sentinel2.conf
cp /root/distributed/redis/sentinel1.conf sentinel3.conf
5.创建docker-compose.yml
touch docker-compose.yml
6.配置docker-compose.yml文件
version: '3'
networks:
consul_consul:
external: true
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26379:26379
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
networks:
- consul_consul
sentinel2:
image: redis
container_name: redis-sentinel-2
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26380:26379
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
networks:
- consul_consul
sentinel3:
image: redis
container_name: redis-sentinel-3
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
restart: always
ports:
- 26381:26379
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
- consul_consul
三.检测redis集群是否生效
3.启动redis-sentinel
docker-compose up -d
4.查看redis-sentinel是否启动成功
进入redis-sentinel容器中,查看redis连接状态
docker ``exec` `-it 容器Id ``bash``<br>redis-cli -p 26379
执行如下命令,查看redis主信息
sentinel master mymaster
显示带红框的信息则表明成功
image执行如下命令,查看从redis信息是否正常
sentinel slaves mymaster
显示如下表明正常
image四.测试主redis挂了之后,哨兵能否正常选举redis
使用如下命令,停掉主redis. [停掉容器也可以使用容器对应的NAMES来停掉]
docker stop redis-master
显示红框则表明redis已经停了
image查看redis-sentinel日志,看其将那个redis选举为主,如下,其将端口位6381的redis选举为主
image测试端口号为6381的redis是否具备写功能,如下则表明成功
image至此,redis一主二从三哨兵模式部署成功.
网友评论