安装redis
我这里装的是一主三从,其中有一个从一直不能切换到主,所以这台机器上不需要配置keepalived,只需要在redis.conf文件配置上加上slaveof 20.200.45.95 6479,这个IP配置的是虚拟IP的地址,因为你不知道主机到时候会是哪一台IP地址。
机器信息:20.200.45.72 主
20.200.45.73 从
20.200.45.74 从
20.200.45.75 从(永远不能升为主)
申请的虚拟IP:20.200.45.95
一.需要gubby环境和gc++环境
yum install gcc zib ruby rubygems tcl
二.下载解压redis
解压:tar -zxvf redis-4.0.2.tar.gz
> cd redis-4.0.2
> make
> make test
> make install
安装成功后,在/usr/local/bin/目录下,会出现6个文件,如下图
你可以在redis-4.0.2目录下,创建bin文件夹,把/usr/local/bin/下的文件拷贝到
redis-4.0.2/bin目录下,在创建个logs文件夹,步骤如下:
> cd redis-4.0.2
> mkdir bin
> mkdir logs
> cp /usr/local/bin /home/user1/redis-4.0.2/bin
给其中三个文件赋予高一点的权限,最高是777
>chmod 777 redis-cli
>chmod 777 redis-server
>chmod 777 redis-sentinel
三.修改配置文件
每个redis配置文件都一致,注意:永远不能升为主的机器上,再加上slaveof 20.200.45.95 6479 这个配置就好了,别的机器不需要这个配置。
redis.conf基本配置:
bind 0.0.0.0
port 6479
protected-mode no
daemonize yes
pidfile "app/redis1z3c/redis/redis_6479.pid"
logfile "app/redis1z3c/redis/logs/redis.log"
stop-writes-on-bgsave-error no
dir "app/redis1z3c/redis/bin"
slave-serve-stale-data yes
slave-read-only no
slave-priority 80
appendonly yes
四.启动redis
我redis安装在/app/redis1z3c/redis目录下
启动72机器:后面73,74,75依次都是这个命令
> cd /app/redis1z3c/redis/bin
> ./redis-server ../redis.conf ##启动redis
> /redis-cli -h 20.200.45.72 -p 6479
输入info,可以看到redis信息,
会发现除了75的机器,每一台都是master
75机器显示是slave,如图:
因为keepalived没有启动,所以虚拟IP显示的是down机状态。
安装keepalived
一、安装依赖环境:
yum install kernel-devel openssl-devel popt-devel ipvsadm libnl libnl-devel
二、下载解压keepalived
我是安装在/app/redis1z3c/目录下
解压:tar -zxvf keepalived-2.0.10.tar.gz
> cd keepalived-2.0.10
> ./configure --prefix=/usr/local/keepalived
> make && make install
在/usr/local/keepalived相应的生成4个文件夹,如图:
三、把相应的文件移到系统相应的目录中
注意:keepalived启动,读取的配置文件一定是/etc/keepalived/keepalived.conf文件
> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
> cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
在/etc目录下,创建keepalived文件夹
> mkdir keepalived
> cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived ##这是keepalived配置文件
> cp /app/redis1z3c/keepalived-2.0.10/keepalived/etc/init.d/keepalived /etc/init.d ##这是启动脚本
四、修改keepalived日志文件存放的地方
默认是放在/var/log/message下面,但是这个文件有很多别的日志信息,所以我们单独配置keepalived日志文件
修改/etc/sysconfig/keepalived文件:
> vi /etc/sysconfig/keepalived
把KEEPALIVED_OPTIONS="-D" 修改成KEEPALIVED_OPTIONS="-D -d -S 0",保存
修改 /etc/rsyslog.conf
在最后一行添加:
local0.* /etc/keepalived/keepalived.log
systemctl restart rsyslog ##重启rsyslog
五、修改配置文件和脚本
脚本文件我放在/etc/keepalived/scripts文件下,在/etc/keepalived文件下在创建log文件
如图:
1. 修改keepalived.conf文件
这是20.200.45.72的配置:
! Configuration File for keepalived
global_defs {
router_id ocs_keepalived
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379"
interval 2
}
vrrp_instance VI_1 {
state MASTER ###主节点,从机器要把MASTER 换成BACKUP
interface eth0 ###网卡
virtual_router_id 25 ###设置VRID,决定多播的MAC地址
mcast_src_ip 20.200.45.72 ###多播地址,73的机器上配置20.200.45.73的IP,74的机器上配置20.200.45.74的IP地址
priority 100 ###优先级,73的机器上配置98,74的机器上配置95
advert_int 1 ###检查间隔
authentication { ###设置的认证
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress { ###虚拟IP地址
20.200.45.95
}
notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 20.200.45.95 6479"
notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 20.200.45.95 6479"
notify_fault /etc/keepalived/scripts/redis_fault.sh
notify_stop "/etc/keepalived/scripts/redis_stop.sh 127.0.0.1 20.200.45.95 6479"
}
2.添加redis_check.sh脚本
#!/bin/bash
ALIVE=`/app/redis1z3c/redis/bin/redis-cli -h $1 -p $2 PING`
LOGFILE="/etc/keepalived/log/keepalived-redis-check.log"
echo "[CHECK]" >> $LOGFILE
date >> $LOGFILE
if [ $ALIVE == "PONG" ]; then :
echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1
exit 0
else
echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1
exit 1
fi
3.添加redis_master.sh脚本
#!/bin/bash
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[master]" >> $LOGFILE
echo "$2 $3" >>$LOGFILE
echo "关闭备份,转换成主机模式" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF NO ONE cmd ... " >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "close slaver ,become master " >> $LOGFILE
sleep 5
echo "发送消息给监控" >> $LOGFILE
4.添加redis_stop.sh脚本
#!/bin/bash
LOGFILE=/etc/keepalived/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
echo "$2 $3" >> $LOGFILE
echo "keepalived 出现故障,redis变为备机">> $LOGFILE
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
5.添加redis_backup.sh脚本
#!/bin/bash
REDISCLI="/app/redis1z3c/redis/bin/redis-cli -h $1 -p $3"
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[BACKUP]" >> $LOGFILE
echo "$1 $3" >> $LOGFILE
echo "关闭主机模式,转为备机模式" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $2 $3 >> $LOGFILE
echo "发送消息给监控" >> $LOGFILE
6.添加redis_fault.sh 脚本
#/bin/bash
LOGFILE="/etc/keepalived/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
echo "redis 出现故障,无法ping通,请检查redis服务" >> $LOGFILE
六.启动keepalived
记住:虚拟IP在哪台机器上,哪台机器就会是主机,虚拟ip是按照优先级去区分主从。
只需要启动72,73,74机器,启动命令如下:
进入/etc/init.d文件夹下
> ./keepalived start
> ps -ef|grep keepalived
> ip a 看虚拟IP是否出来,72会显示虚拟IP
73和74的机器上, 是没有虚拟IP 的,如图:
七.这时再看每一台redis的状态
72机器:
>./redis-cli -h 20.200.45.72 -p 6479
>info ##会看到72变成了主,73,74,75都是从
在任意看其中一台从的机器,比如74
> ./redis-cli -h 20.200.45.74 -p 6479
> info ##会看到74是虚拟IP的从
再看75机器
> ./redis-cli -h 20.200.45.75 -p 6479
> info ##会发现虚拟IP变成up状态
我们也可以直接连接虚拟ip
> ./redis-cli -h 20.200.45.95 -p 6479
> info ##会发现虚拟IP是主机,跟72显示状态一样
这个时候我把72的机器停了,会发现73变成了主,74,75还是从
如果再把72启动起来,会发现72又变回了主
八.附件
下面我把redis配置文件和keepalived配置文件放上去
九.注意事项
1.系统防火墙必须关掉
2.VIP使用需要在同一个网段
3.实现主备模式,需要先全部启动redis,之后按照优先级启动keepalived
4.redis备库掉了之后,重新加入机器需要先杀掉keepalived,之后重启启动redis和keepalived,
如果不这样做,有可能会出现双主。不过我这边测试安装没有出现双主的情况,所以这个按照当时情况再看。
5.结合项目,云平台可能会存在限制,主要是安全组件打开,keepalived无法正常使用,造成VIP出现两台机器上。