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,各自响应,让服务器同时响应请求

第一台

[[email protected]
~]# hostnamectl set-hostname one.zou.com

[[email protected]
~]# vim /etc/hosts

172.16.1.3 
one.zou.com  one

172.16.1.5 
two.zou.com  two

第二台

[[email protected]
~]# hostnamectl set-hostname two.zou.com

[[email protected]
~]# vim /etc/hosts

172.16.1.3 
one.zou.com  one

172.16.1.5 
two.zou.com  two

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

[[email protected] ~]# 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 同步时间(指向这个搭建的时间服务器)

也可以在网上同步时间

[[email protected] ~]# ntpdate 172.16.0.1

安装keepalived

[[email protected] ~]# yum install keepalived -y

另一台也要安装

[[email protected] ~]#  yum
install keepalived -y

配置keepalived

主:

[[email protected] ~]# cp /etc/keepalived/keepalived.conf{,.bak}

[[email protected] ~]# vim /etc/keepalived/keepalived.conf


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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

}

}

从:

[[email protected]two ~]# vim /etc/keepalived/keepalived.conf


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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

}

}

[[email protected] ~]# systemctl start keepalived.service

[[email protected] ~]# 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             免费通告

[[email protected]wo ~]# 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上的主节点启动了,由于节点是争用的,主节点优先级高,所以很开就会把主节点抢过去

[[email protected] ~]# 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 {

[email protected]

}

notification_email_from [email protected]

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 {

[email protected]

}

notification_email_from [email protected]

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

[[email protected] ~]# 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

[[email protected] ~]# 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设置

[[email protected] www]# echo 1 >
/proc/sys/net/ipv4/conf/all/arp_ignore

[[email protected] www]#  echo
1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

[[email protected] www]# echo 2 >
/proc/sys/net/ipv4/conf/lo/arp_announce

[[email protected] www]#  echo
2 > /proc/sys/net/ipv4/conf/all/arp_announce

[[email protected] www]# ip addr add 172.16.1.44/32 broadcast
172.16.1.44 dev lo label lo:0

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

[[email protected] www]# ip route
add 172.16.1.44 dev lo:0

准备第二台的web服务器

[[email protected] ~]# echo 1 >
/proc/sys/net/ipv4/conf/all/arp_ignore

[[email protected] ~]#  echo 1
> /proc/sys/net/ipv4/conf/lo/arp_ignore

[[email protected] ~]# echo 2 >
/proc/sys/net/ipv4/conf/lo/arp_announce

[[email protected] ~]# echo 2 >
/proc/sys/net/ipv4/conf/all/arp_announce

[[email protected] ~]# ifconfig lo:0 172.16.1.44 255.255.255.255
broadcast 172.16.1.44 up

[[email protected] ~]# route add -host 172.16.1.44 dev lo:0

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

[[email protected] ~]# yum install httpd -y

[[email protected] ~]# systemctl start httpd.service

[[email protected] ~]# vim /var/www/html/index.html

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

[[email protected]two ~]# systemctl start httpd.service

[[email protected]two ~]# vim /var/www/html/index.html

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

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

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

[[email protected]one etc]# yum install -y ipvsadm

[[email protected]two ~]# yum install -y ipvsadm

[[email protected] keepalived]# vim keepalived.conf


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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主机重启服务,查看状态

[[email protected] keepalived]# systemctl restart keepalived.service

[[email protected] keepalived]# systemctl status keepalived.service

