Redis主从复制

在开始实现redis的高可用之前,首先来学习一下如何实现redis的主从复制,毕竟高可用也会依赖主从复制的技术。

Redis的主从复制,可以实现一个主节点master可以有多个从节点slave节点,一个slave节点也可以作为下面很多从节点的主节点,类似于mysql的级联复制。

Redis的主从复制策略是通过其持久化的rdb文件来实现的,其过程是先dump出rdb文件,将rdb文件传输给slave,然后再将dump后的操作实时同步到slave中。让从服务器(slave server)成为主服务器(master server)的复制品。

Redis主从复制的实现

首先需要准备两台服务器,两台都要安装redis程序

主服务器IP: 10.220.5.137

从服务器IP: 10.220.5.138

第一步:安装redis(主从两端都要操作)

[root@ken html]# yum install redis -y

第二步:修改配置文件(主从两端都要操作)

大约在61行处,修改绑定的IP地址为本机IP

 #
47 # bind 192.168.1.100 10.0.0.1
48 # bind 127.0.0.1 ::1
49 #
50 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
51 # internet, binding to all the interfaces is dangerous and will expose the
52 # instance to everybody on the internet. So by default we uncomment the
53 # following bind directive, that will force Redis to listen only into
54 # the IPv4 lookback interface address (this means Redis will be able to
55 # accept connections only from clients running into the same computer it
56 # is running).
57 #
58 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
59 # JUST COMMENT THE FOLLOWING LINE.
60 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 bind 10.220.5.137
62
63 # Protected mode is a layer of security protection, in order to avoid that
64 # Redis instances left open on the internet are accessed and exploited.
65 #
66 # When protected mode is on and if:
67 #
  # bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 10.220.5.138 # Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.

第三步:重启redis(主从两端都要操作)

[root@ken html]# systemctl restart redis

第四步:登录redis

修改了绑定的地址之后需要指定本机的IP地址才能登录

使用-h指定ip地址,-p指定端口号,如果你的服务器安装了多个实例的话就要用到-p参数了

[root@ken html]# redis-cli -h 10.220.5.138

第五步:实现主从复制

实现主从复制只需要执行命令slaveof后面跟上主机的ip地址加上端口号即可

10.220.5.138:6379> SLAVEOF 10.220.5.137 6739
OK

第六步:查看

在主服务器端查看所有的key

key后面加上*可以看所有的key

key后面指定key名称可以查看指定的key值

10.220.5.137:6379> keys *
1) "gender"
2) "ken"
3) "tel"
4) "ad"
5) "kenken"
6) "name"
7) "myha"
8) "addr"

在从服务器端查看所有的key.可以看到已经复制过来了

10.220.5.138:6379> keys *
1) "name"
2) "ken"
3) "kenken"
4) "addr"
5) "myha"
6) "ad"
7) "tel"
8) "gender"

关闭主从复制:

只需要在从节点执行: slaveof no one

Redis的多实例

顾名思义多实例就是一台服务器上安装多个redis服务器,这样可以更大程度的利用有限资源,在做一些实验的时候也是强烈建议使用多实例的方式,以节省内存消耗。

多实例的启动需要用到redis-server + 配置文件 的方式启动,而不能再使用systemctl restart redis。

另外多实例的登录需要指定-p ip地址,-p指定端口号。

下面就来看一下如何实现多实例吧。

Redis实现多实例

第一步:创建目录

准备在10.220.5.137服务器端创建三个实例6379 6380 6381

创建三个目录主要是存放每个实例的数据,例如配置文件,日志文件,pid文件,rdb文件等

[root@ken ~]# mkdir /redis/{6379,6380,6381} -p

第二步:复制配置文件到三个目录之下

[root@ken ~]# cp /etc/redis.conf /redis/6379/
[root@ken ~]# cp /etc/redis.conf /redis/6380/
[root@ken ~]# cp /etc/redis.conf /redis/6381/

第三步:修改每个目录之下的配置文件

6379端口实例配置,必须修改的地方:

bind监听的地址

端口号

开启后台启动

pid文件保存位置

日志文件保存位置

rbd文件保存位置

[root@ken ~]# egrep -v '^#|^$' /redis/6379/redis.conf
bind 10.220.5.137
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /redis/6379/redis_6379.pid
loglevel notice
logfile /redis/6379/redis.log
...

6380端口实例配置

配置参考上

[root@ken ~]# egrep -v '^#|^$' /redis/6380/redis.conf
bind 10.220.5.137
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /redis/6380/redis_6379.pid
loglevel notice
logfile /redis/6380/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
....

6381端口实例配置

配置参考上

[root@ken ~]# egrep -v '^#|^$' /redis/6381/redis.conf
bind 10.220.5.137
protected-mode yes
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /redis/6381/redis_6379.pid
loglevel notice
logfile /redis/6381/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
....

第四步:启动后台运行

大约在128行处修改为yes,三个配置文件都需要修改

...
# By default Redis does not run as a daemon. Use 'yes' if you need it.
127 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
128 daemonize yes
129
130 # If you run Redis from upstart or systemd, Redis can interact with your
131 # supervision tree. Options:
...

第五步:启动多实例

启动多实例需要使用redis-server后面加上该实例的配置文件即可

使用ss -tnl查看服务是否已经启动

[root@ken ~]# redis-server /redis/6379/redis.conf
[root@ken ~]# redis-server /redis/6380/redis.conf
[root@ken ~]# redis-server /redis/6381/redis.conf
[root@ken ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 10.220.5.137:6379 *:*
LISTEN 0 128 10.220.5.137:6380 *:*
LISTEN 0 128 10.220.5.137:6381 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::10050 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*

至此多实例已经配置成功

Redis的高可用

Redis高可用即哨兵模式,可以实现对主节点及从节点的监控,当节点出现故障的时候可以实现通知的功能,也可以实现故障转移,失败接管,当主节点出现故障的时候,从节点就可以来接替主节点,实现主从节点的切换。这个时候原本是从节点的服务器,就晋升为主节点了。

主节点发生故障,进行了主从的切换之后,如果修复好原来的主节点,再次启用的话,原来的主节点就成为了从节点。

接着上面配置好的多实例,现在我们来实现redis的高可用

主节点:10.220.5.137:6379

从1节点:10.220.5.137:6380

从2节点:10.220.5.138:6381

监控端:10.220.5.138

第一步:实现主从复制

在从1节点使用slaveof作为10.220.5.137的从节点

[root@ken ~]# redis-cli -h 10.220.5.137 -p 6380
10.220.5.137:6380> SLAVEOF 10.220.5.137 6379
OK
10.220.5.137:6380> exit

从2节点执行相同的命令

[root@ken ~]# redis-cli -h 10.220.5.137 -p 6381
10.220.5.137:6381> SLAVEOF 10.220.5.137 6379
OK
10.220.5.137:6381> exit

第二步:在主节点查看

可以使用info replicationc查看主从信息,在主节点进行操作

可以看到如下信息role为master.连接的slave端为2个

[root@ken ~]# redis-cli -h 10.220.5.137 -p 6379
10.220.5.137:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.220.5.137,port=6380,state=online,offset=365,lag=0
slave1:ip=10.220.5.137,port=6381,state=online,offset=365,lag=1
master_repl_offset:365
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:364

第三步:修改监控端的配置文件

需要修改17行关闭保护模式

以及69行处,添加主节点信息即可

sentinel monitor <master-name> <ip> <redis-port> <quorum>

注:quorum指几个主机同意可以故障切换,这里是1表示,只要一个主机同意故障切换就会切换

[root@ken html]# vim /etc/redis-sentinel.conf

15 # bind 127.0.0.1 192.168.1.1
   16 #
   17 protected-mode no
   18 
   19 # port <sentinel-port>
   20 # The port that this sentinel instance will run on
   21 port 26379

...
67 # Note: master name should not include special characters or spaces.
68 # The valid charset is A-z 0-9 and the three characters ".-_".
69 sentinel monitor mymaster 10.220.5.137 6379 1
70
71 # sentinel auth-pass <master-name> <password>
72 #
...

第四步:启动监控节点

sentinel监控的是26379

[root@ken html]# systemctl restart redis-sentinel
[root@ken html]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:26379 *:*
LISTEN 0 128 10.220.5.138:6379 *:*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::10050 :::*
LISTEN 0 128 :::26379 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*

第五步:登录监控端

[root@ken html]# redis-cli -h 10.220.5.138 -p 26379

第六步:查看主服务端信息

现在可以看到主服务器端节点为10.220.5.137,端口号为6379

10.220.5.138:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "10.220.5.137"
5) "port"
6) "6379"
7) "runid"
8) "1096f9bef5606124ddd437f93e3c7d5027061abf"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
...

