当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
然后依次循环。
需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。
一:安装配置前准备工作
1.在主服务器192.168.1.4上面做下面操作
echo “192.168.1.4 test01” >> /etc/hosts
echo “192.168.1.5 test” >> /etc/hosts
2.在从服务器192.168.1.5上面做下面操作
echo “192.168.1.4 test01” >> /etc/hosts
echo “192.168.1.5 test” >> /etc/hosts
防火墙配置
firewall-cmd --zone=public --add-port=6379/tcp --permanent
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
重启防火墙使配置生效
systemctl restart firewalld.service
1、运行、停止、禁用firewalld
启动:# systemctl start firewalld
查看状态:# systemctl status firewalld 或者 firewall-cmd --state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld
2、配置firewalld
查看版本:$ firewall-cmd --version
查看帮助:$ firewall-cmd --help
查看设置:
显示状态:$ firewall-cmd --state
查看区域信息: $ firewall-cmd --get-active-zones
查看指定接口所属区域:$ firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:# firewall-cmd --panic-on
取消拒绝状态:# firewall-cmd --panic-off
查看是否拒绝:$ firewall-cmd --query-panic
更新防火墙规则:# firewall-cmd --reload
# firewall-cmd --complete-reload
两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务
将接口添加到区域,默认接口都在public
# firewall-cmd --zone=public --add-interface=eth0
永久生效再加上 –permanent 然后reload防火墙
设置默认接口区域
# firewall-cmd --set-default-zone=public
立即生效无需重启
打开端口(貌似这个才最常用)
查看所有打开的端口:
# firewall-cmd --zone=dmz --list-ports
加入一个端口到区域:
# firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上
打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档
# firewall-cmd --zone=work --add-service=smtp
移除服务
# firewall-cmd --zone=work --remove-service=smtp
二、下载安装redis软件包
get http://download.redis.io/releases/redis-stable.tar.gz
tar -zxvf redis-stable.tar.gz
mv redis-stable /usr/local/redis3.2.1
cd /usr/local/redis3.2.1
make
make install
cd /usr/local/bin
ls -ll
然后将源码中的 redis.conf 复制到 /etc/redis.conf
cp /usr/local/redis3.2.1/redis.conf /etc/redis.conf
修改 /etc/redis.conf里面可以把
daemonize yes #开启redis守护进程
pidfile /var/run/redis.pid #redis守护进程的pid存放位置
dbfilename dump.rdb #指定快照文件名
dir /var/lib/redis #指定快照文件存储路径
masterauth 123456 #主库认证密码
requirepass 123456 #客户端连接密码
注释bind 127.0.0.1
`
# requirepass foobared 修改为 requirepass 密码
就可以默认在后台执行redis-server了。
再制作一个 init.d 的启动脚本:
vim /etc/init.d/redis-server
#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis \
# chkconfig --add redis or chkconfig --list redis \
# service redis start or service redis stop
# processname: redis-server
# config: /etc/redis.conf
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi
case "$1" in
status)
ps -A|grep redis
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT -a yunee SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
chmod o+x /etc/init.d/redis-server
chkconfig --add redis-server
service redis-server start
三、从发服务器配置redis
从服务器,配置一样,只不过 修改/etc/redis.conf 中
slaveof 修改为
slaveof 192.168.1.4 6379
masterauth 修改为主服务器所设置的密码
然后开启从服务器的redis服务。
start redis-server start
四、进行redis主从测试
主服务器
redis-cli -p 6379 set hello world
从服务器
redis-cli -p 6379 get hello
"world"
主服务器
redis-cli -p 6379 set hello world2
从服务器
redis-cli -p 6379 get hello
"world2"
redis-cli -p 6379 set hello world
(error) READONLY You can't write against a read only slave.
成功配置主从redis服务器,由于配置中有一条从服务器是只读的,所以从服务器没法设置数据,只可以读取数据。
五、配置keepalived
下载地址:http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
上传到/usr/local/source-gz目录
tar -zxvf keepalived-1.2.23.tar.gz
mv keepalived-1.2.23 /usr/local/keepalived-1.2.23
yum安装opensslyum install -y openssl openssl-devel
./configure
make && make install
转移keepalived的配置文件
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#用于keepalived服务点的启动
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#这个是用于查看keepalived相关的帮助文档
如keepalived –help或者man keepalived
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#将keepalived配置文件做备份
cp /usr/local/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
vi /etc/keepalived/keepalived.conf
配置内容如下:
! Configuration File for keepalived
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state MASTER ###设置为MASTER
interface eno16777736 ###监控网卡
virtual_router_id 51
priority 100 ###权重值
authentication {
auth_type PASS ###加密
auth_pass 1111 ###密码
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.255.129 ######VIP
}
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
}
在Master和Slave上创建监控Redis的脚本
mkdir /etc/keepalived/scripts
vi /etc/keepalived/scripts/redis_check.sh
编辑内容如下:
#!/bin/bash
ALIVE=`/usr/local/bin/redis-cli -a yunee PING`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
5.编写以下负责运作的关键脚本:
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在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
1)首先,在Redis Master上创建notity_master与notify_backup脚本:
vi /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
MASTER="192.168.255.134"
PASSWD="yunee"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF $MASTER 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
MASTER="192.168.255.134"
PASSWD="yunee"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF $MASTER 6379 >> $LOGFILE 2>&1
2)在Redis Slave上创建notity_master与notify_backup脚本:
vi /etc/keepalived/scripts/redis_master.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
MASTER="192.168.255.137"
PASSWD="yunee"
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >> $LOGFILE 2>&1
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF $MASTER 6379 >> $LOGFILE 2>&1
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF NO ONE >> $LOGFILE 2>&1
vi /etc/keepalived/scripts/redis_backup.sh
#!/bin/bash
REDISCLI="/usr/local/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
MASTER="192.168.255.137"
PASSWD="yunee"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >> $LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ..." >> $LOGFILE
$REDISCLI -a $PASSWD SLAVEOF $MASTER 6379 >> $LOGFILE 2>&1
3)然后在Master与Slave创建如下相同的脚本:
vi /etc/keepalived/scripts/redis_fault.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vi /etc/keepalived/scripts/redis_stop.sh
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
4)在主从服务器上面给脚本都加上可执行权限:
chmod +x /etc/keepalived/scripts/*.sh
禁用防火墙
systemctl stop firewalld
查看状态
firewall-cmd --state
查看日志
tail -f /var/log/keepalived-redis-state.log