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

keepalived

时间:2016-08-22 23:50:04      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:keepalived 高可用

keepalived:

vrrp协议 :Virtual Redundant Routing Protocol    虚拟冗余路由协议(网关高可用)

术语:

虚拟路由器(由一个或更多个物理设备组成)、

VRID(同一组的虚拟路由器的唯一标识)、

master路由器(承担转发报文)、backup路由器(预备军)、

VIP(虚拟ip地址,真正提供服务的ip,且在多个节点上面流转)、

VMAC(00-00-5e-00-01-VRID,vRID有效范围0-255)(虚拟mac地址,虚拟ip在哪则虚拟mac就在哪)

但其实实际上是当vip更换了主机之后,自己发起一个免费的ARP通告,并自问自答,让别人知道江山易主了

优先级(每个节点的优先级不同可以判定哪个能够指点江山)

抢占式(只要优先级高,我就抢)、非抢占式(等着主节点挂了,才能接受,暂时不管优先级)

 

vrrp工作流程:至少两个,一主一辅,当平时的时候主的上面跑着ip,发送免费arp让别人知道我这个节点是主动,提供服务;master发送周期性的arrp报文,说明自己的心跳,从在一旁观察这主的心跳信息;当主的服务器出现故障,从的不能接受到心跳信息之后,根据优先级选举,就把vip配置在自己的主机上面,接受服务,对外响应;这个地址是流动的,在这些节点之间进行流转,谁有这个ip,谁就可以响应服务;

vrrp主要是完成地址转移,保障服务始终可以对外响应

 

vrrp的认证:无认证,签名认证和md5认证三种

 

 

工作模式:

主/备    (这样从的只有当主的挂了之后才能上台,有点儿浪费资源)

主/主:配置多个virtual router;(主要是不想让某个服务器始终闲着)

这里的双主,彼此为主,彼此为辅,配置两个vip,各自响应,让服务器同时响应请求


第一台

[root@zou ~]# hostnamectl set-hostname one.zou.com

[root@one ~]# vim /etc/hosts

172.16.1.3  one.zou.com  one

172.16.1.5  two.zou.com  two

 

第二台

[root@zou ~]# hostnamectl set-hostname two.zou.com

[root@one ~]# vim /etc/hosts

172.16.1.3  one.zou.com  one

172.16.1.5  two.zou.com  two

 

之后可以达到通过主机名通信:

[root@one ~]# ping two.zou.com

PING two.zou.com (172.16.1.5) 56(84) bytes of data.

64 bytes from two.zou.com (172.16.1.5): icmp_seq=1 ttl=64 time=1.72 ms

64 bytes from two.zou.com (172.16.1.5): icmp_seq=2 ttl=64 time=0.694 ms

同步时间(两台服务器都要同步时间,保持时间基本相同)

可以自己组建时间服务器

yum install -y chrony

vim /etc/chrony.conf

allow  172.16/16  允许哪些主机同步时间

server 172.16.1.1 iburst 时间服务器同步的时间来源

systemctl start chronyd.service

systemctl enable chronyd.service

chronyc -h 172.16.1.1 同步时间(指向这个搭建的时间服务器)

也可以在网上同步时间

[root@one ~]# ntpdate 172.16.0.1

 

安装keepalived

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

        另一台也要安装

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

 

配置keepalived

技术分享

主:

[root@one ~]# cp /etc/keepalived/keepalived.conf{,.bak}

[root@one ~]# vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

        root@localhost

   }   

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

 

vrrp_instance VI_1 {

    state MASTER

    interface eno16777736

    virtual_router_id 101

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }  

    virtual_ipaddress {

        172.16.1.11

    }

}

从:

[root@two ~]# vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

        root@localhost

   }   

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

 

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 101

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }  

    virtual_ipaddress {

        172.16.1.11

    }

}

[root@two ~]# systemctl start keepalived.service

[root@two ~]# systemctl status keepalived.service

keepalived.service - LVS and VRRP High Availability Monitor

   Active: active (running) since Mon 2016-08-01 07:13:32 CST; 20s ago

   ......

Aug 01 07:13:36 two.zou.com Keepalived_vrrp[128056]: VRRP_Instance(VI_1) Transition to MASTER STATE

上条说先去找主节点

Aug 01 07:13:37 two.zou.com Keepalived_vrrp[128056]: VRRP_Instance(VI_1) Entering MASTER STATE

由于主节点还没有开启,所以虽然它属于从,主的不在,它就变成了主

Aug 01 07:13:37 two.zou.com Keepalived_vrrp[128056]: VRRP_Instance(VI_1) setting protocol VIPs.

