【006】Redis主从/哨兵/分片集群docker搭建

项目源码合集 https://gitee.com/qiuyusy/small-project-study

搭建过程疯狂踩坑,记录一下希望各位少走弯路

目录

    • 主从搭建
      • 配置文件redis.conf
      • 运行容器
      • 测试
      • 优化
    • 哨兵集群
      • 配置文件
      • 运行容器
      • 测试
      • 代码
      • 读写分离
    • 分片集群
      • 配置文件redis.conf
      • 运行容器
      • 测试
      • 集群伸缩
      • 转移插槽
      • 故障转移
      • 代码连接

主从搭建

主 redis_0 7001

从 redis_1 7002

从 redis_2 7003

mkdir -p /opt/docker/redis_study/redis_0/conf
mkdir -p /opt/docker/redis_study/redis_0/data
mkdir -p /opt/docker/redis_study/redis_1/conf
mkdir -p /opt/docker/redis_study/redis_1/data
mkdir -p /opt/docker/redis_study/redis_2/conf
mkdir -p /opt/docker/redis_study/redis_2/data

配置文件redis.conf

/opt/docker/redis_study/redis_0/conf

记得配置文件需要关闭aof appendonly no

replica-announce-ip 192.168.222.128 设置IP地址

加入bind 0.0.0.0,这个很重要

把配置文件cp 到 两个从的目录下

然后在两个从机的配置中添加replicaof 192.168.222.128 7001,设定为主机的从机

5.0之前用 slaveof 5.0以后用replicaof

运行容器

注意这里三个节点容器内的端口别都设为6379!这样IP和端口相同会导致后面哨兵集群无法选出新的主节点

主 redis_0 7001

docker run -p 7001:7001 --name redis_0 \
-v /opt/docker/redis_study/redis_0/data:/data \
-v /opt/docker/redis_study/redis_0/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf

从 redis_1 7002

docker run -p 7002:7002 --name redis_1 \
-v /opt/docker/redis_study/redis_1/data:/data \
-v /opt/docker/redis_study/redis_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf 

从 redis_2 7003

docker run -p 7003:7003 --name redis_2 \
-v /opt/docker/redis_study/redis_2/data:/data \
-v /opt/docker/redis_study/redis_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf

测试

主机redis_0 set name qiuyu

image-20230309182711575

从机redis_2 get name

image-20230309182847869

主从设置成功

可以使用 info replication查看集群信息

【006】Redis主从/哨兵/分片集群docker搭建_第1张图片

从节点只能读无法写

image-20230309183238843

优化

磁盘慢但网络快的话,主节点可以开启repl-diskless-sync no,提高全量同步性能

哨兵集群

sentinel_0 27001

sentinel_1 27002

sentinel_2 27003

mkdir -p /opt/docker/redis_study/sentinel_0/conf
mkdir -p /opt/docker/redis_study/sentinel_1/conf
mkdir -p /opt/docker/redis_study/sentinel_2/conf
mkdir -p /opt/docker/redis_study/sentinel_0/log
mkdir -p /opt/docker/redis_study/sentinel_1/log
mkdir -p /opt/docker/redis_study/sentinel_2/log

配置文件

protected-mode no
bind 0.0.0.0
# 后台运行 docker -d 这里就无需开启
daemonize no
port 27001 # 27001 27002 27003
# log
dir "/home/redis_sentinel/log"
logfile "/home/redis_sentinel/log/27001.log"
# sentinel
sentinel monitor mymaster 192.168.222.128 7001 2
# 超时
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000

sentinel announce-ip 192.168.222.128
sentinel announce-port 27001 # 27001 27002 27003

其他两个哨兵也配置一下,记得修改port 和 log

运行容器

docker run -p 27001:27001 --name sentinel_0 \
--sysctl net.core.somaxconn=1024 \
--privileged=true \
-v /opt/docker/redis_study/sentinel_0/conf:/usr/local/etc/redis/conf/ \
-v /opt/docker/redis_study/sentinel_0/log:/home/redis_sentinel/log \
-d redis redis-sentinel /usr/local/etc/redis/conf/sentinel.conf

docker run -p 27002:27002 --name sentinel_1 \
--sysctl net.core.somaxconn=1024 \
--privileged=true \
-v /opt/docker/redis_study/sentinel_1/conf:/usr/local/etc/redis/conf/ \
-v /opt/docker/redis_study/sentinel_1/log:/home/redis_sentinel/log \
-d redis redis-sentinel /usr/local/etc/redis/conf/sentinel.conf

docker run -p 27003:27003 --name sentinel_2 \
--sysctl net.core.somaxconn=1024 \
--privileged=true \
-v /opt/docker/redis_study/sentinel_2/conf:/usr/local/etc/redis/conf/ \
-v /opt/docker/redis_study/sentinel_2/log:/home/redis_sentinel/log \
-d redis redis-sentinel /usr/local/etc/redis/conf/sentinel.conf

这里坑贼多,配吐了

  • 指定sentinel.conf配置文件映射到容器内时直接使用文件映射, 这么做有可能导致哨兵没有写入配置文件的权限, 表现为WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy. 解决方案:使用文件夹映射或者加上--privileged=true用来提升root权限

  • docker redis The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn 解决 : 加上 --sysctl net.core.somaxconn=1024

测试

关闭redis_0后,sentinel_1被选举为leader,然后sentinel_1选择redis_1成为主节点

1:X 09 Mar 2023 16:17:39.478 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:X 09 Mar 2023 16:17:39.478 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:X 09 Mar 2023 16:17:39.478 # Configuration loaded
1:X 09 Mar 2023 16:17:39.479 * monotonic clock: POSIX clock_gettime
1:X 09 Mar 2023 16:17:39.479 * Running mode=sentinel, port=27001.
1:X 09 Mar 2023 16:17:39.483 # Sentinel ID is bdc44c4a16815f0a2050c378df49c26e5de22874
1:X 09 Mar 2023 16:17:39.483 # +monitor master mymaster 192.168.222.128 7001 quorum 2
1:X 09 Mar 2023 16:17:39.484 * +slave slave 192.168.222.128:7002 192.168.222.128 7002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:17:39.484 * +slave slave 192.168.222.128:7003 192.168.222.128 7003 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:17:41.738 * +sentinel sentinel dc564ce67b88380c9befc8324bee0c021995e570 192.168.222.128 27002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:17:42.290 * +sentinel sentinel 201d9e4b941d7adc262058975dab2f25e587e1cf 192.168.222.128 27003 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.428 # +sdown master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.513 # +odown master mymaster 192.168.222.128 7001 #quorum 3/2
1:X 09 Mar 2023 16:19:05.513 # +new-epoch 1
1:X 09 Mar 2023 16:19:05.513 # +try-failover master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.515 # +vote-for-leader bdc44c4a16815f0a2050c378df49c26e5de22874 1
1:X 09 Mar 2023 16:19:05.521 # dc564ce67b88380c9befc8324bee0c021995e570 voted for bdc44c4a16815f0a2050c378df49c26e5de22874 1
1:X 09 Mar 2023 16:19:05.521 # 201d9e4b941d7adc262058975dab2f25e587e1cf voted for bdc44c4a16815f0a2050c378df49c26e5de22874 1
1:X 09 Mar 2023 16:19:05.587 # +elected-leader master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.587 # +failover-state-select-slave master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.688 # +selected-slave slave 192.168.222.128:7002 192.168.222.128 7002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.688 * +failover-state-send-slaveof-noone slave 192.168.222.128:7002 192.168.222.128 7002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:05.780 * +failover-state-wait-promotion slave 192.168.222.128:7002 192.168.222.128 7002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:06.542 # +promoted-slave slave 192.168.222.128:7002 192.168.222.128 7002 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:06.542 # +failover-state-reconf-slaves master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:06.642 * +slave-reconf-sent slave 192.168.222.128:7003 192.168.222.128 7003 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:07.594 * +slave-reconf-inprog slave 192.168.222.128:7003 192.168.222.128 7003 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:07.594 * +slave-reconf-done slave 192.168.222.128:7003 192.168.222.128 7003 @ mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:07.677 # -odown master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:07.677 # +failover-end master mymaster 192.168.222.128 7001
1:X 09 Mar 2023 16:19:07.677 # +switch-master mymaster 192.168.222.128 7001 192.168.222.128 7002
1:X 09 Mar 2023 16:19:07.677 * +slave slave 192.168.222.128:7003 192.168.222.128 7003 @ mymaster 192.168.222.128 7002
1:X 09 Mar 2023 16:19:07.677 * +slave slave 192.168.222.128:7001 192.168.222.128 7001 @ mymaster 192.168.222.128 7002
1:X 09 Mar 2023 16:19:12.702 # +sdown slave 192.168.222.128:7001 192.168.222.128 7001 @ mymaster 192.168.222.128 7002

【006】Redis主从/哨兵/分片集群docker搭建_第2张图片

然后恢复redis_0 ,成为redis_0的从节点

1:X 09 Mar 2023 16:22:34.113 # -sdown slave 192.168.222.128:7001 192.168.222.128 7001 @ mymaster 192.168.222.128 7002
1:X 09 Mar 2023 16:22:44.100 * +convert-to-slave slave 192.168.222.128:7001 192.168.222.128 7001 @ mymaster 192.168.222.128 7002

代码

导入依赖


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

配置: 无需配置redis节点,直接配置哨兵就行

spring:
  redis:
    sentinel:
      master: mymaster
      nodes:
        - 192.168.222.128:27001
        - 192.168.222.128:27002
        - 192.168.222.128:27003

简单测一下,都是OK的

@SpringBootTest
class MyTest {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void testSelect(){
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
    }

    @Test
    void testInset(){
        stringRedisTemplate.opsForValue().set("test","test");
    }
}

读写分离

启动类中配置下这个@Bean就行了

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);

    }

    @Bean
    public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
        return new LettuceClientConfigurationBuilderCustomizer() {
            @Override
            public void customize(LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigurationBuilder) {
                clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
            }
        };
    }
    
    // 或者直接lambda
    @Bean
    public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }
}

这个bean中配置的就是读写策略,包括四种:

  • MASTER:从主节点读取
  • MASTER_PREFERRED:优先从master节点读取,master不可用才读取replica
  • REPLICA:从slave(replica)节点读取
  • REPLICA _PREFERRED:优先从slave(replica)节点读取,所有的slave都不可用才读取master

分片集群

为了防止一个节点存储太多数据以及提高并发写能力,搭建一个分片集群

【006】Redis主从/哨兵/分片集群docker搭建_第3张图片
IP PORT 角色 名称
192.168.222.128 7001 master redis_1_1
192.168.222.128 7002 master redis_2_1
192.168.222.128 7003 master redis_3_1
192.168.222.128 8001 slave redis_1_2
192.168.222.128 8002 slave redis_2_2
192.168.222.128 8003 slave redis_2_2

创建文件夹存放数据

mkdir -p /opt/docker/redis_study/redis_1_1/conf
mkdir -p /opt/docker/redis_study/redis_1_1/data
mkdir -p /opt/docker/redis_study/redis_1_2/conf
mkdir -p /opt/docker/redis_study/redis_1_2/data
mkdir -p /opt/docker/redis_study/redis_2_1/conf
mkdir -p /opt/docker/redis_study/redis_2_1/data
mkdir -p /opt/docker/redis_study/redis_2_2/conf
mkdir -p /opt/docker/redis_study/redis_2_2/data
mkdir -p /opt/docker/redis_study/redis_3_1/conf
mkdir -p /opt/docker/redis_study/redis_3_1/data
mkdir -p /opt/docker/redis_study/redis_3_2/conf
mkdir -p /opt/docker/redis_study/redis_3_2/data

配置文件redis.conf

需要在之前的redis.conf基础上加入cluster-enabled yes cluster-config-file /tmp/6379/nodes.conf cluster-node-timeout 5000

port 7001 # 7001 7002 7003 8001 8002 8003
# 开启集群功能
cluster-enabled yes
# 集群的配置文件名称,不需要我们创建,由redis自己维护
cluster-config-file /tmp/6379/nodes.conf
# 节点心跳失败的超时时间
cluster-node-timeout 5000
# 绑定地址
bind 0.0.0.0

运行容器

docker run -p 7001:7001 --name redis_1_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_1/data:/data \
-v /opt/docker/redis_study/redis_1_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7001

docker run -p 7002:7002 --name redis_2_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_1/data:/data \
-v /opt/docker/redis_study/redis_2_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7002

docker run -p 7003:7003 --name redis_3_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_1/data:/data \
-v /opt/docker/redis_study/redis_3_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7003

docker run -p 8001:8001 --name redis_1_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_2/data:/data \
-v /opt/docker/redis_study/redis_1_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8001

docker run -p 8002:8002 --name redis_2_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_2/data:/data \
-v /opt/docker/redis_study/redis_2_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8002

docker run -p 8003:8003 --name redis_3_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_2/data:/data \
-v /opt/docker/redis_study/redis_3_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8003

然后进入一个容器的内部

docker exec -it redis_1_1 bash

redis-cli --cluster create --cluster-replicas 1 192.168.222.128:7001 192.168.222.128:7002 192.168.222.128:7003 192.168.222.128:8001 192.168.222.128:8002 192.168.222.128:8003
  • 1 表示 每个主节点一个从节点
  • 总共6个,每组2个也就是三组,前3个默认就是主节点

【006】Redis主从/哨兵/分片集群docker搭建_第4张图片

出现一直等待的问题

image-20230310173936367

原因:

Redis集群中的各个节点,需要开放一个端口,同其他节点建立连接,用于接收心跳数据等操作。也就是说,redis_1_1节点,开放7001端口供client连接时,同时提供17001端口(10000 + 7001),供其他Redis节点连接。

所以多开放一个端口1xxxx或者使用--net host的方式(推荐--net host不然后面会踩坑)

docker run -p 7001:7001 -p17001:17001 --name redis_1_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_1/data:/data \
-v /opt/docker/redis_study/redis_1_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7001

docker run -p 7002:7002 -p17002:17002 --name redis_2_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_1/data:/data \
-v /opt/docker/redis_study/redis_2_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7002

docker run -p 7003:7003 -p17003:17003 --name redis_3_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_1/data:/data \
-v /opt/docker/redis_study/redis_3_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7003

docker run -p 8001:8001 -p18001:18001 --name redis_1_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_2/data:/data \
-v /opt/docker/redis_study/redis_1_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8001

docker run -p 8002:8002 -p18002:18002 --name redis_2_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_2/data:/data \
-v /opt/docker/redis_study/redis_2_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8002

docker run -p 8003:8003 -p18003:18003 --name redis_3_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_2/data:/data \
-v /opt/docker/redis_study/redis_3_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8003

重新试一下,OK!

【006】Redis主从/哨兵/分片集群docker搭建_第5张图片

测试

redis-cli -p 7001 然后 get直接出错

image-20230310180830813

我们在集群模式连接redis时一定要加-c

redis-cli -c -p 7001

image-20230310181036854

可以看到自动根据插槽slot重定到了7002

没有{}时根据整个key crc取余计算插槽,有{}时根据{}内的key计算,这样可以控制同一批到同一个插槽

image-20230310181916850

集群伸缩

添加一个新节点到集群,默认master

【006】Redis主从/哨兵/分片集群docker搭建_第6张图片

mkdir -p /opt/docker/redis_study/redis_4_1/conf
mkdir -p /opt/docker/redis_study/redis_4_1/data
docker run --net host --name redis_4_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_4_1/data:/data \
-v /opt/docker/redis_study/redis_4_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7004

