Redis主从复制多实例及其高可用--技术流ken
Posted kenken2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis主从复制多实例及其高可用--技术流ken相关的知识,希望对你有一定的参考价值。
Redis主从复制
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(主从两端都要操作)
[[email protected] html]# yum install redis -y
第二步:启动redis(主从两端都要操作)
[[email protected] html]# systemctl restart redis
第三步:修改配置文件(主从两端都要操作)
大约在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(主从两端都要操作)
[[email protected] html]# systemctl restart redis
第五步:登录redis
修改了绑定的地址之后需要指定本机的IP地址才能登录
[[email protected] html]# redis-cli -h 10.220.5.138
第六步:实现主从复制
10.220.5.138:6379> SLAVEOF 10.220.5.137 6739 OK
第七步:查看
在主服务器端查看所有的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"
Redis的多实例
顾名思义多实例就是一台服务器上安装多个redis服务器,这样可以更大程度的利用有限资源,在做一些实验的时候也是强烈建议使用多实例的方式,以节省内存消耗。下面就来看一下如何实现多实例吧。
Redis实现多实例
第一步:创建目录
准备在10.220.5.137服务器端创建三个实例6379 6380 6381
[[email protected] ~]# mkdir /redis/{6379,6380,6381} -p
第二步:复制配置文件到三个目录之下
[[email protected] ~]# cp /etc/redis.conf /redis/6379/ [[email protected] ~]# cp /etc/redis.conf /redis/6380/ [[email protected] ~]# cp /etc/redis.conf /redis/6381/
第三步:修改每个目录之下的配置文件
6379端口实例配置
[[email protected] ~]# 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 no supervised no pidfile /redis/6379/redis_6379.pid loglevel notice logfile /redis/6379/redis.log
...
6380端口实例配置
[[email protected] ~]# 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 no 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端口实例配置
[[email protected] ~]# 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 no 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:
...
第五步:启动多实例
[[email protected] ~]# redis-server /redis/6379/redis.conf [[email protected] ~]# redis-server /redis/6380/redis.conf [[email protected] ~]# redis-server /redis/6381/redis.conf [[email protected] ~]# 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的高可用。
接着上面配置好的多实例,现在我们来实现redis的高可用
主节点:10.220.5.137:6379
从1节点:10.220.5.137:6380
从2节点:10.220.5.138:6381
监控端:10.220.5.138
第一步:从节点指向主节点
从1节点
[[email protected] ~]# 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节点
[[email protected] ~]# 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
第二步:在主节点查看
可以看到如下信息role为master.连接的slave端为2个
[[email protected] ~]# 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行处,添加主节点信息即可
[[email protected] 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
[[email protected] html]# systemctl restart redis-sentinel [[email protected] 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 :::*
第五步:登录监控端
[[email protected] 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高可以用测试
第一步:关闭主服务器节点
[[email protected] ~]# 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 [[email protected] ~]# kill -9 4403 [[email protected] ~]# 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主从复制多实例及其高可用--技术流ken的主要内容,如果未能解决你的问题,请参考以下文章