keepalived.conf之vrrp instance部分解读

keepalived既可以配合lvs使用,也可以单独使用,所以它的配置文件分为如下几个部分:

  1. Global Configuration

  2. Global definitions

  3. Static routes/addresses

  4. Vrrpd Configuration

  5. VRRP synchronization group(s)

  6. VRRP instance(s)

  7. Lvs Configuration

  8. Virtual server group(s)

  9. Virtual server(s)

这次由于主要用到vrrp instance,所以重点对配置文件这个部分进行解读

下面是对这个配置的解释

 state MASTER|BACKUP #如果不指定Master或者BACKUP,那priority最高的就是master interface eth0 #监听的实际网口 virtual_router_id 51 #组播ID,通过224.0.0.18可以监听到现在已经存在的VRRP ID,最好不要跟现有ID冲突 priority 100 #权重为100,权重数字越大就越高 advert_int 1 #发送组播包的间隔时间,默认为1秒 smtp_alert #发送邮件报警 authentication { auth_type PASS auth_pass hdtv } #这个是验证类型为PASS(明文),密码为hdtv。验证类型也可以选择IPSEC,但是官方是不推荐的 virtual_ipaddress { 10.1.41.141 } #虚拟IP为10.1.41.141 #############下面这些是文档中存在,但是在上面没有用到的############################# dont_track_primary #忽略网卡错误 track_interface { eth0 eth1 } #监控eth0和eth1这2块网卡的状态 mcast_src_ip #使用这个地址作为多播包的源IP,而不是使用interface eth0上的IP lvs_sync_daemon_interface eth1 #绑定eth1作为lvs同步的 garp_master_delay 2 #master和slave漂移时间改为2秒,默认位5秒,怪不得我昨天发现每次都是5秒才转移 virtual_ipaddress { / brd dev scope label 192.168.200.17/24 dev eth1 192.168.200.18/24 dev eth2 label eth2:1 } #vip可以写成整个网段和某块网卡上的所有IP virtual_ipaddress_excluded { / brd dev scope / brd dev scope ... } #排除哪些IP virtual_routes { src 192.168.100.1 to 192.168.109.0/24 via 192.168.200.254 dev eth1 192.168.110.0/24 via 192.168.200.254 dev eth1 192.168.111.0/24 dev eth2 192.168.112.0/24 via 192.168.100.254 } #当状态切换的时候会增加和删除路由,格式如src \[to\] / via|gw dev scope tab nopreempt #这个参数是用来,当master当掉,slave接替原来的master作为master后,这个时候当master重新起来后,有了这个参数后原来的slave就不会自动再自动切换为slave,而是继续作为master preempt_delay 300 #接上面那个参数,这个表示,只有在老的master重新正常300秒后,老的master才会切换为master,这个参数范围是0-1000,默认为0 notify_master | notify_backup | notify_fault | notify | smtp_alert #各种报警方式,可以定义具体的内容来达到不同的报警信息。 

上面这些是官方的配置文件,下面这些是放狗搜索出来的其它配置,主要是为了做服务状态的检测,不然keepalived只能看网口有没有down掉再进行迁移,那样就要另外写其它的监控脚本来达到当服务挂掉后就把网口down掉。
下面这个是从邮件列表中抄袭而来,但是没有测试过,明日会找时间进行测试。下面这个是在1.1.13版本之后就实现了。

 ! Configuration File for keepalived vrrp_script chk_sshd { script "killall -0 sshd" # cheaper than pidof interval 2 # check every 2 seconds weight -4 # default prio: -4 if KO } vrrp_script chk_haproxy { script "killall -0 haproxy" # cheaper than pidof interval 2 # check every 2 seconds } vrrp_script chk_http_port { script "/tcp/127.0.0.1/80" # connects and exits interval 1 # check every second weight -2 # default prio: -2 if connect fails } vrrp_script chk_https_port { script "/tcp/127.0.0.1/443" interval 1 weight -2 } vrrp_script chk_smtp_port { script "/tcp/127.0.0.1/25" interval 1 weight -2 } vrrp_instance VI_1 { interface eth0 state MASTER virtual_router_id 51 priority 100 virtual_ipaddress { 192.168.200.18/25 } track_interface { eth1 weight 2 # prio = +2 if UP eth2 weight -2 # prio = -2 if DOWN eth3 # no weight, fault if down } track_script { chk_sshd # use default weight from the script chk_haproxy weight 2 # +2 if process is present chk_http_port chk_https_port chk_smtp_port } } vrrp_instance VI_2 { interface eth1 state MASTER virtual_router_id 52 priority 100 virtual_ipaddress { 192.168.201.18/26 } track_interface { eth0 weight 2 # prio = +2 if UP eth2 weight -2 # prio = -2 if DOWN eth3 # no weight, fault if down } track_script { chk_haproxy weight 2 chk_http_port chk_https_port chk_smtp_port } } 

###########################################

Best regards
Timo Seven

()
Linux System Admin & MySQL DBA