docker-compose之redis cluster模式-1

  一、docker容器化的redis cluster最难搞的就是网络问题,这边记录一下集群搭建过程。

  二、dockerfile

FROM redis:5.0.14
MAINTAINER xbd
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  三、docker-compose.yml

version: "2"
services:
  xbd-redis-1:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-redis
    image: xbd-redis-1
    restart: always
    container_name: xbd-redis-1
    ports:
      - 6379:6379
      - 16379:16379
    environment:
      - TZ=Asia/Shanghai
    privileged: true
    command: ['redis-server',
              '--bind 0.0.0.0',
              '--protected-mode yes',
              '--daemonize no',
              '--requirepass xbd',
              '--maxmemory 1GB',
              '--maxmemory-policy allkeys-lru',
              '--cluster-enabled yes',
              '--cluster-config-file nodes-6379.conf',
              '--cluster-node-timeout 15000',
              '--cluster-replica-validity-factor 10',
              '--cluster-migration-barrier 1',
              '--cluster-require-full-coverage yes',
              '--cluster-replica-no-failover no',
              '--cluster-announce-ip 192.168.5.14',
              '--cluster-announce-port 6379',
              '--cluster-announce-bus-port 16379']

  xbd-redis-2:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-redis
    image: xbd-redis-2
    restart: always
    container_name: xbd-redis-2
    ports:
      - 6380:6379
      - 16380:16379
    environment:
      - TZ=Asia/Shanghai
    privileged: true
    command: ['redis-server',
              '--bind 0.0.0.0',
              '--protected-mode yes',
              '--daemonize no',
              '--requirepass xbd',
              '--maxmemory 1GB',
              '--maxmemory-policy allkeys-lru',
              '--cluster-enabled yes',
              '--cluster-config-file nodes-6379.conf',
              '--cluster-node-timeout 15000',
              '--cluster-replica-validity-factor 10',
              '--cluster-migration-barrier 1',
              '--cluster-require-full-coverage yes',
              '--cluster-replica-no-failover no',
              '--cluster-announce-ip 192.168.5.14',
              '--cluster-announce-port 6380',
              '--cluster-announce-bus-port 16380']
  xbd-redis-3:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-redis
    image: xbd-redis-3
    restart: always
    container_name: xbd-redis-3
    ports:
      - 6381:6379
      - 16381:16379
    environment:
      - TZ=Asia/Shanghai
    privileged: true
    command: ['redis-server',
              '--bind 0.0.0.0',
              '--protected-mode yes',
              '--daemonize no',
              '--requirepass xbd',
              '--maxmemory 1GB',
              '--maxmemory-policy allkeys-lru',
              '--cluster-enabled yes',
              '--cluster-config-file nodes-6379.conf',
              '--cluster-node-timeout 15000',
              '--cluster-replica-validity-factor 10',
              '--cluster-migration-barrier 1',
              '--cluster-require-full-coverage yes',
              '--cluster-replica-no-failover no',
              '--cluster-announce-ip 192.168.5.14',
              '--cluster-announce-port 6381',
              '--cluster-announce-bus-port 16381']

  xbd-redis-4:
    build:
      context: ./
      dockerfile: ./config/Dockerfile/Dockerfile-redis
    image: xbd-redis-4
    restart: always
    container_name: xbd-redis-4
    ports:
      - 6382:6379
      - 16382:16379
    environment:
      - TZ=Asia/Shanghai
    privileged: true
    command: ['redis-server',
              '--bind 0.0.0.0',
              '--protected-mode yes',
              '--daemonize no',
              '--requirepass xbd',
              '--maxmemory 1GB',
              '--maxmemory-policy allkeys-lru',
              '--cluster-enabled yes',
              '--cluster-config-file nodes-6379.conf',
              '--cluster-node-timeout 15000',
              '--cluster-replica-validity-factor 10',
              '--cluster-migration-barrier 1',
              '--cluster-require-full-coverage yes',
              '--cluster-replica-no-failover no',
              '--cluster-announce-ip 192.168.5.14',
              '--cluster-announce-port 6382',
              '--cluster-announce-bus-port 16382']

  参数说明

# 绑定IP
bind 0.0.0.0
# 保护模式
protected-mode yes
# docker原因不用守护进程
daemonize no
# 认证密码
requirepass xbd
# 最大内存,按实际要求设定
maxmemory 1GB
# 内存回收策略
maxmemory-policy allkeys-lru
# 集群开启
cluster-enabled yes
# 节点记录文件,自动写入
cluster-config-file nodes-6379.conf
# 节点超时时间
cluster-node-timeout 15000
# 故障转移的超时时间
cluster-replica-validity-factor 10
# 最少副本数量
cluster-migration-barrier 1
# 控制集群高可用,设置为yes,则允许在分片不可用或者集群路由未完全分配的情况下,其他的分片仍然提供服务
cluster-require-full-coverage yes
# # 选项设置为yes时,会阻止replicas尝试对其master在主故障期间进行故障转移
# 然而,master仍然可以执行手动故障转移,如果强制这样做的话。
cluster-replica-no-failover no
# 真实IP,docker时使用
cluster-announce-ip 192.168.5.14
# 端口
cluster-announce-port 6379
# 内部端口
cluster-announce-bus-port 16379'

  详细参数说明:https://www.cnblogs.com/ll409546297/p/16922764.html

  四、部署

  1)运行docker容器

docker-compose up &

  2)手动建立集群

redis-cli -a xbd --cluster create 192.168.5.14:6379 192.168.5.14:6380 192.168.5.14:6381 192.168.5.14:6382

  集群会出现:

  

  3)集群查看和确认hash槽

  

   

   可以确认,集群已经成功,并且自动分配了hash槽。

  3)测试(一定要带-c,主要声明集群模式):

redis-cli -h 192.168.5.14 -p 6379 -a xbd -c

  

 

   确认自己做了节点迁移,数据存储和查询。

posted @ 2022-11-25 10:59  小不点丶  阅读(93)  评论(0编辑  收藏  举报