Keepalived配置:
安装必要的软件:
yum -y install gcc gcc+ openssl openssl-devel tcl wget -y
安装redis
cd /home
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -xf ./redis-3.2.4.tar.gz
mv redis-3.2.4 /usr/local/
cd /usr/local/redis-3.2.4/deps/
make hiredis lua jemalloc linenoise
cd ..
make test
make
make install
mkdir -p /usr/local/redis/bin
mkdir -p /data4/redisdata
cd /usr/local/redis-3.2.4/src
cp redis-benchmark redis-check-aof redis-cli redis-server mkreleasehdr.sh /usr/local/redis/bin/
cd /usr/local/redis-3.2.4
cp redis.conf /usr/local/redis/
cp /usr/local/bin/redis-cli /usr/bin/
安装keepalived
cd /home
wget http://www.keepalived.org/software/keepalived-1.3.4.tar.gz
tar xf keepalived-1.3.4.tar.gz
cd keepalived-1.3.4
./configure
make
make install
在master上修改配置文件/usr/local/redis/redis.conf
参数
bind 0.0.0.0
daemonize yes
dir /data4/redisdata
slave 上修改相同的信息,并添加slaveof的配置:
bind 0.0.0.0
daemonize yes
dir /data4/redisdata
slaveof 192.168.56.11 6379
master和slave上为redis配置单独用户并启动服务
useradd redisuser
passwd redisuser
chown -R redisuser:redisuser /usr/local/redis/redis.conf
chown -R redisuser:redisuser /usr/local/redis/bin/redis-server
chown redisuser /data4/redisdata
su - redisuser
cd /usr/local/redis
./bin/redis-server ./redis.conf
username: redisuser
password: redi13%&
在master节点修改/etc/keepalived/keepalived.conf
为如下配置:
global_defs {
lvs_id LVS_redis
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
weight -20
interval 2
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
nopreempt
priority 200
advert_int 5
track_script {
chk_redis
}
virtual_ipaddress {
192.168.56.100
}
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
}
Keepalived 的Backup节点修改/etc/keepalived/keepalived.conf
配置文件内容为如下配置:
global_defs {
lvs_id LVS_redis
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh"
weight -20
interval 2
}
vrrp_instance VI_1 {
state backup
interface eth0
virtual_router_id 51
priority 190
advert_int 5
track_script {
chk_redis
}
virtual_ipaddress {
192.168.56.100
}
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
}
/etc/keepalived/
目录下,解压到scripts目录,并添加脚本执行权限。mkdir /var/log/keepalived/
启动keepalived
先启动主节点keepalved,再启动slave节点
systemctl start keepalived
查看keepalived和redis工作状态
ip a|grep gl
redis-cli info replication
测试redis+keepalived主从是否正常
tail -f /var/log/keepalived/redis-state.log
tail -f /var/log/messages
当redis-master出现问题停机或者应用终止后,需要对进行故障排查,正确的恢复启动顺序如下:
su - redisuser
cd /usr/local/redis
./bin/redis-server ./redis.conf #启动redis
netstat -lntp
sh /etc/keepalived/scripts/redis_backup.sh # 使用脚本同步redis
redis-cli info replication #查看同步状态
[root@ost-redis1 ~]# systemctl start keepalived
[root@ost-redis1 ~]# redis-cli INFO replication
[root@ost-redis2 ~]# systemctl restart keepalived
[root@ost-redis2 ~]# redis-cli INFO replication
原文地址:http://blog.51cto.com/tryingstuff/2093119