部署Redis+Keepalived高可用环境
192.168.150.15 elk-node01192.168.150.16 elk-node02192.168.150.17 elk-node03安装redis服务以下操作分别在node01、node02上安装redis服务:#yum install redis -y安装不上的请先配置epel扩展源。修改主从配置:# vim /etc/redis.conf更改如下:bind 0.0.0.0 /
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
}
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从节点配置:
将主节点配置文件拷贝至从节点配置中:
#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。
更多推荐
所有评论(0)