• Redis docker 主从模式与哨兵sentinel


    更多技术记录,请参考软件开发 | 编程 | RustFisher

    为实现redis的高可用,我们采用主从模式加哨兵的方法。

    一主二从三哨兵,共启动6个redis容器。本文示例在同一个服务器上进行操作。

    开发环境

    • centos 假设ip地址为 x.x.x.1
    • docker 1.13.1
    • redis 7.0.2

    系统为centos

    cat /proc/version
    Linux version 3.10.0-693.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) ) #1 SMP Tue Aug 22 21:09:27 UTC 2017
    

    后面的操作将在root权限下进行

    docker版本

    docker -v 
    Docker version 1.13.1, build 7d71120/1.13.1
    

    redis版本

    我们使用7.0.2版本。用到的服务器上的redis最好统一版本。

    docker pull redis:7.0.2
    

    不同版本的redis的配置不一定相同。如果启动容器出现一直在restarting的情况,去看一下log

    查看已经启动的redis容器中的redis版本

    docker exec -it [容器id] redis-server -v
    
    Redis server v=7.0.2 sha=00000000:0 malloc=jemalloc-5.2.1 bits=64 build=40f017f9608e455e
    

    来官网找对应的安装包 http://download.redis.io/releases/

    解压后可以得到redis.confsentinel.conf文件

    主从结构

    一个主redis,2个从redis。它们使用不同的3个端口,注意检查防火墙的设置。

    本文假设服务器的ip为x.x.x.1

    启动主redis

    主redis,即master。

    启动主redis容器

    docker run --restart=always -p 6400:6379 --name redis-CNT-MASTER \
    -d redis:7.0.2 redis-server --requirepass 778899 --masterauth 778899
    
    • 6400:6379 指定服务器的6400对应redis容器里的6379端口
    • --requirepass 778899 设定密码
    • --masterauth 778899 从redis连上来需要的密码

    进入容器查看状态

    docker exec -it redis-CNT-MASTER redis-cli
    127.0.0.1:6379> auth 778899
    OK
    127.0.0.1:6379> info replication
    

    启动1号从redis

    从redis使用配置的方式

    文件结构 /home/dapp/projects/rustfisher/redis-slave1

    ├── data
    └── redis.conf
    

    1号从redis的redis.conf除掉注释后的部分

    #bind 0.0.0.0 # 不用bind
    
    slaveof x.x.x.1 6400 # 记得改成你的服务器ip
    replica-announce-ip x.x.x.1 # 记得改成你的服务器ip
    replica-announce-port 6401 # 从redis对外的端口,后面启动的时候也要配置的
    protected-mode no
    port 6379
    masterauth 778899
    requirepass 778899
    
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    pidfile /var/run/redis_6379.pid
    loglevel notice
    logfile ""
    databases 16
    always-show-logo no
    set-proc-title yes
    proc-title-template "{title} {listen-addr} {server-mode}"
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    rdb-del-sync-files no
    dir ./
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync yes
    repl-diskless-sync-delay 5
    repl-diskless-sync-max-replicas 0
    repl-diskless-load disabled
    repl-disable-tcp-nodelay no
    replica-priority 100
    acllog-max-len 128
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    lazyfree-lazy-user-del no
    lazyfree-lazy-user-flush no
    oom-score-adj no
    oom-score-adj-values 0 200 800
    disable-thp yes
    appendonly yes
    appendfilename "appendonly.aof"
    appenddirname "appendonlydir"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    aof-timestamp-enabled no
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-listpack-entries 512
    hash-max-listpack-value 64
    list-max-listpack-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-listpack-entries 128
    zset-max-listpack-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
    jemalloc-bg-thread yes
    

    启动1号从redis

    docker run --restart=always -p 6401:6379 --name redis-CNT-S1 \
    -v /home/dapp/projects/rustfisher/redis-slave1/redis.conf:/etc/redis/redis.conf \
    -v /home/dapp/projects/rustfisher/redis-slave1/data:/data \
    -d redis:7.0.2 redis-server /etc/redis/redis.conf
    
    • 6401:6379 1号从redis对外端口是6401
    • redis-CNT-S1 容器名字

    启动2号从redis

    配置文件可以复制1号的。然后记得修改它的宣称ip和端口

    replica-announce-ip x.x.x.1
    replica-announce-port 6402 # 2号用的端口
    

    启动2号从redis,需要注意对应的路径

    docker run --restart=always -p 6402:6379 --name redis-CNT-S2 \
    -v /home/dapp/projects/rustfisher/redis-slave2/redis.conf:/etc/redis/redis.conf \
    -v /home/dapp/projects/rustfisher/redis-slave2/data:/data \
    -d redis:latest redis-server /etc/redis/redis.conf
    

    查看主从信息info replication

    进入主redis容器,输入redis-cliauth后,检查情况info replication

    例如进入主redis容器查看。此时connected_slaves:2。slave的ip和port应该和它们宣称replica-announce的一致。

    在主redis中set a 123,在1号和2号从redis里get a可以看到效果。

    哨兵 sentinel

    我们会启动3个新的redis容器,即3个哨兵。这3个哨兵都监听主redis。

    哨兵1号

    新建一个哨兵1号用的目录 /home/dapp/projects/rustfisher/sentinel1

    ├── conf
    │   └── sentinel.conf
    └── data
    

    先编辑配置文件 sentinel.conf

    port 6411
    dir "/tmp"
    sentinel monitor master001 x.x.x.1 6400 2 # 记得修改成你的主redis的ip和端口
    sentinel auth-pass master001 778899 # 密码是前面定的
    sentinel down-after-milliseconds master001 30000
    sentinel parallel-syncs master001 1
    sentinel failover-timeout master001 180000
    sentinel deny-scripts-reconfig yes
    
    • port 6411 指定的是哨兵容器里自己的端口
    • sentinel monitor 指定了要监听的主master的ip和端口,最后那个2表示需要2个哨兵投票
    • master001 是我们给主redis起的名字,后面都用这个

    启动1号哨兵

    docker run --restart=always -p 6411:6411 --name redis-sentinel-CNT-1 --privileged=true \
    -v /home/dapp/projects/rustfisher/sentinel1/conf:/usr/local/etc/redis/conf/ \
    -d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
    

    注意:这里需要映射的是目录。-v目录对目录。

    进入容器后,可以查看相关信息。redis-cli需要指定端口-p 6411

    root@aa8d208546d1:/data# redis-cli -p 6411
    127.0.0.1:6411> sentinel master master001
    

    查看服务器上1号哨兵的 sentinel.conf ,发现多了一些内容,是redis哨兵写进来的

    # Generated by CONFIG REWRITE
    latency-tracking-info-percentiles 50 99 99.9
    user default on nopass ~* &* +@all
    sentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370
    sentinel config-epoch master001 0
    sentinel leader-epoch master001 0
    sentinel current-epoch 0
    
    sentinel known-replica master001 x.x.x.1 6401
    
    sentinel known-replica master001 x.x.x.1 6402
    

    启动2号哨兵

    配置2号的路径/home/dapp/projects/rustfisher/sentinel2

    sentinel.conf配置内容和前面一样,启动时候端口用-p 6412:6411

    docker run --restart=always -p 6412:6411 --name redis-sentinel-CNT-2 --privileged=true \
    -v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/ \
    -d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
    

    启动3号哨兵

    同理,路径用3号自己的 /home/dapp/projects/rustfisher/sentinel3/conf

    端口-p 6413:6411

    docker run --restart=always -p 6413:6411 --name redis-sentinel-CNT-3 --privileged=true \
    -v /home/dapp/projects/rustfisher/sentinel3/conf:/usr/local/etc/redis/conf/ \
    -d redis:7.0.2 redis-sentinel /usr/local/etc/redis/conf/sentinel.conf
    

    查看哨兵情况

    进入哨兵redis容器查看情况

    root@5dc0468fb71f:/data# redis-cli -p 6411
    127.0.0.1:6411> sentinel master master001
    
    
    • num-slaves 表示从redis的数量
    • num-other-sentinels 表示除自己外的哨兵数量

    哨兵测试

    停掉主redis容器

    docker stop [id]
    

    例如

    docker stop redis-CNT-MASTER
    

    过一会等选出新的主redis。然后再启动刚才停掉的容器redis-CNT-MASTER。查看信息,发现它是role:slave

    [root@rustfisher_test01 redis-slave1]# docker exec -it redis-CNT-MASTER redis-cli -a 778899 info replication
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    # Replication
    role:slave
    master_host:x.x.x.1
    master_port:6402
    master_link_status:up
    master_last_io_seconds_ago:0
    master_sync_in_progress:0
    slave_read_repl_offset:886622
    slave_repl_offset:886622
    slave_priority:100
    slave_read_only:1
    replica_announced:1
    connected_slaves:0
    master_failover_state:no-failover
    master_replid:6e8a8cb6c167abeb743e668b654e2f470a537742
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:886622
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:409622
    repl_backlog_histlen:477001
    

    从上面看出,现在的主redis变成x.x.x.1:6402了。

    哨兵常见问题

    配置文件映射

    指定配置文件sentinel.conf映射到容器内时,直接了使用文件映射。
    这么做有可能导致哨兵没有写入配置文件的权限, 查看log会看到:

    WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy.

    解决方法是使用目录映射。像上面那样:
    -v /home/dapp/projects/rustfisher/sentinel2/conf:/usr/local/etc/redis/conf/

    哨兵myid

    主从与哨兵redis都启动后,看起来OK了。但stop掉主redis后,哨兵并没有选出新的主redis。

    有一种可能是哨兵改写的sentinel.conf里使用了相同的myid

    grep -nr myid
    sentinel1/conf/sentinel.conf:11:sentinel myid b43e361ff80b8f9106cb1d4bb59421aa909ac370
    sentinel2/conf/sentinel.conf:11:sentinel myid e19342addbcdd8d034c1e91ed74ff94a7aec2e2a
    sentinel3/conf/sentinel.conf:11:sentinel myid d0393d72f69556f2047cf8c84cfa20f4df6ed4ff
    

    解决方法是stop掉那个哨兵,删掉myid那行,然后重启哨兵。它会自动生成新的myid

    参考

    本文链接:Redis docker 主从模式与哨兵sentinel

  • 相关阅读:
    Python——方法
    Python——类和对象(二)
    Python——类和对象(一)
    Python——函数的高级应用
    Python——函数入门(三)
    Python——函数入门(二)
    Python——函数入门(一)
    Python——序列封包与序列解包
    min(T)方法
    max(T)方法
  • 原文地址:https://www.cnblogs.com/rustfisher/p/16425561.html
Copyright © 2020-2023  润新知