欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

redis主从架构的搭建及添加哨兵

程序员文章站 2022-06-19 10:04:59
1111...

单机版主从架构读写操作问题
redis主从架构的搭建及添加哨兵
主从架构搭建步骤

1,在opt目录下创建一个docker_redis_master_salve文件夹,并创建docker-compose.yml文件,输入配置内容

2,在docker_redis_master_salve文件夹下创建一个conf文件夹

3,在conf文件夹下分别创建redis1.conf,redis2.conf,redis3.conf文件

4,在redis2.conf,redis3.conf文件在分别配置replicaof 192.168.206.140 6379来连接上master主机,并设置相应的端口

5,docker-compose up -d启动容器,如果出现UnixHTTPConnectionPool(host=‘localhost’, port=None): Read timed out. (read timeout=60)的错误,则需要在/etc/profile配置文件中添加如下配置:

export DOCKER_CLIENT_TIMEOUT=120

export COMPOSE_HTTP_TIMEOUT=120

6,通过docker-compose logs -f 查看日志,没问题后进行测试

7,在master主机进行set和get操作,测试是否成功

8,在slave从机测试是否可以获取主机set的数据,再测试slave从机set数据

version: "3.1"
services:
  redis1:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis1
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis1.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
  redis2:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis2
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis2.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
  redis3:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis3
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis3.conf:/usr/local/redis/redis.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
    

redis1主节点的配置

port 6379

redis2从节点配置

replicaof 10.10.10.10  6379
port 6380

redis3从节点配置

replicaof 10.10.10.10   6379
port 6381

加入哨兵机制
哨兵可以帮助我们解决主从架构中的单点故障问题
redis主从架构的搭建及添加哨兵
每个redis块中多加入了一个数据
卷 - ./conf/sentinel.conf:/data/sentinel.conf

version: "3.1"
services:
  redis1:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis1
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis1.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel1.conf:/data/sentinel.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
  redis2:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis2
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis2.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel2.conf:/data/sentinel.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]
  redis3:
    image: daocloud.io/library/redis:5.0.7
    restart: always
    container_name: redis3
    environment:
      - TZ=Asia/Shanghai
    network_mode: host
    volumes:
      - ./conf/redis3.conf:/usr/local/redis/redis.conf
      - ./conf/sentinel3.conf:/data/sentinel.conf
    command: ["redis-server","/usr/local/redis/redis.conf"]

redis1主节点的配置

port 6379

redis2从节点配置

replicaof 10.10.10.10  6379
port 6380

redis3从节点配置

replicaof 10.10.10.10  6379
port 6381

准备哨兵的配置文件,并且在容器内部手动启动哨兵即可
每个配置文件不同,只需要修改ip地址,和指定哨兵的端口号

哨兵需要后台启动cd

daemonize yes

指定Master节点的ip和端口(主),从机台数

sentinel monitor master 10.10.10.10 6379 2

指定哨兵的端口号

port 26379

哨兵每隔多久监听一次redis架构

sentinel down-after-milliseconds master 10000

在Redis容器内部启动sentinel
redis-sentinel在/usr/local/bin中

redis-sentinel sentinel.conf

本文地址:https://blog.csdn.net/weixin_45892531/article/details/107344803

相关标签: 环境搭建