192.168.150.15 elk-node01
192.168.150.16 elk-node02
192.168.150.17 elk-node03

安装redis服务

以下操作分别在node01、node02上安装redis服务:

#yum install redis -y
安装不上的请先配置epel扩展源。

修改主从配置:

# vim /etc/redis.conf
更改如下:
bind 0.0.0.0  //或者直接写自己的IP地址
daemonize yes  //守护进程的方式
appendonly yes  //持久化保存

 重启并设置开机自启动:# systemctl restart redis
                                        # systemctl enable redis

从redis比主配置多一行:
slaveof 192.168.150.15 6379

 Keepalived安装 -----node01 node02 两个节点都安装

# yum install -y keepalived

 redis+keepalived配置

1>、node01主节点配置

#vim /etc/keepalived/keepalived.conf
里面配置如下:
global_defs {
router_id redis-master
}
vrrp_script chk_redis {
script "killall -0 redis-server"
interval 2
timeout 2
fall 3
}
vrrp_instance redis {
state BACKUP
interface ens33
lvs_sync_daemon_interface ens33
virtual_router_id 202
priority 150
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.150.100
}
track_script {
chk_redis
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.150.16
6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.150.16
6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
}

以上的 keepalived.conf 文件中的切换模式设置为 nopreempt ,意思是:
不抢占 VIP 资源,此种模式要是所有的节点都必须设置为 state BACKUP 模式!
需要注意无论主备服务器都需要设置为 BACKUP ,与以往 KeepAlived 的配置不同,其目的就是防止主 服务器恢复后重新抢回VIP ,导致 Redis 切换从而影响稳定。
主节点脚本文件:

node01主节点:
#cd /etckeepalived/
#mkdir scripts
#cd scripts

脚本1:
#vim redis_master.sh
#!/bin/bash
REDISCLI="redis-cli -h $1 -p $3"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE 2>&1
echo "SLAVEOF $2 cmd can't excute ... " >> $LOGFILE
sleep 10
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
:wq

脚本2 :
#vim redis_backup.sh
#!/bin/bash
REDISCLI="redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
sleep 100
exit 0
:wq

脚本3:
#vim redis_fault.sh
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
:wq

脚本4:
#vim redis_stop.sh
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
:wq

给权限:
#chmod +x *.sh

2> node02从节点配置:

主节点脚本复制到从节点,保证有执行权限。
        Keepalived 在转换状态时会依照状态来呼叫:
                                当进入 Master 状态时会呼叫 notify_master
                                当进入 Backup 状态时会呼叫 notify_backup
                                当发现异常情况时进入 Fault 状态呼叫 notify_fault
                                当 Keepalived 程序终止时则呼叫 notify_stop

 将主节点配置文件拷贝至从节点配置中:

#scp /etc/keepalived/keepalived.conf elk-node02:/etc/keepalived/

 将主节点脚本文件拷贝至从节点中:

#scp -r /etc/keepalived/scripts/  elk-node02:/etc/keepalived/

 在从节点上:在进行修改配置文件:

#vim /etc/keepalived/keepalived.conf

router_id redis-slave
priority 100   //优先级要低,主要决定谁是主从节点。
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.150.15
6379"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.150.15
6379"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop /etc/keepalived/scripts/redis_stop.sh
:wq

主从节点启动服务

# systemctl restart keepalived.service
# systemctl enable keepalived.service

redis+keepalived测试  

通过VIP连接Redis:

# redis-cli -h 192.168.150.100 INFO|grep role
role:master
# redis-cli -h 192.168.150.15 INFO|grep role
role:master
# redis-cli -h 192.168.150.16 INFO|grep role
role:slave

 插入数据测试:

# redis-cli -h 192.168.150.100 SET Hello Redis
OK
获取数值:
# redis-cli -h 192.168.150.100 GET Hello
"Redis"
# redis-cli -h 192.168.150.15 GET Hello
"Redis"
# redis-cli -h 192.168.150.16 GET Hello
"Redis"

模拟故障测试:

先停止redis服务 :  #systemctl stop redis 
查看日志:
[root@elk-node01 ~]# tailf /var/log/keepalived-redis-state.log
[fault]
2019年 11月 26日 星期二 22:54:07 CST
# redis-cli -h 192.168.150.100 INFO|grep role
role:master
# redis-cli -h 192.168.150.15 INFO|grep role
Could not connect to Redis at 192.168.150.11:6379: Connection refused
# redis-cli -h 192.168.150.16 INFO|grep role
role:master

 

继续查看如下:
# redis-cli -h 192.168.150.100 INFO|grep role   //任何时候查看都是master
role:master
# redis-cli -h 192.168.150.15 INFO|grep role 
Could not connect to Redis at 192.168.150.11:6379: Connection refused
# redis-cli -h 192.168.150.16 INFO|grep role
role:master 

 恢复主节点redis:

[root@elk-node01 ~]# systemctl start redis

监控主日志,发现进入Fault状态呼notify_fault:

从节点在连接:

 [root@elk-node01 ~]# tailf /var/log/keepalived-redis-state.log
[BACKUP]
2019年 11月 26日 星期二 22:57:42 CST
Being slave....
Run SLAVEOF cmd ...
OK
# redis-cli -h 192.168.150.100 INFO|grep role
role:master
# redis-cli -h 192.168.150.15 INFO|grep role
role:slave

  再停止从节点redis服务:

# systemctl stop redis

 监控日志,发现进入Fault状态呼叫notify_fault。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