目录
1 机器规划
2 redis的部署安装
2.1配置文件内容
2.2 启动2台机器上的redis服务
2.3 查看2台机器上redis数据信息
3 redis主从之间手动切换
3.1 停掉22上的redis进程,到23从redis上执行上面讲到的SLAVEOF NO ONE参数
3.2 恢复22上的redis数据库
4.keepalived实现主从自动切换
4.1 keepalived执行原理:
4.2 下面我们贴出脚本:
4.3 主从切换
机器 | 角色 |
172.18.1.22 | 主 |
172.18.1.23 | 从 |
这里我们直接贴出简略的配置项,主要测试高可用功能,22机器上的配置内容:
[root@bgsbtsp0006-dqf:scripts]# cat /opt/redis/conf/redis6379.conf
bind 172.18.1.22
port 6379
appendonly yes
timeout 3600
daemonize yes
pidfile /opt/redis/pid/redis6379.pid
logfile "/opt/redis/log/redis6379.log"
dir "/opt/redis/data/6379"
23上机器配置内容,注意这里要加上slaveof参数配置:
bind 172.18.1.23
port 6380
appendonly yes
timeout 3600
daemonize yes
pidfile /opt/redis/pid/redis6380.pid
logfile "/opt/redis/log/redis6380.log"
dir "/opt/redis/data/6380"
slaveof 172.18.1.22 6379
SLAVEOF 命令用于在 Redis 运行时动态地修改复制(replication)功能的行为。
通过执行 SLAVEOF host port
命令,可以将当前服务器转变为指定服务器的从属服务器(slave server)。
如果当前服务器已经是某个主服务器(master server)的从属服务器,那么执行 SLAVEOF host port
将使当前服务器停止对旧主服务器的同步,丢弃旧数据集,转而开始对新主服务器进行同步。
另外,对一个从属服务器执行命令 SLAVEOF NO ONE
将使得这个从属服务器关闭复制功能,并从从属服务器转变回主服务器,原来同步所得的数据集不会被丢弃。
利用『 SLAVEOF NO ONE
不会丢弃同步所得数据集』这个特性,可以在主服务器失败的时候,将从属服务器用作新的主服务器,从而实现无间断运行。
#启动22上redis
[root@bgsbtsp0006-dqf:opt]# redis-server /opt/redis/conf/redis6379.conf
#启动23上redis
[root@bgsbtsp0007-dqf:opt]# redis-server /opt/redis/conf/redis6380.conf
查看22上redis的信息,之前搭建过,所有有一些值。
[root@bgsbtsp0006-dqf:opt]# redis-cli -h 172.18.1.22
172.18.1.22:6379> get bfd
"123"
172.18.1.22:6379> keys *
1) "wyl"
2) "zyx"
3) "bfd"
4) "lhf"
查看23上redis信息,我们可以看到同步到22上redis数据信息了,但是自己本身是不能够set写操作的
[root@bgsbtsp0007-dqf:opt]# redis-cli -h 172.18.1.23 -p 6380
172.18.1.23:6380> KEYS *
1) "lhf"
2) "bfd"
3) "zyx"
4) "wyl"
172.18.1.23:6380> set mm '123'
#同步到22主上的数据了,但是自己本身是无法写入操作的
(error) READONLY You can't write against a read only slave.
172.18.1.23:6380>
SLAVEOF NO ONE
参数
[root@bgsbtsp0006-dqf:opt]# ps -ef |grep redis
root 2562482 1 0 20:32 ? 00:00:00 redis-server 172.18.1.22:6379
root 2562701 2537889 0 20:32 pts/2 00:00:00 grep --color=auto redis
[root@bgsbtsp0006-dqf:opt]# kill 2562482
到23从redis上操作,看到ok字样,表示这时候已经不是slave角色了,我们可以进行写操作.
[root@bgsbtsp0006-dqf:opt]# redis-cli -h 172.18.1.23 -p 6380
172.18.1.23:6380> SLAVEOF no one
OK
172.18.1.23:6380> set wyl '123456'
OK
此时主从切换已经完成。
此时切换已经完成,但是我们还需要启动之前的主redis,但是这个时候我们已经进行过插入、删除等操作,以前的主redis并没有记录这些,那么就需要将现在的23上redis的dump.rdb替换之前22上redis 的dump.rdb文件。
启动22上的redis,并且指定新的主redis,即23上的redis
[root@bgsbtsp0006-dqf:scripts]# redis-cli -h 172.18.1.22 -p 6379
172.18.1.22:6379> slaveof 172.18.1.23 6380
OK
172.18.1.22:6379> get wyl
"123456"
172.18.1.22:6379> set ymh1 '123'
(error) READONLY You can't write against a read only slave.
172.18.1.22:6379>
我们可以看到,22上也查询到了刚刚在23机器上创建的wyl=123456的数据。并且22也变成read only 从角色了。
keepalived的基础这里不再描述,之前的文章有些,由于上面我们讲到手动切换,如果这块理解了,keepalived就是将上面的手动过程通过脚本的方式去实现,废话不多说,我们看看keepalived脚本是如何去操作的。
22是redis的主库,我们把keepalived的master节点也放在22上,通过check_redis_status.sh判断redis进程是否存在,如果不在了,执行falut脚本,打印日志告诉我们主库已经宕机,并且vip会漂移到23机器上
23机器根据keepalived的状态判断,发现vip到自己这边了,并且状态变成master状态,这时候调用master脚本,我们就在这个脚本里执行了上面手动过程中的slaveof no one这个指令。
除了检查redis脚本端口不一样,其余3个脚本都是一样的,同步到23机器上。
#检查redis脚本
[root@bgsbtsp0006-dqf:scripts]# cat check_redis.sh
#!/bin/bash
num=`ps -ef|grep 6379|grep -v grep|wc -l`
if [ $num -ge 1 ];then
status=0
else
status=1
fi
exit $status
#master脚本
[root@bgsbtsp0006-dqf:scripts]# cat master.sh
#!/bin/bash
LOGFILE=/etc/keepalived/scripts/keepalived.log
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
hostname=`hostname`
if [ $hostname = 'bgsbtsp0006-dqf' ];then
echo 22 >>$LOGFILE
echo 'SLAVEOF no one'|/opt/redis/bin/redis-cli -h 172.18.1.22 -p 6379 >> $LOGFILE
echo -e `date +"%F %T"` "the current redis is master" >> $LOGFILE
elif [ $'bgsbtsp0007-dqf = 'bfd-yiz-1p23' ];then
echo 23 >>$LOGFILE
echo 'SLAVEOF no one'|/opt/redis/bin/redis-cli -h 172.18.1.23 -p 6380 >>$LOGFILE
echo -e `date +"%F %T"` "the current redis is master" >> $LOGFILE
fi
#backup脚本
[root@bgsbtsp0006-dqf:scripts]# cat backup.sh
#!/bin/bash
LOGFILE=/etc/keepalived/scripts/keepalived.log
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
echo -e `date +"%F %T"` "the current redis is slave" >> $LOGFILE
# fault脚本
[root@bgsbtsp0006-dqf:scripts]# cat fault.sh
#!/bin/bash
LOGFILE=/etc/keepalived/scripts/keepalived.log
echo "[Fault]" >> $LOGFILE
date >> $LOGFILE
echo -e `date +"%F %T"` "the current redis is down" >> $LOGFILE
这时候,我们启动22,23上keepalived服务,22查看keepalived的状态,vip绑定在22机器上。
[root@bgsbtsp0006-dqf:scripts]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2019-01-08 20:17:12 CST; 15min ago
Process: 2558442 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 2558443 (keepalived)
CGroup: /system.slice/keepalived.service
├─2558443 /usr/sbin/keepalived -D
├─2558444 /usr/sbin/keepalived -D
└─2558445 /usr/sbin/keepalived -D
Jan 08 20:32:23 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:23 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:23 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:23 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Opening script file /etc/keepalived/scripts/ma....sh
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: VRRP_Instance(VI_1) Sending/queueing gratuitou....72
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
Jan 08 20:32:28 bgsbtsp0006-dqf Keepalived_vrrp[2558445]: Sending gratuitous ARP on bond0 for 172.18.1.72
23查看keepalived的状态
[root@bgsbtsp0007-dqf:scripts]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2019-01-08 20:17:57 CST; 12min ago
Process: 3863461 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 3863462 (keepalived)
CGroup: /system.slice/keepalived.service
├─3863462 /usr/sbin/keepalived -D
├─3863463 /usr/sbin/keepalived -D
└─3863464 /usr/sbin/keepalived -D
Jan 08 20:22:35 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: VRRP_Instance(VI_1) Now in FAULT state
Jan 08 20:22:35 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:37 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:39 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:41 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:43 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:45 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: /etc/keepalived/scripts/check_redis.sh exited wit...s 1
Jan 08 20:22:47 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: VRRP_Script(check_nginx_alived) succeeded
Jan 08 20:22:47 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: VRRP_Instance(VI_1) Entering BACKUP STATE
Jan 08 20:22:47 bgsbtsp0007-dqf Keepalived_vrrp[3863464]: Opening script file /etc/keepalived/scripts/backup.s
停掉22机器上主redis服务
[root@bgsbtsp0006-dqf:opt]# ps -ef |grep redis
root 2562482 1 0 20:32 ? 00:00:00 redis-server 172.18.1.22:6379
root 2562701 2537889 0 20:32 pts/2 00:00:00 grep --color=auto redis
[root@bgsbtsp0006-dqf:opt]# kill 2562482
查看22上脚本执行的过程,显示调用了master脚本,当停掉redis服务后,执行了fault脚本
[root@bgsbtsp0006-dqf:scripts]# cat keepalived.log
[Master]
Tue Jan 8 20:30:09 CST 2019
[Fault]
Tue Jan 8 20:33:07 CST 2019
2019-01-08 20:33:07 the current redis is down
查看23上脚本执行的过程,显示调用了backup脚本,当停掉22上redis服务后,23上执行了master脚本
[root@bfd-yiz-1p23 /etc/keepalived/scripts]# cat keepalived.log
[Backup]
Tue Jan 8 20:31:09 CST 2019
2019-01-08 20:33:09 the current database is master
[Master]
Tue Jan 8 20:33:09 CST 2019
2019-01-08 20:33:09 the current database is master
我们发现已经自动切换了,其实我们也可以用客户端去连接一下,去验证,这比较准确,我已经验证过了,这里就不再过多赘述。
同理,当我们恢复22上主机后,要手动执行slaveof 172.18.1.23 6380,这样数据就同步到23了,后面你们也可以试一下当23上redis挂了,22是否会变成主。