准备协议VIPS

Aug 01 07:13:37 two.zou.com Keepalived_vrrp[128056]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.11                设置好了vip

Aug 01 07:13:37 two.zou.com Keepalived_healthcheckers[128055]: Netlink reflector reports IP 172.16.1.11 added          免费通告,自己有vip,为主节点

Aug 01 07:13:42 two.zou.com Keepalived_vrrp[128056]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.11             免费通告

[root@two ~]# ip addr show eno16777736

    inet 172.16.1.5/16 brd 172.16.255.255 scope global eno16777736

       valid_lft forever preferred_lft forever

    inet 172.16.1.11/32 scope global eno16777736

       valid_lft forever preferred_lft forever

 

好了这个时候该把,172.16.1.3上的主节点启动了,由于节点是争用的,主节点优先级高,所以很开就会把主节点抢过去

[root@one ~]# systemctl start keepalived.service

You have new mail in /var/spool/mail/root          主节点发生变化,就要发送邮件了

之后通过状态监测keepalived.service,可以看到

 one.zou.com Keepalived_vrrp[2385]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.11    已经把VIP给争到了,它就是主的了,相对而言1.5那边就变成了从节点

 two.zou.com Keepalived_healthcheckers[128055]: Netlink reflector reports IP 172.16.1.11 removed

 

主主模型(互为主从)

使用环境:

1)两个主都配置在一个服务器上面,剩下的两个从配置一个上面;

在有内外网的时候,且同时提供对内和对外的服务,为了保障服务不中断,因此配置两个虚拟段,互为主从,保障在keepalived服务器即使在一台有问题的情况下可以运转

(这里这个配置和上面一样,就是多加个虚拟路由而已,所以就不写操作步骤了)

2)互为主从,配置两个虚拟路由,一个上面为主,同时住上面还有一个虚拟路由是另一个的从节点

技术分享


在网络配置完毕,同时都安装了keepalived服务之后,修改配置文件:

172.16.1.3上面相关操作与配置

172.16.1.5上面相关操作与配置

@one ~]# vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eno16777736

    virtual_router_id 101

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }

    virtual_ipaddress {

        172.16.1.11

    }

}

 

vrrp_instance VI_2 {

    state BACKUP

    interface eno16777736

    virtual_router_id 111

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 456.com

    }

    virtual_ipaddress {

        172.16.1.22

    }

}


@two ~]# vim/etc/keepalived/keepalived.conf

global_defs {

   notification_email {

        root@localhost

   }   

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

 

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 101

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }  

    virtual_ipaddress {

        172.16.1.11

    }

}

 

vrrp_instance VI_2 {

    state MASTER

    interface eno16777736

    virtual_router_id 111

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 456.com

    }  

    virtual_ipaddress {

        172.16.1.22

    }  

}   


之后两台服务器启动服务,两台服务器上分配的ip

[root@one ~]# ip addr show eno16777736

    inet 172.16.1.3/16 brd 172.16.255.255 scope global eno16777736

    inet 172.16.1.11/32 scope global eno16777736

  

[root@two ~]# ip addr show eno16777736

    inet 172.16.1.5/16 brd 172.16.255.255 scope global eno16777736

         inet 172.16.1.22/32 scope global eno16777736

 

 

根据拓扑图完成后续操作

技术分享

首先定义好后端的两台wab服务器,同时给他们设置好限制通告和vip设置

[root@luffy www]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

[root@luffy www]#  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@luffy www]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

[root@luffy www]#  echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

[root@luffy www]# ip addr add 172.16.1.44/32 broadcast 172.16.1.44 dev lo label lo:0

     让每次首先经由vip转发(此命令可以不执行)

[root@luffy www]# ip route add 172.16.1.44 dev lo:0 

 

准备第二台的web服务器

