一、redis-3.28部署

yum install gcc tcl
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
mkdir -p /roobo/server/redis-3.2.8
make install PREFIX=/roobo/server/redis-3.2.8
ln -s /roobo/server/redis-3.2.8 /roobo/server/redis
cd /roobo/server/redis/bin

#启动redis
./redis-server

#!/bin/bash
cd /roobo/server/redis
mkdir -p /roobo/server/redis/6379
mkdir -p /roobo/server/redis/6380
mkdir -p /roobo/server/redis/6381
sleep 5
echo "##############6379配置文件####################"
cat > /roobo/server/redis/6379/6379.conf <<EOF
# Redis使用后台模式
daemonize yes

# 关闭保护模式
protected-mode no

# 注释以下内容开启远程访问
# bind 127.0.0.1

# 修改启动端口为6379
port 6379

# 修改pidfile指向路径
pidfile /roobo/server/redis/6379/redis_6379.pid
EOF
sleep 2
echo "##############6380配置文件####################"
cat > /roobo/server/redis/6380/6380.conf <<EOF
# Redis使用后台模式
daemonize yes

# 关闭保护模式
protected-mode no

# 注释以下内容开启远程访问
# bind 127.0.0.1

# 修改启动端口为6380
port 6380

# 修改pidfile指向路径
pidfile /roobo/server/redis/6380/redis_6380.pid
EOF
sleep 2
echo "##############6381配置文件####################"
cat > /roobo/server/redis/6381/6381.conf <<EOF
# Redis使用后台模式
daemonize yes

# 关闭保护模式
protected-mode no

# 注释以下内容开启远程访问
# bind 127.0.0.1

# 修改启动端口为6381
port 6381

# 修改pidfile指向路径
pidfile /roobo/server/redis/6381/redis_6381.pid
EOF

#启动三个实例
/roobo/server/redis/bin/redis-server /roobo/server/redis/6379/6379.conf
/roobo/server/redis/bin/redis-server /roobo/server/redis/6380/6380.conf
/roobo/server/redis/bin/redis-server /roobo/server/redis/6381/6381.conf

二、主从模式
一主--->二从

介绍在Redis中设置主从有2种方式:
1.在redis.conf中设置slaveof
a) slaveof <masterip> <masterport>

2、 使用redis-cli客户端连接到redis服务,执行slaveof命令
a) slaveof <masterip> <masterport>

我们演示:第2种方式在重启后将失去主从复制关系。

主库 6371
从库 6380,6381

#设置6380和6381为6379的从库
#redis-cli
[root@instance-m4p7hj0t bin]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6380
172.16.0.4:6380> slaveof 172.16.0.4 6379
OK

[root@instance-m4p7hj0t bin]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6381
172.16.0.4:6381> slaveof 172.16.0.4 6379
OK

#查看主从,主库:6379 ,从库:6380,6381
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
172.16.0.4:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.0.4,port=6380,state=online,offset=687,lag=1
slave1:ip=172.16.0.4,port=6381,state=online,offset=687,lag=1
master_repl_offset:687
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:686
172.16.0.4:6379> exit

role:角色信息
slaveX:从库信息
connected_slaves:从库数量

三、测试
在主库写入数据
[root@instance-m4p7hj0t bin]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
172.16.0.4:6379> set nulige 123
OK

在从库读取数据

#6380
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6380
172.16.0.4:6380> get nulige
"123"

#从库6381
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6381
172.16.0.4:6381> get nulige
"123"


一、需求
(1)、模拟6379宕机,把6380切换为主,6381为从
(2)、当6379修复好后,再把6379挂为6380的从库。

1、开始模拟6379宕机
/roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379

#关机(相当于宕机了)
shutdown

#取消主从关系
slave-read-only no

2、把6380切换为主
/roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6380
#把从提升为主
172.16.0.4:6380> slaveof no one
OK
#查看
172.16.0.4:6380> 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

3、设置6381为6380的从
/roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6381
slaveof 172.16.0.4 6380

#再在6380上面查看(备注:现在6380为主,6381就为从)
/roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6380
172.16.0.4:6380> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.16.0.4,port=6381,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
172.16.0.4:6380>

