单机使用Docker搭建Redis各种实例

docker run 运行单节点

docker run -d --name redis --network base \
          --ip 178.10.0.50 -p 6379:6379 \
	   -v /data/configs/redis/redis-master-6.0.9.conf:/usr/local/etc/redis/redis.conf \
	   -v /data/docker_mnt/redis/data:/data \
	   redis redis-server /usr/local/etc/redis/redis.conf
  • 使用指定docker网络,暴露容器6379端口到主机6379端口
  • 将指定的配置文件映射到容器内指定路径
  • 将主机目录映射到容器内redis默认数据目录
  • 设置redis-server运行时加载指定配置文件

使用docker-compose运行主从+哨兵实例

version: "3.8"
services: 
  master:
    image: 'redis:latest'
    container_name: redis-master
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/redis-master:/data
    command: redis-server --appendonly yes --requirepass your_redis_password --masterauth your_redis_password --port 6380
    network_mode: host

  slave:
    image: 'redis:latest'
    container_name: redis-slave-01
    depends_on:
      - master
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/redis-slave-01:/data
    command: redis-server --appendonly yes --requirepass your_redis_password --masterauth your_redis_password --port 6381 --replicaof [your_host_ip] 6380
    network_mode: host

  sentinel01:
    image: 'redis:latest'
    container_name: sentinel01
    depends_on:
      - slave
    volumes:
      - /data/docker_mnt/redis/sentinels:/data
      - /etc/localtime:/etc/localtime:ro
    command: redis-server /data/sentinel01.conf --sentinel
    network_mode: host

  sentinel02:
    image: 'redis:latest'
    container_name: sentinel02
    depends_on:
      - slave
    volumes:
      - /data/docker_mnt/redis/sentinels:/data
      - /etc/localtime:/etc/localtime:ro
    command: redis-server /data/sentinel02.conf --sentinel
    network_mode: host

  sentinel03:
    image: 'redis:latest'
    container_name: sentinel03
    depends_on:
      - slave
    volumes:
      - /data/docker_mnt/redis/sentinels:/data
      - /etc/localtime:/etc/localtime:ro
    command: redis-server /data/sentinel03.conf --sentinel
    network_mode: host

  • /etc/localtime:/etc/localtime:ro 使容器实例时间与主机时间同步
  • 容器数据目录挂载到主机指定目录
  • 由于示例这里没有指定主从redis的配置文件,在运行命令command处指定
  • 每个哨兵使用

sentinel配置文件示例

protected-mode yes
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
dir ./
sentinel monitor docker-redis-master 178.10.0.51 6379 2
sentinel auth-pass docker-redis-master master_password
sentinel down-after-milliseconds docker-redis-master 60000
sentinel down-after-milliseconds docker-redis-master 30000
requirepass sentinel_password
sentinel parallel-syncs docker-redis-master 1
sentinel failover-timeout docker-redis-master 180000
sentinel deny-scripts-reconfig yes
  • protected-mode yes
    • 设置为yes时,如果没有使用bind命令绑定指定ip,并且也没有设置密码,则只接受本地连接请求
  • port 26381
    • 这里使用自定义端口,默认是26379,即20000+6379
  • sentinel monitor <master-group-name> <ip> <port> <quorum>
    • 指定哨兵要监控的主节点组名称,这里可以随意设置,但监控通过节点所有哨兵必须使用同一个主节点组名称
    • ip和port是要监控的主节点ip和端口
    • quorum用来检测和确认失败,上面配置2表示只要有2个哨兵确认主节点失联,即可将该节点标记为故障并且如果条件允许的话,最终会启动故障转移流程
  • sentinel auth-pass docker-redis-master master_password
    • 设置主节点密码,如果有才需要
  • requirepass sentinel_password
    • 设置哨兵密码
sentinel monitor <master-group-name> <ip> <port> <quorum>

sentinel monitor mymaster 127.0.0.1 6379 2

 This config is used to tell Redis to monitor a master called mymaster, that is at address 127.0.0.1 and port 6379, with a quorum of 2. Everything is pretty obvious but the quorum argument:

  • The quorum is the number of Sentinels that need to agree about the fact the master is not reachable, in order to really mark the master as failing, and eventually start a failover procedure if possible.
  • However the quorum is only used to detect the failure. In order to actually perform a failover, one of the Sentinels need to be elected leader for the failover and be authorized to proceed. This only happens with the vote of the majority of the Sentinel processes.

So for example if you have 5 Sentinel processes, and the quorum for a given master set to the value of 2, this is what happens:

  • If two Sentinels agree at the same time about the master being unreachable, one of the two will try to start a failover.
  • If there are at least a total of three Sentinels reachable, the failover will be authorized and will actually start.

In practical terms this means during failures Sentinel never starts a failover if the majority of Sentinel processes are unable to talk (aka no failover in the minority partition).

使用docker-compose运行集群实例

version: "3.8"
services: 
  cluster01:
    image: 'redis:latest'
    container_name: cluster01
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data01:/data
      - /data/configs/redis/cluster/c01.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6391:6379'
      - '16391:16379'
    networks:
      base:
        ipv4_address: 178.10.0.61

  cluster02:
    image: 'redis:latest'
    container_name: cluster02
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data02:/data
      - /data/configs/redis/cluster/c02.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6392:6379'
      - '16392:16379'
    networks:
      base:
        ipv4_address: 178.10.0.62

  cluster03:
    image: 'redis:latest'
    container_name: cluster03
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data03:/data
      - /data/configs/redis/cluster/c03.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6393:6379'
      - '16393:16379'
    networks:
      base:
        ipv4_address: 178.10.0.63

  cluster04:
    image: 'redis:latest'
    container_name: cluster04
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data04:/data
      - /data/configs/redis/cluster/c04.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6394:6379'
      - '16394:16379'
    networks:
      base:
        ipv4_address: 178.10.0.64

  cluster05:
    image: 'redis:latest'
    container_name: cluster05
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data05:/data
      - /data/configs/redis/cluster/c05.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6395:6379'
      - '16395:16379'
    networks:
      base:
        ipv4_address: 178.10.0.65

  cluster06:
    image: 'redis:latest'
    container_name: cluster06
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/docker_mnt/redis/cluster/data06:/data
      - /data/configs/redis/cluster/c06.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - '6396:6379'
      - '16396:16379'
    networks:
      base:
        ipv4_address: 178.10.0.66

networks:
  base:
    external: true

The following is a minimal Redis cluster configuration file:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

可能还需要配置通知相关信息

cluster-announce-ip 主机IP
cluster-announce-port 映射到主机的redis端口
cluster-announce-bus-port 映射到主机的bus端口

cluster-enabled 需要设置为yes

cluster-config-file 这个文件由当前节点自动维护用来持久化当前节点配置

cluster-node-timeout 单位为毫米(milliseconds)如果集群节点连不到且持续超过这个时间,则会认为节点挂掉;同时如果当前节点与大多数集群主节点都连续不上且超过这个时间,那么当前节点会停止接受查询操作

使用实例创建集群

redis-cli --cluster create [主机IP]:6391 [主机IP]:6392 [主机IP]:6393 [主机IP]:6394 [主机IP]:6395 [主机IP]:6396 --cluster-replicas 1 -a [密码]

命令行连接到集群:

redis-cli -c -h [主机IP] -p 6391 -a [密码]

  • -c 启用集群模式
  • -a 指定密码 ,不使用则连接后再输入,且跳转不同节点也要输入,命令行指定则连接后不需要再输入

Comments are closed