[root@zou ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

[root@zou ~]#  echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[root@zou ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

[root@zou ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

[root@zou ~]# ifconfig lo:0 172.16.1.44 255.255.255.255 broadcast 172.16.1.44 up

[root@zou ~]# route add -host 172.16.1.44 dev lo:0

 

keepalive上(两台)都准备好sorry web,当然也要安装web服务(简单配置即可)

[root@one ~]# yum install httpd -y

[root@one ~]# systemctl start httpd.service

[root@one ~]# vim /var/www/html/index.html

 

[root@two ~]# yum install httpd -y    或者安装nginx也可以

[root@two ~]# systemctl start httpd.service

[root@two ~]# vim /var/www/html/index.html    

生产环境这个sorry网页是一样的,但是里测试故意把这个服务器的sorry网页写的不一样,看看是否东能够正常工作

 

好了可以准备配置keepalived了,这里还需要说明一下,这个时候不用安装ipvsadm,因为keepalived会自动向内核中找ipvs的系统调用去完成dr模型的调度,完成负载,同时两台keepalived完成高可用设置,

不过,为了我们在实验环节能够更好的查看(仅为查看使用)可以安装上ipvsadm软件包

[root@one etc]# yum install -y ipvsadm

[root@two ~]# yum install -y ipvsadm

 

[root@one keepalived]# vim keepalived.conf 

global_defs {

   notification_email {

root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one                            另一个为two

   vrrp_mcast_group4 224.0.101.11

}

 

vrrp_instance VI_1 {

    state MASTER          另一台主机为BACKUP

    interface eno16777736

    virtual_router_id 22

    priority 100                另一个为90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }

    virtual_ipaddress {

        172.16.1.44/32 dev eno16777736 brd 172.16.1.44 label eno16777736:0

    }

}

 

virtual_server 172.16.1.44 80 {

    delay_loop 3

    lb_algo rr

    lb_kind DR

    nat_mask 255.255.0.0

    #persistence_timeout 50

    protocol TCP

     sorry_server 127.0.0.1 80

 

   real_server 172.16.1.1 80 {

        weight 1

        HTTP_GET {

            url {

              path /index.html

              #digest 640205b7b0fc66c1ea91c463fac6334d

      status_code 200

            }

            #url {

             # path /testurl2/test.jsp

              #digest 640205b7b0fc66c1ea91c463fac6334d

            #}

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

    real_server 172.16.1.2 80 {

        weight 1

        HTTP_GET {

            url {

              path /index.html

      status_code 200

            }

            connect_timeout 3

            nb_get_retry 3

            delay_before_retry 3

        }

    }

}

之后两台keepalived主机重启服务,查看状态

[root@two keepalived]# systemctl restart keepalived.service

[root@two keepalived]# systemctl status keepalived.service

[root@one keepalived]# ip a show eno16777736

    inet 172.16.1.3/16 brd 172.16.255.255 scope global eno16777736

          inet 172.16.1.44/32 brd 172.16.1.44 scope global eno16777736:0

当然我们一开始安装ipvsadm就是为了这个时候查看状态的,不然安装了也就用不着了

[root@one keepalived]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.16.1.44:80 rr

  -> 172.16.1.1:80                Route   1      0          0        

  -> 172.16.1.2:80                Route   1      0          0 

查看调度器是否能够轮训调度

[root@two keepalived]# for i in {1..6};do curl http://172.16.1.44;done

this is web 2

this is web 1

this is web 2

this is web 1

this is web 2

this is web 1

测试web服务器宕机,是否还可正常使用:

[root@zou ~]# /etc/init.d/httpd stop   (这里停掉的是1.2

之后我们用ipvsadm去查看

[root@one ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.16.1.44:80 rr

  -> 172.16.1.1:80                Route   1      0          0

我们可以从结果看到现在只有一个服务在线

[root@two ~]#  for i in {1..6};do curl http://172.16.1.44;done

this is web 1

this is web 1

this is web 1

this is web 1

this is web 1

this is web 1

测试,同时把web1web2给停掉

[root@luffy ~]# nginx -s stop

继续查看和监测

[root@one ~]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.16.1.44:http rr

  -> localhost:http               Route   1      0          0     

可以看到sorryserver上台了

[root@one ~]# curl http://172.16.1.44

sorry!!! this is web 1.3

测试停掉keepalived主,节点

[root@one ~]# systemctl stop keepalived.service

[root@two ~]# curl http://172.16.1.44

sorry!this is web 1.5

 

基于状态的监测

[root@one ~]# vim /etc/keepalived/keepalived.conf 

global_defs {

   notification_email {

root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

   vrrp_mcast_group4 224.0.101.11

}

 

vrrp_script test_down {

script "[[ -f /etc/keepalived/down ]] && exit1 || exit 0"

interval 10

weight -5

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eno16777736

    virtual_router_id 22

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }

    virtual_ipaddress {

        172.16.1.66

    }

    track_script {

            test_down

    }

}

[root@one ~]# scp /etc/keepalived/keepalived.conf two:/etc/keepalived/keepalived.conf

[root@two ~]# vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id two

   vrrp_mcast_group4 224.0.101.11

}

 

vrrp_script test_down {

script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"   判断是否有down这个文件,如果有文件就返回失败值1,如果不存在就返回成功值0

interval 10

weight -5       当失败的时候权重减少5

}

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 22

    priority 98

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }

    virtual_ipaddress {

        172.16.1.66

    }

    track_script {

            test_down

    }

}

之后两个服务器重启keepalived服务,这个时候应该是主节点,也就是有优先级为100的为主

这个时候在主几点上创建/etc/keepalived/down文件,那么其优先级就会降低3,变成97,而从的默认为98,则从节点上就会变成主节点了

[root@one ~]# ip addr show eno16777736 | grep 32

    inet 172.16.1.66/32scope global eno16777736

[root@two ~]# ip addr show eno16777736 | grep 32

之后主节点创建down文件

[root@one ~]# touch /etc/keepalived/down

[root@one ~]# ip addr show eno16777736 | grep 32

[root@one keepalived]# systemctl status keepalived.service

 VRRP_Instance(VI_1)Received higher prio advert

 VRRP_Instance(VI_1)Entering BACKUP STATE

: VRRP_Instance(VI_1) removing protocol VIPs.

 Netlink reflectorreports IP 172.16.1.66 removed

          [root@two ~]# ip addr showeno16777736 | grep 32

  inet172.16.1.66/32 scope global eno16777736

 

如果把这个down文件给删了,原先的主节点一会就会成为主的

[root@one keepalived]# rm -rf down

[root@one keepalived]#systemctl status keepalived.service   这是回复节点的日志

:VRRP_Script(chk_down) succeeded

 VRRP_Instance(VI_1)forcing a new MASTER election

 VRRP_Instance(VI_1)forcing a new MASTER election

 VRRP_Instance(VI_1)Transition to MASTER STATE

 VRRP_Instance(VI_1)Entering MASTER STATE

 VRRP_Instance(VI_1)setting protocol VIPs.

 VRRP_Instance(VI_1)Sending gratuitous ARPs on eno16777736 for 172.16.1.66

 Netlink reflectorreports IP 172.16.1.66 added

 VRRP_Instance(VI_1)Sending gratuitous ARPs on eno16777736 for 172.16.1.66

 

从上面这个实验,down文件的存在与否可以判断哪个是作为主节点;同理,我们可以判断一个服务时候存在,之后来调整keepalived的活动节点,从而完成高可用的。不如我们可以监测httpd或者nginx服务是否在线,从而来调整keepalived的活动节点,从而让httpd或者nginx服务达到高可用的目的

这里补充一下,如果killallcentos7上没有的话,就安装psmisc软件包

注:如果式样过程有不顺利的地方,可能是keepalived没有启动好,先用killall keepalived 将其关闭,之后启动再测试

[root@two keepalived]# killall -0 nginx   探测服务是否存在

[root@two keepalived]# echo $?

0       如果为0则说明服务正常响应

[root@two keepalived]# nginx -s stop

[root@two keepalived]# killall -0 nginx

nginx: no process found

[root@two keepalived]# echo $?

1

[root@one keepalived]# killall -0 httpd

[root@one keepalived]# echo $?

0                                                        所以以后可以使用kill -0  来探测某服务是否存在了

下面来设置一下,keepalived.conf ,在里面加入

global_defs {

   notification_email {

root@localhost

   }

   notification_email_from keepalived@localhost

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id one

   vrrp_mcast_group4 224.0.101.11

}

 

vrrp_script chk_httpd {

        script "killall -0 httpd && exit 0 || exit 1"

        interval 1

        weight -5

        }

 

vrrp_instance VI_1 {

    state MASTER

    interface eno16777736

    virtual_router_id 22

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123.com

    }

    virtual_ipaddress {

  172.16.1.66

    }

     track_script {

         chk_httpd   

        }     

 }

接下来设置另一个服务器上的,这是1.5上面的,我这上面跑的是nginx

vrrp_script chk_nginx {

        script "killall -0 nginx && exit 0 || exit 1"

        interval 1

        weight -5

        }

 track_script {

        test_down

        chk_nginx

    }

重启两个keepalived服务

测试,将1.3上面的httpd关闭,看起主节点是否发生变化:

[root@one keepalived]# systemctl stop httpd

[root@one keepalived]# systemctl status keepalived.service

Aug 01 08:22:18 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Entering MASTER STATE

Aug 01 08:22:18 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) setting protocol VIPs.

