• Redis集群搭建


    一.Redis集群搭建的前提
    首先,在centos7上安装Redis和Docker
    1)Redis安装教程
    2) 安装好docker和docker-compose
    (1)Docker安装教程
    (2)Docker-Compose安装教程
    3)启动Docker

    # 查看docker服务是否启动
    sudo systemctl status docker
    # 如果没有启动
    sudo systemctl start docker
    
    • 1
    • 2
    • 3
    • 4

    4)启动Redis(参考上面的Redis安装教程)
    二.Redis集群搭建
    Redis集群的哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
    哨兵模式作用:

    • 通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。
    • 当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。

    在这里插入图片描述
    除了监控Redis服务之外,哨兵之间也会互相监控。本文采用一主、双从、三哨兵方式
    在这里插入图片描述
    部署方式为:docker compose:

    第一步:创建redis docker-compose.yml配置文件 目录,配置文件可根据需要调整

    cd  #首先进入根目录下/root
    mkdir redis
    vi docker-compose.yml
    
    • 1
    • 2
    • 3

    docker-compose.yml内容如下:

    version: '3.4'
    services:
      master:
        image: redis
        container_name: redis-master
        restart: always
        command: redis-server --port 16380 --requirepass 123456   # 16380 是定义的主库端口,默认:6379;  --requirepass 123456 是redis密码。
        ports:
          - 16380:16380   # 将容器的16380端口映射到宿主机的16380端口上,第一个16380为宿主机端口。
    
      slave1:
        image: redis
        container_name: redis-slave-1
        restart: always
        command: redis-server --slaveof 127.0.0.1 16380 --port 16381 --requirepass 123456 --masterauth 123456   # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16381 是定义的从库端口,默认:6379;  --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
        ports:
          - 16381:16381
    
    
      slave2:
        image: redis
        container_name: redis-slave-2
        restart: always
        command: redis-server --slaveof 127.0.0.1 16380 --port 16382 --requirepass 123456 --masterauth 123456   # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16382 是定义的从库端口,默认:6379;  --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
        ports:
          - 16382:16382
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    第二步:执行启动命令
    在当前目录下执行启动命令

    docker-compose -f docker-compose.yml up -d
    
    • 1

    在这里插入图片描述
    第三步:创建sentinel docker-compose.yml配置文件

    cd  #进入根目录下/root
    mkdir sentinel
    vi docker-compose.yml
    
    • 1
    • 2
    • 3

    docker-compose.yml 文件内容如下:

    version: '3.4'
    services:
      sentinel1:
        image: redis
        container_name: redis-sentinel-1
        command: redis-sentinel /root/sentinel/sentinel1.conf # 自定义路径,可更改,但是需要和volumes中的路径相同。
        restart: always
        ports:
          - 26380:26380
        volumes:
          - ./sentinel1.conf:/root/sentinel/sentinel1.conf # 自定义路径,可更改,但是需要和command中的路径相同。
    
      sentinel2:
        image: redis
        container_name: redis-sentinel-2
        command: redis-sentinel /root/sentinel/sentinel2.conf
        restart: always
        ports:
          - 26381:26381
        volumes:
          - ./sentinel2.conf:/root/sentinel/sentinel2.conf
    
      sentinel3:
        image: redis
        container_name: redis-sentinel-3
        command: redis-sentinel /root/sentinel/sentinel3.conf
        restart: always
        ports:
          - 26382:26382
        volumes:
          - ./sentinel3.conf:/root/sentinel/sentinel3.conf
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    sentinel1.conf

    port 26380
    daemonize no
    pidfile /var/run/redis-sentinel.pid
    dir /tmp
    sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
    sentinel auth-pass mymaster 123456 # 主机密码
    sentinel down-after-milliseconds mymaster 30000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    sentinel2.conf

    port 26381
    daemonize no
    pidfile /var/run/redis-sentinel.pid
    dir /tmp
    sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
    sentinel auth-pass mymaster 123456 # 主机密码
    sentinel down-after-milliseconds mymaster 30000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    sentinel3.conf

    port 26382
    daemonize no
    pidfile /var/run/redis-sentinel.pid
    dir /tmp
    sentinel monitor mymaster 127.0.0.1 16380 2  # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
    sentinel auth-pass mymaster 123456 # 主机密码
    sentinel down-after-milliseconds mymaster 30000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    sentinel deny-scripts-reconfig yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    第四步:执行启动命令
    在当前目录下执行启动命令

    docker-compose -f docker-compose.yml up -d
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    络达开发---自定义BLE服务(一):相关数据结构讲解
    计算机毕业设计ssm电商后台管理系统tgm41系统+程序+源码+lw+远程部署
    Mac:如何配置java和maven环境变量
    SpringBoot基础篇学习笔记
    梳理Langchain-Chatchat-UI接口文档
    Spark数据倾斜
    git初步使用
    OAuth 2.1 框架
    【java进阶03: package和import】及访问控制权限
    大数据Flink(七十九):SQL 的容错(Checkpoint)
  • 原文地址:https://blog.csdn.net/qq_42393720/article/details/125915501