Skip to content

Instantly share code, notes, and snippets.

@tetsun
Last active April 11, 2017 00:47
Show Gist options
  • Save tetsun/80b0102a270128c075950ba4de90692c to your computer and use it in GitHub Desktop.
Save tetsun/80b0102a270128c075950ba4de90692c to your computer and use it in GitHub Desktop.

redis+keepalived

Hosts

  1. vip => 192.168.18.101
  2. redis(master) + keepalived => 192.168.18.102
  3. redis(slave) + keepalived => 192.168.18.103
  4. sidekiq => 192.168.18.110

OS

centos7

redis

Install

yum -y install epel-release
yum -y install redis

Config

/etc/redis.conf

master

bind 0.0.0.0

slave

bind 0.0.0.0
slaveof 192.168.18.102 6379

Start Service

systemctl start redis
systemctl enable redis

Check

master

redis-cli role
1) "master"
2) (integer) 0
3) (empty list or set)

slave

redis-cli role
1) "slave"
2) "192.168.18.102"
3) (integer) 6379
4) "connect"
5) (integer) -1

keeplived

Install

yum -y install keepalived

Config

/etc/keepalived/keepalived.conf

master

global_defs {
    notification_email {
        admin@example.com
    }
    notification_email_from keepalived@example.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id REDIS_KEEPALIVED
}

vrrp_script check_redis {
    script "/usr/local/bin/check_redis.sh"
    interval 2
    fall 2
    rise 2
}

vrrp_instance REDIS {
    state BACKUP
    nopreempt
    interface eth1
    virtual_router_id 51
    priority 110
    advert_int 1
    unicast_peer {
        192.168.18.103
    }
    authentication {
        auth_type PASS
        auth_pass redis
    }
    virtual_ipaddress {
        192.168.18.101
    }
    track_script {
        check_redis
    }
    notify_master /usr/local/bin/notify_master_redis.sh
    notify_backup /usr/local/bin/notify_slave_redis.sh
}

slave

global_defs {
    notification_email {
        admin@example.com
    }
    notification_email_from keepalived@example.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id REDIS_KEEPALIVED
}

vrrp_script check_redis {
    script "/usr/local/bin/check_redis.sh"
    interval 2
    fall 2
    rise 2
}

vrrp_instance REDIS {
    state BACKUP
    nopreempt
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_peer {
        192.168.18.102
    }
    authentication {
        auth_type PASS
        auth_pass redis
    }
    virtual_ipaddress {
        192.168.18.101
    }
    track_script {
        check_redis
    }
    notify_master /usr/local/bin/notify_master_redis.sh
    notify_backup /usr/local/bin/notify_slave_redis.sh
}

/usr/local/bin/check_redis.sh

#!/bin/sh

/bin/redis-cli info

/usr/local/bin/notify_master_redis.sh

#!/bin/sh

/bin/redis-cli slaveof no one

/usr/local/bin/notify_slave_redis.sh

master

#!/bin/sh

/bin/redis-cli slaveof 192.168.18.103 6379

slave

#!/bin/sh

/bin/redis-cli slaveof 192.168.18.102 6379

Start Service

systemctl start keepalived
systemctl enable keepalived

Check

ip addr show dev eth1 | grep 192.168.18.101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment