Docker安装Redis+Sentinel哨兵集群,SpringBoot连接Redis集群配置

一、准备工作

  1. 两台Centos7服务器(虚拟机即可)
    • 192.168.32.131(主)
    • 192.168.32.129(从)
  2. 两台服务器安装Docker
  3. Docker下载Redis镜像

二、Redis配置主从节点

1、131服务器安装Redis主节点

创建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

2、131服务器安装Redis从节点1

大体步骤与主节点类似,配置文件略有不同,请仔细观看

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

3、131服务器安装Redis从节点2

大体步骤与主节点类似,配置文件略有不同,请仔细观看

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

4、129服务器安装Redis从节点3

大体步骤与主节点类似,配置文件略有不同,请仔细观看

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

5、验证主从节点配置正确性

docker exec -it redisMaster bash

redis-cli -p 6380

info Replication

如果上述配置没问题的话,正常可以看到主从节点的所有信息。

三、Docker安装Sentinel监听Redis

1、131服务器安装Sentinel1、Sentinel2、Sentinel3

创建配置文件存放目录、创建配置文件、编辑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

2、129服务器安装Sentinel4

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

3、启动四个Sentinel节点

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

4、验证Sentinel配置正确性

docker exec -it sentinel1 bash

redis-cli -p 26379

info Sentinel

四、SpringBoot配置文件连接Redis集群

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>