Keepalived+lvs

# yum install ipvsadm

Keepalived :

1. get keepalived.

# wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

2.解压keepalived.

# tar xzvf keepalived-1.2.12.tar.gz

3. 进入目录

# cd keepalived-1.2.12

4. 安装包.

# yum install openssl-devel.x86_64 kernel-headers kernel-devel

5. 配置keepalived.

#./configure

6. 编译.

# make

7. 编译安装.

# make install

8.自启动服务.

#  cd /etc/sysconfig  
#  ln -s /usr/local/etc/sysconfig/keepalived .  
#  cd /etc/rc3.d/  
#  ln -s /usr/local/etc/rc.d/init.d/keepalived S100keepalived  
#  cd /etc/init.d/  
#  ln -s /usr/local/etc/rc.d/init.d/keepalived
#  ln -s /usr/local/sbin/keepalived /usr/sbin/
#  cd /etc/keepalived
#  ln -s /usr/local/etc/keepalived/keepalived.conf .

keepalived.conf

! Configuration File for keepalived
global_defs {  
   router_id MYSQLD  
}
vrrp_instance MYSQLD_3306 {  
        state MASTER  
        interface em2  
        virtual_router_id 22  
        priority 101  
        advert_int 1  
        mcast_src_ip 192.168.253.9  
        authentication {  
                auth_type PASS  
                auth_pass lekan  
        }
        virtual_ipaddress {  
                192.168.253.100/24 dev em2 scope global  
        }  
        notify_master "/etc/keepalived/notification_email.sh master"  
        notify_backup "/etc/keepalived/notification_email.sh backup"  
}
virtual_server 192.168.253.100 3306 {  
        delay_loop 2  
        lb_algo rr  
        lb_kind DR  
        persistence_timeout 10  
        protocol TCP
        real_server 192.168.253.6 3306 {  
                weight 1  
                inhibit_on_failure  
                notify_up "/etc/keepalived/mysql.sh up"  
                notify_down "/etc/keepalived/mysql.sh down"  
                TCP_CHECK {  
                        connect_timeout 3  
                        nb_get_retry 3  
                        delay_before_retry 3  
                        connect_port 3306  
                }  
        }
        real_server 192.168.253.8 3306 {  
                weight 1  
                inhibit_on_failure  
                notify_down "/etc/keepalived/mysql.sh down"  
                notify_up "/etc/keepalived/mysql.sh up"  
                TCP_CHECK {  
                        connect_timeout 3  
                        nb_get_retry 3  
                        delay_before_retry 3  
                        connect_port 3306  
                }  
        }  
}

mysql.sh

#!/bin/bash
case $1 in  
        up)  
                echo "mysqld up" | mutt -s "keepalived mysqld" [email protected]  
        ;;  
        down)  
                echo "mysqld down" | mutt -s "keepalived mysqld" [email protected]  
        ;;  
        *)  
                echo "keepalived" >> /home/tong.li/keepalived.txt  
        ;;  
esac

notification_email.sh

#!/bin/bash
case $1 in  
        master)  
                echo "keepalived master failover" | mutt -s "keepalived" [email protected]  
        ;;  
        backup)  
                echo "keepalived backup failover" | mutt -s "keepalived" [email protected]  
        ;;  
        *)  
                echo "keepalived" >> /home/tong.li/keepalived.txt  
        ;;  
esac

realserver.sh

#!/bin/bash 
#description:start realserver 
VIP=192.168.253.100 
. /etc/rc.d/init.d/functions 
case "$1" in
start) 
    /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up 
    /sbin/route add -host $VIP dev lo:0 
    echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore 
    echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce 
    echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore 
    echo "2">/proc/sys/net/ipv4/conf/all/arp_announce 
    sysctl -p >/dev/null 2>&1 
    echo "start LVS of RealServer DR" 
    ;;
stop) 
    /sbin/ifconfig lo:0 down 
    route del $VIP>/dev/null 2>&1 
    echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore 
    echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce 
    echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore 
    echo "0">/proc/sys/net/ipv4/conf/all/arp_announce 
    echo "close LVS of RealServer DR" 
    ;; 
*) 
    echo "Usage:$0 {start|stop}" 
    exit 1 
;; 
esac


你可能感兴趣的:(LVS,keepalived)