进入容器,新节点加入集群

redis-cli --cluster add-node 192.168.222.128:7004 192.168.222.128:7001

image-20230310183951540

通过命令查看集群状态:

redis-cli -p 7004 cluster nodes

如图,7004加入了集群,并且默认是一个master节点:

image-20230310184200651

但是,可以看到7004节点的插槽数量为0,因此没有任何数据可以存储到7004上,需要进行转移插槽

转移插槽

我们插入 num 发现被插入到7001中,我们想要将num插入到7004中就需要转移插槽reshard

image-20230310184813019

使用reshard可以转移插槽

【006】Redis主从/哨兵/分片集群docker搭建_第7张图片

redis-cli --cluster reshard 192.168.222.128:7001

他会问你丫的要多少插槽,这里就定3000个

image-20230310185343481

然后问你要把插槽给谁,这里找到7004的ID填入

image-20230310185527632

image-20230310185558326

然后问你想要从谁那移动插槽过来?

  • all 全部,就是其他三个节点都给你一部分
  • ID 给你插槽的节点ID
  • done 表示结束

【006】Redis主从/哨兵/分片集群docker搭建_第8张图片

输入7001的ID,然后输入done,就开始准备移动了

然后他会为你确定转吗?yes

【006】Redis主从/哨兵/分片集群docker搭建_第9张图片

移动完我们看看集群什么情况

redis-cli -p 7004 cluster nodes

可以看到7001的0-2999插槽被转移到了7004上

image-20230310190220429

再试试set num,OK没有重定向到7001上了

image-20230310190330398

故障转移

可以看到7002还是master

image-20230310190626109

我们把7002的容器给停了docker stop redis_2_1

再看下集群状态,可以看到与7002对应的8003以及升为了master

image-20230310190828139

重新开启7002后,7002成为了slave

image-20230310191028946

如果想要手动故障转移的话

利用cluster failover命令可以手动让集群中的某个master宕机,切换到执行cluster failover命令的这个slave节点,实现无感知的数据迁移。其流程如下:

【006】Redis主从/哨兵/分片集群docker搭建_第10张图片

这种failover命令可以指定三种模式:

  • 缺省:默认的流程,如图1~6歩
  • force:省略了对offset的一致性校验
  • takeover:直接执行第5歩,忽略数据一致性、忽略master状态和其它master的意见

现在我们让7002夺回他的master地位

步骤如下:

1)利用redis-cli连接7002这个节点redis-cli -c -p 7002

2)执行cluster failover命令

image-20230310191514809

来看看集群信息,看到7002又变为了master,8003则变回slave

image-20230310191609853

代码连接

spring:
	redis:
        cluster:
          nodes:
            - 192.168.222.128:7001
            - 192.168.222.128:7002
            - 192.168.222.128:7003
            - 192.168.222.128:7004
            - 192.168.222.128:8001
            - 192.168.222.128:8002
            - 192.168.222.128:8003
2023-03-11 15:07:45.386  WARN 11612 --- [oEventLoop-4-10] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.4:7003]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.386  WARN 11612 --- [oEventLoop-4-13] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.7:8003]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.386  WARN 11612 --- [oEventLoop-4-11] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.5:8001]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.386  WARN 11612 --- [ioEventLoop-4-9] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.3:7002]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.387  WARN 11612 --- [ioEventLoop-4-8] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.2:7001]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.387  WARN 11612 --- [ioEventLoop-4-7] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.1:7004]: java.nio.channels.ClosedChannelException
2023-03-11 15:07:45.387  WARN 11612 --- [oEventLoop-4-12] i.l.c.c.t.DefaultClusterTopologyRefresh  : Unable to connect to [172.17.0.6:8002]: java.nio.channels.ClosedChannelException

报错!这里为什么连接的不是配置的地址

docker network inspect bridge看一下,原来docker默认桥接模式,连接到容器内了,我们改为host模式

