Based docker build redis-sentinel clusters

1 Overview

Redis clusters can achieve high availability and a group redis sharding between nodes. There will be a master and multiple slave nodes in the cluster. When the master node fails, a slave node should be elected as the new master. However Redis itself (including many of its client) does not automatically discover faults and the standby switching capability, the need for external monitoring solution to achieve automatic failover.

Redis Sentinel is the official recommendation of the high-availability solutions. It is a monitoring and management tool Redis cluster node can provide monitoring, notification, automatic failover and client configuration discovery services.

2, problems encountered

1, docker host network

When using a host network for docker windows, mac does not take effect (could not find a solution), and finally gave up the windows using centos deployment cluster.

2, without using a host network connection problems sentinel

Without the use of the network host you can specify a connection port so that sentinel node cluster master Unicom normal, but when the primary node fails sentinel node acquired from the master to the virtual IP is IP in the container results in the cluster can not properly connected.

3, build process

1, the directory structure

2, sentinel profile

1 sentinel1.conf

#端口号
port 26379
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster <ip> <port> 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000

复制代码

2 sentinel2.conf

#端口号
port 26380
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster <ip> <port> 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000

复制代码

3 sentinel3.conf

#端口号
port 26381
dir /tmp
# mymaster:自定义集群名,2:投票数量必须2个sentinel才能判断主节点是否失败
sentinel monitor mymaster <ip> <port> 2
# 指的是超过5000秒,且没有回复,则判定主节点不可达
sentinel down-after-milliseconds mymaster 5000
# 表示在故障转移的时候最多有numslaves在同步更新新的master
sentinel parallel-syncs mymaster 1
# 故障转移超时时间
sentinel failover-timeout mymaster 5000

复制代码

3、docker-compose.yml

version: '2'
services:
  master:
    image: redis:4.0
    restart: always
    container_name: redis-master
    #使用主机网络
    network_mode: "host"
    command: redis-server --port 16379  
   
  slave1:
    image: redis:4.0
    restart: always
    container_name: redis-slave-1
    network_mode: "host"
    # 指定端口并指定master ip 端口
    command: redis-server --port 16380 --slaveof <master ip> 16379
   
  slave2:
    image: redis:4.0
    restart: always
    container_name: redis-slave-2
    network_mode: "host"    
    command: redis-server --port 16381 --slaveof <master ip> 16379
    
  sentinel1:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-1
    network_mode: "host"
    # 指定sentinel文件位置
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    # 使用数据卷映射文件到指定sentinel位置
    volumes:
      - ./sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
   
  sentinel2:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-2
    network_mode: "host"    
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
   
  sentinel3:
    image: redis:4.0
    restart: always
    container_name: redis-sentinel-3
    network_mode: "host"    
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf  
    
复制代码

4, using centos deploy cluster test results

1, the test cluster is connected via sentinel1

2, the test data synchronization master node subnode

3, close view standby switching master

sentinel normal Unicom
Master node switches from 16,379 to 16,381

end

Dragon stole a lazy week after, before you set up a sentinel network model cluster due docker cause problems after standby switch cluster node can not connect, yesterday to see the host can not be achieved just put on centos tested on some perfect window get.

Reproduced in: https: //juejin.im/post/5d07ac98e51d4577583ddccc

Guess you like

Origin blog.csdn.net/weixin_33695082/article/details/93181757