数据库
首页 > 数据库> > docker-compose优雅启动redis 集群

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

标签:node,compose,cluster,redis,REDIS,CLUSTER,docker,172.22
来源: https://blog.csdn.net/xie408979832/article/details/120318477