码迷,mamicode.com
首页 > 系统相关 > 详细

linux之keepalived详解

时间:2016-04-08 20:15:25      阅读:509      评论:0      收藏:0      [点我收藏+]

标签:双机热备

一、keepalive简介

     

     Keepalived是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健检查功能,如判断LVS负载调度器、节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集。在非LVS群集环境中使用时Keepalived也可以作为热备软件使用。

Keepalived采用VRRP(virtual  router  redundancy protocol,虚拟路由冗余协议)热备份协议,以软件的方式实现linux服务器的多机热备功能。VRRP是针对路由器的一种备份解决方案-----由多台路由器组成一个热备组。通过共用的虚拟IP地址对外提供服务;每个热备组内同一时刻只有一台主服务器提供服务,其他服务器处于冗余状态,若当前在线的服务器失败,其他服务器会自动接替(优先级决定接替顺序)虚拟IP地址,以继续提供服务。

热备组内的每台服务器都可以成为主服务器,虚拟IP地址(VIP)可以在热备组内的服务器之间进行转移,所以也称为漂移IP地址,使用Keepalived时,漂移地址的实现不需要手动建立虚拟接口配置文件(如eth0:0),而是由Keepalived根据配置文件自动管理。


二、keepalive原理


技术分享

keepalived启动后会有三个进程
父进程:内存管理,子进程管理等等
子进程:VRRP子进程
子进程:healthchecker子进程

有图可知,两个子进程都被系统WatchDog看管,两个子进程各自复杂自己的事,healthchecker子进程复杂检查各自服务器的健康程度,例如HTTP,LVS等等,如果healthchecker子进程检查到MASTER上服务不可用了,就会通知本机上的兄弟VRRP子进程,让他删除通告,并且去掉虚拟IP,转换为BACKUP状态。


三、部署keepalived服务

环境:Centos6.5-x64位系统

IP:172.16.16.15(master),172.16.16.16(backup)

VIP:172.16.16.100

关闭iptables,selinux


只是演示keepalive的常用配置,因此yum安装,如有需要,请百度源码安装。

[root@localhost ~]# yum install keepalived -y


keepalive主配置文件在/etc/keepalived/下,


1.实现简单的VIP漂移


master配置文件:


! Configuration File for keepalived


global_defs {

   notification_email {

    root@localhost   ##########收件人邮箱,每行一个收件人

   }

   notification_email_from keepadmin@localhost   #########发件人邮箱,可以不存在

   smtp_server 127.0.0.1       ###########邮件服务器地址

   smtp_connect_timeout 30     ########邮件服务器连接超时时间

   router_id LVS_DEVEL       #########服务器的标识

}

vrrp_instance VI_1 {        ########热备实例

    state MASTER         ########热备状态

    interface eth0       #########监控心跳,向备发送宣告

    virtual_router_id 51   #########虚拟路由id,主备必须一致

    priority 100        #########定义优先级,值越大优先级越高

    advert_int 1       #########主备宣告的时间间隔,

    authentication {    #########设置验证

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.16.100   ######定义vip,注此机器只有一块网卡,默认加到eth0上,若多块网卡,或者子卡,可以设置为172.16.16.100 dev eth1[label eth0:0]等指定设备

    }

}


backup配置文件:

! Configuration File for keepalived


global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from keepadmin@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_instance VI_1 {

    state BACKUP          ###修改此节点为备

    interface eth0

    virtual_router_id 51

    priority 99         ########优先级要比主低

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.16.100

    }

}


启动两个几点keepalived

master:

root@localhost keepalived]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c2:cc:ff brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.15/16 brd 172.16.255.255 scope global eth0

    inet 172.16.16.100/32 scope global eth0

    inet6 fe80::20c:29ff:fec2:ccff/64 scope link 

       valid_lft forever preferred_lft forever


backup

[root@localhost keepalived]# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5c:ef:24 brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.16/16 brd 172.16.255.255 scope global eth0

    inet6 fe80::20c:29ff:fe5c:ef24/64 scope link 

       valid_lft forever preferred_lft forever


关闭master的keepalive,

master


[root@localhost keepalived]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c2:cc:ff brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.15/16 brd 172.16.255.255 scope global eth0

    inet6 fe80::20c:29ff:fec2:ccff/64 scope link 

       valid_lft forever preferred_lft forever


backup


[root@localhost keepalived]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5c:ef:24 brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.16/16 brd 172.16.255.255 scope global eth0

    inet 172.16.16.100/32 scope global eth0   #######vip漂移到backup

    inet6 fe80::20c:29ff:fe5c:ef24/64 scope link 

       valid_lft forever preferred_lft forever


2.定义简单的脚本命令

在/etc/keepalived/目录下,检测到down文件,vip漂移


master/backup


global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from keepadmin@localhost

   smtp_server 127.0.0.1

! Configuration File for keepalived


global_defs {

   notification_email {

     root@localhost

   }

   notification_email_from keepadmin@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}


vrrp_script chk_maintainace {      #######定义检测脚本参数vrrp_script,chk_maintainace自定义

   script "[[  -f /etc/keepalived/down ]] && exit 1 || exit 0" ##可以为脚本,或命令

   interval 1  ######检测down文件的时间间隔为1秒

   weight -2   ########检测到down文件,优先级-2(即100-2)

}


vrrp_instance VI_1 {

    state MASTER/BACKUP

    interface eth0

    virtual_router_id 51

    priority 100/99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        172.16.16.100

    }

    track_script {                                 

    chk_maintainace   ############追踪脚本

    }

}

           

[root@localhost keepalived]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c2:cc:ff brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.15/16 brd 172.16.255.255 scope global eth0

    inet 172.16.16.100/32 scope global eth0

    inet6 fe80::20c:29ff:fec2:ccff/64 scope link 

       valid_lft forever preferred_lft forever

[root@localhost keepalived]# touch down

[root@localhost keepalived]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c2:cc:ff brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.15/16 brd 172.16.255.255 scope global eth0

    inet6 fe80::20c:29ff:fec2:ccff/64 scope link   ######vip漂移

       valid_lft forever preferred_lft forever


[root@localhost keepalived]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:5c:ef:24 brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.16/16 brd 172.16.255.255 scope global eth0

    inet 172.16.16.100/32 scope global eth0 ######vip漂移到backup

    inet6 fe80::20c:29ff:fe5c:ef24/64 scope link 

       valid_lft forever preferred_lft forever

[root@localhost keepalived]# rm -rf down 主删除down

[root@localhost keepalived]# ip addr show eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:c2:cc:ff brd ff:ff:ff:ff:ff:ff

    inet 172.16.16.15/16 brd 172.16.255.255 scope global eth0

    inet 172.16.16.100/32 scope global eth0  ######vip漂移回master

    inet6 fe80::20c:29ff:fec2:ccff/64 scope link 

       valid_lft forever preferred_lft forever



linux之keepalived详解

标签:双机热备

原文地址:http://weilantiankong.blog.51cto.com/9469693/1761809

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!