此版本为无密码配置版本,关于compose请查阅我其它文章,这里不再描述。
#######步骤1:home下创建文件夹:sentinelredis,按照图片创建三个文件夹。
如图:
步骤2:每个文件夹下对应描述:
masterslave: docker-compose.yml
redisconf: sentinel.conf
sentinel: docker-compose.yml , sentinel1.conf , sentinel2.conf, sentinel3.conf
文件内容如下:
masterslave: docker-compose.yml
version: '3.7'
services:
master:
image: redis
container_name: redis-master
restart: always
command: bash -c "redis-server --protected-mode no --slave-announce-ip {宿主机ip地址} --slave-announce-port 6379"
ports:
- 6379:6379
#数据卷目录挂载,都是当前目录,跟yml文件同级
volumes:
- ./data1:/data
slave1:
image: redis
container_name: redis-slave-1
restart: always
command: bash -c "redis-server --protected-mode no --slaveof redis-master 6379 --slave-announce-ip {宿主机ip地址} --slave-announce-port 6380"
ports:
- 6380:6379
volumes:
- ./data2:/data
slave2:
image: redis
container_name: redis-slave-2
restart: always
command: bash -c "redis-server --protected-mode no --slaveof redis-master 6379 --slave-announce-ip {宿主机ip地址} --slave-announce-port 6381"
ports:
- 6381:6379
volumes:
- ./data3:/data
redisconf: sentinel.conf
#6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 #2),kingmaster是可以自定义集群的名字
port 26379
dir /tmp
sentinel monitor kingmaster {宿主机ip地址} 6379 2
sentinel down-after-milliseconds kingmaster 30000
sentinel parallel-syncs kingmaster 1
sentinel failover-timeout kingmaster 180000
sentinel deny-scripts-reconfig yes
sentinel: docker-compose.yml , sentinel1.conf , sentinel2.conf, sentinel3.conf
version: '3.7'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
restart: always
ports:
- 26379:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel1.conf:/home/sentinelredis/redisconf/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
restart: always
ports:
- 26380:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel2.conf:/home/sentinelredis/redisconf/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /home/sentinelredis/redisconf/sentinel.conf
volumes:
- ./sentinel3.conf:/home/sentinelredis/redisconf/sentinel.conf
networks:
default:
#关于masterslave_default配置,docker compose会自动创建一个网络,就是以文件夹的名称+_default命名。可以执行命令 docker network ls,找到
name: masterslave_default
external: true
port 26379
dir /tmp
sentinel monitor kingmaster {宿主机ip地址} 6379 2
sentinel down-after-milliseconds kingmaster 30000
sentinel parallel-syncs kingmaster 1
sentinel failover-timeout kingmaster 180000
sentinel deny-scripts-reconfig yes
port 26379
dir /tmp
sentinel monitor kingmaster {宿主机ip地址} 6379 2
sentinel down-after-milliseconds kingmaster 30000
sentinel parallel-syncs kingmaster 1
sentinel failover-timeout kingmaster 180000
sentinel deny-scripts-reconfig yes
port 26379
dir /tmp
sentinel monitor kingmaster {宿主机ip地址} 6379 2
sentinel down-after-milliseconds kingmaster 30000
sentinel parallel-syncs kingmaster 1
sentinel failover-timeout kingmaster 180000
sentinel deny-scripts-reconfig yes
开始启动测试:
-
进入主从配置文件夹:
2.启动成功:
3:进入主从查看信息:
进入主节点:
进入从节点:
4.启动哨兵
[图片上传中...(image.png-edaeaf-1714293208948-0)]
验证哨兵:
重新创建容器实现修改:
docker-compose up --force-recreate -d
docker exec -it redis-sentinel-2 redis-cli -h {宿主机ip地址} -p 26381 info sentinel
如图:
5.关于主从通信测试,哨兵选举测试这里不再描述。
启动成功后,springboot项目配置启动;
yml:
spring:
redis:
database: 0
timeout: 1800000
sentinel:
master: kingmaster
nodes:
- 172.21.170.133:26379
- 172.21.170.133:26380
- 172.21.170.133:26381
lettuce:
pool:
max-idle: 10
max-active: 20
max-wait: 10000ms
min-idle: 5