Redis

Redis 高级部分

一、主从复制
在这里插入图片描述

Redis的主从复制特点
1、一个master可以有多个slave
2、一个slave只能有一个master
3、数据流向是单向的,master到slave

1、配置主从

实现方式同样有两种:命令方式和配置文件方式

命令方式

只需要在从服务器上执行如下命令:

1 | slaveof 主服务器的IP   端口号

slaveof 命令是异步的,不阻塞。
并且此时,从服务器现有的数据会先被清空,之后再同步主服务器的数据。

停止一台从服务器的复制操作,在此台服务器上执行如下命令:

1 | slaveof no one

配置文件的方式如下
只需要在从服务器上配置即可
修改配置文件
假如主服务器的ip是:192.168.64.150
端口是:6379

1 | slaveof  192.168.64.150
2 |
3 | //此服务器只提供读取操作
4 | slave-read-only yes

重新启动从主机的Redis 服务
查看主从信息

1 | 127.0.0.1:6379> info  replication

二、主从+ Sentinel 哨兵模式

Redis Sentinel是Redis官方的高可用性解决方案。

Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
1、
监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
2、
提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
3、
自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将主从复制组合中的其中一个从服务器升级为新的主服务器, 并让其他从服务器指向新的主服务器; 当客户端试图连接失效的主服务器时, Sentinel也会向客户端返回新主服务器的地址, 使得新主服务器代替失效服务器。

Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。

虽然 Redis 为 Sentinel 生成一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器。

此种模式下,客户端要访问的 服务 IP 不是主节点,而是 sentiner 服务器的 IP。

架构图
在这里插入图片描述
Redis Sentinel 故障转移
在这里插入图片描述
架构的扩展应用
在这里插入图片描述

1.配置主从

a. 首先创建一个目录

1 | mkdir -p /etc/redis/

b.将源文件copy一份去上面的目录下,修改文件名为redis-6380.conf

1 | cp /usr/local/redis-4.0.10/redis.conf /etc/redis/
2 | mv redis.conf  redis-6380.conf

c.可以将redis-6380.conf里的文件内容全部删除

编译全新文件/etc/redis/redis-6380.conf, 添加如下内容

port 6380
daemonize yes
protected-mode no
pidfile /var/run/redis-6380.pid
logfile /var/log/redis-6380.log
dir /redis/data/    #此目录需要手动创建

假如是多个主机实现的,就需要更改为 protected-mode yes,
并且添加 bind 0.0.0.0

d.快速生成从节点的配置文件

1 |  sed 's/6380/6381/g' /etc/redis/redis6380.conf/etc/redis/redis-6381.conf
2 |  sed 's/6380/6382/g' /etc/redis/redis-6380.conf > /etc/redis/redis-6382.conf

查看配置文件内容,检验配置结果

[root@ela2 ~]# cat /etc/redis/redis-6381.conf
port 6381
daemonize yes
pidfile /var/run/redis-6381.pid
logfile /var/log/redis-6381.log
dir /redis/data/

[root@ela2 ~]# cat /etc/redis/redis-6382.conf
port 6382
daemonize yes
pidfile /var/run/redis-6382.pid
logfile /var/log/redis-6382.log
dir /redis/data/

e.配置主从关系

[root@ela2 ~]# echo "slaveof  192.168.64.150 6380" >> /etc/redis/redis-6381.conf
[root@ela2 ~]# echo "slaveof  192.168.64.150 6380" >> /etc/redis/redis-6382.conf

f.启动服务,并验证进程

[root@ela2 ~]# /usr/local/bin/redis-server /etc/redis/redis-6380.conf
[root@ela2 ~]# /usr/local/bin/redis-server /etc/redis/redis-6381.conf
[root@ela2 ~]# /usr/local/bin/redis-server /etc/redis/redis-6382.conf
[root@ela2 ~]# ps -ef |grep redis
root       4335      1  0 19:30 ?        00:00:03 /usr/local/bin/redis-server *:6380
root       4490      1  0 20:17 ?        00:00:00 /usr/local/bin/redis-server *:6381
root       4495      1  0 20:17 ?        00:00:00 /usr/local/bin/redis-server *:6382
root       4500   3755  0 20:17 pts/0    00:00:00 grep --color=auto redis

假如日志中出现如下警告信息

1 | 4668:S 17 Feb 20:28:42.107 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2 | 4668:S 17 Feb 20:28:42.107 # Server initialized
3 | 4668:S 17 Feb 20:28:42.108 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4 |4668:S 17 Feb 20:28:42.108 * DB loaded from disk: 0.000 seconds
5 | 4668:S 17 Feb 20:28:42.110 * Before turning into a slave, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.

解决方法

The TCP backlog…

方法1: 永久生效:

修改/etc/sysctl.conf文件,增加一行

1 | net.core.somaxconn=1024
2 | vm.overcommit_memory=1

执行此命令让配置生效

1 | sysctl -p

G.查看主从复制信息

在这里插入图片描述

2.配置Sentinel 获取程序

Sentinel 程序可以在编译后的 src 文档中发现, 它是一个命名为 redis-sentinel 的程序。

