Redis Master-Slave

2-node Redis master-slave with automatic swithover.
# On both nodes
yum install redis
systemctl enable redis

# on both nodes in /etc/redis.conf
#
# 1. comment line #bind 127.0.0.1
#
# 2. set protected-mode no

# start on both
systemctl start redis

install and configure keepalived
yum install keepalived -y && systemctl enable keepalived
/etc/keepalives/notify.sh
#!/bin/bash

ENDSTATE=$3
NAME=$2
TYPE=$1
host=`hostname`
current_date=`/bin/date +"%b %d %H:%M:%S"`

case $ENDSTATE in
    "MASTER")
        logger "$current_date Keepalived: $host is now Master server with VIP"
        redis-cli slaveof no one
        exit 0
    ;;
    *)
        logger "$current_date Keepalived: $host is now Backup"
	# on node 1
        redis-cli slaveof 10.0.0.12 6379
	# on node 2
        # redis-cli slaveof 10.0.0.11 6379
        exit 1
    ;;
esac
/etc/keepalived/keepalived.conf
vrrp_script chk_redis {
 #  script "redis-cli info replication | grep -q 'role:master'"
    script "killall -0 redis-server"
    interval 2
    weight 2
}
vrrp_instance VI_1 {
    interface ens160
 ## for master node
    state MASTER
    priority 101
 ## for backup node
 #    state BACKUP
 #    priority 100
    virtual_router_id 10
    advert_int 1
    virtual_ipaddress {
        10.0.0.10 label ens160:vip
    }
    track_script {
        chk_redis
    }
    authentication {
        auth_type PASS
        auth_pass [email protected]
    }
    notify "/etc/keepalived/notify.sh"
    notify_stop "/etc/keepalived/notify.sh 1 1 FAULT"
}
start keepalived and check redis
# on both
systemctl start keepalived

# check virtual ip is ok
ip a

# check redis replica on both
redis-cli info replication

# check data replica
# on node 1
redis-cli set a 1

# on node 2
redis-cli get a
> "1"