创建Redis配置文件和数据挂载目录
mkdir -p /data/redisMaster
cd /data/redisMaster
touch myredis.conf
vim myredis.conf
myredis.conf配置文件内容:
# 允许任何IP访问
bind 0.0.0.0
# 关闭保护模式
protected-mode no
# 设置端口号
port 6380
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
# 设置Redis密码
# requirepass 123456
# 设置外部访问IP,如果不加,其他IP访问会报错
slave-announce-ip 192.168.32.131
Docker启动Redis主节点命令:
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6380:6380 \
--name redisMaster \
-v /data/redisMaster/myredis.conf:/etc/redis/redis.conf \
-v /data/redisMaster/data:/data \
-d redis redis-server /etc/redis/redis.conf
大体步骤与主节点类似,配置文件略有不同,请仔细观看
mkdir -p /data/redisSlave1
cd /data/redisSlave1
touch myredis.conf
vim myredis.conf
bind 0.0.0.0
protected-mode no
port 6381
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.131
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6381:6381 \
--name redisSlave1 \
-v /data/redisSlave1/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave1/data:/data \
-d redis redis-server /etc/redis/redis.conf
大体步骤与主节点类似,配置文件略有不同,请仔细观看
mkdir -p /data/redisSlave2
cd /data/redisSlave2
touch myredis.conf
vim myredis.conf
bind 0.0.0.0
protected-mode no
port 6382
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.131
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6382:6382 \
--name redisSlave2 \
-v /data/redisSlave2/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave2/data:/data \
-d redis redis-server /etc/redis/redis.conf
大体步骤与主节点类似,配置文件略有不同,请仔细观看
mkdir -p /data/redisSlave3
cd /data/redisSlave3
touch myredis.conf
vim myredis.conf
bind 0.0.0.0
protected-mode no
port 6383
timeout 0
save 900 1
save 300 10
save 60 10000
rdbcompression yes
dbfilename dump.rdb
dir /data
appendonly yes
appendfsync everysec
slave-announce-ip 192.168.32.129
# 设置主节点IP和端口号
replicaof 192.168.32.131 6380
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6383:6383 \
--name redisSlave3 \
-v /data/redisSlave3/myredis.conf:/etc/redis/redis.conf \
-v /data/redisSlave3/data:/data \
-d redis redis-server /etc/redis/redis.conf
docker exec -it redisMaster bash
redis-cli -p 6380
info Replication
如果上述配置没问题的话,正常可以看到主从节点的所有信息。
创建配置文件存放目录、创建配置文件、编辑sentinel配置文件
mkdir -p /data/sentinel1
mkdir -p /data/sentinel2
mkdir -p /data/sentinel3
touch /data/sentinel1/sentinel1.conf
touch /data/sentinel2/sentinel2.conf
touch /data/sentinel3/sentinel3.conf
sentinel1.conf配置文件内容:
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
sentinel2.conf配置文件内容:
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26380
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
sentinel3.conf配置文件内容:
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.131
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
mkdir -p /data/sentinel4
cd /data/sentinel4
touch sentinel4.conf
vim sentinel4.conf
sentinel4.conf配置文件内容:
# 关闭保护模式
protected-mode no
# 配置端口号,各个节点不能相同
port 26382
daemonize no
pidfile /var/run/redis-sentinel.pid
loglevel notice
# 日志存放地址
logfile "/var/tmp/sentinel.log"
dir /tmp
# 外部IP访问地址,解决Docker-Net容器IP跨服务器无法访问问题
sentinel announce-ip 192.168.32.129
# 设置Redis主节点信息
sentinel monitor mymaster 192.168.32.131 6380 2
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
SENTINEL master-reboot-down-after-period mymaster 0
131主服务器启动命令
docker run --name sentinel1 \
-v /data/sentinel1/sentinel1.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run --name sentinel2 \
-v /data/sentinel2/sentinel2.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run --name sentinel3 \
-v /data/sentinel3/sentinel3.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf
129从服务器启动命令
docker run --name sentinel4 \
-v /data/sentinel4/sentinel4.conf:/usr/local/etc/redis/sentinel.conf \
-d --net=host redis redis-sentinel /usr/local/etc/redis/sentinel.conf
docker exec -it sentinel1 bash
redis-cli -p 26379
info Sentinel
spring:
redis:
sentinel:
nodes:
- 192.168.31.131:26379
- 192.168.31.131:26380
- 192.168.31.131:26381
- 192.168.31.129:26382
#连接超时时间(毫秒)
timeout: 30000
jedis:
pool:
#连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
#连接池中的最小空闲连接
min-idle: 0
#连接池中的最大空闲连接
max-idle: 8
#连接池最大连接数(使用负值表示没有限制)
max-active: 8