分类: Redis
Keepalived 2.0.9 + Redis5.0部署redis主从高可用

项目需要部署搭建redis主从高可用环境,对外使用VIP提供服务,以下是实现步骤:
Keepalived 实现VRRP(虚拟路由冗余)协议,从路由级别实现VIP切换,可以完全避免类似heartbeat脑裂问题,可以很好的实现主从、主备、互备方案。
实现切换逻辑如下:A和B两台机器

1)A 、B机器依次启动,A机作为主、B机为从。
2)主A挂掉,B接管业务并作为主。
3)A机起来,作为从SLAVEOF B。
4)B机挂掉,A机再切回主。

在Keepalived 有两个角色:Master(一个)、Backup(多个),如果设置一个为Master,但Master挂了后再起来,必然再次业务又一次切换,这对于有状态服务是不可接受的。解决方案就是两台机器都设置为Backup,而且优先级高的Backup设置为nopreemt 不抢占主。
服务器环境:

192.168.255.198    VIP
192.168.255.64    redis-master     安装CentOS6.10 redis(5.0版本)、keepalived(2.0.9版本)
192.168.255.65    redis-slave      安装CentOS6.10 redis(5.0版本)、keepalived(2.0.9版本)

部署开始,免去系统初始化、调优等步骤:
以下步骤两台机器都要安装:
1、安装redis5.0

cd /opt
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xvf redis-5.0.0.tar.gz
mv redis-5.0.0 /usr/local/redis
cd /usr/local/redis
make
ln -s /usr/local/redis/src/redis-cli /usr/bin/

2、修改配置文件

bind 0.0.0.0
protected-mode yes
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6380.pid
loglevel notice
logfile "/usr/local/redis/6380.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/
replica-serve-stale-data yes
slaveof 192.168.255.65 6380   # slave机器设置为slaveof 192.168.255.64 6380
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
maxclients 60000
maxmemory 30064771072
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

3、启动redis

/usr/local/redis/src/redis-server /usr/local/redis/redis.conf

4、安装keepalived2.0.9 两台都安装

yum install popt popt-devel libnl libnl-devel libnfnetlink-devel -y #安装基础库文件
wget http://www.keepalived.org/software/keepalived-2.0.9.tar.gz
tar xvf keepalived-2.0.9.tar.gz
cd keepalived-2.0.9.tar.gz
./configure --prefix=/usr/local/keepalived --sbindir=/usr/local/keepalived/sbin
make && make install
ln -s /usr/local/sbin/keepalived /usr/sbin/  
ln -s /usr/local/keepalived/etc/keepalived/ /etc/keepalived
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

Keepalived Master配置文件:

cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
 
global_defs {
  router_id redis-master
}
 
vrrp_script chk_redis {
  script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6380"        #监控脚本 
  interval 2                                    #监控时间
  timeout 2                                     #超时时间
  fall 3
}
 
vrrp_instance redis {
  state BACKUP                             
  interface em1
  lvs_sync_daemon_interface em1
  virtual_router_id 202
  priority 150                              #权重值                 
  nopreempt                                 #nopreempt:设置不抢占,这里只能设置在state为backup的节点上,而且这个节点的优先级必须比另外节点的高
  advert_int 1
 
  authentication {                          #all node must same             
    auth_type PASS                            #加密   
    auth_pass 1111                            #密码
  }
 
  virtual_ipaddress {
    192.168.255.189                            #VIP地址                 
  }
 
  track_script {
    chk_redis
  }
 
notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.255.64 6380"
notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.255.65 6380"
notify_fault /usr/local/keepalived/scripts/redis_fault.sh
notify_stop /usr/local/keepalived/scripts/redis_stop.sh
}

Keepalived Slave配置文件:

cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
 
global_defs {
  router_id redis-slave
}
 
vrrp_script chk_redis{
  script "/usr/local/keepalived/scripts/redis_check.sh 127.0.0.1 6380"                           
  interval 2                                    
  timeout 2                                    
  fall 3                                   
}
 
