Linux高可用(HA)之Redis+Keepalived 实现高可用Redis集群

  • A+
所属分类:Linux  Linux集群高可用
摘要

基本原理是通过keepalived的vrrp_scrips,定期用redis-cli 去取redis的info返回,如果返回时间超过keepalived vrrp_scripts的超时时间,keepalived会进入faild 状态,从而触发keepalived集群开始进行vip漂移.

一、设计思路:

当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;

当 Master 挂掉,Slave 正常时, Slave接管服务,有写权限,同时关闭主从复制功能;

当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,同时Slave等待Master同步数据完成之后,恢复Slave身份。
Linux高可用(HA)之Redis+Keepalived 实现高可用Redis集群

二、添加host解析、时间同步和ssh互信

[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/root
[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 you 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 into the machine, with "ssh 'root@redis-node02'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that 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/root
[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 you 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 into the machine, with "ssh 'root@redis-node01'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@Legion102 ~]#

三、安装Redis:

1、在node1上安装redis

[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` == 32 ] && [ `getconf LONG_BIT` == 32 ] && { 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 <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、配置node1上的主redis

[root@Legion100 ~]# sed -ri 's/^(# )?(daemonize).*/\2 yes/' /usr/local/redis/etc/redis.conf
# 配置Redis作为守护进程来运行。

3、在node2上安装从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` == 32 ] && [ `getconf LONG_BIT` == 32 ] && { 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 <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
停止 redis-server:                                        [确定]
[root@Legion102 ~/redis-3.0.6]#

4、配置node02上的从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、测试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]# 

四、安装Keepalived

1、在node1上安装Keepalived

[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_%H%M%S"`_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、配置node1上的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 100                      ###权重值

	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、在node1上添加相关脚本

①、在Master上创建redis的监控脚本

[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年12月26日 星期六 13时53分51秒
#########################################################################

ALIVE=`/usr/local/redis/bin/redis-cli PING`
[ "$ALIVE" == "PONG" ] && { echo $ALIVE && exit 0; } || { echo $ALIVE && exit 1; }

[root@Legion100 /etc/keepalived/scripts]# 

②、Redis Master上创建notity_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年12月26日 星期六 13时51分11秒
#########################################################################

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 #延迟10秒以后待数据同步完成后再取消同步状态

echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

③、在Redis Master上创建notify_backup脚本

[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年12月26日 星期六 13时59分05秒
#########################################################################

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 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.16.6.102 6379 >> $LOGFILE  2>&1

④、然后在Master上创建redis_fault.sh脚本

[root@Legion100 /etc/keepalived/scripts]# cat redis_fault.sh 
#!/bin/bash
#########################################################################
# File Name: redis_fault.sh
# Author: LookBack
# Email: admin#dwhd.org
# Version:
# Created Time: 2015年12月26日 星期六 14时15分15秒
#########################################################################

LOGFILE=/var/log/keepalived-redis-state.log
echo -e "[fault]\t$(date '+%F %T')" >> $LOGFILE

⑤、然后在Master上创建redis_stop.sh脚本

[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年12月26日 星期六 14时17分00秒
#########################################################################

LOGFILE=/var/log/keepalived-redis-state.log
echo -e "[stop]\t$(date '+%F %T')" >> $LOGFILE

⑥、给所有的脚本执行权限

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

4、在node2上安装keepalived

[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_%H%M%S"`_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、配置node2上的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 51
	priority 10                      ###比MASTRE权重值低 
	authentication {
		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、创建node2上keepalived的相关脚本

①、与node1使用脚本相同的直接scp过来就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/
总用量 12
-rwxr-xr-x 1 root root 431 12月 26 20:22 redis_check.sh
-rwxr-xr-x 1 root root 396 12月 26 20:22 redis_fault.sh
-rwxr-xr-x 1 root root 394 12月 26 20:22 redis_stop.sh
[root@Legion102 /etc/keepalived]# 

②、在Redis Slave上创建notity_master脚本:

[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年12月26日 星期六 20时24分50秒
#########################################################################

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 #延迟10秒以后待数据同步完成后再取消同步状态 
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

③、在Redis Slave上创建notify_backup脚本:

[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年12月26日 星期六 20时27分34秒
#########################################################################

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 #延迟15秒待数据被对方同步完成之后再切换主从角色 
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF 172.16.6.100 6379 >> $LOGFILE  2>&1

④、给所有的脚本执行权限

[root@Legion102 /etc/keepalived]# chmod +x /etc/keepalived/scripts/*.sh
[root@Legion102 /etc/keepalived]# ls -l /etc/keepalived/scripts/
总用量 20
-rwxr-xr-x 1 root root 653 12月 26 20:30 redis_backup.sh
-rwxr-xr-x 1 root root 431 12月 26 20:22 redis_check.sh
-rwxr-xr-x 1 root root 396 12月 26 20:22 redis_fault.sh
-rwxr-xr-x 1 root root 735 12月 26 20:25 redis_master.sh
-rwxr-xr-x 1 root root 394 12月 26 20:22 redis_stop.sh
[root@Legion102 /etc/keepalived]# 

五、测试高可用性

1、启动node1和node2节点上keepalived

[root@Legion100 ~]# service keepalived start
正在启动 keepalived:                                      [确定]
[root@Legion100 ~]# 
[root@Legion102 ~]# service keepalived start
正在启动 keepalived:                                      [确定]
[root@Legion102 ~]# 

2、查看vip绑定情况

[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 link 
       valid_lft forever preferred_lft forever
[root@Legion100 ~]#
[root@Legion102 ~]# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:F1:35:D7  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

[root@Legion102 ~]# 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: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、测试vip连接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、测试插入和取出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、测试vip漂移和redis高可用

[root@Legion100 ~]# service redis-server stop
停止 redis-server:                                        [确定]
[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: <LOOPBACK,UP,LOWER_UP> 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: <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
    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: <LOOPBACK,UP,LOWER_UP> 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: <BROADCAST,MULTICAST,UP,LOWER_UP> 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、测试故障恢复redis master恢复

[root@Legion100 ~]# service redis-server start
正在启动 redis-server:                                    [确定]
[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: <LOOPBACK,UP,LOWER_UP> 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: <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
    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 ~]# 
lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: