Hướng dẫn cài đặt địa chỉ ảo bằng Keepalived trên linux

Khởi tạo máy ảo

Vagrant.configure("2") do |config|
    config.ssh.insert_key = false
    config.vm.define "keepalived-1" do |cf1|
      cf1.vm.box = "centos/7"
      cf1.vm.hostname ="keepalived-1"
      cf1.vm.network :private_network, ip: "10.0.0.10"
      cf1.vm.provider "virtualbox" do |vb|
          vb.memory = "2000"
      end
    end

    config.ssh.insert_key = false
    config.vm.define "keepalived-2" do |cf1|
      cf1.vm.box = "centos/7"
      cf1.vm.hostname ="keepalived-2"
      cf1.vm.network :private_network, ip: "10.0.0.15"
      cf1.vm.provider "virtualbox" do |vb|
          vb.memory = "2000"
      end
    end

end
vagrant up

Cài đặt các package cần thiết để build

yum -y install kernel-headers kernel-devel curl gcc openssl-devel libnl3-devel net-snmp-devel wget

Cài đặt keepalive

cd /opt
wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz --no-check-certificate
tar -xvzf keepalived-2.2.8.tar.gz
ln -s keepalived-2.2.8 keepalived
cd keepalived
./configure --prefix=/usr/local/keepalived
make && make install

Cấu hình keepalive

vrrp_instance KEEPALIVED_VIP {
    state MASTER # or "BACKUP" on backup
    mcast_src_ip 10.0.0.10
    interface eth1
    priority 101 # 101 on master, 100 on backup
    virtual_router_id 101
    advert_int 1
    use_vmac
    #smtp_alert # Activate SMTP notifications

    authentication {
        auth_type PASS
        auth_pass keepalivedlab_pass
    }

    virtual_ipaddress {
        10.0.0.100/24 dev eth0
    }
}
vrrp_instance KEEPALIVED_VIP {
    state BACKUP # or "BACKUP" on backup
    mcast_src_ip 10.0.0.15
    interface eth1
    priority 100 # 101 on master, 100 on backup
    virtual_router_id 102
    advert_int 1
    use_vmac
    #smtp_alert # Activate SMTP notifications

    authentication {
        auth_type PASS
        auth_pass keepalivedlab_pass
    }

    virtual_ipaddress {
        10.0.0.100/24 dev eth0
    }
}
#!/bin/sh
PID_FILE="/var/run/keepalived.pid"
BIN_DIR="/usr/local/keepalived/sbin"
CONFIG_DIR="/usr/local/keepalived/etc"
RETVAL=0

. /etc/init.d/functions

start() {
    echo -n "Starting Keepalived for LVS: "
    daemon $BIN_DIR/keepalived -f $CONFIG_DIR/keepalived.conf -D --log-facility 5
    RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/keepalived
        return $RETVAL
}

stop() {
    echo -n "Shutting down Keepalived for LVS: "
    killproc keepalived
    RETVAL=0
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/keepalived
        return $RETVAL
}

reload() {
    echo -n "Reloading Keepalived config: "
    killproc keepalived -1
    RETVAL=$?
        echo
        return $RETVAL
}


case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  reload)
        reload
        ;;
  status)
        status keepalived
        ;;
  condrestart)
        [ -f /var/lock/subsys/keepalived ] && $0 restart || :
        ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
    exit 1
esac

exit 0
chmod +x /opt/init.d/keepalived
/opt/init.d/keepalived start