第七步:查看从服务器点状态

这里会看到所有的从节点信息

10.220.5.138:26379> sentinel slaves mymaster
1) 1) "name"
2) "10.220.5.137:6380"
3) "ip"
4) "10.220.5.137"
5) "port"
6) "6380"
7) "runid"
8) "bc8524006a66bfee2aaa15d3b5615fb0cdb50cb1"
9) "flags"
10) "slave"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "521"
19) "last-ping-reply"
20) "521"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "8370"
25) "role-reported"
...

Redis高可以用测试

第一步:关闭主服务器节点

可以使使用ps aux 结合grep来获取到不同的实例的pid号码

获取到pid号码之后使用kill -9 pid即可关闭该进程

[root@ken ~]# ps aux | grep redis
root 4403 0.2 0.4 142952 2240 ? Rsl 20:50 0:01 redis-server 10.220.5.137:6379
root 4408 0.1 0.4 142952 2248 ? Ssl 20:50 0:01 redis-server 10.220.5.137:6380
root 4413 0.1 0.4 142952 2248 ? Ssl 20:50 0:01 redis-server 10.220.5.137:6381
root 4457 0.0 0.1 112704 968 pts/0 S+ 21:04 0:00 grep --color=auto redis
[root@ken ~]# kill -9 4403
[root@ken ~]# ps aux | grep redis
root 4408 0.1 0.4 142952 2264 ? Ssl 20:50 0:01 redis-server 10.220.5.137:6380
root 4413 0.1 0.4 142952 2264 ? Ssl 20:50 0:01 redis-server 10.220.5.137:6381
root 4459 0.0 0.1 112704 964 pts/0 R+ 21:04 0:00 grep --color=auto redis

第二步:在监控节点查看主服务器端节点状态.

可以看到现在的主服务器节点已经变成10.220.5.137端口号为6380

注意:现在再把之前的主节点重启启动的话,之前的主节点会变成现在的主节点的从节点,即之前的主机点故障之后再启动已经变成从节点了。

10.220.5.138:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "10.220.5.137"
5) "port"
6) "6380"
7) "runid"
8) "bc8524006a66bfee2aaa15d3b5615fb0cdb50cb1"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "777"
19) "last-ping-reply"
20) "777"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"

