• redis 高可用


    机器信息

    主节点 192.168.56.11 Master
    从节点 192.168.56.12 Slave-node1
    从节点 192.168.56.13 Slave-node2

    安装redis

    三台节点安装一样
    1.安装
    [root@linux-node1 tools]# wget http://download.redis.io/redis-stable.tar.gz
    [root@linux-node1 tools]# tar zxvf redis-stable.tar.gz
    [root@linux-node1 tools]# cd redis-stable/
    [root@linux-node1 redis-stable]# make
    [root@linux-node1 redis-stable]# cd src/
    [root@linux-node1 src]# cp redis-server redis-cli redis-check-aof redis-check-rdb redis-sentinel redis-trib.rb /usr/local/bin/

    2.新建目录,存放配置文件
    [root@linux-node1 src]# mkdir /etc/redis
    [root@linux-node1 src]# mkdir /var/redis
    [root@linux-node1 src]# mkdir /var/redis/log
    [root@linux-node1 src]# mkdir /var/redis/run
    [root@linux-node1 src]# mkdir /var/redis/redis

    3.配置模版cp 到指定配置文件
    [root@linux-node1 src]# cd ..
    [root@linux-node1 redis-stable]# ll
    total 304
    -rw-r--r-- 1 elasticsearch elasticsearch 149854 Jan 24 18:16 00-RELEASENOTES
    -rw-r--r-- 1 elasticsearch elasticsearch 53 Jan 24 18:16 BUGS
    -rw-r--r-- 1 elasticsearch elasticsearch 1815 Jan 24 18:16 CONTRIBUTING
    -rw-r--r-- 1 elasticsearch elasticsearch 1487 Jan 24 18:16 COPYING
    drwxr-xr-x 6 elasticsearch elasticsearch 4096 Jan 31 11:06 deps
    -rw-r--r-- 1 elasticsearch elasticsearch 11 Jan 24 18:16 INSTALL
    -rw-r--r-- 1 elasticsearch elasticsearch 151 Jan 24 18:16 Makefile
    -rw-r--r-- 1 elasticsearch elasticsearch 4223 Jan 24 18:16 MANIFESTO
    -rw-r--r-- 1 elasticsearch elasticsearch 20543 Jan 24 18:16 README.md
    -rw-r--r-- 1 elasticsearch elasticsearch 58353 Jan 24 18:16 redis.conf
    -rwxr-xr-x 1 elasticsearch elasticsearch 271 Jan 24 18:16 runtest
    -rwxr-xr-x 1 elasticsearch elasticsearch 280 Jan 24 18:16 runtest-cluster
    -rwxr-xr-x 1 elasticsearch elasticsearch 281 Jan 24 18:16 runtest-sentinel
    -rw-r--r-- 1 elasticsearch elasticsearch 7606 Jan 24 18:16 sentinel.conf
    drwxr-xr-x 3 elasticsearch elasticsearch 8192 Jan 31 11:07 src
    drwxr-xr-x 10 elasticsearch elasticsearch 4096 Jan 24 18:16 tests
    drwxr-xr-x 8 elasticsearch elasticsearch 4096 Jan 24 18:16 utils
    [root@linux-node1 redis-stable]# cp redis.conf /etc/redis/redis.conf

    4.配置启动脚本
    [root@linux-node1 redis-stable]# cd utils/
    [root@linux-node1 utils]# ll
    total 52
    -rw-r--r-- 1 elasticsearch elasticsearch 593 Jan 24 18:16 build-static-symbols.tcl
    -rw-r--r-- 1 elasticsearch elasticsearch 1303 Jan 24 18:16 cluster_fail_time.tcl
    -rw-r--r-- 1 elasticsearch elasticsearch 1070 Jan 24 18:16 corrupt_rdb.c
    drwxr-xr-x 2 elasticsearch elasticsearch 57 Jan 24 18:16 create-cluster
    -rwxr-xr-x 1 elasticsearch elasticsearch 2137 Jan 24 18:16 generate-command-help.rb
    drwxr-xr-x 3 elasticsearch elasticsearch 30 Jan 24 18:16 graphs
    drwxr-xr-x 2 elasticsearch elasticsearch 37 Jan 24 18:16 hashtable
    drwxr-xr-x 2 elasticsearch elasticsearch 67 Jan 24 18:16 hyperloglog
    -rwxr-xr-x 1 elasticsearch elasticsearch 9567 Jan 24 18:16 install_server.sh
    drwxr-xr-x 2 elasticsearch elasticsearch 60 Jan 24 18:16 lru
    -rw-r--r-- 1 elasticsearch elasticsearch 1277 Jan 24 18:16 redis-copy.rb
    -rwxr-xr-x 1 elasticsearch elasticsearch 1098 Jan 24 18:16 redis_init_script
    -rwxr-xr-x 1 elasticsearch elasticsearch 1047 Jan 24 18:16 redis_init_script.tpl
    -rw-r--r-- 1 elasticsearch elasticsearch 1762 Jan 24 18:16 redis-sha1.rb
    drwxr-xr-x 2 elasticsearch elasticsearch 130 Jan 24 18:16 releasetools
    -rwxr-xr-x 1 elasticsearch elasticsearch 3787 Jan 24 18:16 speed-regression.tcl
    -rwxr-xr-x 1 elasticsearch elasticsearch 693 Jan 24 18:16 whatisdoing.sh
    [root@linux-node1 utils]# cp redis_init_script /etc/init.d/redis
    [root@linux-node1 utils]# chmod 755 /etc/init.d/redis

    5.修改脚本pid及conf 路径为实际路径
    [root@linux-node1 utils]# vim /etc/init.d/redis
    1、#!/bin/sh
    2、# Simple Redis init.d script conceived to work on Linux systems
    3、# as it does use of the /proc filesystem.

    4、REDISPORT=6379
    5、EXEC=/usr/local/bin/redis-server
    6、CLIEXEC=/usr/local/bin/redis-cli

    7、PIDFILE=/var/redis/run/redis_${REDISPORT}.pid
    8、CONF="/etc/redis/redis.conf"

    。。。。。。

    6.主从复制配置
    [root@linux-node1 utils]# vim /etc/redis/redis.conf
    .......
    port 6379
    .......
    daemonize yes #这个修改为yes
    .......
    bind 192.168.56.11 #绑定的主机地址。
    .......
    pidfile /var/redis/run/redis_6379.pid
    .......
    logfile /var/redis/log/redis_6379.log
    .......
    dir /var/redis/redis #redis数据目录
    .......
    appendonly yes #启用AOF持久化方式
    appendfilename "appendonly.aof" #AOF文件的名称,默认为appendonly.aof
    appendfsync everysec #每秒钟强制写入磁盘一次,在性能和持久化方面做了很好的折中,是受推荐的方式。
    .....
    save 900 1 #启用RDB快照功能,默认就是启用的
    save 300 10
    save 60 10000 #即在多少秒的时间内,有多少key被改变的数据添加到.rdb文件里
    .......
    dbfilename dump.rdb #快照文件名称
    ......

    slave-node2和slave-node3两个从节点相比于主节点的redis.conf配置,只是多了下面一行配置,其它都一样:
    slaveof 192.168.56.11 6379

    7.启动redis
    [root@linux-node1 utils]# /etc/init.d/redis start
    Starting Redis server...
    [root@linux-node1 utils]# ps -aux|grep redis
    root 112101 0.0 0.3 145248 7552 ? Ssl 11:28 0:00 /usr/local/bin/redis-server 192.168.56.11:6379
    root 112106 0.0 0.0 112644 952 pts/2 R+ 11:28 0:00 grep --color=auto redis
    [root@linux-node1 utils]# lsof -i:6379
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    redis-ser 112101 root 6u IPv4 1560215 0t0 TCP linux-node1:6379 (LISTEN)
    [root@linux-node1 utils]#

    8.登陆主节点查看状态
    [root@linux-node1 utils]# redis-cli -h 192.168.56.11 -p 6379
    192.168.56.11:6379> info replication

    Replication

    role:master
    connected_slaves:2
    slave0:ip=192.168.56.12,port=6379,state=online,offset=322,lag=0
    slave1:ip=192.168.56.13,port=6379,state=online,offset=322,lag=0
    master_replid:65bd9ce01cb8ef098a503f654bb4cc6d9cf58cf1
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:322
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:322
    192.168.56.11:6379>

    9.登陆从节点查看状态
    [root@linux-node2 utils]# redis-cli -h 192.168.56.12 -p 6379
    192.168.56.12:6379> info replication

    Replication

    role:slave
    master_host:192.168.56.11
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:10
    master_sync_in_progress:0
    slave_repl_offset:9100
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:65bd9ce01cb8ef098a503f654bb4cc6d9cf58cf1
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:9100
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:9100

    [root@linux-node3 utils]# redis-cli -h 192.168.56.13 -p 6379
    192.168.56.13:6379> info replication

    Replication

    role:slave
    master_host:192.168.56.11
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:7
    master_sync_in_progress:0
    slave_repl_offset:9184
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:65bd9ce01cb8ef098a503f654bb4cc6d9cf58cf1
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:9184
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:295
    repl_backlog_histlen:8890
    192.168.56.13:6379>

    10.测试数据同步
    1):主节点写入数据
    [root@linux-node1 utils]# redis-cli -h 192.168.56.11 -p 6379
    192.168.56.11:6379> set name ll
    OK
    192.168.56.11:6379> get name
    "ll"
    192.168.56.11:6379>

    2)从节点检查数据是否写入
    [root@linux-node2 utils]# redis-cli -h 192.168.56.12 -p 6379
    192.168.56.12:6379> get name
    "ll"
    192.168.56.12:6379>
    [root@linux-node3 utils]# redis-cli -h 192.168.56.13 -p 6379
    192.168.56.13:6379> get name
    "ll"
    192.168.56.13:6379>

    11.配置Sentinel (三台都要配置)
    [root@linux-node1 ~]# vim /etc/redis/sentinel.conf
    daemonize yes #后台运行
    protected-mode no #sentinel 保护模式关掉
    port 26379 #端口号
    sentinel monitor redis-master 192.168.56.11 6379 2 #sentinel 去监视一个叫redis-master的master,ip为192.168.56.11 端口为6379,2表示2个sentinel检测到master异常才会判定其失效,即只有2个sentinel 都判定master失效才会自动迁移,如果sentinel 不达标,则不会自动故障迁移。

    sentinel down-after-milliseconds redis-master 15000 #指定sentinel判定master断线的时间
    sentinel failover-timeout redis-master 900000 #如果在多少秒内没有把宕机的那台master恢复,那么就认为这是一次真正的宕机。
    sentinel auth-pass redis-master redispass #设置主从服务器进行身份验证的密码
    logfile "/var/redis/log/sentinel.log" #日志路径
    12.启动
    [root@linux-node1 ~]# redis-sentinel /etc/redis/sentinel.conf

    13.检查服务状态
    [root@linux-node1 ~]# redis-cli -p 26379
    127.0.0.1:26379> ping
    PONG
    127.0.0.1:26379> info sentinel

    Sentinel

    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=redis-master,status=ok,address=192.168.56.11:6379,slaves=2,sentinels=4
    127.0.0.1:26379>

    14.测试停掉主节点
    [root@linux-node1 redis]# ps -aux|grep redis
    root 2971 0.0 0.0 107932 188 pts/0 S+ 14:51 0:00 tail -f /var/redis/log/sentinel.log
    root 4292 0.3 0.4 147296 9804 ? Rsl 16:12 0:04 /usr/local/bin/redis-server 192.168.56.11:6379
    root 4445 0.4 0.3 145248 7764 ? Ssl 16:21 0:03 redis-sentinel *:26379 [sentinel]
    root 4956 0.0 0.0 112644 948 pts/2 R+ 16:34 0:00 grep --color=auto redis
    [root@linux-node1 redis]# kill 4292
    [root@linux-node1 redis]# kill 4292
    -bash: kill: (4292) - No such process
    [root@linux-node1 redis]# ps -aux|grep redis
    root 2971 0.0 0.0 107932 188 pts/0 S+ 14:51 0:00 tail -f /var/redis/log/sentinel.log
    root 4445 0.4 0.3 145248 7764 ? Ssl 16:21 0:03 redis-sentinel *:26379 [sentinel]
    root 4959 0.0 0.0 112644 952 pts/2 R+ 16:34 0:00 grep --color=auto redis

    15.查看Sentinel信息
    [root@linux-node1 bin]# redis-cli -p 26379
    127.0.0.1:26379> info sentinel

    Sentinel

    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=redis-master,status=ok,address=192.168.56.13:6379,slaves=2,sentinels=4
    127.0.0.1:26379>

    16.查看从节点信息
    [root@linux-node2 redis]# redis-cli -h 192.168.56.12 -p 6379
    192.168.56.12:6379> info replication

    Replication

    role:slave
    master_host:192.168.56.13
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:0
    master_sync_in_progress:0
    slave_repl_offset:282669
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:8e7d68eeb02372affe3117487ab02dd79edeb452
    master_replid2:d11ebc69f22e7b543c649396d1c687d504c965ac
    master_repl_offset:282669
    second_repl_offset:271992
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:169
    repl_backlog_histlen:282501
    192.168.56.12:6379>

    [root@linux-node3 redis]# redis-cli -h 192.168.56.13 -p 6379
    192.168.56.13:6379> info replication

    Replication

    role:master
    connected_slaves:1
    slave0:ip=192.168.56.12,port=6379,state=online,offset=286612,lag=1
    master_replid:8e7d68eeb02372affe3117487ab02dd79edeb452
    master_replid2:d11ebc69f22e7b543c649396d1c687d504c965ac
    master_repl_offset:286612
    second_repl_offset:271992
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:286612
    192.168.56.13:6379>

    17.测试数据同步
    主节点
    [root@linux-node3 redis]# redis-cli -h 192.168.56.13 -p 6379
    192.168.56.13:6379> info replication

    Replication

    role:master
    connected_slaves:1
    slave0:ip=192.168.56.12,port=6379,state=online,offset=286612,lag=1
    master_replid:8e7d68eeb02372affe3117487ab02dd79edeb452
    master_replid2:d11ebc69f22e7b543c649396d1c687d504c965ac
    master_repl_offset:286612
    second_repl_offset:271992
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:286612
    192.168.56.13:6379> set name lls
    OK
    192.168.56.13:6379> get name
    "lls"
    192.168.56.13:6379>

    从节点
    [root@linux-node2 redis]# redis-cli -h 192.168.56.12 -p 6379
    192.168.56.12:6379> info replication

    Replication

    role:slave
    master_host:192.168.56.13
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:0
    master_sync_in_progress:0
    slave_repl_offset:282669
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:8e7d68eeb02372affe3117487ab02dd79edeb452
    master_replid2:d11ebc69f22e7b543c649396d1c687d504c965ac
    master_repl_offset:282669
    second_repl_offset:271992
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:169
    repl_backlog_histlen:282501
    192.168.56.12:6379> get name
    "lls"
    192.168.56.12:6379>

    查看日志
    [root@linux-node1 ~]# tail -f /var/redis/log/sentinel.log
    2899:X 31 Jan 14:49:31.246 # Redis version=4.0.7, bits=64, commit=00000000, modified=0, pid=2899, just started
    2899:X 31 Jan 14:49:31.246 # Configuration loaded
    2900:X 31 Jan 14:49:31.250 * Running mode=sentinel, port=26379.
    2900:X 31 Jan 14:49:31.251 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    2900:X 31 Jan 14:49:31.252 # Sentinel ID is 9e463b4a274b60575d54922048421d2bc145ec0f
    3322:X 31 Jan 15:09:47.689 # +new-epoch 1
    3322:X 31 Jan 15:09:47.972 * +sentinel sentinel 3fa2e74e1dcc85ee309844e56160f747c0623d88 192.168.56.12 26379 @ redis-master 192.168.56.11 6379
    3739:X 31 Jan 15:34:37.711 # +sdown sentinel 3fa2e74e1dcc85ee309844e56160f747c0623d88 192.168.56.12 26379 @ redis-master 192.168.56.11 6379
    3739:X 31 Jan 15:34:37.711 # +sdown sentinel e76dae936d392ede537557bf10d0e2b20d011285 192.168.56.13 0 @ redis-master 192.168.56.11 6379
    3739:X 31 Jan 15:34:37.711 # +sdown sentinel d99b9551d44054e6f6c09e3a7f6e6ff7ae34fa08 192.168.56.13
    4445:X 31 Jan 16:34:36.305 # +sdown master redis-master 192.168.56.11 6379
    4445:X 31 Jan 16:34:36.414 # +new-epoch 8
    4445:X 31 Jan 16:34:36.416 # +vote-for-leader d99b9551d44054e6f6c09e3a7f6e6ff7ae34fa08 8
    4445:X 31 Jan 16:34:36.698 # +config-update-from sentinel d99b9551d44054e6f6c09e3a7f6e6ff7ae34fa08 192.168.56.13 26379 @ redis-master 192.168.56.11 6379
    4445:X 31 Jan 16:34:36.698 # +switch-master redis-master 192.168.56.11 6379 192.168.56.13 6379
    4445:X 31 Jan 16:34:36.699 * +slave slave 192.168.56.12:6379 192.168.56.12 6379 @ redis-master 192.168.56.13 6379
    4445:X 31 Jan 16:34:36.699 * +slave slave 192.168.56.11:6379 192.168.56.11 6379 @ redis-master 192.168.56.13 6379
    4445:X 31 Jan 16:34:51.759 # +sdown slave 192.168.56.11:6379 192.168.56.11 6379 @ redis-master 192.168.56.13 6379

  • 相关阅读:
    tarjan——强连通分量+缩点
    题解报告——Financial Crisis
    计算几何刷题计划(转)
    [bzoj1042] [HAOI2008]硬币购物
    [bzoj1584] [Usaco2009 Mar]Cleaning Up 打扫卫生
    bzoj 2783 JLOI2012 树
    bzoj 1150 [CTSC2007]数据备份Backup
    bzoj 1079 [SCOI2008]着色方案
    bzoj 2957 楼房重建
    bzoj 3208 花神的秒题计划I
  • 原文地址:https://www.cnblogs.com/fengmeng1030/p/8427965.html
Copyright © 2020-2023  润新知