运行一个 Sentinel 所需的最少配置如下所示:

Redis 源码中包含了一个名为 sentinel.conf 的文件, 这个文件是一个带有详细注释的 Sentinel 配置文件示例。

运行一个 Sentinel 所需的最少配置如下所示:

// 监控一个 Redis 服务器
// 名称为 mymaster ,IP 为 127.0.0.1 端口为 6380
// 最后的 2  是指最少有 2 给 Sentinel 实例同意一台 redis 服务器宕机,才会认为 客观下线。
// sentinel monitor  自定义的主节点名称 主节点的 IP  主节点端口   票数 

sentinel monitor mymaster 127.0.0.1 6380 2

sentinel down-after-milliseconds mymaster 3000

// 180 秒后开始故障自动装换
sentinel failover-timeout mymaster 5000

sentinel parallel-syncs mymaster 1

各个选项的功能如下:

down-after-milliseconds 选项指定了 Sentinel 认为服务器已经断线所需的毫秒数。
如果服务器在给定的毫秒数之内, 没有返回 Sentinel 发送的 PING 命令的回复, 或者返回一个错误, 那么 Sentinel 将这个服务器标记为主观下线(subjectively down,简称 SDOWN )。

不过只有一个 Sentinel 将服务器标记为主观下线并不一定会引起服务器的自动故障迁移: 只有在足够数量的 Sentinel 都将一个服务器标记为主观下线之后, 服务器才会被标记为客观下线(objectively down, 简称 ODOWN ), 这时自动故障迁移才会执行。

将服务器标记为客观下线所需的 Sentinel 数量由对主服务器的配置决定。

parallel-syncs 选项指定了在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步, 这个数字越小, 完成故障转移所需的时间就越长。

a.在您安装redis的目录下有sentinel.conf文件

在这里插入图片描述

b.在上面这个目录下将sentinel.conf文件copy一份去/etc/redis/目录下

1 | cp sentinel.conf /etc/redis/sentinel-27000.conf

c.修改/etc/redis/sentinel-27000.conf的文件内容

daemonize yes
port 27000
dir "/tmp"
logfile "27000.log"

sentinel monitor mymaster 127.0.0.1 6380 2 #或者将ip改为服务器ip
sentinel down-after-milliseconds mymaster 3000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1

哨兵的领导者选举

票数和领导者选举有关系

领导者选举的事件发生,必须满足下面的条件

max(票数, (哨兵的个数 / 2) + 1 ) 个哨兵参加选举

才可以选举出领导者,从而完成故障转移。

比如有 8 个哨兵, 配置的票数是 6

max( 6 , (8 / 2) + 1)

max(6, 5)
6 最大
结果就是需要 6 个哨兵参与选举才可以。

a. 获取并修改配置文件

快速创建三个 sentinel 配置文件
进入到 Redis 源码的目录下,执行如下命令

1 | [root@ela2 redis-4.0.10]# grep -E -v '^#|^$' sentinel.conf > /etc/redis/sentinel-27000.conf
2 | [root@ela2 redis-4.0.10]# grep -E -v '^#|^$' sentinel.conf > /etc/redis/sentinel-27001.conf
3 | [root@ela2 redis-4.0.10]# grep -E -v '^#|^$' sentinel.conf > /etc/redis/sentinel-27002.conf

修改监听端口

1 | [root@ela2 redis-4.0.10]# sed -i 's/26379/27000/g' /etc/redis/sentinel-27000.conf
2 | [root@ela2 redis-4.0.10]# sed -i 's/26379/27001/g' /etc/redis/sentinel-27001.conf
3 | [root@ela2 redis-4.0.10]# sed -i 's/26379/27002/g' /etc/redis/sentinel-27002.conf

27000、27001、27002修改dir 配置项的目录

daemonize yes
dir /redis/data/
logfile  "/var/log/27000.log"

daemonize yes
dir /redis/data/
logfile  "/var/log/27001.log"

daemonize yes
dir /redis/data/
logfile  "/var/log/27002.log"

修改监控的主服务的IP和端口 6380
查看其中的一个配置文件

 [root@ela2 redis-4.0.10]# cat /etc/redis/sentinel-27000.conf
 daemonize yes
 port 27000
 dir "/tmp"
 logfile "/var/log/27000.log"
 sentinel myid eb1742c1e29f083cd749d249f6c1a8f3bb31128f
 sentinel monitor mymaster 192.168.64.133 6380 2
 sentinel down-after-milliseconds mymaster 3000
 sentinel failover-timeout mymaster 5000

b. 启动服务并验证

启动服务的语法:

1 | redis-sentinel   sentinel的配置文件
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231182655721.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjU0NTgzMQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231182744445.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjU0NTgzMQ==,size_16,color_FFFFFF,t_70#pic_center)
可以使用以下命令查看哨兵的信息
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201231182946460.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjU0NTgzMQ==,size_16,color_FFFFFF,t_70#pic_center)
## 3.故障演练
停止Master 节点的服务

```go
1 | [root@ela2 ~]#  redis-cli -p 6380 shutdown

不断的刷新其中一个 Sentinel 节点的信息,观察最后一行信息的变化
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值