Loading

Redis哨兵集群搭建-Docker-Compose

代码地址:https://github.com/li-zheng-hao/redis-sentinel-docker-compose

部署方式

一主两从三哨兵:

  1. 1个主节点
  2. 2个从节点
  3. 3个哨兵节点

服务器配置

  • 系统:CentOS 7.9
  • IP:172.10.2.52
  • 服务器数量:1台

这里只是用一台服务器部署进行演示,后面会在备注中指明如何在多个服务器上分开部署

部署脚本

主从脚本

version: '3'
services:
  master:
    image: redis
    container_name: redis-master
    command: redis-server  --requirepass lzh123456  --masterauth lzh123456
    network_mode: host
  slave1:
    image: redis
    container_name: redis-slave-1
    network_mode: host
    command:  redis-server --slaveof 172.10.2.52 6379 --masterauth lzh123456  --requirepass lzh123456 --port 6380
  slave2:
    image: redis
    container_name: redis-slave-2
    network_mode: host
    command: redis-server --slaveof 172.10.2.52 6379 --masterauth lzh123456 --requirepass lzh123456 --port 6381

其中几个注意点:

  1. network_mode使用的是host模式,所以不需要绑定端口
  2. 由于这个脚本是两个slave节点都和master在一个节点,因此需要在运行时配置端口

如果是要部署在三个不同的服务器上,需要更换为下面两个脚本

# master脚本
version: '3'
services:
  master:
    image: redis
    container_name: redis-master
    command: redis-server  --requirepass lzh123456  --masterauth lzh123456
    network_mode: host
# slave脚本
version: '3'
services:
  slave:
    image: redis
    container_name: redis-slave
    network_mode: host
    command:  redis-server --slaveof 172.10.2.52 6379 --masterauth lzh123456  --requirepass lzh123456

上面master脚本部署master节点,只需要部署一次,slave脚本部署slave节点,需要部署几个就在几个服务器上运行,同一台服务器上如果需要运行多个slave,那么还是得像第一步一样配置端口。

运行的脚本为:

docker-compose -f xxxx.yml up -d

上面没有挂载配置文件和数据文件,如果有需要的可以使用如下方式挂载:

volumes:
    - ./config.conf:/etc/redis/redis.conf
    - ./data:/data

如果有以下需求,建议手动挂载:

  1. 设置最大内存限制
  2. 设置数据存储策略(AOF或RDB,定时存储的频率),默认的是RDB,没有开启AOF
  3. 挂载数据,保存到宿主机上防止容器被删数据丢失

哨兵脚本

下面是部署三个哨兵的脚本

version: '3'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1 
    command: redis-sentinel /usr/local/etc/redis/sentinel1.conf --port 26379
    network_mode: host
    volumes:
      - ./conf/sentinel1:/usr/local/etc/redis
  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    network_mode: host
    command: redis-sentinel /usr/local/etc/redis/sentinel2.conf --port 26380
    volumes:
      - ./conf/sentinel2:/usr/local/etc/redis
  sentinel3:
    image: redis
    network_mode: host
    container_name: redis-sentinel-3
    command: redis-sentinel /usr/local/etc/redis/sentinel3.conf --port 26381
    volumes:
      - ./conf/sentinel3:/usr/local/etc/redis

哨兵的配置文件:

port 26379
dir /tmp
sentinel monitor mymaster 172.10.2.52 6379 2
sentinel auth-pass mymaster lzh123456
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
sentinel deny-scripts-reconfig yes

注意点:

  1. 同样都是部署在同一台服务器,因此需要对三个哨兵分别配置端口
  2. 配置脚本需要复制三份,因为哨兵进程会对这个文件写入一些配置,因此不能用同一份共享

如果要分成多个服务器进行部署,需要改写成下面这样:

version: '3'
services:
  sentinel:
    image: redis
    container_name: redis-sentinel-1 
    command: redis-sentinel /usr/local/etc/redis/sentinel1.conf --port 26379
    network_mode: host
    volumes:
      - ./conf/sentinel1:/usr/local/etc/redis

需要在几台服务器上运行就部署几遍,部署命令与部署主从节点相同

客户端连接

客户端我使用的是AnotherRedisDesktopManager,配置连接方式如下:

image-20220921175830057

可以用这个工具查看slave节点的信息:

image-20220921175912620

之后可以手动测试下线master或者slave节点测试是否能够正常切换

在.Net Core中连接的方式根据使用的库决定,我用的是FreeRedis,配置方式如下:

RedisClient redisClient = new RedisClient(
                GlobalConfig.Instance.Redis.RedisConn,
                GlobalConfig.Instance.Redis.SentinelAdders.ToArray(),
                true //是否读写分离
            );
serviceCollection.AddSingleton<IRedisClient>(redisClient);

配置信息:

 "Redis": {
    "ServiceName": "mymaster",
    "Password": "lzh123456",
    "RedisConn": "mymaster,password=lzh123456",
    "SentinelAdders": [
      "172.10.2.52:26379",
      "172.10.2.52:26380",
      "172.10.2.52:26381"
    ],
    "RedisCacheExpireSec": 300
  },

如果是其它库,需要参考官方文档来配置

posted @ 2023-01-12 09:21  李正浩  阅读(138)  评论(0编辑  收藏  举报