4、6379修好了,再把他加到6380的主下面,变成从库。
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-server /roobo/server/redis/6379/6379.conf
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
172.16.0.4:6379> slaveof 172.16.0.4 6380
OK

5、查看6380上面,现在已经是6380为主,6379和6381为从
/roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6380
172.16.0.4:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.0.4,port=6381,state=online,offset=239,lag=1
slave1:ip=172.16.0.4,port=6379,state=online,offset=239,lag=1
master_repl_offset:239
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:238
172.16.0.4:6380> exit


四、设置从库只读
默认情况下redis数据库充当slave角色时是只读的不能进行写操作
可以在配置文件中开启非只读:
slave-read-only no

五、取消主从关系命令
SLAVEOF NO ONE
注:如果要取消Redis主从关系,可以在对应的从库执行SLAVEOF NO ONE命令,取消主从关系

六、开启无磁盘复制:
配置文件加参数:
repl-diskless-sync yes

七、主从从架构

后补。。。。。

八、redis3.2.8 sentinel模式一主两从高可用环境搭建测试

原理:

例如下图所示:

在Server1 掉线后:

升级Server2 为新的主服务器:

实现功能:
a、6379为主,6380为从库,6381为从库
b、6379为主,宕机自动切换到6381为主,再挂载6380和6379为从库

(备注:因演示环境只有一台机,所以只配置了一个sentinel.conf,一主两从环境,三台都要配置,并改参数为2,才能实现。)

实施步骤如下:
1、6379服务器为主,配置sentinel.conf
sentinel.conf
[root@instance-m4p7hj0t 6379]# pwd
/roobo/server/redis/6379
[root@instance-m4p7hj0t 6379]# ll
总用量 16
-rw-r--r-- 1 root root 253 8月 15 16:44 6379.conf
-rw-r--r-- 1 root root 91 8月 16 09:30 dump.rdb
-rw-r--r-- 1 root root 6 8月 16 09:30 redis_6379.pid
-rw-r--r-- 1 root root 192 8月 16 09:50 sentinel.conf

#sentinel配置文件
[root@instance-m4p7hj0t 6379]# cat sentinel.conf
port 26379
pidfile "/roobo/server/redis/6379/redis-sentinel.pid"
dir "/roobo/server/redis-3.2.8/6379"
daemonize yes
protected-mode no
logfile "/roobo/server/redis/6379/redis-sentinel.log"
#最后一位叁数1表示:sentinel只安装了一台机,多台机设为2
sentinel monitor redisMaster 172.16.0.4 6381 1
sentinel down-after-milliseconds redisMaster 10000
sentinel parallel-syncs redisMaster 1
sentinel failover-timeout redisMaster 60000

2、查看6379配置信息,确认6379为主,其它两节点为从
[root@instance-m4p7hj0t 6379]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
172.16.0.4:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.0.4,port=6381,state=online,offset=11016,lag=1
slave1:ip=172.16.0.4,port=6380,state=online,offset=11016,lag=1
master_repl_offset:11016
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:11015

3、启动6379节点的sentinel服务
[root@instance-m4p7hj0t bin]# /roobo/server/redis/bin/redis-sentinel /roobo/server/redis/6379/sentinel.conf

#切换目录到6379文件夹中
[root@instance-m4p7hj0t 6379]# cd /roobo/server/redis/6379
[root@instance-m4p7hj0t 6379]# ll
总用量 24
-rw-r--r-- 1 root root 253 8月 15 16:44 6379.conf
-rw-r--r-- 1 root root 91 8月 16 10:10 dump.rdb
-rw-r--r-- 1 root root 6 8月 16 09:30 redis_6379.pid
-rw-r--r-- 1 root root 1690 8月 16 10:14 redis-sentinel.log
-rw-r--r-- 1 root root 6 8月 16 10:14 redis-sentinel.pid
-rw-r--r-- 1 root root 611 8月 16 10:14 sentinel.conf

#查看日志
[root@instance-m4p7hj0t 6379]# tail -f redis-sentinel.log
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

28048:X 16 Aug 10:14:00.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
28048:X 16 Aug 10:14:00.628 # Sentinel ID is 9d235e0e09237ec250f33b00bc7369d080c4703a
28048:X 16 Aug 10:14:00.628 # +monitor master redisMaster 172.16.0.4 6379 quorum 1
28048:X 16 Aug 10:14:00.629 * +slave slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:14:00.632 * +slave slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 6379

