Redis主从复制、多实例、高可用(三)--技术流ken

时间:2023-03-09 00:18:04
Redis主从复制、多实例、高可用(三)--技术流ken

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"
...