Docker compose to arrange redis cluster

Time:2021-4-7

Common architecture of redis

Master and slave

  • One master and many followers
  • Master slave read write separation

Daemonize no docker requires at least one foreground process in the container, so it cannot be set as a daemon

Setting master IP replicaof < masterip > < masterport > from slave node
version: '3'

services:
  master:
    image: redis
    container_name: redis-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-master-follower/master/conf:/etc/redis
      - /usr/local/etc/docker-compose/redis-master-follower/master/data:/data
      - /usr/local/etc/docker-compose/redis-master-follower/master/log:/log
    ports:
      - 6380:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.80


  follower:
    image: redis
    container_name: redis-follower
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-master-follower/follower/conf:/etc/redis
      - /usr/local/etc/docker-compose/redis-master-follower/follower/data:/data
      - /usr/local/etc/docker-compose/redis-master-follower/follower/log:/log
    ports:
      - 6381:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.81



networks:
  redisNet:
    external: true 

Sentinel

  • Ensure high availability, and solve the problem that the master node cannot write after downtime
  • After the primary node is down, the Sentry will issue sdown (subjective downtime). When the number of sdows reaches the preset value, it is odown (objective downtime)
  • Generally, it is configured as half sdown, that is, odown, so the minimum unit of sentinel architecture should be 1 master 2 slave 3 sentry
  • Write myid after sentry starts successfully
Set the master server and the number of odown for sentinel monitoring (when the total number of sdown reaches odown, the master-slave handoff will be started)
sentinel monitor mymaster 192.168.11.128 6379 2

If there is no response in x seconds, the single sentinel is determined as sdown
sentinel down-after-milliseconds

The longest time of a single fail over is considered as a fail over
sentinel failover-timeout mymaster 10000 

How many slaves can synchronize with the new master at the same time when the master / slave handoff occurs? The smaller the number is, the faster the failover is. It is usually set to 1
sentinel parallel-syncs <master-name> <numslaves>

The unique ID automatically written after the sentinel is started. Each sentinel is different, so the same configuration file cannot be used
sentinel myid

Here, the sentinel container should wait for the redis container to start first, and use dependencies_ On Control

version: '3'

services:
  master:
    image: redis
    container_name: redis-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/master/conf:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/master/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/master/log:/log
    ports:
      - 6380:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.80

  follower1:
    image: redis
    container_name: redis-follower1
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/follower1/conf:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/follower1/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/follower1/log:/log
    ports:
      - 6381:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.81

  follower2:
    image: redis
    container_name: redis-follower2
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/follower2/conf:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/follower2/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/follower2/log:/log
    ports:
      - 6382:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.82
      

  sentinel1:
    image: redis
    depends_on:
      - master
      - follower1
      - follower2
    container_name: redis-sentinel1
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel1:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel1/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel1/log:/log
    ports:
      - 26379:26379
    networks:
      redisNet:
        ipv4_address: 192.168.88.83

  sentinel2:
    image: redis
    depends_on:
      - master
      - follower1
      - follower2
    container_name: redis-sentinel2
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel2:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel2/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel2/log:/log
    ports:
      - 26380:26379
    networks:
      redisNet:
        ipv4_address: 192.168.88.84

  sentinel3:
    image: redis
    depends_on:
      - master
      - follower1
      - follower2
    container_name: redis-sentinel3
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel3:/etc/redis
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel3/data:/data
      - /usr/local/etc/docker-compose/redis-sentinel/sentinel/sentinel3/log:/log
    ports:
      - 26381:26379
    networks:
      redisNet:
        ipv4_address: 192.168.88.85


networks:
  redisNet:
    external: true  
      

redis-cluter

  • After redis 5.0, you can directly use redis cli to create clusters without ruby
  • Redis will allocate the slot to the master equally. All data operations will determine the partition of this operation by the value obtained from CRC16 (key) mod 16384
Cluster cluster enabled yes

Cluster node timeout: connection timeout

Cluster announcement IP: IP address of each node in the cluster (it is OK not to configure the following, and redis will search automatically)

Cluster announcement port: cluster node mapping port

Cluster announcement bus port: cluster bus port

cluster-require-full-coverage no 
Redis cluster needs 16384 slots to be normal before it can provide external services. In other words, as long as any slot is abnormal, the whole cluster will not provide external services. Therefore, the production environment is generally No
version: '3'

services:
  master1:
    image: redis
    container_name: redis-cluster1-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/master:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/master:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/master:/log
    ports:
      - 6379:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.80

  follow1:
    image: redis
    container_name: redis-cluster1-follower
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/follower:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/follower:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster1/follower:/log
    ports:
      - 6380:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.81

  master2:
    image: redis
    container_name: redis-cluster2-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/master:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/master:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/master:/log
    ports:
      - 6381:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.82

  follow2:
    image: redis
    container_name: redis-cluster2-follower
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/follower:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/follower:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster2/follower:/log
    ports:
      - 6382:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.83

  master3:
    image: redis
    container_name: redis-cluster3-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/master:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/master:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/master:/log
    ports:
      - 6383:6379
    networks:
      redisNet:
        ipv4_address: 192.168.88.84

  follow3:
    image: redis
    container_name: redis-cluster3-follower
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/follower:/etc/redis
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/follower:/data
      - /usr/local/etc/docker-compose/redis-cluster/cluster3/follower:/log
    ports:
      - 6384:6379   
    networks:
      redisNet:
        ipv4_address: 192.168.88.85

networks:
  redisNet:
    external: true  
      

Here, the master / follower in YML has no practical significance, and the specific master-slave assignment is determined by redis

After docker compose successfully starts six redis containers, enter any one and configure redis cluster

Choreography container
docker-compose up -d

Enter the container
docker exec -it container_id -bin/bash

Configure the redis cluster, one follower for each master
redis-cli --cluster create
192.168.88.80:6379
192.168.88.81:6379
192.168.88.82:6379
192.168.88.83:6379
192.168.88.84:6379
192.168.88.85:6379
--cluster-replicas 1

As shown in the figure, 16384 slots are equally allocated to three masters
83 replicaof 82
85 replicaof 81
84 replicaof 80
Docker compose to arrange redis cluster

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]