Redis master-slave replication and sentinels in Docker

Master-slave replication

  1. Install wegt

    yum -y install wget
    
  2. Download redis configuration file

    wget http://download.redis.io/redis-stable/redis.conf
    
  3. And make three copies, two of which are subordinate and one is mainly

    [root@localhost conf]# ls
    redis.conf  redis-master.conf  redis-slave1.conf  redis-slave2.conf
    
  4. Modify configuration file

    • Host configuration file

      # 允许所有IP访问Redis
      bind 0.0.0.0
      
    • Two slave configuration files

      # 允许所有IP访问Redis
      bind 0.0.0.0
      # 设置主机的地址:IP 端口。接下去通过--lin进行地址解析,docker自动将master解析为地址
      replicaof master 6379
      

      Tip: The second configuration can also not be specified in the configuration file slaveof master 6379, but enter the command in redis0-cli , but then every time the slave restarts, it must re-enter the command to specify the host

  5. Start the container

    • Host

      docker run --name redis-master \
      -v /mydata/redis-test/conf/redis-master.conf:/usr/local/etc/redis/redis.conf \
      -d redis redis-server /usr/local/etc/redis/redis.conf
      
    • Slave 1

      docker run --name redis-slave1 \
      -v /mydata/redis-test/conf/redis-slave1.conf:/usr/local/etc/redis/redis.conf \
      --link redis-master:master \
      -d redis redis-server /usr/local/etc/redis/redis.conf
      
    • Slave 2

      docker run --name redis-slave2 \
      -v /mydata/redis-test/conf/redis-slave2.conf:/usr/local/etc/redis/redis.conf \
      --link redis-master:master \
      -d redis redis-server /usr/local/etc/redis/redis.conf
      
  6. View Redis master-slave status

    进入哨兵容器
    docker exec -it 容器名/容器ID redis-cli
    查看主从状态
    info replication
    

--link: --link 关联容器名/关联容器ID:alias. Among them, alias is the alias of the source container under the link.

In this way, the container can communicate through the alias or the associated container name (associated container ID), because Docker will automatically perform DNS resolution

Sentinel mode

Single sentry

  1. Download sentinel configuration file

    wget http://download.redis.io/redis-stable/sentinel.conf
    
  2. Modify configuration filesentinel.conf

    # 参数说明:mymaster:哨兵名称;master 6379:被监控主机IP和端口;1:执行故障恢复操作前至少需要几个哨兵节点同意
    # 因为当前只配了一个哨兵,所以设为1,一般以一主二仆要配置三个哨兵,那么最后一个值就配为2
    sentinel monitor mymaster master 6379 1
    
    # 配置日志文件,默认会在容器/data下,如果不需要可以不管
    logfile "sentinel.log"
    
  3. Running sentry

    docker run --name redis-sentinel \
    -v /mydata/redis-test/conf/sentinel.conf:/usr/local/etc/redis/sentinel.conf \
    --link redis-master:master \
    -d redis redis-sentinel /usr/local/etc/redis/sentinel.conf
    

The above method is for a situation where there is only one sentinel. At this time, the sentinel may also be down, resulting in failure to recover

Multi-sentinel

The following demo configures three sentries

  1. Download sentinel configuration file

    wget http://download.redis.io/redis-stable/sentinel.conf
    
  2. Modify configuration file

    # 关闭安全模式
    protected-mode no
    
    # 最后一个值修改为2,即需要两个哨兵节点投票同意才能故障恢复
    sentinel monitor mymaster master 6379 2
    
  3. Make three copies

    [root@localhost conf]# ls
    sentinel1.conf  sentinel2.conf  sentinel3.conf
    
  4. Start three sentinel containers

    docker run --name redis-sentinel1 \
    -v /mydata/redis-test/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf \
    --link redis-master:master \
    -d redis redis-sentinel /usr/local/etc/redis/sentinel.conf
    
    docker run --name redis-sentinel2 \
    -v /mydata/redis-test/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf \
    --link redis-master:master \
    --link redis-sentinel1:sentinel \
    -d redis redis-sentinel /usr/local/etc/redis/sentinel.conf
    
    docker run --name redis-sentinel3 \
    -v /mydata/redis-test/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf \
    --link redis-master:master \
    --link redis-sentinel2:sentinel \
    -d redis redis-sentinel /usr/local/etc/redis/sentinel.conf
    

    Note that the last two containers need more configuration and the former sentinel --link, otherwise the sentinels cannot communicate

  5. Check the operation of the sentry

    # 进入哨兵容器
    docker exec -it 哨兵容器名/哨兵容器ID bash
    # 进入哨兵
    redis-cli -p 26379
    # 查看哨兵运行情况
    info sentinel
    

Guess you like

Origin www.cnblogs.com/JMWan233/p/12681608.html