[[email protected] 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就是为了这个时候查看状态的,不然安装了也就用不着了

[[email protected] 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

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

[[email protected] 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服务器宕机,是否还可正常使用:

[[email protected] ~]#
/etc/init.d/httpd stop   (这里停掉的是1.2)

之后我们用ipvsadm去查看

[[email protected] ~]# 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

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

[[email protected] ~]#  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

测试,同时把web1和web2给停掉

[[email protected] ~]# nginx -s stop

继续查看和监测

[[email protected] ~]# 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上台了

[[email protected] ~]# curl http://172.16.1.44

sorry!!! this is web 1.3

测试停掉keepalived主,节点

[[email protected] ~]# systemctl stop keepalived.service

[[email protected] ~]# curl http://172.16.1.44

sorry!this is web 1.5

基于状态的监测

[[email protected] ~]# vim /etc/keepalived/keepalived.conf


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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

}

}

[[email protected] ~]# scp /etc/keepalived/keepalived.conf
two:/etc/keepalived/keepalived.conf

[[email protected] ~]# vim /etc/keepalived/keepalived.conf


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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,则从节点上就会变成主节点了

[[email protected] ~]# ip addr show eno16777736 | grep 32

inet 172.16.1.66/32scope global eno16777736

[[email protected] ~]# ip addr show eno16777736 | grep 32

之后主节点创建down文件

[[email protected] ~]# touch /etc/keepalived/down

[[email protected] ~]# ip addr show eno16777736 | grep 32

[[email protected] 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

[[email protected] ~]# ip addr showeno16777736 | grep 32

inet172.16.1.66/32 scope global eno16777736

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

[[email protected] keepalived]# rm -rf down

[[email protected] 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服务达到高可用的目的

(这里补充一下,如果killall在centos7上没有的话,就安装psmisc软件包)

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


[[email protected] keepalived]# killall -0 nginx   探测服务是否存在

[[email protected]
keepalived]# echo $?

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

[[email protected]
keepalived]# nginx -s stop

[[email protected]
keepalived]# killall -0 nginx

nginx:
no process found

[[email protected]
keepalived]# echo $?

1

[[email protected]
keepalived]# killall -0 httpd

[[email protected]
keepalived]# echo $?

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

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


global_defs
{

notification_email {

[email protected]

}

notification_email_from [email protected]

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关闭,看起主节点是否发生变化:

[[email protected] keepalived]# systemctl stop httpd

[[email protected] 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服务的话:

[[email protected] keepalived]# systemctl start httpd

[[email protected] keepalived]# ip a show eno16777736 | grep 32

inet 172.16.1.66/32
scope global eno16777736

[[email protected] 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=‘[email protected]‘

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"

}

时间: 2024-10-13 01:25:04

keepalived的相关文章

Redis+keepalived实现双机热备

一. 简介 安装使用centos 5.6(64) Master 192.168.2.235 Slave 192.168.2.236 Vip 192.168.2.200 编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby: 当 Master 挂掉,Slave 正常时, Slave接管服务,同时关闭主从

keepalived服务配置

keepalived keepalived.service - LVS and VRRP High Availability Monitor server1(10.71)--server2(10.72) 注意:建议实验环境使用NAT模式来连接,否则客户端可能无法正常访问测试.需要在两台keepalived上配置默认路由,如ip route add default dev eth0. 需要先同步服务器的时间,centos7.3建议使用chrony来同步时间 systemctl restart ch

keepalived+nginx

高集成:keepalived 负载均衡:nginx 1.服务器IP Client: 172.25.254.25 Keepalived+Nginx1: 172.25.254.115 Vip: 172.25.254.100 Keepalived+Nginx2: 172.25.254.215 Vip: 172.25.254.100 二.安装 1.安装keepalived yum install keepalived 2.编译安装Nginx [[email protected] ~]#useradd -

Keepalived安装配置

1.安装依赖包 yum -y install libnl libnl-devel yum install -y libnfnetlink libnfnetlink-devel rpm -ivhlibnfnetlink-1.0.0-1.el6.x86_64.rpm libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm 2.安装Keepalived cd /app tar -zxfkeepalived-1.3.5.tar.gz cd/app/keepalived-1.3

centos 7 LVS+keepalived实现nginx的高可用以及负载均衡

一.准备工作:关闭防火墙,selinux以免对实验结果造成影响,准备虚机,设置IP地址.主机名 hostname:Nginx01 IP:192.168.1.87 Role:Nginx Server hostname:Nginx02 IP: 192.168.1.88 Role:Nginx Server hostname:LVS01 IP: 192.168.1.89 Role:LVS+Keepalived hostname:LVS02 IP: 192.168.1.90 Role:LVS+Keepal

Keepalived安装与配置

一.简介 Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层.第4层和第7层交换机的功能.主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务. 上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space

Keepalived+Nginx实现高可用(HA)

keepalived的HA分为抢占模式和非抢占模式,抢占模式即MASTER从故障中恢复后,会将VIP从BACKUP节点中抢占过来.非抢占模式即MASTER恢复后不抢占BACKUP升级为MASTER后的VIP.下面分别介绍CentOS7下抢占模式和非抢占模式的配置方式: 1.方案规划 VIP IP 主机名 Nginx端口 192.168.1.210 192.168.1.201 nginx-01 80 192.168.1.210 192.168.1.202 nginx-02 80 两台服务器的VIP

keepalived 详解

什么是Keepalived呢,keepalived观其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,那说到keepalived时不得不说的一个协议就是VRRP协议,可以说这个协议就是keepalived实现的基础,那么首先我们来看看VRRP协议 VRRP术语: VRRP虚拟路由(VRRP router): VRRP的优势: 冗余:可以使用多个路由器设备作为LAN客户端的默认网关,大大降低

keepalived编译安装

编译安装keepalived 使用源码先安装keepalived 1.2.6 cd/usr/local/src wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz tarzxf keepalived-1.2.6.tar.gz cdkeepalived-1.2.6 ./configure--prefix=/usr/local/keepalived make makeinstall 所设keepalived安装到/usr/l

Mysql-mmm兼Keepalived高可用性mysql底层架构

一.架构图 二.配置步骤 所需软件 Mysql-5.6.20 mysql-mmm-agent-2.2.1 mysql-mmm-2.2.1 mysql-mmm-monitor-2.2.1 keepalived-1.2.13 安装步骤 1.四台机器安装mysql数据库(可直接yum安装或者编译安装) 2.设置主从关系 function IP Server Name server id monitor 192.168.1.234 - - monitor 192.168.1.241 master 192