美文网首页
docker搭建redis集群

docker搭建redis集群

作者: onnoA | 来源:发表于2020-06-14 00:32 被阅读0次

    Docker 搭建redis集群

    此次搭建一个6节点的Redis集群,包括3个主节点和3个从节点。

    安装目录 /usr/local/docker/redis-cluster 没有目录要新建此目录

    cd /usr/local/docker/redis-cluster

    一、编写docker-compose.yml 文件

    vi docker-compose.yml

    version: "3"
    services:
      redis-master1:
        image: redis:5.0 # 基础镜像
        container_name: redis-master1 # 容器名称
        working_dir: /config # 切换工作目录
        environment: # 环境变量
          - PORT=6374 # 会使用config/nodes-${PORT}.conf这个配置文件
        ports: # 映射端口,对外提供服务
          - 6374:6374 # redis的服务端口
          - 16374:16374 # redis集群监控端口
        stdin_open: true # 标准输入打开
        tty: true # 后台运行不退出
        network_mode: host # 使用host模式
        privileged: true # 拥有容器内命令执行的权限
        volumes:
          - /usr/local/docker/redis-cluster/config:/config #配置文件目录映射到宿主机
        entrypoint: # 设置服务默认的启动程序
          - /bin/bash
          - redis.sh
      redis-master2:
        image: redis:5.0
        working_dir: /config
        container_name: redis-master2
        environment:
          - PORT=6375
        ports:
          - 6375:6375
          - 16375:16375
        stdin_open: true
        network_mode: host
        tty: true
        privileged: true
        volumes:
          - /usr/local/docker/redis-cluster/config:/config
        entrypoint:
          - /bin/bash
          - redis.sh
      redis-master3:
        image: redis:5.0
        container_name: redis-master3
        working_dir: /config
        environment:
          - PORT=6376
        ports:
          - 6376:6376
          - 16376:16376
        stdin_open: true
        network_mode: host
        tty: true
        privileged: true
        volumes:
          - /usr/local/docker/redis-cluster/config:/config
        entrypoint:
          - /bin/bash
          - redis.sh
      redis-slave1:
        image: redis:5.0
        container_name: redis-slave1
        working_dir: /config
        environment:
          - PORT=6377
        ports:
          - 6377:6377
          - 16377:16377
        stdin_open: true
        network_mode: host
        tty: true
        privileged: true
        volumes:
          - /usr/local/docker/redis-cluster/config:/config
        entrypoint:
          - /bin/bash
          - redis.sh
      redis-slave2:
        image: redis:5.0
        working_dir: /config
        container_name: redis-slave2
        environment:
          - PORT=6378
        ports:
          - 6378:6378
          - 16378:16378
        stdin_open: true
        network_mode: host
        tty: true
        privileged: true
        volumes:
          - /usr/local/docker/redis-cluster/config:/config
        entrypoint:
          - /bin/bash
          - redis.sh
      redis-slave3:
        image: redis:5.0
        container_name: redis-slave3
        working_dir: /config
        environment:
          - PORT=6379
        ports:
          - 6379:6379
          - 16379:16379
        stdin_open: true
        network_mode: host
        tty: true
        privileged: true
        volumes:
          - /usr/local/docker/redis-cluster/config:/config
        entrypoint:
          - /bin/bash
          - redis.sh
    

    二、 新建config目录,并配置好相关配置信息

    mkdir config
    进入config目录
    cd config

    1. 配置 nodes-6374.conf 配置文件

    vi nodes-6374.conf

    bind 0.0.0.0
    protected-mode no
    port 6374
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6374.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    
    1. 配置nodes-6375.conf 配置文件

    vi nodes-6375.conf

    bind 0.0.0.0
    protected-mode no
    port 6375
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6375.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    
    1. 配置 nodes-6376.conf 配置文件

    vi nodes-6376.conf

    bind 0.0.0.0
    protected-mode no
    port 6376
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6376.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    
    1. 配置 nodes-6377.conf 配置文件
      vi nodes-6377.conf
    bind 0.0.0.0
    protected-mode no
    port 6377
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6377.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    
    1. 配置 nodes-6378.conf 配置文件

    vi nodes-6378.conf

    bind 0.0.0.0
    protected-mode no
    port 6378
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6378.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    
    1. nodes-6379.conf 配置文件

    vi nodes-6379.conf

    bind 0.0.0.0
    protected-mode no
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    
    daemonize no
    supervised no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo yes
    
    save 900 1
    save 300 10
    save 60 10000
    
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir "/data"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    
    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    
    

    三、 配置redis.sh 配置信息

    vi redis.sh

    redis-server /config/nodes-${PORT}.conf
    

    四、启动redis,并初始化集群配置

    • 配置好后进入 /usr/local/docker/redis-cluster 目录,执行docker-compose up -d 启动,启动后进入redis容器,初始化redis集群配置
    # 进入Redis容器
    docker exec -it redis-master1 /bin/bash
    # 初始化Redis集群命令
    redis-cli --cluster create \
    192.168.118.131:6374 192.168.118.131:6375 192.168.118.131:6376 \
    192.168.118.131:6377 192.168.118.131:6378 192.168.118.131:6379 \
    --cluster-replicas 1
    
    
    • 集群创建过程中会让你确认配置,输入yes确认即可;
    image
    • 创建成功后我们可以使用redis-cli命令连接到其中一个Redis服务;
    # 单机模式启动
    redis-cli -h 127.0.0.1 -p 6374
    # 集群模式启动
    redis-cli -c -h 127.0.0.1 -p 6374
    
    
    • 通过cluster nodes命令可以查看节点信息,发现符合3主3从的预期。

    redis 的节点信息如下

    image

    springboot 整合 redis集群

    application.yml

    spring:
    # redis 集群配置方式一
    #  redis:
    #    password: # Redis服务器连接密码(默认为空)
    #    timeout: 3000ms # 连接超时时间
    #    jedis:
    #      pool:
    #        # 连接池最大连接数(使用负值表示没有限制)
    #        max-active: 8
    #        # 连接池中的最大空闲连接
    #        max-idle: 8 # 连接池最大空闲连接数
    #        # 连接池中的最小空闲连接
    #        min-idle: 0 # 连接池最小空闲连接数
    #        # 连接池最大阻塞等待时间(使用负值表示没有限制)
    #        max-wait: -1ms # 连接池最大阻塞等待时间,负值表示没有限制
    #        # 超时关闭时间
    #      shutdown-timeout: 100ms
    #    cluster:
    #      nodes:
    #        - 192.168.118.131:6374
    #        - 192.168.118.131:6375
    #        - 192.168.118.131:6376
    #        - 192.168.118.131:6377
    #        - 192.168.118.131:6378
    #        - 192.168.118.131:6379
      # redis 集群配置方式二
      redis:
        password:
        cluster:
          nodes: 192.168.118.131:6374,192.168.118.131:6375,192.168.118.131:6376,192.168.118.131:6377,192.168.118.131:6378,192.168.118.131:6379
          max-redirects: 6
          jedis:
            shutdown-timeout: 100ms
            pool:
              max-idle: 8
              max-active: 32
              min-idle: 8
              max-wait: -1ms # 连接池最大阻塞等待时间,负值表示没有限制
    

    相关文章

      网友评论

          本文标题:docker搭建redis集群

          本文链接:https://www.haomeiwen.com/subject/yfmtxktx.html