通过Keepalived实现Redis双机(2)

启动Keepalived服务

启动Master和Slave机器上的keepalived服务 #service keepalived start

查看vip是否生效并看日志信息是否有报错

[root@node3 keepalived]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000 link/ether 00:23:7d:33:2f:32 brd ff:ff:ff:ff:ff:ff inet 192.168.172.3/24 brd 192.168.172.255 scope global eth0 inet 192.168.172.100/32 scope global eth0 inet6 fe80::223:7dff:fe33:2f32/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 00:23:7d:33:2f:4c brd ff:ff:ff:ff:ff:ff

查看系统日志

[root@node3 keepalived]# tail /var/log/nodes Nov 11 18:02:32 node3 Keepalived_vrrp: Using LinkWatch kernel netlink reflector... Nov 11 18:02:32 node3 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. Nov 11 18:02:32 node3 Keepalived_healthcheckers: Configuration is using : 7114 Bytes Nov 11 18:02:32 node3 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)] Nov 11 18:02:32 node3 Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector... Nov 11 18:02:33 node3 Keepalived_vrrp: VRRP_Instance(mes_Redis) Transition to MASTER STATE Nov 11 18:02:34 node3 Keepalived_vrrp: VRRP_Instance(mes_Redis) Entering MASTER STATE Nov 11 18:02:34 node3 Keepalived_vrrp: VRRP_Instance(mes_Redis) setting protocol VIPs. Nov 11 18:02:34 node3 Keepalived_vrrp: VRRP_Instance(mes_Redis) Sending gratuitous ARPs on eth0 for 192.168.172.100 Nov 11 18:02:44 node3 Keepalived_vrrp: VRRP_Instance(mes_Redis) Sending gratuitous ARPs on eth0 for 192.168.172.100

在网络中其它机器ping vip

这时keepalived就实现了基本功能了,下面来配置redis服务。

Redis实现Master-Slave架构,Master只使用内存存储数据,对外提供读写服务,Slave仅作为Standby,进行数据持久化。

配置Redis Master并启动,Master采用内存工作的方式。

[root@node3 etc]# more redis.master.conf daemonize yes port 6379 #bind 10.25.28.18 pidfile /tmp/redis.pid loglevel verbose logfile /data/logs/redis/redis_master.log timeout 300 databases 16 vm-enabled no maxmemory-policy volatile-ttl slave-serve-stale-data yes appendonly no appendfsync everysec no-appendfsync-on-rewrite yes hash-max-zipmap-entries 64 hash-max-zipmap-value 512 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entries 512 activerehashing yes [root@node3 etc]# /usr/local/redis/bin/redis-server /etc/redis/redis.master.conf

配置Redis Slave并启动

[root@node4 etc]# more redis.slave.conf daemonize yes port 6379 #bind 10.25.28.18 slaveof 192.168.172.3 6379 pidfile /tmp/redis.pid loglevel verbose logfile /data/logs/redis/redis_slave.log dir /data/redis timeout 300 databases 16 vm-enabled no maxmemory-policy volatile-ttl rdbcompression yes slave-serve-stale-data yes appendonly yes appendfsync everysec no-appendfsync-on-rewrite yes hash-max-zipmap-entries 64 hash-max-zipmap-value 512 list-max-ziplist-entries 512 list-max-ziplist-value 64 set-max-intset-entr [root@node4 etc]# /usr/local/redis/bin/redis-server /etc/redis/redis.slave.conf 进行测试 测试数据同步

在Master上插入一些测试数据

[root@node3 etc]# redis-cli redis 127.0.0.1:6379> set foo bar OK redis 127.0.0.1:6379> get foo "bar"

然后在Slave上获取测试数据

[root@node4 etc]# redis-cli redis 127.0.0.1:6379> get foo "bar" 配置状态检查

配置keepalived对Redis进行状态检查

在Redis Master上配置如下,修改keepalived.conf

[root@node3 redis]# more /etc/keepalived/keepalived.conf global_defs { notification_email { admin@qixing318.com } notification_email_from keepalived@node3 router_id node3 } vrrp_script chk_redis { script "/etc/keepalived/redis/redis_check.sh" interval 1 } vrrp_instance mes_Redis { state MASTER interface eth0 garp_master_delay 10 smtp_alert virtual_router_id 3 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.172.100 } track_script { chk_redis } notify_master /etc/keepalived/redis/master.sh notify_backup /etc/keepalived/redis/backup.sh notify_fault /etc/keepalived/redis/fault.sh notify_stop /etc/keepalived/redis/stop.sh }

