Bootstrap

redis 哨兵模式部署

介绍:通过单个服务器部署的伪集群,采用不同的端口号模拟正式环境的多个节点,借助docker-compose快速搭建环境。

一. 三节点哨兵模式(1主2从3哨兵):
节点信息:
redis-master: 172.16.7.108 6379
redis-slave-1: 172.16.7.108 6380
redis-slave-2: 172.16.7.108 6381
redis-sentinel-1: 172.16.7.108 26379
redis-sentinel-2: 172.16.7.108 26380
redis-sentinel-3: 172.16.7.108 26381

二. 搭建流程

  1. 创建redis文件目录
    /opt/redis/redis-master
    /opt/redis/redis-slave-1
    /opt/redis/redis-slave-2
    /opt/redis/redis-sentinel-1
    /opt/redis/redis-sentinel-2
    /opt/redis/redis-sentinel-3

  2. 下载redis.conf配置文件,修改以下配置
    bind 0.0.0.0
    protected-mode no
    #三个结点端口分别改成6379(主) 6380(从) 6381(从)
    port 6379
    dir /data
    logfile “/data/redis.log”

#本机ip或者域名
replica-announce-ip 127.0.0.1
#redis的端口
replica-announce-port 6379

  1. 编写docker-compose.yml 文件
    目录:/opt/redis
version: "3.0"
 
services:           
  redis-master:     
    image: redis
    container_name: redis-master  
    network_mode: host
    restart: always          
    volumes:  
      - /opt/redis/redis-master/redis.conf:/opt/redis/redis.conf
      - /opt/redis/redis-master/data:/data
    command: redis-server /opt/redis/redis.conf
 
  redis-slave-1:
    image: redis
    container_name: redis-slave-1
    network_mode: host
    restart: always
    volumes:
      - /opt/redis/redis-slave-1/redis.conf:/opt/redis/redis.conf
      - /opt/redis/redis-slave-1/data:/data
    command: redis-server /opt/redis/redis.conf
    depends_on:
      - redis-master
 
  redis-slave-2:
    image: redis
    container_name: redis-slave-2
    network_mode: host
    restart: always
    volumes:
      - /opt/redis/redis-slave-2/redis.conf:/opt/redis/redis.conf
      - /opt/redis/redis-slave-2/data:/data
    command: redis-server /opt/redis/redis.conf
    depends_on:
      - redis-master
  1. 编写sentinel.conf配置文件
    其他两个端口分别改成:26380、26381
    #哨兵的端口
    port 26379
    #工作路径
    dir “/data”

指明日志文件名
logfile “/data/sentinel.log”

sentinel monitor mymaster 172.16.7.108 6379 2

#这两项配置非常重要,不同哨兵的节点通信的地址
sentinel announce-ip 127.0.0.1
sentinel announce-port 26379

5.启动容器
docker-compose up -d

6.查看哨兵信息
172.16.7.108-26379:0>info sentinel
"# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.16.7.108:6379,slaves=2,sentinels=3

7.查看redis节点
172.16.7.108-6379:0>info replication
"# Replication
role:master
connected_slaves:1
slave0:ip=172.16.7.108,port=6380,state=online,offset=846020,lag=0
master_failover_state:no-failover
master_replid:8e3d157744b710851666d0d5f25aa41026327afe
master_replid2:fcb7d7425c3f8f3182257829f96560c9c97d3c8c
master_repl_offset:846156
second_repl_offset:456245
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:456245
repl_backlog_histlen:389912

三. 测试
1.查看redis进程
[xiaoi@xiaoi-07108 ~]$ ps aux|grep redis
root 1209 0.3 0.0 54852 13000 ? Ssl 14:22 0:07 redis-sentinel *:26381 [sentine

;