4、演示6379宕机,6381原来是从库,会自动切换为主库(6381)
[root@instance-m4p7hj0t 6379]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
172.16.0.4:6379>shutdown (模拟宕机)

5、查看6379服务器上面的sentinel日志
[root@instance-m4p7hj0t 6379]# cd /roobo/server/redis/6379
[root@instance-m4p7hj0t 6379]# tail -f redis-sentinel.log
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

28048:X 16 Aug 10:14:00.614 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
28048:X 16 Aug 10:14:00.628 # Sentinel ID is 9d235e0e09237ec250f33b00bc7369d080c4703a
28048:X 16 Aug 10:14:00.628 # +monitor master redisMaster 172.16.0.4 6379 quorum 1
28048:X 16 Aug 10:14:00.629 * +slave slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:14:00.632 * +slave slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.258 # +sdown master redisMaster 172.16.0.4 6379 #显示6379宕机了
28048:X 16 Aug 10:16:58.259 # +odown master redisMaster 172.16.0.4 6379 #quorum 1/1
28048:X 16 Aug 10:16:58.259 # +new-epoch 1
28048:X 16 Aug 10:16:58.260 # +try-failover master redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.263 # +vote-for-leader 9d235e0e09237ec250f33b00bc7369d080c4703a 1
28048:X 16 Aug 10:16:58.263 # +elected-leader master redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.264 # +failover-state-select-slave master redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.326 # +selected-slave slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.326 * +failover-state-send-slaveof-noone slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster4 6379
28048:X 16 Aug 10:16:58.378 * +failover-state-wait-promotion slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster 17279
28048:X 16 Aug 10:16:58.722 # +promoted-slave slave 172.16.0.4:6381 172.16.0.4 6381 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.723 # +failover-state-reconf-slaves master redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:58.795 * +slave-reconf-sent slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:16:59.776 * +slave-reconf-inprog slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 63
28048:X 16 Aug 10:17:00.786 * +slave-reconf-done slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:17:00.886 # +failover-end master redisMaster 172.16.0.4 6379
28048:X 16 Aug 10:17:00.887 # +switch-master redisMaster 172.16.0.4 6379 172.16.0.4 6381 #切换6381为主
28048:X 16 Aug 10:17:00.887 * +slave slave 172.16.0.4:6380 172.16.0.4 6380 @ redisMaster 172.16.0.4 6381
28048:X 16 Aug 10:17:00.887 * +slave slave 172.16.0.4:6379 172.16.0.4 6379 @ redisMaster 172.16.0.4 6381
28048:X 16 Aug 10:17:10.923 # +sdown slave 172.16.0.4:6379 172.16.0.4 6379 @ redisMaster 172.16.0.4 6381

6、如何确认6381为主,并加入6380为从
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6381
172.16.0.4:6381> info replication
# Replication
role:master #当前6381为主
connected_slaves:1
slave0:ip=172.16.0.4,port=6380,state=online,offset=1998,lag=0 #6380为从
master_repl_offset:1998
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1997

7、修改6379,再把他加为6381的从
#启动6379服务
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-server /roobo/server/redis/6379/6379.conf
#登录6379
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6379
#加到6381为从
172.16.0.4:6379> slaveof 172.16.0.4 6381
OK

8、查看6381是否,加入6379和8380为从
[root@instance-m4p7hj0t ~]# /roobo/server/redis/bin/redis-cli -h 172.16.0.4 -p 6381
172.16.0.4:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.0.4,port=6380,state=online,offset=11173,lag=1
slave1:ip=172.16.0.4,port=6379,state=online,offset=11173,lag=1
master_repl_offset:11173
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:11172
172.16.0.4:6381> exit

这样就实现了,6379为主,宕机自动切换到6381为主,再挂载6380和6379为从的全部过程。

参考:
一主多从模式和主从从架构
https://blog.csdn.net/RobertoHuang/article/details/70766809

redis3.2.8 sentinel模式一主两从高可用环境搭建测试
https://blog.csdn.net/busbanana/article/details/71213812

redis集群模式 (集群三主三从模式)