vrrp_instance redis {
  state BACKUP                                    
  interface eth0                               
  lvs_sync_daemon_interface eth0                     
  virtual_router_id 202
  priority  100                              
  nopreempt
  advert_int 1
 
  authentication {  
    auth_type PASS                              
    auth_pass 1111                        
  }
 
  virtual_ipaddress {
    192.168.255.189                                    
  }
 
  track_script {
  chk_redis
  }
 
notify_master "/usr/local/keepalived/scripts/redis_master.sh 127.0.0.1 192.168.255.64 6380"
notify_backup "/usr/local/keepalived/scripts/redis_backup.sh 127.0.0.1 192.168.255.65 6380"
notify_fault /usr/local/keepalived/scripts/redis_fault.sh
notify_stop /usr/local/keepalived/scripts/redis_stop.sh
}

以上的keepalived.conf文件中的切换模式设置为nopreempt,意思是:
不抢占VIP资源,此种模式要是所有的节点都必须设置为state BACKUP模式
需要注意无论主备服务器都需要设置为BACKUP,与以往KeepAlived的配置不同,其目的就是防止主服务器恢复后重新抢回VIP,导致Redis切换从而影响稳定
创建脚本目录:

mkdir -p /usr/local/keepalived/scripts

创建脚本文件:

vim /usr/local/keepalived/scripts/redis_backup.sh 
#!/bin/bash
REDISCLI="/usr/local/redis/src/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                                             #延迟100秒以后待数据同步完成后再取消同步状态
exit(0)
vim /usr/local/keepalived/scripts/redis_check.sh 
#!/bin/bash
ALIVE=`/usr/local/redis/src/redis-cli -h $1 -p $2 PING`
LOGFILE="/var/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
vim /usr/local/keepalived/scripts/redis_fault.sh 
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
vim /usr/local/keepalived/scripts/redis_master.sh 
#!/bin/bash
REDISCLI="/usr/local/redis/src/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                                               #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE
$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
vim /usr/local/keepalived/scripts/redis_stop.sh 
#!/bin/bash
LOGFILE="/var/log/keepalived-redis-state.log"
echo "[stop]" >> $LOGFILE
date >> $LOGFILE

给脚本执行权限:

chmod +x /usr/local/keepalived/scripts/ -R

脚本同步至slave:

scp -r scripts 192.168.255.65:/usr/local/keepalived/

两台机器依次启动keepalived、redis:

/usr/local/keepalived/sbin/keepalived -D
/usr/local/redis/src/redis-server /usr/local/redis/redis.conf

查看redis、keepalived进程是否启动

ps -ef | grep redis-server
root       2654      1  0 17:22 ?        00:00:05 /usr/local/redis/src/redis-server 0.0.0.0:6380               
root      12140   2731  0 18:49 pts/0    00:00:00 grep redis-server
[root@sso-redis-a opt]# ps -ef | grep keep
root       2644      1  0 17:22 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
root       2645   2644  0 17:22 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
root      11987   2731  0 18:48 pts/0    00:00:00 grep keep

将redis、keepalived服务加入开机启动

echo '/usr/local/keepalived/sbin/keepalived -D' >> /etc/rc.d/rc.local
echo '/usr/local/redis/src/redis-server /usr/local/redis/redis.conf'  >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local

查看IP地址,VIP已经起来:

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 14:18:77:52:28:11 brd ff:ff:ff:ff:ff:ff
    inet 192.168.255.64/24 brd 192.168.255.255 scope global em1
    inet 192.168.255.189/32 scope global em1
    inet6 fe80::1618:77ff:fe52:2811/64 scope link 
       valid_lft forever preferred_lft forever

查看redis状态

redis-cli -h 192.168.255.189.230 -p 6380 INFO|grep role
role:master
redis-cli -h 192.168.255.189.64 -p 6380 INFO|grep role
role:master
redis-cli -h 192.168.255.189.65 -p 6380 INFO|grep role
role:slave

在master 64上set数据,65会有,主从生效
重启64机器,vip会在2秒内漂移到65上,同时65提升到master,在65插入数据,64服务器起来以后会自动同步。
依次类推,至此redis双机搭建完毕。
via: https://www.cnblogs.com/kevingrace/p/9001975.html


相关博文:

发表新评论