Linux high Availability (HA) redis+keepalived to achieve high availability Redis cluster

Source: Internet
Author: User
Tags benchmark chmod curl install openssl redis ssh centos redis cluster
First, design ideas:
When Master and Slave are operating normally, Master is responsible for service, Slave is responsible for standby;

When Master hangs out, Slave Normal, Slave takes over the service, has the Write permission, simultaneously closes the master-slave copy function;

When Master returns to normal, the data is synchronized from the slave, the master-slave copy function is turned off after the data is synchronized, and the master is restored, while slave waits for master to synchronize the data to restore slave identity.


Second, add host resolution, time synchronization and SSH mutual trust

[Root@legion100 ~]# Echo ' 172.16.6.100 redis-node01


172.16.6.102 redis-node02 ' >>/etc/hosts


[Root@legion100 ~]# echo "*/5 * * */usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1" >>/var/spool/cron/r Oot


[root@legion100 ~]# ssh-keygen-t rsa-f ~/.ssh/id_rsa-p '


Generating public/private RSA key pair.


Your identification has been saved In/root/.ssh/id_rsa.


Your public key has been saved in/root/.ssh/id_rsa.pub.


The key fingerprint is:


5e:c6:92:c9:11:d5:8c:20:e6:86:52:5c:e0:01:73:38 root@Legion100.dwhd.org


The key ' s Randomart image is:


+--[RSA 2048]----+


| o+=o+ oo.+ |


| e=.=. .. o |


| ..        o O. |


| . . . =       |


| S + |


| . +       |


|        . |


| |


| |


+-----------------+


[root@legion100 ~]# ssh-copy-id-i ~/.ssh/id_rsa.pub root@redis-node02


The authenticity of host ' redis-node02 (172.16.6.102) ' can ' t be established.


RSA key fingerprint is 68:70:b9:44:8b:c5:8f:1f:48:0d:fe:60:68:f1:19:22.


Are you sure your want to continue connecting (yes/no)? Yes


warning:permanently added ' redis-node02,172.16.6.102 ' (RSA) to the list of known hosts.


Root@redis-node02 ' s Password:


Now try logging to the machine, with SSH ' root@redis-node02 ', and check in:
. Ssh/authorized_keys
To make sure we haven ' t added extra keys this you weren ' t expecting.
[Root@legion100 ~]#


[root@legion102 ~]# Echo ' 172.16.6.100 redis-node01


172.16.6.102 redis-node02 ' >>/etc/hosts


[Root@legion102 ~]# echo "*/5 * * */usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1" >>/var/spool/cron/r Oot


[root@legion102 ~]# ssh-keygen-t rsa-f ~/.ssh/id_rsa-p '


Generating public/private RSA key pair.


Your identification has been saved In/root/.ssh/id_rsa.


Your public key has been saved in/root/.ssh/id_rsa.pub.


The key fingerprint is:


Cd:75:c3:a9:b5:8e:50:0e:2b:a1:68:ee:22:40:0b:f9 root@Legion102.dwhd.org


The key ' s Randomart image is:


+--[RSA 2048]----+


| |


| . . |


| . . . O * |


|o. . . + * + O |


|o.. O.  S = O. |


|.. Eo. . o |


|.       . .  . |


|.             .. |


| .            .. |


+-----------------+


[root@legion102 ~]# ssh-copy-id-i ~/.ssh/id_rsa.pub root@redis-node01


The authenticity of host ' redis-node01 (172.16.6.100) ' can ' t be established.


RSA key fingerprint is 54:c9:a7:17:99:3f:1d:a7:18:92:3e:a2:e4:39:d0:00.


Are you sure your want to continue connecting (yes/no)? Yes


warning:permanently added ' redis-node01,172.16.6.100 ' (RSA) to the list of known hosts.


Root@redis-node01 ' s Password:


Now try logging to the machine, with SSH ' root@redis-node01 ', and check in:
. Ssh/authorized_keys
To make sure we haven ' t added extra keys this you weren ' t expecting.
[Root@legion102 ~]#
Third, the installation Redis:
1, install the Redis on the Node1


