微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

docker-compose优雅启动redis 集群

redis关于NAT网络环境的配置

在redis配置文件示例中, 有这么一段关于集群运行在NAT环境下的表述。
简单来讲,为了组成redis 集群拓扑,需要在配置中暴露节点最终的网络环境的静态信息。
redis 6.0 配置地址

########################## CLUSTER DOCKER/NAT support ########################

# In certain deployments, Redis Cluster nodes address discovery fails, because
# addresses are NAT-ted or because ports are forwarded (the typical case is
# Docker and other containers).
#
# In order to make Redis Cluster working in such environments, a static
# configuration where each node kNows its public address is needed. The
# following two options are used for this scope, and are:
#
# * cluster-announce-ip
# * cluster-announce-port
# * cluster-announce-bus-port
#
# Each instructs the node about its address, client port, and cluster message
# bus port. The information is then published in the header of the bus packets
# so that other nodes will be able to correctly map the address of the node
# publishing the information.
#
# If the above options are not used, the normal Redis Cluster auto-detection
# will be used instead.
#
# Note that when remapped, the bus port may not be at the fixed offset of
# clients port + 10000, so you can specify any port and bus-port depending
# on how they get remapped. If the bus-port is not set, a fixed offset of
# 10000 will be used as usual.
#
# Example:
#
# cluster-announce-ip 10.1.1.5
# cluster-announce-port 6379
# cluster-announce-bus-port 6380

docker-compose

参考上面的表述,在docker-compose.yaml中可以配置REdis_CLUSTER_ANNOUNCE_IP为最终外部网络地址,启用redis-node-0~5六个节点,并且为了最终网络地址可知,构建虚拟网络,分配静态ip,最终使用redis-cluster-init进行集群拓扑的搭建,即使用官网的镜像,使用redis-cli执行以下命令。

redis-cli -a Lucky2021 --cluster create 172.22.0.100:7000 172.22.0.101:7001 172.22.0.102:7002 172.22.0.103:7003 172.22.0.104:7004 172.22.0.105:7005 --cluster-replicas 1 --cluster-yes

docker-compose.yaml完整实例

执行 docker-compose up -d 启动集群

version: '3.8'
services:
  redis-node-0:
    image: bitnami/redis-cluster
    container_name: redis-node-0
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.100
    hostname: redis-node-0
    environment:
      - 'REdis_PORT_NUMBER=7000'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7000'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17000'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7000:7000"
      - "17000:17000"

  redis-node-1:
    image: bitnami/redis-cluster
    container_name: redis-node-1
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.101
    hostname: redis-node-1
    environment:
      - 'REdis_PORT_NUMBER=7001'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7001'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17001'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7001:7001"
      - "17001:17001"

  redis-node-2:
    image: bitnami/redis-cluster
    container_name: redis-node-2
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.102
    hostname: redis-node-2
    environment:
      - 'REdis_PORT_NUMBER=7002'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7002'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17002'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7002:7002"
      - "17002:17002"

  redis-node-3:
    image: bitnami/redis-cluster
    container_name: redis-node-3
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.103
    hostname: redis-node-3
    environment:
      - 'REdis_PORT_NUMBER=7003'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7003'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17003'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7003:7003"
      - "17003:17003"

  redis-node-4:
    image: bitnami/redis-cluster
    container_name: redis-node-4
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.104
    hostname: redis-node-4
    environment:
      - 'REdis_PORT_NUMBER=7004'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7004'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17004'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7004:7004"
      - "17004:17004"

  redis-node-5:
    image: bitnami/redis-cluster
    container_name: redis-node-5
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.105
    hostname: redis-node-5
    environment:
      - 'REdis_PORT_NUMBER=7005'
      - 'REdis_PASSWORD=Lucky2021'
      - 'REdis_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REdis_CLUSTER_ANNOUNCE_PORT=7005'
      - 'REdis_CLUSTER_ANNOUNCE_IP=192.168.68.92'
      - 'REdis_CLUSTER_BUS_ANNOUNCE_PORT=17005'
      - 'REdis_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7005:7005"
      - "17005:17005"

  redis-cluster-init:
    image: redis:6.2
    container_name: redis-cluster-init
    restart: 'no'
    networks:
      redis:
        ipv4_address: 172.22.0.106
    depends_on:
      - redis-node-0
      - redis-node-1
      - redis-node-2
      - redis-node-3
      - redis-node-4
      - redis-node-5
    entrypoint: []
    command:
      - /bin/bash
      - -c
      - redis-cli -a Lucky2021 --cluster create 172.22.0.100:7000 172.22.0.101:7001 172.22.0.102:7002 172.22.0.103:7003 172.22.0.104:7004 172.22.0.105:7005 --cluster-replicas 1 --cluster-yes


networks:
  redis:
    driver: bridge
    ipam:
      config:
        - subnet: 172.22.0.0/16
          gateway: 172.22.0.1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