keepalived 安装

------------------------------------
yum install kernel-devel
 
wget http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
 
  tar zxvf keepalived-1.2.1.tar.gz 
 
 cd keepalived-1.2.1
 
 
  ./configure --with-kernel-dir=/usr/src/kernels/2.6.18-308.13.1.el5-i686/
 
  make 
  make  install
 
 
 
mkdir /etc/keepalived  --创建文件夹
 
vim /etc/keepalived/keepalived.conf
 
----------------------------------
 
 
################添加删除虚拟IP#############
ip addr del 192.168.1.198/32 dev eth0 
 
 
 
 ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0
 
 
 
-------------------增加keepalived日志功能---------------
vim  /usr/local/etc/sysconfig/keepalived
 
增加 KEEPALIVED_OPTIONS="-D -d -S 0"
 
 
 
 
 
-----增加 vim /etc/syslog.conf
 
# keepalived -S 0 
 local0.*                                                /var/log/keepalived.log
 
 
重启一下 /etc/init.d/syslog restart 
 
------------------------------------------------------------------
相关配置文件:
192.168.1.100   192.168.1.101
 
100 上的  vim  /etc/keepalived/keepalived.conf 
 
---------------------------------------
! Configuration File for keepalived
 
 
global_defs {
 
   router_id LVS_DEVEL
 
}
 
 
# 20081013 written by :netseek
 
# VIP1
 
vrrp_instance VI_1 {
 
    state MASTER             #备份服务器上将MASTER改为BACKUP   
 
    interface eth0
 
    virtual_router_id 51
 
    priority 100    # 备份服务上将100改为99
 
    advert_int 1
 
    nopreempt  #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
 
    authentication {
 
        auth_type PASS
 
        auth_pass 1111
 
    }
 
    virtual_ipaddress {
 
        192.168.1.222
 
        #(如果有多个VIP,继续换行填写.)
 
    }
 
}
 
virtual_server 192.168.1.222 3306 {
 
    delay_loop 3                  #(每隔10秒查询realserver状态)
 
    lb_algo wrr                  #(lvs 算法)
 
    lb_kind DR                  #(Direct Route)
 
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
 
    protocol TCP                #(用TCP协议检查realserver状态)
 
 
    real_server 192.168.1.100 3306 {
 
        weight 1               #(权重)
 
        notify_down /usr/local/mysql/bin/mysql.sh  #检测到服务down后执行的脚本
 
        TCP_CHECK {
 
        connect_timeout 10       #(10秒无响应超时)
 
        nb_get_retry 3
 
        delay_before_retry 3
 
        connect_port 3306
 
        }
 
    }
 
 
 
}
 
 
 
----------------------------------------------------------------------
102 上的  vim  /etc/keepalived/keepalived.conf 
 
! Configuration File for keepalived
 
 
global_defs {
 
   router_id LVS_DEVEL
 
}
 
 
# 20081013 written by :netseek
 
# VIP1
 
vrrp_instance VI_1 {
 
    state MASTER             #备份服务器上将MASTER改为BACKUP
 
    interface eth0
 
    virtual_router_id 51
 
    priority 90    # 备份服务上将100改为99
  
     advert_int 1
 
    authentication {
 
        auth_type PASS
 
        auth_pass 1111
 
    }
 
    virtual_ipaddress {
 
        192.168.1.222
 
        #(如果有多个VIP,继续换行填写.)
 
    }
 
}
 
 
virtual_server 192.168.1.222 3306 {
 
    delay_loop 3                  #(每隔10秒查询realserver状态)
 
    lb_algo wrr                  #(lvs 算法)
 
    lb_kind DR                  #(Direct Route)
 
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
 
    protocol TCP                #(用TCP协议检查realserver状态)
 
 
    real_server 192.168.1.102 3306 {
 
        weight 1               #(权重)
 
        notify_down /usr/local/mysql/bin/mysql.sh  #检测到服务down后执行的脚本
 
        TCP_CHECK {
 
        connect_timeout 10       #(10秒无响应超时)
 
 nb_get_retry 3
 
        delay_before_retry 3
 
        connect_port 3306
 
        }
 
    }
 
 
 
}
 
 
------------------------------------------------------
 
 
 ------------------------------------------------------
vim /usr/local/mysql/bin/mysql.sh
 
#!/bin/sh  
   pkill keepalived
 
-----------------------------------------
 
 
 
测试:
 
1.先开启100 mysql服务,102 mysql 服务
 
2.keepalived -D -d -S 0
 
3.ip addr (100 和101 上都看看,100优先级高,就所以虚拟IP  一开始绑定在100 上)
4.100 service mysqld stop
 
5.查看 ip addr(ip  去了101上了)
 
6.100的mysql 服务再启动
 
7.ip addr
 虚拟IP  又回来了
 
 
----------------------------------