Redis Configures the NAT network environment

In the redis configuration file example, there is a statement about the cluster running in NAT. Simply put, in order to compose the Redis cluster topology, static information about the nodes’ final network environment needs to be exposed in the configuration. Redis 6.0 configures the address

########################## 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

In docker-comemage. yaml, you can configure REDIS_CLUSTER_ANNOUNCE_IP as the final external network address and enable six nodes from Redis-node-0 to 5. In order to know the final network address, build a virtual network and assign static IP addresses. Finally, use redis-cluster-init to set up the cluster topology, that is, use the image of the official website and run the following command using 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-yesCopy the code

Docker-comemage. yaml complete instance

Run docker-compose up -d to start the cluster

version: '3.8'
services:
  redis-node-0:
    image: bitnami/redis-cluster
    container_name: redis-node-0
    restart: always
    networks:
      redis:
        ipv4_address: 172.22. 0100.
    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. 0101.
    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. 0102.
    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. 0103.
    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. 0104.
    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. 0105.
    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. 0106.
    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. 0100.: 7000 172.22. 0101.: 7001 172.22. 0102.: 7002 172.22. 0103.: 7003 172.22. 0104.: 7004 172.22. 0105.: 7005 --cluster-replicas 1 --cluster-yes


networks:
  redis:
    driver: bridge
    ipam:
      config:
        - subnet: 172.22. 0. 0/ 16
          gateway: 172.22. 01.

Copy the code