CentOS下利用Keepalived实现双击热备且监听到端口

几年前试验过老版本的Keepalived实现VRRP的功能参考http://yang-min.iteye.com/admin/blogs/227534

 

最近同事需要此功能,然后就重新梳理一下,发现新版本的Keepalived功能强大多了..下面进行介绍:

1.安装Keepalived

拷贝安装文件到/var/opt目录下,其实位置无所谓.MASTER,BACKUP两台机器都需要拷贝.

 

文件名:keepalived-1.2.8.tar.gz

 

tar -zxf keepalived-1.2.8.tar.gz
cd keepalived-1.2.8

#yum -y install gcc kernel-headers kernel-devel openssl-devel

./configure --sysconfdir=/etc
make && make install

cp /usr/local/sbin/keepalived /usr/sbin/

 2.Master配置

 

vi /etc/keepalived/keepalived.conf
覆盖为如下内容:

! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   #notification_email_from [email protected]
   #smtp_server smtp.qq.com
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {  
        script "</dev/tcp/127.0.0.1/80"
        interval 1
        weight -2
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.101.110
    }
	track_script {
			chk_http_port
	}
}

 注:这的state MASTER ,priority(类似优先级MASTER>BACKUP) virtual_ipaddress 为虚拟地址 chk_http_port 中配置的为本机所需要监控的服务端口

然后启动MASTER服务器/etc/init.d/keepalived start

 

3.BACKUP配置,除priority属性不同小于MASTER设置的值之外其余相同

 

 

vi /etc/keepalived/keepalived.conf
文件内容如下:

! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   #notification_email_from [email protected]
   #smtp_server smtp.qq.com
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {  
        script "</dev/tcp/127.0.0.1/80"
        interval 1
        weight -2
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.101.110
    }
    track_script {
        chk_http_port
    }
}

 

 

注:在BACKUP的keepalived.conf文件中state属性仍然是MASTER此种是根据priority设置的值进行区分MASTER和BACKUP,否则无法进行端口服务的监听.

然后启动BACKUP服务器/etc/init.d/keepalived start

 

 

然后可以进行测试了....

测试方案:这里的配置的虚拟ip:192.168.101.110 一直访问这个ip对应的应用,关闭MASTER机器上的80端口的服务,

则自动切换到BACKUP对于的80服务,启动MASTER的服务则又会自动切换到MASTER机器所在的服务.

你可能感兴趣的:(keepalived)