搭建 Redis 集群

搭建 Redis 集群

vim docker-compose.yml

Redis Desktop Manager下载

https://www.jianshu.com/p/6895384d2b9e

version: '3.7'
services:
  master:
    image: redis
    container_name: redis-master
    ports:
      - 6379:6379

  slave1:
    image: redis
    container_name: redis-slave-1
    ports:
      - 6380:6379
    command: redis-server --slaveof redis-master 6379

  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
      - 6381:6379
    command: redis-server --slaveof redis-master 6379

搭建 Sentinel 集群

我们至少需要创建三个 Sentinel 服务,docker-compose.yml 配置如下:

version: '3.7'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf

  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    ports:
      - 26380:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf

  sentinel3:
    image: redis
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

修改 Sentinel 配置文件

需要三份 sentinel.conf 配置文件,分别为 sentinel1.confsentinel2.confsentinel3.conf,配置文件内容相同

port 26379
dir /tmp
# 自定义集群名,其中 127.0.0.1 为 redis-master 的 ip,6379 为 redis-master 的端口,2 为最小投票数(因为有 3 台 Sentinel 所以可以设置成 2)
sentinel monitor mymaster 192.168.100.11 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

查看集群是否生效

进入 Sentinel 容器,使用 Sentinel API 查看监控情况:

docker exec -it redis-sentinel-1 /bin/bash
redis-cli -p 26379
sentinel master mymaster
sentinel slaves mymaster

127.0.0.1:26379> sentinel slaves mymaster
1)  1) "name"
    2) "172.19.0.3:6379"
    3) "ip"
    4) "172.19.0.3"
    5) "port"
    6) "6379"
    7) "runid"
    8) ""
    9) "flags"
   10) "s_down,slave"
   11) "link-pending-commands"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建redis集群,需要进行以下步骤: 1. 下载redis集群源码包并解压 2. 编译redis集群源码包 3. 创建redis集群配置文件 4. 启动redis集群 5. 验证redis集群是否正常工作 下面我们逐一介绍每个步骤的具体操作: 1. 下载redis集群源码包并解压 可以在redis官网上下载redis集群的源码包,下载地址为:https://redis.io/download。下载完成后,解压到指定的目录下。 2. 编译redis集群源码包 进入redis集群源码包所在的目录,使用以下命令编译redis集群: make 3. 创建redis集群配置文件 在redis集群源码包所在的目录下,创建一个名为redis_cluster.conf的配置文件,并添加以下内容: port 7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes 说明: port:redis集群中的一个节点所使用的端口号。 cluster-enabled:是否启用集群模式。 cluster-config-file:集群配置文件的名称。 cluster-node-timeout:如果一个节点在指定的时间内没有响应,那么它就被认为是不可用的。 appendonly:是否启用AOF持久化。 根据实际需要,可以添加更多的配置项。 4. 启动redis集群redis集群源码包所在的目录下,启动redis集群,使用以下命令: ./src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 说明: ./src/redis-trib.rb:redis集群管理工具。 create:创建一个新的集群。 --replicas:指定每个主节点的从节点数量。 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005:指定redis集群中的所有节点。 5. 验证redis集群是否正常工作 使用以下命令验证redis集群是否正常工作: ./src/redis-trib.rb check 127.0.0.1:7000 说明: check:检查集群中的所有节点是否正常工作。 127.0.0.1:7000:指定一个节点进行检查。 如果所有节点都正常工作,那么redis集群搭建成功了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值