Aug 01 08:22:18 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.66

Aug 01 08:22:18 one.zou.com Keepalived_healthcheckers[8804]: Netlink reflector reports IP 172.16.1.66 added

Aug 01 08:22:23 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.66

Aug 01 08:22:31 one.zou.com Keepalived_vrrp[8805]: VRRP_Script(chk_httpd) failed

Aug 01 08:22:32 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Received higher prio advert

Aug 01 08:22:32 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Entering BACKUP STATE

Aug 01 08:22:32 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) removing protocol VIPs.

Aug 01 08:22:32 one.zou.com Keepalived_healthcheckers[8804]: Netlink reflector reports IP 172.16.1.66 removed

当然这个时候ip已经转移到1.5主机上面去了

这是时候原主机如果开启httpd服务的话:

[root@one keepalived]# systemctl start httpd

[root@one keepalived]# ip a show eno16777736 | grep 32

    inet 172.16.1.66/32 scope global eno16777736

[root@one keepalived]# systemctl status keepalived.service

Aug 01 08:22:32 one.zou.com Keepalived_healthcheckers[8804]: Netlink reflector reports IP 172.16.1.66 removed

Aug 01 08:24:48 one.zou.com Keepalived_vrrp[8805]: VRRP_Script(chk_httpd) succeeded

