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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是使用docker-compose部署Redis集群的步骤: 1.创建一个目录,例如redis-cluster,并在该目录中创建一个docker-compose.yaml文件。 2.在docker-compose.yaml文件中编写以下内容: ```yaml version: '3' services: redis-1: image: redis:6.0.9 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./redis.conf:/usr/local/etc/redis/redis.conf ports: - "6379" networks: - redis-cluster redis-2: image: redis:6.0.9 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./redis.conf:/usr/local/etc/redis/redis.conf ports: - "6380" networks: - redis-cluster redis-3: image: redis:6.0.9 command: redis-server /usr/local/etc/redis/redis.conf volumes: - ./redis.conf:/usr/local/etc/redis/redis.conf ports: - "6381" networks: - redis-cluster networks: redis-cluster: driver: bridge ``` 3.在redis-cluster目录中创建一个redis.conf文件,并将以下内容复制到文件中: ```conf bind 0.0.0.0 port 6379 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes requirepass 1111 masterauth 1111 ``` 4.在终端中导航到redis-cluster目录,并运行以下命令启动Redis集群: ```shell docker-compose up -d ``` 5.使用以下命令进入redis-1容器: ```shell docker exec -it redis-cluster_redis-1_1 /bin/bash ``` 6.在redis-1容器中,使用以下命令创建Redis集群: ```shell redis-cli --cluster create 172.20.0.2:6379 172.20.0.3:6379 172.20.0.4:6379 --cluster-replicas 0 ``` 7.现在,您已经成功地使用docker-compose部署了Redis集群。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值