keepalived+redis主从双机热备部署
keepalived+redis主从双机热备部署@[TOC](keepalived+redis主从双机热备部署)最近的项目上遇到了这样的需求,客户要求做应用与数据库的分离。客户给了4台服务器。1、安装部署Redis2、安装部署keepalived最近的项目上遇到了这样的需求,客户要求做应用与数据库的分离。客户给了4台服务器。1、安装部署Redis准备环境1.两台服务器192.168.0.55 ma
最近的项目上遇到了这样的需求,客户要求做应用与数据库的分离。客户给了4台服务器。
服务器环境:
- 192.168.110.130 (数据库master)
- 192.168.110.131 (数据库slave)
- 192.168.110.140 (应用master)
- 192.168.110.141 (应用slave)
- 第一台130 和第二台131服务器用于安装mysql数据库与redis
- keepalived+redis主从双机热备部署为这次的主要内容。
- 两台服务器mysql服务做了mysql的双向同步。由于mysql有连接池原因,没有给mysql做keepalived高可用部署,所以140服务器连接130服务器mysql,141服务器应用连接131服务器mysql。
- 第三台和第四台服务器用于安装应用服务,做了keepalived高可用部署,这里就不多说了。
一、安装部署Redis
两台服务器都按照此方式安装
1.1.执行redis安装脚本。
./redis_install.sh
(如果在安装时报错:/bin/sh: cc: command not found 说明没有安装gcc)
1.2.安装完成之后,进入redis安装目录,测试是否安装成功。
cd /usr/local/redis/
redis-cli -a 123456
set name sunwu
get name
1.3. redis双机热备配置
cd /usr/local/redis/
vim redis.conf
192.168.110.130 master 中redis.conf 配置
daemonize yes
requirepass 123456
bind 0.0.0.0
slave-serve-stale-data yes
slave-read-only no
192.168.110.131 slave 中redis.conf 配置
slaveof 192.168.110.130 6379
masterauth 123456
slave-serve-stale-data yes
slave-read-only no
测试是否成功
192.168.110.130 master 中
cd /usr/local/redis/
redis-cli -a 123456
set name sunwu
get name
keys *
192.168.110.131 slave 中
如果可以get到name的值,就证明成功了。
cd /usr/local/redis/
redis-cli -a 123456
get name
keys *
二、安装部署keepalived
2.1.环境准备
安装openssl: yum -y install openssl-devel
安装tcl: yum install tcl
安装Expect:yum install expect
(离线安装的话,自己去网上查吧,写起来太累了)
2.2.keepalived安装
官网下载地址:https://www.keepalived.org/download.html
到官网下载keepalived,上传到服务器(这次使用的是keepalived-2.0.20)
- 解压:tar -zxvf keepalived-2.0.20.tar.gz
- 进入解压后的文件:cd keepalived-2.0.20
- 执行配置命令:./configure --prefix=/usr/local/keepalived
- 执行:make && make install
- 将编译好的keepalived 配置文件进行地方修改
- mkdir /etc/keepalived
- cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- ln -s /usr/local/keepalived/sbin/keepalived /sbin/
- 配置开机自启:vi /etc/rc.local,在下面追加systemctl start keepalived
2.3.keepalived安装完成,接下来修改配置文件及脚本
- 修改 192.168.110.130 master keepalived.conf 配置文件
cd /etc/keepalived/
vim keepalived.conf
keepalived.conf 内容如下:
! Configuration File for keepalived
global_defs {
router_id 130
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" #监控脚本
interval 2
}
vrrp_instance VI_1 {
state MASTER
interface ens192 #网卡地址,linux查看ip addr
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress {
192.168.110.132 #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
}
- 修改 192.168.110.131 salve keepalived.conf 配置文件
cd /etc/keepalived/
vim keepalived.conf
keepalived.conf 内容如下:
! Configuration File for keepalived
global_defs {
router_id 131
}
vrrp_script chk_redis {
script "/etc/keepalived/scripts/redis_check.sh" #监控脚本
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens192 #网卡地址,linux查看ip addr
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_redis
}
virtual_ipaddress {
192.168.1190.132 #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
}
- 添加 192.168.110.130 master 脚本文件
cd /etc/keepalived/
mkdir scripts
cd scripts
touch redis_check.sh
touch redis_master.sh
touch redis_backup.sh
touch redis_fault.sh
touch redis_stop.sh
redis_master.sh文件代码
#!/bin/bash
REDISCLI="/usr/local/redis/src/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
sleep 15
echo "[master]" >> $LOGFILE
date >> $LOGFILE
echo "Being master...." >>$LOGFILE 2>&1
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.110.131 6379 >>$LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILE 2>&1
else
echo "data rsync OK." >> $LOGFILE 2>&1
fi
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE 2>&1
fi
redis_backup.sh文件代码
#!/bin/bash
REDISCLI="/usr/local/redis/src/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.110.131 6379 >>$LOGFILE 2>&1
- 添加 192.168.110.131 salve 脚本文件
redis_master.sh文件代码
#!/bin/bash
REDISCLI="/usr/local/redis/src/redis-cli -a 123456"
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 192.168.110.130 6379 >>$LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "data rsync fail." >>$LOGFILE 2>&1
else
echo "data rsync OK." >> $LOGFILE 2>&1
fi
sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ...">> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
if [ $? -ne 0 ];then
echo "Run SLAVEOF NO ONE cmd fail." >>$LOGFILE 2>&1
else
echo "Run SLAVEOF NO ONE cmd OK." >> $LOGFILE 2>&1
fi
redis_backup.sh文件代码
#!/bin/bash
REDISCLI="/usr/local/redis/src/redis-cli -a 123456"
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being slave...." >>$LOGFILE 2>&1
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run SLAVEOF cmd ...">> $LOGFILE
$REDISCLI SLAVEOF 192.168.110.130 6379 >>$LOGFILE 2>&1
- redis_check.sh、redis_fault.sh、redis_stop.sh 这三个脚本文件两台服务器一样。
redis_check.sh文件代码
#!/bin/bash
ALIVE=`/usr/local/redis/src/redis-cli -a 123456 PING 2>/dev/null`
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
redis_fault.sh文件代码
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
redis_stop.sh文件代码
#!/bin/bash
lOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
2.2.4. keepalived 脚本目录
然后再 master和 salve 两台服务器中 执行:
chmod +x /etc/keepalived/scripts/*.sh
2.2.5. 验证是否成功
1、启动master 和 salve中 的redis
service redisd restart
2、启动master 和 salve中的 keepalived
service keepalived restart
3、用VIP连接redis
redis-cli -h 192.168.110.132 -a 123456
- 输入 info 查看redis信息
master 中
salve中
-
redis数据同步测试
master 中set 值进去
salve中,是否可以get到
-
停掉 master 中的redis服务,看是否VIP会飘到salve上
service redisd stop
输入info 查看Replication / connected_slaves 为0
在slaves中,set 一个值
启动 master 中的redis服务
service redisd start
连接redis服务
redis-cli -h 192.168.119.132 -a 123456
查看信息是否同步过来
到这里,keepalived+redis 双机热备就部署完成啦,有问题的小伙伴下面留言哦。
更多推荐
所有评论(0)