Aug 01 08:24:48 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) forcing a new MASTER election

Aug 01 08:24:48 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) forcing a new MASTER election

Aug 01 08:24:49 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Transition to MASTER STATE

Aug 01 08:24:50 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Entering MASTER STATE

Aug 01 08:24:50 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) setting protocol VIPs.

Aug 01 08:24:50 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.66

Aug 01 08:24:50 one.zou.com Keepalived_healthcheckers[8804]: Netlink reflector reports IP 172.16.1.66 added

Aug 01 08:24:55 one.zou.com Keepalived_vrrp[8805]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eno16777736 for 172.16.1.66

从上面的实验来看,一旦主的上面的服务挂了之后,keepalived的优先级降低,这样会促成新的keepalived选举,从而,主节点发生变化,这样的话,从的上面的几点就能对外响应web服务了,从而达到了高可用web服务器;

那同样的道理,这个方案也适合其他服务,keepalived在响应的过程中消耗资源非常小,适合中等并发量的高可用服务

 

当然以上的后续步骤是在状态监测完毕之后,可以执行脚本,从而完成更多的自动化任务:

通知脚本示例:(每个节点都有一份儿)

~# vim /etc/keepalived/notify.sh

#!/bin/bash

#

#指定接收脚本的用户root以及地址

contact=‘root@localhost‘

 

notify() {

mailsubject="$(hostname) to be $1, vip floating."

mailbody="$(date +‘%F %T‘): vrrp transition, $(hostname) changed to be $1"

echo "$mailbody" | mail -s "$mailsubject" $contact

}

 

case $1 in

master)

notify master

;;

backup)

notify backup

systemctlstart httpd.service

;;

fault)

notify fault

;;

*)

echo "Usage: $(basename $0) {master|backup|fault}"

exit 1

;;

esac        

脚本调用方法示例:

在vrrp_instance VI_1 {  。。}   上下文里面写入一下三行,这里是在虚拟路由配置项里面写的,如果有多个虚拟路由配置段,这个就要写多个;且每台keepalived服务器也都要写上,这样就可以以邮件的方式知道节点之间的状态信息变化了。

~#  vim /etc/keepalived/keepalived.conf

vrrp_instance VI_2 {

...... 

    virtual_ipaddress {

        172.16.1.44

    }  

notify_master "/etc/keepalived/notify.sh master"

notify_backup "/etc/keepalived/notify.sh backup"

notify_fault "/etc/keepalived/notify.sh fault"

}   

上面这个脚本的和在keepalived的相应配置所达到的效果:

一旦主节点上面的httpd服务宕机,就会使主节点的优先级下降,主节点变成back从节点;脚本定义好了一旦参数为backup,就会启动httpd服务,启动httpd服务之后,节点的优先级回复,这样又变成主节点;所以主节点永远是主的了, (当然这里所有的配置为抢占式);如果把keepalived配置成为非抢占式,那主从节点就有可能轮流坐庄,但还是默认主节点会响应更多的服务,所以如果要使用这个脚本配置,配置成非抢占式

非抢占模式为    nopreempt 

vrrp_instance VI_2 {

...... 

    virtual_ipaddress {

        172.16.1.44

    }  

nopreempt

notify_master "/etc/keepalived/notify.sh master"

notify_backup "/etc/keepalived/notify.sh backup"

notify_fault "/etc/keepalived/notify.sh fault"

}   


本文出自 “北极的linux” 博客,请务必保留此出处http://941012521.blog.51cto.com/9253690/1841141

keepalived

标签:keepalived 高可用

原文地址:http://941012521.blog.51cto.com/9253690/1841141

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