https://blog.csdn.net/varyall/article/details/78856450

redis-3.28 一主二从模式介绍、主从从模式介绍、sentinel模式一主两从高可用的更多相关文章

  1. Redis安装、主从配置及两种高可用集群搭建

    Redis安装.主从配置及两种高可用集群搭建 一.            准备 Kali Linux虚拟机 三台:192.168.154.129.192.168.154.130.192.168.154 ...

  2. Redis系列(二):Redis高可用集群

    一.集群模式 Redis集群是一个由多个主从节点组成的高可用集群,它具有复制.高可用和分片等特性 二.集群部署 1.环境 3台主机分别是: 192.168.160.146 192.168.160.15 ...

  3. Redis(九)高可用专栏之Sentinel模式

    本文讲述Redis高可用方案中的哨兵模式--Sentinel,RedisClient中的Jedis如何使用以及使用原理. Redis主从复制 Redis Sentinel模式 Jedis中的Senti ...

  4. docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】

    一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...

  5. Redis Sentinel(哨兵)主从高可用方案

    环境搭建 三台服务器: 192.168.126.100(master) 192.168.126.110(slaver) 192.168.126.120(slaver) 拷贝192.168.126.10 ...

  6. 想要彻底搞懂大厂是如何实现Redis高可用的?看这篇文章就够了!(1.2W字,建议收藏)

    高可用HA(High Availability)是分布式系统架构设计中必须考虑的因素之一,它通常是指,通过设计减少系统不能提供服务的时间. 假设系统一直能够提供服务,我们说系统的可用性是100%.如果 ...

  7. 近千节点的Redis Cluster高可用集群案例:优酷蓝鲸优化实战(摘自高可用架构)

    (原创)2016-07-26 吴建超 高可用架构导读:Redis Cluster 作者建议的最大集群规模 1,000 节点,目前优酷在蓝鲸项目中管理了超过 700 台节点,积累了 Redis Clus ...

  8. 企业级Nginx负载均衡与keepalived高可用实战(二)keepalived篇

    1.Keepalived高可用软件 1.1.Keepalived介绍 Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实 ...

  9. Redis(九)高可用专栏之《简介篇》

    在互联网的大趋势下,用户体验.服务的可用性日趋重要.任何一个服务的不可用,都可能导致连锁式功能故障. 前言 高可用模型的已经逐渐形成一种套路: 主备/主从模式 集群模式 主备/主从模式 至少有两台服务 ...

随机推荐

  1. 简写代码:当变量为false时['',false,null,undefined,0,NaN]时,返回默认值

    当变量为'',false,null,undefined,0,NaN时,返回默认值 var a='' a || 'hello world'   "hello world" var a ...

  2. Gas Station——又是一道经典问题

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  3. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  4. vue2.0--组件通信(非vuex法)

    写在前面: 1.父组件的data写法与子组件的data写法不同 //父组件 data:{ //对象形式 } //子组件 data:function(){ return { //函数形式 } } 2.引 ...

  5. 可持久化01Trie树【p4735(bzoj3261)】最大异或和

    Description 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的长度\(N ...

  6. 【tomcat】tomcat远程调试

    修改tomcat bin目录下的catalina.sh,增加下面这行: CATALINA_OPTS="-agentlib:jdwp=transport=dt_socket,address=9 ...

  7. Linux基础系列-Day5

    网络管理 ifconfig网络管理工具 ifconfig依赖于命令中使用一些选项属性,不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置,但是通过ifconfig修改的通常为临时配置,即系统 ...

  8. Visual Studio 2017启动x86的Android模拟器失败

     Visual Studio 2017启动x86的Android模拟器失败 Visual Studio 2017默认提供多个Android模拟器.其中,x86模拟器运行较快.但是由于和Hyper-V服 ...

  9. angularjs学习笔记1-angular总体简介及其特点

    以前开发(web或者移动端)前端主要使用jQuery+原生js,如果使用某些前端UI框架的话,它自己还可能提供一些API可以使用.而且目前很多UI框架都是基于jQuery的,所以说一下由jQuery跨 ...

  10. Hive 空指针(NPE)异常

    空指针NullPointerException 1 Hive之前的一些BUG [HIVE-9430] - NullPointerException on ALTER TABLE ADD PARTITI ...