redis_主从配置以及自动切换

3台虚拟机

192.168.1.52

192.168.1.53

192.168.1.54

上来做这个操作:

1.机器上编译安装(3.0.7版本)redis->wget http://download.redis.io/releases/redis-3.0.7.tar.gz

2.配置:

主机:192.168.1.52  从机:192.168.1.53   192.168.1.54

配置文件(redis.conf):

192.168.1.52:

daemonize yes
pidfile "/var/run/redis.pid"
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/home/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/redis-3.0.7"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

从机(redis.conf):192.168.1.53  192.168.1.54

[[email protected] redis-3.0.7]# cat redis.conf | egrep -v "^$|^#"
daemonize yes
pidfile "/var/run/redis.pid"
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/home/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/usr/local/redis-3.0.7"
slaveof 192.168.1.52 6379
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

3.将redis_server跑起来

查看信息:

redis-cli -h 192.168.1.52 -p 6379 info replication

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

redis-cli -h 192.168.1.53 -p 6379 info replication

# Replication
role:slave
master_host:192.168.1.52
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:15
slave_priority:100
slave_read_only:1
connected_slaves:1
slave0:ip=192.168.1.54,port=6379,state=online,offset=1,lag=0
master_repl_offset:1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:0

redis-cli -h 192.168.1.54 -p 6379 info replication

# Replication
role:slave
master_host:192.168.1.52
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:169
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

此时再来看192.168.1.52主从信息

syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 52 info
52
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.53,port=6379,state=online,offset=211,lag=1
slave1:ip=192.168.1.54,port=6379,state=online,offset=211,lag=0
master_repl_offset:211
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:210

3.开启redis-sentinel进行主从监控切换管理(只开启一个实例)

配置

port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.1.52 6379 2
daemonize yes
sentinel down-after-milliseconds mymaster 10000
sentinel config-epoch mymaster 10
sentinel leader-epoch mymaster 10
logfile "/home/log/sen.log"
sentinel known-sentinel mymaster 127.0.0.1 26379 efbd30332f50847e0b0e9c5ab0adc5dfb91823d2
sentinel current-epoch 10

运行后:

3595:X 15 Apr 11:57:29.146 # Sentinel runid is fdc1c1d8a5adf48a5fecc229be42810a61eb8603
3595:X 15 Apr 11:57:29.146 # +monitor master mymaster 192.168.1.52 6379 quorum 2
3595:X 15 Apr 11:57:30.146 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379
3595:X 15 Apr 11:57:30.156 * +slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379
3595:X 15 Apr 11:57:30.871 * -dup-sentinel master mymaster 192.168.1.52 6379 #duplicate of 127.0.0.1:26379 or fdc1c1d8a5adf48a5fecc229be42810a61eb8603
3595:X 15 Apr 11:57:30.871 * +sentinel sentinel 127.0.0.1:26379 127.0.0.1 26379 @ mymaster 192.168.1.52 6379

已经监控到两个slaves

4.测试主从切换

关闭掉主redis: redis-cli -h 192.168.1.52 -p 6379 shutdown

发现54被切换成主redis了,53依然是从redis,不过从属于54了

syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 54 info
54
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.53,port=6379,state=online,offset=179,lag=1
master_repl_offset:179
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:178
syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 53 info
53
# Replication
role:slave
master_host:192.168.1.54
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:320
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
syswjdeMacBook-Air:shell syswj$

查看主从管理日志:

3670:X 15 Apr 12:06:14.376 # +sdown master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.435 # +odown master mymaster 192.168.1.52 6379 #quorum 2/2
3670:X 15 Apr 12:06:14.435 # +new-epoch 12
3670:X 15 Apr 12:06:14.435 # +try-failover master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.444 # +vote-for-leader 96e852d3d56a2dc45b2505e22bd430d1d6c7de13 12
3670:X 15 Apr 12:06:14.447 # 127.0.0.1:26379 voted for 96e852d3d56a2dc45b2505e22bd430d1d6c7de13 12
3670:X 15 Apr 12:06:14.548 # +elected-leader master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.549 # +failover-state-select-slave master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.610 # +selected-slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.610 * +failover-state-send-slaveof-noone slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:14.678 * +failover-state-wait-promotion slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:15.491 # +promoted-slave slave 192.168.1.54:6379 192.168.1.54 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:15.491 # +failover-state-reconf-slaves master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:15.548 * +slave-reconf-sent slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.550 * +slave-reconf-inprog slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.550 * +slave-reconf-done slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.648 # +failover-end master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.648 # +switch-master mymaster 192.168.1.52 6379 192.168.1.54 6379
3670:X 15 Apr 12:06:16.651 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:06:16.652 * +slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:06:26.683 # +sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379

来测试重新开启机器52:

稍等一段世界发现:

52也成为54得slave了

syswjdeMacBook-Air:shell syswj$ ./watch_redis.sh 52 info
52
# Replication
role:slave
master_host:192.168.1.54
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:8252
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

哨兵日志如下:

3670:X 15 Apr 12:06:16.550 * +slave-reconf-done slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.648 # +failover-end master mymaster 192.168.1.52 6379
3670:X 15 Apr 12:06:16.648 # +switch-master mymaster 192.168.1.52 6379 192.168.1.54 6379
3670:X 15 Apr 12:06:16.651 * +slave slave 192.168.1.53:6379 192.168.1.53 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:06:16.652 * +slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:06:26.683 # +sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:07:54.741 # -sdown slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379
3670:X 15 Apr 12:08:04.734 * +convert-to-slave slave 192.168.1.52:6379 192.168.1.52 6379 @ mymaster 192.168.1.54 6379