Redis主从复制、多实例、高可用的更多相关文章

  1. redis主从复制与哨兵高可用

    redis主从复制 话不多说,直接看案例: 环境准备, 主从规划 主节点:6380 从节点:6381.6382 运行3个redis数据库,达到 1主 2从的配置 #主库 6379.conf port ...

  2. Dubbo入门到精通学习笔记(十五):Redis集群的安装(Redis3+CentOS)、Redis集群的高可用测试(含Jedis客户端的使用)、Redis集群的扩展测试

    文章目录 Redis集群的安装(Redis3+CentOS) 参考文档 Redis 集群介绍.特性.规范等(可看提供的参考文档+视频解说) Redis 集群的安装(Redis3.0.3 + CentO ...

  3. Redis从出门到高可用--Redis复制原理与优化

    Redis从出门到高可用–Redis复制原理与优化 单机有什么问题? 1.单机故障; 2.单机容量有瓶颈 3.单机有QPS瓶颈 主从复制:主机数据更新后根据配置和策略,自动同步到备机的master/s ...

  4. net core 实战之 redis 负载均衡和"高可用"实现

    net core 实战之 redis 负载均衡和"高可用"实现 1.概述 分布式系统缓存已经变得不可或缺,本文主要阐述如何实现redis主从复制集群的负载均衡,以及 redis的& ...

  5. Redis系列4:高可用之Sentinel(哨兵模式)

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 1 背景 从第三篇 Redis系列3:高可用之主从架构 ,我们知道,为Re ...

  6. 一站式学习Redis 从入门到高可用分布式实践

    1:redis 是用c语言来实现的,速度快 持久化 单线程 复杂的数据类型有bitmap和hyperloglog和geo地理信息2:高可用.分布式 v2.8开始支持Redis-Sentinel(哨兵) ...

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

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

  8. Redis系列3:高可用之主从架构

    Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 1 主从复制介绍 上一篇<Redis系列2:数据持久化提高可用性>中,我们介绍了Redis中的数据 ...

  9. asp.net core 实战之 redis 负载均衡和"高可用"实现

    1.概述 分布式系统缓存已经变得不可或缺,本文主要阐述如何实现redis主从复制集群的负载均衡,以及 redis的"高可用"实现, 呵呵双引号的"高可用"并不是 ...

  10. Redis系列(四)-低成本高可用方案设计

    关于Redis高可用方案,看到较多的是keepalived.zookeeper方案. keepalived是主备模式,意味着总有一台浪费着.zookeeper工作量成本偏高. 本文主要介绍下使用官方s ...

随机推荐

  1. python 在目标位置建立文件夹

    import os path = r'D:\pywork\12' # 指定位置 if not os.path.exists(path + '/' + '任务集'): #如果目标位置 不存在该文件夹“任 ...

  2. SpringSecurity权限管理系统实战—二、日志、接口文档等实现

    系列目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战 ...

  3. 【支付宝SDK】沙箱调试,以及遇到的坑

    from rest_framework.views import APIView from alipay import AliPay, DCAliPay, ISVAliPay from django. ...

  4. FIRST集合、FOLLOW集合及LL(1)文法求法

    FIRST集合 定义 可从α推导得到的串的首符号的集合,其中α是任意的文法符号串. 规则 计算文法符号 X 的 FIRST(X),不断运用以下规则直到没有新终结符号或 ε可以被加入为止 : (1)如果 ...

  5. Flutter 状态管理之BLoC

    在正式介绍 BLoC之前, 为什么我们需要状态管理.如果你已经对此十分清楚,那么建议直接跳过这一节.如果我们的应用足够简单,Flutter 作为一个声明式框架,你或许只需要将 数据 映射成 视图 就可 ...

  6. keepalived的工作原理解析以及安装使用

    一.keepalived keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障. keepalived官网http://www.keepali ...

  7. 团队作业4:第四篇Scrum冲刺博客(歪瑞古德小队)

    目录 一.Daily Scrum Meeting 1.1 会议照片 1.2 项目进展 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3.2 Code Review 记录 3.3 issue ...

  8. Docker 最常用的镜像命令和容器命令

    本文列出了 Docker 使用过程中最常用的镜像命令和容器命令,以及教大家如何操作容器数据卷,实现容器数据的备份.熟练练习这些命令以后,再来一些简单的应用部署练习,大家就可以学习 Docker 的镜像 ...

  9. os.path获取当前路径及父路径

    import os pwd = os.getcwd() print("当前目录: " + pwd) father_path_method1 = os.path.dirname(pw ...

  10. typedef的陷阱

    typedef定义了一种类型的新别名,不同于宏,它不是简单的字符串替换.比如: 先定义: typedef char* PSTR; 然后: int mystrcmp(const PSTR, const ...