[Root@legion100 ~]# wget http://download.redis.io/releases/redis-3.0.6.tar.gz


[root@legion100 ~]# Tar XF redis-3.0.6.tar.gz


[root@legion100 ~]# CD redis-3.0.6/


[Root@legion100 ~/redis-3.0.6]# [' getconf word_bit ' =] && [' getconf long_bit ' =] && {sed-i ' 1i\cflags=-march=i686 ' src/makefile && sed-i ' s@^opt=.* @OPT =-o2-march=i686@ ' src/.make-settings; }


[Root@legion100 ~/redis-3.0.6]# make


[Root@legion100 ~/redis-3.0.6]# mkdir-p/usr/local/redis/{bin,etc,var}


[Root@legion100 ~/redis-3.0.6]# cp-af Src/redis-{benchmark,check-aof,check-dump,cli,sentinel,server}/usr/local/ Redis/bin


[Root@legion100 ~/redis-3.0.6]# cp-a redis.conf/usr/local/redis/etc


[Root@legion100 ~/redis-3.0.6]# echo "Export path=/usr/local/redis/bin:\ $PATH" >/etc/profile.d/redis.sh


[Root@legion100 ~/redis-3.0.6]#. /etc/profile.d/redis.sh


[root@legion100 ~/redis-3.0.6]# sed-i ' s@pidfile.* @pidfile/var/run/redis.pid@ '/usr/local/redis/etc/redis.conf


[root@legion100 ~/redis-3.0.6]# sed-i "s@logfile.* @logfile/usr/local/redis/var/redis.log@"/usr/local/redis/etc/ Redis.conf


[root@legion100 ~/redis-3.0.6]# sed-i "s@^dir.* @dir/usr/local/redis/var@"/usr/local/redis/etc/redis.conf


[root@legion100 ~/redis-3.0.6]# sed-i ' s@daemonize no@daemonize yes@ '/usr/local/redis/etc/redis.conf


[Root@legion100 ~/redis-3.0.6]# [-z] ' grep ^maxmemory/usr/local/redis/etc/redis.conf '] && sed-i '/maxmemory & lt;bytes>/a maxmemory 360000000 '/usr/local/redis/etc/redis.conf


[root@legion100 ~/redis-3.0.6]# curl-slo-http://www.dwhd.org/script/redis-server-init-centos >/etc/init.d/ Redis-server


[Root@legion100 ~/redis-3.0.6]# chmod +x/etc/init.d/redis-server


[root@legion100 ~/redis-3.0.6]# chkconfig redis-server on


[root@legion100 ~/redis-3.0.6]# Service Redis-server start


2, configure the Node1 on the main redis


[root@legion100 ~]# Sed-ri ' s/^ (#)? (daemonize). */\2 yes/'/usr/local/redis/etc/redis.conf


# Configure Redis to run as a daemon.


3, installed on the node2 from the Redis


[root@legion102 ~]# SCP root@redis-node01:~/redis-3.0.6.tar.gz redis-3.0.6.tar.gz


[root@legion102 ~]# Tar XF redis-3.0.6.tar.gz


[root@legion102 ~]# CD redis-3.0.6/


[root@legion102 ~/redis-3.0.6]# [' getconf word_bit ' =] && [' getconf long_bit ' =] && {sed-i ' 1i\cflags=-march=i686 ' src/makefile && sed-i ' s@^opt=.* @OPT =-o2-march=i686@ ' src/.make-settings; }


[Root@legion102 ~/redis-3.0.6]# make


[root@legion102 ~/redis-3.0.6]# mkdir-p/usr/local/redis/{bin,etc,var}


[root@legion102 ~/redis-3.0.6]# cp-af Src/redis-{benchmark,check-aof,check-dump,cli,sentinel,server}/usr/local/ Redis/bin


[root@legion102 ~/redis-3.0.6]# cp-a redis.conf/usr/local/redis/etc


[Root@legion102 ~/redis-3.0.6]# echo "Export path=/usr/local/redis/bin:\ $PATH" >/etc/profile.d/redis.sh


[root@legion102 ~/redis-3.0.6]#. /etc/profile.d/redis.sh


[root@legion102 ~/redis-3.0.6]# sed-i ' s@pidfile.* @pidfile/var/run/redis.pid@ '/usr/local/redis/etc/redis.conf


[root@legion102 ~/redis-3.0.6]# sed-i "s@logfile.* @logfile/usr/local/redis/var/redis.log@"/usr/local/redis/etc/ Redis.conf


[root@legion102 ~/redis-3.0.6]# sed-i "s@^dir.* @dir/usr/local/redis/var@"/usr/local/redis/etc/redis.conf


[root@legion102 ~/redis-3.0.6]# sed-i ' s@daemonize no@daemonize yes@ '/usr/local/redis/etc/redis.conf


[root@legion102 ~/redis-3.0.6]# [-z] ' grep ^maxmemory/usr/local/redis/etc/redis.conf '] && sed-i '/maxmemory & lt;bytes>/a maxmemory 360000000 '/usr/local/redis/etc/redis.conf


[root@legion102 ~/redis-3.0.6]# curl-slo-http://www.dwhd.org/script/redis-server-init-centos >/etc/init.d/ Redis-server


[root@legion102 ~/redis-3.0.6]# chmod +x/etc/init.d/redis-server


[root@legion102 ~/redis-3.0.6]# chkconfig redis-server on


[root@legion102 ~/redis-3.0.6]# Service Redis-server start


[root@legion102 ~/redis-3.0.6]# Ss-l|grep 6379


LISTEN 0 511 *:6379 *:*


LISTEN 0 511::: 6379:::*


[root@legion102 ~/redis-3.0.6]# service Redis-server stop


Stop redis-server: [OK]


[Root@legion102 ~/redis-3.0.6]#


4, configure the NODE02 from the Redis


[root@legion102 ~/redis-3.0.6]# Sed-ri ' s/^ (#)? (slaveof). */\2 172.16.6.100 6379/'/usr/local/redis/etc/redis.conf


#指定redis的master的ip和端口


[root@legion102 ~/redis-3.0.6]# Service Redis-server start


5. Master and slave copy of test Redis


[root@legion100 ~]# redis-cli-p 6379 set Fristkey lookback


Ok


[root@legion100 ~]# redis-cli-p 6379 set Secondkey Legion


Ok


[root@legion100 ~]# redis-cli-p 6379 get Fristkey


"Lookback"


[root@legion100 ~]# redis-cli-p 6379 get Secondkey


"Legion"


[Root@legion100 ~]# redis-cli-p 6379


127.0.0.1:6379> keys *


1) "Fristkey"


2) "Secondkey"


127.0.0.1:6379> Get Fristkey


"Lookback"


127.0.0.1:6379> Get Secondkey


"Legion"


127.0.0.1:6379> exit


[Root@legion100 ~]#


[root@legion102 ~/redis-3.0.6]# redis-cli-p 6379 get Fristkey


"Lookback"


[root@legion102 ~/redis-3.0.6]# redis-cli-p 6379 get Secondkey


"Legion"


[Root@legion102 ~/redis-3.0.6]#


Iv. installation of keepalived
1, install the keepalived on the Node1


[root@legion100 ~]# yum install openssl-devel kernel-devel-y


[Root@legion100 ~]# wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz


[root@legion100 ~]# Tar XF keepalived-1.2.19.tar.gz


[root@legion100 ~]# CD keepalived-1.2.19


[root@legion100 ~/keepalived-1.2.19]#./configure--prefix=/usr/local/keepalived--sysconfdir=/etc-- with-kernel-dir=/usr/src/kernels/' Uname-r '--ENABLE-SHA1


[Root@legion100 ~/keepalived-1.2.19]# make-j $ (awk '/processor/{i++}end{print i} '/proc/cpuinfo) && make Install


