码迷,mamicode.com
首页 > 其他好文 > 详细

Keepalived 的script及双主

时间:2016-10-22 18:34:34      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:script   keepalived   双主   


    环境:

    K1:192.168.11.26

    K2:192.168.11.28

    VIP1:192.168.11.17

    VIP2:192.168.11.18

一、vrrp_script   

1、K1配置

[root@K1 keepalived]# cat keepalived.conf                        #配置内容
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from xxx@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
  # vrrp_mcast_group4 224.0.100.18
}

vrrp_script  chk_down {                             #定义vrrp_script脚本扩展
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"     #判断文件是否存在,也可以定义脚本
   interval 3                                 #检查周期
   weight -10                                 #文件存在权重-10 根据上面script定义
}


vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.17
    }
    track_script {                              #跟踪script,如果不写,则不调用也就是不会执行我们是定义的vrrp_script定义的内容
	chk_down
    }
	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault "/etc/keepalived/notify.sh fault"
}

2、K2配置

[root@K2 keepalived]# cat keepalived.conf                          #配置内容
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from xxx@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
  # vrrp_mcast_group4 224.0.100.18
}

vrrp_script  chk_down {                             #定义vrrp_script脚本扩展
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"     #判断文件是否存在,也可以定义脚本   
   interval 3                                                                   #检查周期
   weight -10                                                                   #文件存在权重-10 根据上面script定义
}
}


vrrp_instance VI_1 {
    state BAKCUP                                #填写BACKUP,因为K1是MASTER
    interface eth0
    virtual_router_id 51
    priority 98                                 #优先级也需要设定比K1小
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.17
    }
    track_script {                               #跟踪script,如果不写,则不调用也就是不会执行我们是定义的vrrp_script定义的内容
	chk_down
    }
	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault "/etc/keepalived/notify.sh fault"
}


3、测试

#启动keepalived服务,这里就不演示了
[root@K1 keepalived]# touch down                                                     #在K1MASTER上touche down文件
[root@K1 keepalived]# ls                                                             #查看是否存在,发现有down文件
down  keepalived.conf  keepalived.conf.bak  keepalived.conf.real  notify.sh
[root@K1 keepalived]# tail -f /var/log/messages                                      #在另外一个K1的session tail -f
Jun 24 17:43:57 web2 Keepalived_vrrp[9530]: VRRP_Script(chk_down) failed             #发现我们定义的track_script(chk_down) failed ,自动权限-10
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Received higher prio advert  #发现更高优先级的
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Entering BACKUP STATE        #自己转为BACKUP STATE
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) removing protocol VIPs.      #迁移VIPs协议
Jun 24 17:43:58 web2 Keepalived_healthcheckers[9529]: Netlink reflector reports IP 192.168.11.17 removed  #宣布删除IP 192.168.11.17

[root@K1 keepalived]# rm -f down                                                      #删除down文件
You have new mail in /var/spool/mail/root                                             #查看,down的确被删除
[root@K1 keepalived]# ls
keepalived.conf  keepalived.conf.bak  keepalived.conf.real  notify.sh
[root@K1 keepalived]# tail -f /var/log/messages                                       #切换回刚刚的session
Jun 24 17:43:57 web2 Keepalived_vrrp[9530]: VRRP_Script(chk_down) failed              #发现我们定义的track_script(chk_down) failed ,自动权限-10,此时权限为90
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Received higher prio advert  #发现更高优先级的
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Entering BACKUP STATE        #自己转为BACKUP STATE
Jun 24 17:43:58 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) removing protocol VIPs.      #迁移VIPs协议
Jun 24 17:43:58 web2 Keepalived_healthcheckers[9529]: Netlink reflector reports IP 192.168.11.17 removed  #宣布删除IP 192.168.11.17

========================================================割了,实际是连在一起的,只不过为了方便解释,割了==============================================

Jun 24 17:50:00 web2 Keepalived_vrrp[9530]: VRRP_Script(chk_down) succeeded           #发现我们定义的tarck_script(chk_down) succeeded,此时权限为100,就是把之前-10的加回来了
Jun 24 17:50:00 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) forcing a new MASTER election    #推选了一个新的MATER
Jun 24 17:50:00 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) forcing a new MASTER election    #推选了一个新的MASTER
Jun 24 17:50:01 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Transition to MASTER STATE       #正在进入MASTER状态中
Jun 24 17:50:02 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Entering MASTER STATE            #进入MASTER状态
Jun 24 17:50:02 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) setting protocol VIPs.           #设定VIPs协议
Jun 24 17:50:02 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17     #从eth0发送192.168.11.17免费的ARPs
Jun 24 17:50:02 web2 Keepalived_healthcheckers[9529]: Netlink reflector reports IP 192.168.11.17 added                #添加192.168.11.17
Jun 24 17:50:07 web2 Keepalived_vrrp[9530]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17     #从eth0发送192.168.11.17免费的ARPs

