1. 程式人生 > >Redis主從復制、多實例及其高可用(三)--技術流ken

Redis主從復制、多實例及其高可用(三)--技術流ken

ups not run mct redis 監控 主從復制 傳輸 recv cep

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(主從兩端都要操作)

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

第二步:啟動redis(主從兩端都要操作)

[root@ken 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(主從兩端都要操作)

[root@ken html]# systemctl restart redis

第五步:登錄redis

修改了綁定的地址之後需要指定本機的IP地址才能登錄

[root@ken 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

[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端口實例配置

[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 no
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 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端口實例配置

[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 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:
...

第五步:啟動多實例

[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的高可用。

接著上面配置好的多實例,現在我們來實現redis的高可用

主節點:10.220.5.137:6379

從1節點:10.220.5.137:6380

從2節點:10.220.5.138:6381

監控端:10.220.5.138

第一步:從節點指向主節點

從1節點

[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

第二步:在主節點查看

可以看到如下信息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行處,添加主節點信息即可

[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高可以用測試

第一步:關閉主服務器節點

[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主從復制、多實例及其高可用(三)--技術流ken