5自带shell脚本方便操作

#!/bin/bash

set_redis()
{
    result=`redis-cli -h 192.168.1.$1 -p 6379 set $2 $3`
    echo "192.168.1.$1:$result"
}

get_redis()
{
   result=`redis-cli -h 192.168.1.$1 -p 6379 get $2`
    echo "192.168.1.$1:$result"
}

get_info()
{
    echo $1
    redis-cli -h 192.168.1.$1 -p 6379 info replication
}

shut_down()
{
    echo $1
    redis-cli -h 192.168.1.$1 -p 6379 shutdown
}

if [ $# -lt 2 ];
then
    echo "ip op a b"
    exit 0
fi

case $2 in
    set)
        set_redis $1 $3 $4
        ;;
    get)
        get_redis $1 $3
        ;;
    info)
        get_info $1
        ;;
    shut)
        shut_down $1
        ;;
    *)
       echo "invalid parameter!..."
       ;;
esac

时间: 2024-10-25 13:47:34

redis_主从配置以及自动切换的相关文章

DRBD+Pacemaker实现DRBD主从角色的自动切换

DRBD+Pacemaker实现DRBD主从角色的自动切换 前提: drbd设备名,以及drbd设备的挂载点都要与对端节点保持一致: 因为我们 定义资源,使用到设备名及挂载点,所以两端的drbd设备名和设备的挂载点都必须保持一致: 如何定义主从资源? 主从资源是一类特殊的克隆资源: 要成为克隆资源,首先必须定义成主资源; 因此,要想定义成主从资源,首先必须定义成主资源.为保证成为主资源的同时,drdb设备可以同时挂载,还需定义Filesystem clone-max: 在集群中最多能运行多少份克

redis主从配置及手动切换遇到的问题

解析我做redis主从遇到的问题: redis只要下载好压缩包后解压缩然后make就可以直接使用了,启动服务是: /usr/local/src/redis/src/redis-server 然后进入交互界面是: /usr/lcoal/src/redis/src/reids-cli 假如没有更改port那么默认监听6379端口,如果改了,登录时务必在后面-p  端口号.才能登录. 然后如果更改了redis.conf文件那么启动时: /usr/local/src/redis/src/redis-se

html+css+jQuery+JavaScript实现tab自动切换功能

tab1.html内容 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>tab选项卡</title> <style> * { margin: 0; padding: 0; } ul,li { list-style: none; } body { background-color: #323232; font-size: 12px;

redis主从配置及通过keepalived实现redis自动切换高可用

一:环境介绍: Master: 192.168.1.4 Slave: 192.168.1.5 Virtural IP Address (VIP): 192.168.1.253 二:设计思路: 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正时, Slave接管服务,同时关闭主从复制功能: 当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Sl

Redis主从、sentinel故障自动切换

一.什么是redis主从复制? 主从复制,当用户往Master端写入数据时,通过Redis Sync机制将数据文件发送至Slave,Slave也会执行相同的操作确保数据一致:且实现Redis的主从复制非常简单. 二.redis主从复制特点 1.同一个Master可以拥有多个Slaves. 2.Master下的Slave还可以接受同一架构中其它slave的链接与同步请求,实现数据的级联复制,即Master->Slave->Slave模式: 3.Master以非阻塞的方式同步数据至slave,这将

利用Redis Sentinel实现redis主从自动切换

redis主从配置很简单,只需要在slave的配置里加slaveof 192.168.0.100 6379(master的ip和端口) 如果master有密码再设置 masterauth password.主从设置以后要提高可靠性就要用到Sentinel. Sentinel主要作用有 监控.Sentinel不断检查Master和Slave是否工作正常. 通知.Sentinel可以通过API通知系统管理员,另一台计算机程序,受监控的Redis实例有问题. 自动故障切换.如果主机不按预期工作,Sen

Redis集群redis主从自动切换Sentinel(哨兵模式)

Redis SentinelSentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中 一.Sentinel作用:1):Master状态检测 2):如果Master异常,则会进行Master-Slave切换,将其中一个Slave作为Master,将之前的Master作为Slave3):Master-Slave切换后,master_redis.conf.slave_redis.conf和sentinel.conf的内容都会发生改变,即mast

【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

架构简介 前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构.此架构主要是由keepalived实现双机高可用,维护了一个外网VIP,一个内网VIP.正常情况时,外网VIP和内网VIP都绑定在server1服务器,web请求发送到server1的nginx,nginx对于静态资源请求就直接在本机检索并返回,对于php的动态请求,则负载均衡到server1和server2.对于SQ

Mycat在MySQL主从模式(1主1从)下读写分离和及自动切换模式的验证

实验环境 两台Centos7  MySQL5.7.12 IP地址为:192.168.10.36  192.168.10.37 一台Centos7 Mycat IP地址为:192.168.10.31 一:安装mysql,如下图所示(这里采用yum安装): 二:配置MYSQL,以及建立MYSQL主从 1:初始化密码,由于我没在/root目录下找到第一次启动的随机密码,所以我只能用如下操作 vi /etc/my.cnf mysqd 字段添加 #skip-grant-tables 然后重启mysql