[root@localhost ~]# docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "c8ec522db624553660584ed74d453b343d1c3dec1f166b0b8f9275a30a82710e",
        "Created": "2023-03-10T17:41:51.080737251+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1dc658345986e33c2a42f65ef788aec8b6d852bda43ceeb0e0b76210fee24da7": {
                "Name": "redis_3_1",
                "EndpointID": "cd9ad02d0702a55c0ed0f912a4a19bc8591074c0085e828f3441c9262de6655b",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "a03b8920396bfc3c6c744875929931c8a77aa914d699372a9ba857ca859acc6c": {
                "Name": "redis_2_2",
                "EndpointID": "00cd297374b4945ae0a54eae3f6d2211047fb35647fd09de279bac707c938553",
                "MacAddress": "02:42:ac:11:00:06",
                "IPv4Address": "172.17.0.6/16",
                "IPv6Address": ""
            },
            "bd110c2b8afdcf347a7c0b33c3d1507bc19fe2cf60c6a91fb5a22f8b13484090": {
                "Name": "redis_1_1",
                "EndpointID": "5f290865f19023f6d57dd47af2d5c3c10b0d84397485080d6a0efc4f01fa221f",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "c75e53ca79f1e5f3c89252eb0fed0e06126847daaefd12b6ecab6a7fda4bf383": {
                "Name": "redis_1_2",
                "EndpointID": "d7efddb06930b6bcb8a177d05b2a297a1edac79ecb70c383358c59a95315d829",
                "MacAddress": "02:42:ac:11:00:05",
                "IPv4Address": "172.17.0.5/16",
                "IPv6Address": ""
            },
            "d83c9d9217fec3e06480594a7d81a8268966c59b8cdf6ef2398f854af8fe7507": {
                "Name": "redis_3_2",
                "EndpointID": "00b754b7e72a746e1b1d5a162d2152e06787826996f94d2b304ac2b8762dfbcb",
                "MacAddress": "02:42:ac:11:00:07",
                "IPv4Address": "172.17.0.7/16",
                "IPv6Address": ""
            },
            "da919e410d9a671d26e23252def156efcdedb232106e8cb894cd026b90d2d709": {
                "Name": "redis_4_1",
                "EndpointID": "cb245d40d171d174d8b835a77ce944d14c7ec30c19751592ad16528c3d3f5e69",
                "MacAddress": "02:42:ac:11:00:08",
                "IPv4Address": "172.17.0.8/16",
                "IPv6Address": ""
            },
            "dacb6b6e6d96c3f9f66a80475a537bb3efdd9488e6a96655edb5dfce44206997": {
                "Name": "redis_2_1",
                "EndpointID": "2ea48a1e14578fbb4e360676b1b953282825a06c84d01fa77b4b66ce3d7d8856",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }

行吧使用-net host重新生成容器

docker run --net host --name redis_1_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_1/data:/data \
-v /opt/docker/redis_study/redis_1_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7001

docker run --net host --name redis_2_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_1/data:/data \
-v /opt/docker/redis_study/redis_2_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7002

docker run --net host --name redis_3_1 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_1/data:/data \
-v /opt/docker/redis_study/redis_3_1/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 7003

docker run --net host --name redis_1_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_1_2/data:/data \
-v /opt/docker/redis_study/redis_1_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8001

docker run --net host --name redis_2_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_2_2/data:/data \
-v /opt/docker/redis_study/redis_2_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8002

docker run --net host --name redis_3_2 \
--privileged=true \
-v /opt/docker/redis_study/redis_3_2/data:/data \
-v /opt/docker/redis_study/redis_3_2/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf \
--cluster-enabled yes --appendonly yes --port 8003

然后进容器重新配置集群

redis-cli --cluster create --cluster-replicas 1 192.168.222.128:7001 192.168.222.128:7002 192.168.222.128:7003 192.168.222.128:8001 192.168.222.128:8002 192.168.222.128:8003

终于OK!

【006】Redis主从/哨兵/分片集群docker搭建_第11张图片

你可能感兴趣的:(小项目集合,redis,docker,缓存)