4、通过vrrp_script简单检查服务并设定漂移

[root@K2 keepalived]# cat keepalived.conf                               #查看修改后的配置文件内容
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from 855@usfund.com.cn
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
  # vrrp_mcast_group4 224.0.100.18
}

vrrp_script  chk_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 3
   weight -10
}

vrrp_script chk_httpd {                                                  #增加一个新的vrrp_script,为chk_httpd
  script "killall -0 httpd"                                              #killall -0 信号,如果服务存在返回0
  interval 3                                                             #检查周期
  weight -10                                                             #httpd服务存在权重-10
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.17
    }
    track_script {
	chk_down
        chk_httpd                                                        #增加了一个新的track_script chk_httpd
    }
	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault "/etc/keepalived/notify.sh fault"
}
[root@K2 keepalived]# service keepalived  stop                                     #在K2停止keepalived
Stopping keepalived:                                       [  OK  ]
[root@K2 keepalived]# netstat -tnlp| grep httpd                                    #查看httpd服务是否启动
tcp        0      0 :::80                       :::*                        LISTEN      19187/httpd         
[root@K2 keepalived]# service keepalived start                                     #启动keepalived服务
Starting keepalived:                                       [  OK  ]
[root@K2 keepalived]# ip add sh                                                    #查看IP,发现:inet 192.168.11.17/32 scope global eth0
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:2d:ba:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.28/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:bab0/64 scope link 
       valid_lft forever preferred_lft forever
     
[root@K1 keepalived]# service keepalived stop                                     #在K1停止keepalived
正在启动 keepalived:                                      [确定]
[root@K1 keepalived]# netstat -tunlp | grep httpd                                 #查看httpd是否启动
tcp        0      0 :::80                       :::*                        LISTEN      5457/httpd 
[root@K1 keepalived]# service keepalived start                                    #启动keepalived    
[root@K1 keepalived]# ip add sh                                                   #查看IP,发现:192.168.11.17被抢占过来了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
       valid_lft forever preferred_lft forever     
       
[root@K1 keepalived]# service httpd stop                                         #在K1停止httpd服务
停止 httpd:                                               [确定]
You have new mail in /var/spool/mail/root
[root@K1 keepalived]# ip add sh                                                  #查看IP,发信192.168.11.17不见了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
       valid_lft forever preferred_lft forever     
   
[root@K1 keepalived]# tail -f /var/log/messages                                   #在K1停止httpd服务器前,开启的一个新session
Jun 24 18:26:43 web2 Keepalived_vrrp[11701]: VRRP_Script(chk_httpd) failed        #定义的trackscript(chk_httpd)faileld
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Received higher prio advert    #发现更高的优先级
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Entering BACKUP STATE          #进入BACKUP状态
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) removing protocol VIPs.        #迁移VIPs协议
Jun 24 18:26:44 web2 Keepalived_healthcheckers[11700]: Netlink reflector reports IP 192.168.11.17 removed      #宣布删除IP 192.168.11.17 

[root@K1 keepalived]# service httpd start                                         #启动httpd服务
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for K1
httpd: Could not reliably determine the server‘s fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [确定]
You have new mail in /var/spool/mail/root
[root@K1 keepalived]# ip add sh                                                   #查看IP,发现:192.168.11.17被抢占过来了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
       valid_lft forever preferred_lft forever
       
[root@K1 keepalived]#tail -f /var/log/messages                                   #在K1停止httpd服务器前,开启的一个新session 
Jun 24 18:26:43 web2 Keepalived_vrrp[11701]: VRRP_Script(chk_httpd) failed       #定义的trackscript(chk_httpd)faileld
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Received higher prio advert    #发现更高的优先级
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Entering BACKUP STATE          #进入BACKUP状态
Jun 24 18:26:44 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) removing protocol VIPs.        #迁移VIPs协议
Jun 24 18:26:44 web2 Keepalived_healthcheckers[11700]: Netlink reflector reports IP 192.168.11.17 removed      #宣布删除IP 192.168.11.17 

