Redis Sentinel + HAProxy代理主节点(一主一备)

Redis Sentinel + HAProxy代理主节点(一主一备)

前言

Redis是现今最常用的缓存数据库之一,高可用一直是我们对软件服务的要求。常见的 Redis 高可用是主从+哨兵配置,在主节点挂掉后哨兵监控程序进行主从切换将从节点升级。本文采用 Redis 哨兵模式加 HAProxy 代理层实现 Redis 对后台服务的隐藏,后台服务只需要像连接单机 Redis 服务一样连接到代理程序即可。

HAProxy是一款自由、快速、可靠的、开源的TCP和HTTP应用程序负载均衡器和代理服务器软件。它提供了高可用性、负载均衡、反向代理等功能,可以实现多台服务器之间的负载均衡和故障切换,确保服务器的高可用性和稳定性。HAProxy的优点包括高性能、可靠性、易于配置,因此被广泛应用于各种规模的企业,是一个非常受欢迎的负载均衡软件。

一、master节点

# /data/redis.conf
bind 0.0.0.0
port 6379
requirepass "root"
appendonly yes
logfile "/data/redis.log"
masterauth "root"
$ redis-server /data/redis.conf

二、slave节点

# /data/redis.conf
bind 0.0.0.0
port 6379
requirepass "root"
appendonly yes
logfile "/data/redis.log"
masterauth "root"
replicaof 172.16.10.64 6379 # 主节点ip、端口
$ redis-server /data/redis.conf

三、sentinel节点(一般建议3个或5个)

# /data/sentinel.conf
bind 0.0.0.0
port 26379
requirepass "sentinel"
logfile "/data/sentinel.log"

sentinel monitor redis-master 172.16.10.64 6379 1 # 主节点ip、端口、哨兵数量一半(节点down的判定要求半数哨兵赞成)
sentinel auth-pass redis-master "root" # Redis密码
sentinel down-after-milliseconds redis-master 10000 # 超时时间
$ redis-sentinel /data/sentinel.conf

四、docker-compose.yaml

version: '3'

services:
  redis-0:
    container_name: redis-0
    restart: always
    image: redis:7.0
    command: ["redis-server", "/data/redis.conf"]
    network_mode: host
    volumes:
    - /data/redis-0:/data

  redis-1:
    container_name: redis-1
    restart: always
    image: redis:7.0
    command: ["redis-server", "/data/redis.conf"]
    network_mode: host
    volumes:
    - /data/redis-1:/data

  redis-sentinel:
    restart: always
    image: redis:7.0
    container_name: redis-sentinel
    command: ["redis-sentinel", "/data/sentinel.conf"]
    # network_mode: host
    volumes:
    - /data/redis-sentinel:/data
$ docker-compose up -d

五、使用HAProxy代理主节点(主备模式)

docker-entrypoint.sh

#!/bin/sh
set -e

if [ -n "$1" ]; then
    exec "$@"
fi
if [ -z "$REDIS_SERVERS" ]; then
    echo "ERROR: expected REDIS_SERVERS" 1>&2
    exit 1
fi
if [ -z "$HOST" ]; then
    HOST='0.0.0.0'
fi
if [ -z "$PORT" ]; then
    PORT='6379'
fi

echo 'listen master'                                                >> /var/lib/haproxy/haproxy.cfg
echo '    mode tcp'                                                 >> /var/lib/haproxy/haproxy.cfg
echo "    bind $HOST:$PORT"                                         >> /var/lib/haproxy/haproxy.cfg
echo '    maxconn 20000'                                            >> /var/lib/haproxy/haproxy.cfg
echo '    timeout connect 3s'                                       >> /var/lib/haproxy/haproxy.cfg
echo '    timeout client  1m'                                       >> /var/lib/haproxy/haproxy.cfg
echo '    timeout server  1m'                                       >> /var/lib/haproxy/haproxy.cfg
echo ''                                                             >> /var/lib/haproxy/haproxy.cfg
echo '    retries 3'                                                >> /var/lib/haproxy/haproxy.cfg
echo '    option redispatch'                                        >> /var/lib/haproxy/haproxy.cfg
echo '    option tcp-check'                                         >> /var/lib/haproxy/haproxy.cfg
if [ -n "$REDIS_PASSWORD" ]; then
echo "    tcp-check send auth\ $REDIS_USER\ $REDIS_PASSWORD\r\n"    >> /var/lib/haproxy/haproxy.cfg
echo "    tcp-check expect string +OK"                              >> /var/lib/haproxy/haproxy.cfg
fi
echo '    tcp-check send PING\r\n'                                  >> /var/lib/haproxy/haproxy.cfg
echo '    tcp-check expect string +PONG'                            >> /var/lib/haproxy/haproxy.cfg
echo '    tcp-check send INFO\ REPLICATION\r\n'                     >> /var/lib/haproxy/haproxy.cfg
echo '    tcp-check expect string role:master'                      >> /var/lib/haproxy/haproxy.cfg
echo '    tcp-check send QUIT\r\n'                                  >> /var/lib/haproxy/haproxy.cfg
echo '    tcp-check expect string +OK'                              >> /var/lib/haproxy/haproxy.cfg
echo -n $REDIS_SERVERS | sed 's/,/\n/g' | xargs -i \
echo "    server {} {} check inter 1s"                              >> /var/lib/haproxy/haproxy.cfg

haproxy -W -db -f /var/lib/haproxy/haproxy.cfg
$ sudo chmod 755 docker-entrypoint.sh
FROM haproxy:lts-alpine
COPY docker-entrypoint.sh /bin
ENTRYPOINT ["/bin/docker-entrypoint.sh"]
version: '3'
services:
  redis-haproxy:
    container_name: redis-haproxy
    restart: always
    image: redis-haproxy
    build: ./redis-haproxy
    network_mode: host
    environment:
      # HOST: '0.0.0.0'
      # PORT: '6379'
      REDIS_SERVERS: '172.16.10.64:6380,172.16.10.64:6381'
      REDIS_USER: ''
      REDIS_PASSWORD: ''
$ docker-compose up -d

后记

这套方案的优点是对服务透明,自动故障处理。后面我们会再出一套集群 Redis 方案(三主三从),欢迎点赞收藏!

你可能感兴趣的:(redis,sentinel,数据库)