创建以下脚本:

[root@node3 redis]# pwd /etc/keepalived/redis [root@node3 redis]# ll total 16 -rwxr-xr-x 1 root root 442 Nov 12 10:03 backup.sh -rwxr-xr-x 1 root root 280 Nov 12 10:01 fault.sh -rwxr-xr-x 1 root root 564 Nov 12 10:03 master.sh -rwxr-xr-x 1 root root 149 Nov 12 09:24 redis_check.sh [root@node3 redis]# more * :::::::::::::: backup.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log RLOG=/data/logs/redis/redis-state.log echo "[backup]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG echo "[backup]" >> $RLOG date >> $RLOG echo "being slave...." >> $RLOG 2>&1 echo " run slaveof cmd ..." >> $RLOG echo SLAVEOF 192.168.172.4 6379 | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 echo " run sync cmd ..." >> $RLOG echo SYNC | /usr/local/redis/bin/redis-cli >> $RLOG 2>&1 :::::::::::::: fault.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log echo "[fault]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG [root@CentOSV1 redis]# more stop.sh #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log echo "[stop]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG :::::::::::::: master.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log RLOG=/data/logs/redis/redis-state.log echo "[master]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG echo "[master]" >> $RLOG date >> $RLOG echo "being master...." >> $RLOG 2>&1 echo " run slaveof cmd ..." >> $RLOG echo SLAVEOF 192.168.172.4 6379 | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 sleep 1 echo " run sync cmd ..." >> $RLOG echo SYNC | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 sleep 1 echo " run slaveof no one cmd ..." >> $RLOG echo SLAVEOF NO ONE | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 :::::::::::::: redis_check.sh :::::::::::::: #!/bin/sh alive=`/usr/local/redis/bin/redis-cli PING` if [ "$alive" == "PONG" ]; then echo "OK" exit 0 fi #echo "$alive" echo "NG" exit 1

在Redis Slave机器上配置如下:

[root@node4 keepalived]# vi /etc/keepalived/keepalived.conf global_defs { notification_email { admin@qixing318.com } notification_email_from keepalived@node4 router_id node4 } vrrp_script chk_redis { script "/etc/keepalived/redis/redis_check.sh" interval 1 } vrrp_instance mes_Redis { state BACKUP interface eth0 garp_master_delay 10 smtp_alert virtual_router_id 3 priority 90 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.172.100 } track_script { chk_redis } notify_master /etc/keepalived/redis/master.sh notify_backup /etc/keepalived/redis/backup.sh notify_fault /etc/keepalived/redis/fault.sh notify_stop /etc/keepalived/redis/stop.sh }

创建脚本如下:

[root@node4 redis]# cd /etc/keepalived/redis/ [root@node4 redis]# ls backup.sh fault.sh master.sh redis_check.sh [root@node4 redis]# more * :::::::::::::: backup.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log RLOG=/data/logs/redis/redis-state.log echo "[backup]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG echo "[backup]" >> $RLOG date >> $RLOG sleep 10 echo "being slave...." >> $RLOG 2>&1 echo " run slaveof cmd ..." >> $RLOG echo SLAVEOF 192.168.172.3 6379 | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 echo " run sync cmd ..." >> $RLOG echo SYNC | /usr/local/redis/bin/redis-cli >> $RLOG 2>&1 :::::::::::::: fault.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log echo "[fault]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG [root@CentosV1 redis]# more stop.sh #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log echo "[stop]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG :::::::::::::: master.sh :::::::::::::: #!/bin/sh KLOG=/data/logs/redis/keepalived-state.log RLOG=/data/logs/redis/redis-state.log echo "[master]" >> $KLOG date >> $KLOG echo "----------" >> $KLOG echo "[master]" >> $RLOG date >> $RLOG echo "being master...." >> $RLOG 2>&1 echo " run slaveof cmd ..." >> $RLOG echo SLAVEOF 192.168.172.3 6379 | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 sleep 1 echo " run sync cmd ..." >> $RLOG echo SYNC | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 sleep 1 echo " run slaveof no one cmd ..." >> $RLOG echo SLAVEOF NO ONE | /usr/local/redis/bin/redis-cli >>$RLOG 2>&1 :::::::::::::: redis_check.sh :::::::::::::: #!/bin/sh alive=`/usr/local/redis/bin/redis-cli PING` if [ "$alive" == "PONG" ]; then echo "OK" exit 0 fi #echo "$alive" echo "NG" exit 1

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/73db2b9ac894f4d429e2baea159fc29c.html