[Root@legion100 ~/keepalived-1.2.19]# echo "Export path=/usr/local/keepalived/sbin:\ $PATH" >/etc/profile.d/ keepalived.sh


[Root@legion100 ~/keepalived-1.2.19]#. /etc/profile.d/keepalived.sh


[Root@legion100 ~/keepalived-1.2.19]# sed-i $ (awk '/^manpath\t/{n=nr}end{print n} '/etc/man.config) a MANPATH\t/usr/ Local/keepalived/share/man "/etc/man.config


[root@legion100 ~/keepalived-1.2.19]# which keepalived


/usr/local/keepalived/sbin/keepalived


[root@legion100 ~/keepalived-1.2.19]# mv/etc/keepalived/keepalived.conf{,_ ' date ' +%f-%t ' _backup}


[Root@legion100 ~/keepalived-1.2.19]# cp/etc/keepalived/samples/keepalived.conf.virtualhost/etc/keepalived/ Keepalived.conf


[Root@legion100 ~/keepalived-1.2.19]# mkdir-p/etc/keepalived/scripts


[Root@legion100 ~/keepalived-1.2.19]# ln-sv/usr/local/keepalived/sbin/keepalived/usr/sbin/


[Root@legion100 ~/keepalived-1.2.19]# cd/etc/keepalived/


2, configure the Node1 on the keepalived


[root@legion100/etc/keepalived]# Cat keepalived.conf