========================================================割了,实际是连在一起的,只不过为了方便解释,割了==============================================
Jun 24 18:31:10 web2 Keepalived_vrrp[11701]: VRRP_Script(chk_httpd) succeeded                        #发现我们定义的tarck_script(chk_httpd) succeeded,此时权限为100,就是把之前-10的加回来了    
Jun 24 18:31:10 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) forcing a new MASTER election       #推选了一个新的MATER
Jun 24 18:31:10 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) forcing a new MASTER election       #推选了一个新的MATER
Jun 24 18:31:11 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Transition to MASTER STATE          #正在进入MASTER状态中      
Jun 24 18:31:12 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Entering MASTER STATE               #进入MASTER状态       
Jun 24 18:31:12 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) setting protocol VIPs.              #设定VIPs协议
Jun 24 18:31:12 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17    #从eth0发送192.168.11.17免费的ARPs
Jun 24 18:31:12 web2 Keepalived_healthcheckers[11700]: Netlink reflector reports IP 192.168.11.17 added               #添加192.168.11.17
Jun 24 18:31:17 web2 Keepalived_vrrp[11701]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.11.17    #从eth0发送192.168.11.17免费的ARPs

二、双主模型

[root@K2 keepalived]# cat keepalived.conf                                 #查案配置内容,K1的就不贴上来了,新增段配置改为相反的
! Configuration File for keepalived

global_defs {
   notification_email {
	root@localhost
   }
   notification_email_from xxx@163.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
  # vrrp_mcast_group4 224.0.100.18
}

vrrp_script  chk_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 3
   weight -10
}

vrrp_script chk_httpd {
  script "killall -0 httpd"
  interval 3
  weight -10
}


vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.17
    }
    track_script {
	chk_down
        chk_httpd
    }
	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault "/etc/keepalived/notify.sh fault"
}

vrrp_instance VI_2 {                                                     #相对上面的复制了VI_1内容并改为VI_2,配置也做了相应的修改
    state MASTER                                                         #由复制BACKUP改为MASTER
    interface eth0
    virtual_router_id 52                                                 #由51改为52
    priority 100                                                         #由98改为100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 6Nb6zjWB
    }
    virtual_ipaddress {
	192.168.11.18                                                    #地址192.168.11.17改为192.168.11.18
    }
    track_script {
	chk_down
        chk_httpd
    }
	notify_master "/etc/keepalived/notify.sh master"
	notify_backup "/etc/keepalived/notify.sh backup"
	notify_fault "/etc/keepalived/notify.sh fault"
}
[root@K2 keepalived]# service keepalived stop                            #停止keepalived服务
Stopping keepalived:                                       [  OK  ]
[root@K2 keepalived]# service keepalived start                           #启动keepalived服务【需要等待两边都stop keepalived 服务,不然会出现一些小问题】
[root@K2 keepalived]# ip add sh                                          #查看VIP地址都在这台,因为另外一台没有开启
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:2d:ba:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.28/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.18/32 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:bab0/64 scope link 
       valid_lft forever preferred_lft forever
       
[root@K1 keepalived]# service keepalived stop                            #停止keepalived服务
停止 keepalived:                                          [确定]    
[root@K1 keepalived]# service keepalived start                           #启动keepalived服务
正在启动 keepalived:                                      [确定]
[root@K1 keepalived]# ip add sh                                          #哈哈,我胡三又回来了(192.168.11.17)
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:ad:98:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.26/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.17/32 scope global eth0
    inet6 fe80::20c:29ff:fead:985c/64 scope link 
       valid_lft forever preferred_lft forever
[root@K2 keepalived]# ip add sh                                          #在K2查看,192.168.11.17 没了
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:2d:ba:b0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.28/24 brd 192.168.11.255 scope global eth0
    inet 192.168.11.18/32 scope global eth0
    inet6 fe80::20c:29ff:fe2d:bab0/64 scope link 
       valid_lft forever preferred_lft forever
You have new mail in /var/spool/mail/root

#测试就不测试了 ^-^ 不管是httpd访问停止还是在/etc/keepalived/下touch down 服务都会跑到另外一边去


本文出自 “SunshineBoySZF” 博客,请务必保留此出处http://sunshineboyszf.blog.51cto.com/12087328/1864488

Keepalived 的script及双主

标签:script   keepalived   双主   

原文地址:http://sunshineboyszf.blog.51cto.com/12087328/1864488

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