! Configuration File for Keepalived
Global_defs {
Notification_email {
Admin@dwhd.org #配置收件人邮箱
}
Notification_email_from redis-keepalived@ds.com #配置发件人信箱
Smtp_server 127.0.0.1 #配置发件server
Smtp_connect_timeout 30
router_id Lvs_devel
}
Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
}
Vrrp_instance Vi_1 {
State MASTER # # #设置为MASTER
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 51
Priority # # #权重值
Authentication {
Auth_type Pass # # # #加密
Auth_pass 1111 # # #密码
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
172.16.7.200 label eth0:0 ##### #VIP绑定在eth0:0
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh
}
3, add the relevant script on the Node1
①, creating a Redis monitoring script on Master
[root@legion100/etc/keepalived/scripts]# Cat redis_check.sh
#!/bin/bash
#########################################################################
# File Name:redis_check.sh
# Author:lookback
# email:admin#dwhd.org
# Version:
# Created time:2015 Year December 26 Saturday 13:53 51 seconds
#########################################################################
Alive= '/usr/local/redis/bin/redis-cli PING '
[$ALIVE = = "PONG"] && {echo $ALIVE && exit 0;} | | {echo $ALIVE && exit 1;}
[root@legion100/etc/keepalived/scripts]#
Create Notity_master on ②, Redis master
[root@legion100/etc/keepalived/scripts]# Cat redis_master.sh
#!/bin/bash
#########################################################################
# File Name:redis_master.sh
# Author:lookback
# email:admin#dwhd.org
# Version:
# Created time:2015 Year December 26 Saturday 13:51 11 seconds
#########################################################################
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
Echo-e "[master]\t$ (Date ' +%f%T ') \nbeing master....\nrun-slaveof cmd ..." >> $LOGFILE 2>&1
$REDISCLI slaveof 172.16.6.102 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1
③, creating the Notify_backup script on Redis master
[root@legion100/etc/keepalived/scripts]# Cat redis_backup.sh
#!/bin/bash
#########################################################################
# File Name:redis_backup.sh
# Author:lookback
# email:admin#dwhd.org
# Version:
# Created time:2015 Year December 26 Saturday 13:59 05 seconds
#########################################################################
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
Echo-e "[backup]\t$ (Date ' +%f%T ') \nbeing slave ..." >> $LOGFILE 2>&1
Sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI slaveof 172.1 6.6.102 6379 >> $LOGFILE   2>&1
④, then create redis_fault.sh scripts on Master
[Root@legion100/etc/keepaliv ed/scripts]# cat redis_fault.sh
#!/bin/bash
############################################################## ###########
# File Name:redis_fault.sh
# author:lookback
# email:admin#dwhd.org
# Version:
# Created time:2015 year December 26 Saturday 14:15 15 seconds
##################################################################### ####
Logfile=/var/log/keepalived-redis-state.log
Echo-e "[fault]\t$ (Date ' +%f%T ')" >> $LOGFILE
⑤, then create the redis_stop.sh script on master
[root@legion100/etc/keepalived/scripts]# Cat redis_stop.sh
#!/bin/bash
#########################################################################
# File Name:redis_stop.sh
# Author:lookback
# email:admin#dwhd.org
# Version:
# Created time:2015 Year December 26 Saturday 14:17 00 seconds
#########################################################################
Logfile=/var/log/keepalived-redis-state.log


Echo-e "[stop]\t$ (Date ' +%f%T ')" >> $LOGFILE


⑥, execute permissions for all scripts


[root@legion100/etc/keepalived]# chmod +x/etc/keepalived/scripts/*.sh


4, install the keepalived on the Node2


[root@legion102 ~]# SCP root@redis-node01:~/keepalived-1.2.19.tar.gz.


keepalived-1.2.19.tar.gz 100% 322KB 322.4kb/s 00:00


[root@legion102 ~]# Tar XF keepalived-1.2.19.tar.gz


[root@legion102 ~]# CD keepalived-1.2.19


[root@legion102 ~/keepalived-1.2.19]#./configure--prefix=/usr/local/keepalived--sysconfdir=/etc-- with-kernel-dir=/usr/src/kernels/' Uname-r '--ENABLE-SHA1


[root@legion102 ~/keepalived-1.2.19]# make-j $ (awk '/processor/{i++}end{print i} '/proc/cpuinfo) && make Install


[Root@legion102 ~/keepalived-1.2.19]# echo "Export path=/usr/local/keepalived/sbin:\ $PATH" >/etc/profile.d/ keepalived.sh


[root@legion102 ~/keepalived-1.2.19]#. /etc/profile.d/keepalived.sh


[root@legion102 ~/keepalived-1.2.19]# sed-i $ (awk '/^manpath\t/{n=nr}end{print n} '/etc/man.config) a MANPATH\t/usr/ Local/keepalived/share/man "/etc/man.config


[root@legion102 ~/keepalived-1.2.19]# mv/etc/keepalived/keepalived.conf{,_ ' date ' +%f-%t ' _backup}


[Root@legion102 ~/keepalived-1.2.19]# cp/etc/keepalived/samples/keepalived.conf.virtualhost/etc/keepalived/ Keepalived.conf


[root@legion102 ~/keepalived-1.2.19]# mkdir-p/etc/keepalived/scripts


[Root@legion102 ~/keepalived-1.2.19]# cd/etc/keepalived/


[root@legion102/etc/keepalived]# CD


[Root@legion102 ~]# ln-sv/usr/local/keepalived/sbin/keepalived/usr/sbin/


5, configure the Node2 on the keepalived


[root@legion102/etc/keepalived]# Cat keepalived.conf


! Configuration File for Keepalived
Global_defs {
Notification_email {
Admin@dwhd.org #配置收件人邮箱
}
Notification_email_from redis-keepalived@ds.com #配置发件人信箱
Smtp_server 127.0.0.1 #配置发件server
Smtp_connect_timeout 30
router_id Lvs_devel
}
vrrp_script Chk_redis {
 script "/etc/keepalived/scripts/redis_check.sh"     # # #监控脚本
 interval 2                   # #监控时间
}
vrrp_instance vi_1 {
 state backup                      # #设置为BACKUP
 interface eth0                    # #监控网卡
  virtual_router_id Wuyi
 priority 10                       # #比MASTRE权重值低
 authentication {
 & Nbsp;auth_type Pass
  auth_pass 1111                # #密码与MASTRE相同
 }
 
 track_script {
  chk_redis                      # #执行上面定义的chk_redis
 }
virtual_ipaddress {
172.16.7.200 label eth0:0 ### #vip
}
notify_master/etc/keepalived/scripts/redis_master.sh


notify_backup/etc/keepalived/scripts/redis_backup.sh


notify_fault/etc/keepalived/scripts/redis_fault.sh


notify_stop/etc/keepalived/scripts/redis_stop.sh


}


6, create node2 on the keepalived of the relevant scripts


①, the same direct SCP as the Node1 using the script come on OK


[Root@legion102/etc/keepalived]# SCP root@redis-node01:/etc/keepalived/scripts/redis_{check.sh,fault.sh,stop.sh} /etc/keepalived/scripts/


redis_check.sh 100% 431 0.4kb/s 00:00


redis_fault.sh 100% 396 0.4kb/s 00:00


redis_stop.sh 100% 394 0.4kb/s 00:00


[root@legion102/etc/keepalived]# ls-l/etc/keepalived/scripts/


Total Dosage 12


-rwxr-xr-x 1 root 431 December 20:22 redis_check.sh


-rwxr-xr-x 1 root 396 December 20:22 redis_fault.sh


-rwxr-xr-x 1 root 394 December 20:22 redis_stop.sh


[root@legion102/etc/keepalived]#


②, create Notity_master script on Redis slave:


[root@legion102/etc/keepalived/scripts]# Cat redis_master.sh


#!/bin/bash


#########################################################################


# File Name:redis_master.sh


# Author:lookback


# email:admin#05hd.com


# Version:


# Created time:2015 Year December 26 Saturday 20:24 50 seconds


#########################################################################
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
Echo-e "[master]\t$ (Date ' +%f%T ') \nbeing master....\nrun-slaveof cmd ..." >> $LOGFILE 2>&1
$REDISCLI slaveof 172.16.6.100 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1
③, create Notify_backup script on Redis slave:
[root@legion102/etc/keepalived/scripts]# Cat redis_backup.sh
#!/bin/bash
#########################################################################
# File Name:redis_backup.sh
# Author:lookback
# email:admin#05hd.com
# Version:
# Created time:2015 Year December 26 Saturday 20:27 34 seconds
#########################################################################
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
Echo-e "[backup]\t$ (Date ' +%f%T ') \nbeing slave ..." >> $LOGFILE 2>&1
Sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI slaveof 172.16.6.100 6379 >> $LOGFILE 2>&1
④, execute permissions for all scripts
[root@legion102/etc/keepalived]# chmod +x/etc/keepalived/scripts/*.sh
[root@legion102/etc/keepalived]# ls-l/etc/keepalived/scripts/
Total dosage 20
-rwxr-xr-x 1 root 653 December 20:30 redis_backup.sh
-rwxr-xr-x 1 root 431 December 20:22 redis_check.sh
-rwxr-xr-x 1 root 396 December 20:22 redis_fault.sh
-rwxr-xr-x 1 root 735 December 20:25 redis_master.sh
-rwxr-xr-x 1 root 394 December 20:22 redis_stop.sh
[root@legion102/etc/keepalived]#
V. Testing for high availability
1, start Node1 and Node2 node keepalived


[root@legion100 ~]# Service keepalived start


Starting keepalived: [OK]


[Root@legion100 ~]#


[root@legion102 ~]# Service keepalived start


Starting keepalived: [OK]


[Root@legion102 ~]#


2, view the VIP binding situation


[Root@legion100 ~]# ifconfig eth0:0


eth0:0 Link encap:ethernet hwaddr 00:0c:29:67:94:d4


inet addr:172.16.7.200 bcast:0.0.0.0 mask:255.255.255.255


Up broadcast RUNNING multicast mtu:1500 metric:1
[root@legion100 ~]# IP addr Show eth0:0
2:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU 1500 Qdisc pfifo_fast State up Qlen 1000
    ; Link/ether 00:0c:29:67:94:d4 BRD ff:ff:ff:ff:ff:ff
    inet 172.16.6.100/16 BRD 172.16.255.255 Scope Global eth0
    inet 172.16.7.200/32 Scope Global eth0:0
    INET6 2001:470:24:9E2:20C:29FF:FE67:94D4/64 scope Global Dynamic
        valid_lft 7045sec preferred_lft 1645sec
    inet6 fe80::20c:29ff:fe67:94d4/64 Scope Lin K
       Valid_lft forever Preferred_lft Forever
[root@legion100 ~]#
[roo t@legion102 ~]# ifconfig eth0:0
eth0:0    Link encap:ethernet  hwaddr 00:0c:29:f1:35:d7 
    & nbsp;     up broadcast RUNNING multicast  mtu:1500  metric:1
[root@legion102 ~]# IP addr Show eth0:0


2:eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; MTU 1500 Qdisc pfifo_fast State up Qlen 1000


Link/ether 00:0c:29:f1:35:d7 BRD FF:FF:FF:FF:FF:FF


inet 172.16.6.102/16 BRD 172.16.255.255 Scope Global eth0


INET6 2001:470:24:9E2:20C:29FF:FEF1:35D7/64 Scope Global Dynamic


Valid_lft 7001sec preferred_lft 1601sec


Inet6 FE80::20C:29FF:FEF1:35D7/64 Scope link


Valid_lft Forever Preferred_lft Forever


[Root@legion102 ~]#


3. Test VIP Connection Redis


[root@legion102 ~]# redis-cli-h 172.16.7.200 INFO |grep-a8 Replication


# Replication


Role:master


Connected_slaves:1


Slave0:ip=172.16.6.102,port=6379,state=online,offset=505,lag=1


master_repl_offset:505


Repl_backlog_active:1


repl_backlog_size:1048576


Repl_backlog_first_byte_offset:2


repl_backlog_histlen:504


[Root@legion102 ~]#


[root@legion100 ~]# redis-cli-h 172.16.7.200 INFO |grep-a8 Replication


# Replication


Role:master


Connected_slaves:1


Slave0:ip=172.16.6.102,port=6379,state=online,offset=547,lag=0


master_repl_offset:547


Repl_backlog_active:1


repl_backlog_size:1048576


Repl_backlog_first_byte_offset:2


repl_backlog_histlen:546


[Root@legion100 ~]#


4. Test insert and remove key


[root@legion102 ~]# redis-cli-h 172.16.7.200 SET thirdkey www.dwhd.org #通过vip写key


Ok


[root@legion102 ~]# redis-cli-h 172.16.7.200 get Thirdkey #通过vip取key值


"Www.dwhd.org"


[root@legion102 ~]# redis-cli-h 172.16.6.102 get Thirdkey #通过slave取key值


"Www.dwhd.org"


[root@legion102 ~]# redis-cli-h 172.16.6.100 get Thirdkey #通过master取key值


"Www.dwhd.org"


[Root@legion102 ~]#


5, test VIP drift and Redis high availability


[root@legion100 ~]# service Redis-server stop


Stop redis-server: [OK]


[Root@legion100 ~]# Tail/var/log/keepalived-redis-state.log


[Master] 2015-12-26 21:11:59


Being master ....


Run slaveof cmd ...


Ok


Run slaveof NO One cmd ...


Ok


[Fault] 2015-12-26 21:13:02


[root@legion100 ~]# IP Addr


1:lo: &lt;LOOPBACK,UP,LOWER_UP&gt; MTU 65536 qdisc noqueue State UNKNOWN


Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00


inet 127.0.0.1/8 Scope host Lo


INET6:: 1/128 Scope Host


Valid_lft Forever Preferred_lft Forever


2:eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; MTU 1500 Qdisc pfifo_fast State up Qlen 1000


Link/ether 00:0C:29:67:94:D4 BRD FF:FF:FF:FF:FF:FF


inet 172.16.6.100/16 BRD 172.16.255.255 Scope Global eth0


INET6 2001:470:24:9E2:20C:29FF:FE67:94D4/64 Scope Global Dynamic


Valid_lft 7140sec preferred_lft 1740sec


Inet6 FE80::20C:29FF:FE67:94D4/64 Scope link


Valid_lft Forever Preferred_lft Forever


[Root@legion100 ~]#


[root@legion102/etc/keepalived/scripts]# IP Addr


1:lo: &lt;LOOPBACK,UP,LOWER_UP&gt; MTU 65536 qdisc noqueue State UNKNOWN


Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00


inet 127.0.0.1/8 Scope host Lo


INET6:: 1/128 Scope Host


Valid_lft Forever Preferred_lft Forever


2:eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; MTU 1500 Qdisc pfifo_fast State up Qlen 1000


Link/ether 00:0c:29:f1:35:d7 BRD FF:FF:FF:FF:FF:FF


inet 172.16.6.102/16 BRD 172.16.255.255 Scope Global eth0


inet 172.16.7.200/32 Scope Global eth0


INET6 2001:470:24:9E2:20C:29FF:FEF1:35D7/64 Scope Global Dynamic


Valid_lft 7132sec preferred_lft 1732sec


Inet6 FE80::20C:29FF:FEF1:35D7/64 Scope link


Valid_lft Forever Preferred_lft Forever


[root@legion102/etc/keepalived/scripts]# Tail/var/log/keepalived-redis-state.log


[Backup] 2015-12-26 21:11:58


Being slave .....


Run slaveof cmd ...


Ok


[Master] 2015-12-26 21:13:04


Being master ....


Run slaveof cmd ...


OK already connected to specified master


Run slaveof NO One cmd ...


Ok


[root@legion102/etc/keepalived/scripts]# redis-cli-h 172.16.7.200 INFO |grep-a8 Replication


# Replication


Role:master


connected_slaves:0


master_repl_offset:0


repl_backlog_active:0


repl_backlog_size:1048576


Repl_backlog_first_byte_offset:2


Repl_backlog_histlen:28
[root@legion102/etc/keepalived/scripts]#


6, test recovery Redis Master Recovery


[root@legion100 ~]# Service Redis-server start


Starting redis-server: [OK]


[Root@legion100 ~]# Tail/var/log/keepalived-redis-state.log


Being master ....


Run slaveof cmd ...


Ok


Run slaveof NO One cmd ...


Ok


[Fault] 2015-12-26 21:13:02


[Master] 2015-12-26 21:15:24


Being master ....


Run slaveof cmd ...


Ok


[root@legion100 ~]# IP Addr


1:lo: &lt;LOOPBACK,UP,LOWER_UP&gt; MTU 65536 qdisc noqueue State UNKNOWN


Link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:00


inet 127.0.0.1/8 Scope host Lo


INET6:: 1/128 Scope Host


Valid_lft Forever Preferred_lft Forever


2:eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; MTU 1500 Qdisc pfifo_fast State up Qlen 1000


Link/ether 00:0C:29:67:94:D4 BRD FF:FF:FF:FF:FF:FF


inet 172.16.6.100/16 BRD 172.16.255.255 Scope Global eth0


inet 172.16.7.200/32 Scope Global eth0


INET6 2001:470:24:9E2:20C:29FF:FE67:94D4/64 Scope Global Dynamic


Valid_lft 6998sec preferred_lft 1598sec


Inet6 FE80::20C:29FF:FE67:94D4/64 Scope link


Valid_lft Forever Preferred_lft Forever


[root@legion100 ~]# redis-cli-h 172.16.7.200 get Thirdkey


"Www.dwhd.org"


[root@legion100 ~]# redis-cli-h 172.16.7.200 INFO |grep-a8 Replication


# Replication


Role:master


Connected_slaves:1


Slave0:ip=172.16.6.102,port=6379,state=online,offset=44,lag=0


Master_repl_offset:44


Repl_backlog_active:1


repl_backlog_size:1048576


Repl_backlog_first_byte_offset:3


Repl_backlog_histlen:42


[Root@legion100 ~]#
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.