Linux 高可用(HA)集群之keepalived详解
http://freeloda.blog.51cto.com/2033581/1280962
大纲
一、前言
二、Keepalived 详解
三、环境准备
四、LVS+Keepalived 实现高可用的前端负载均衡器
一、前言
这篇文章是前几篇文章的总结,我们先简单的总结一下我们前面讲解的内容,前面我们讲解了,LVS(负载均衡器)、Heartbeat、Corosync、Pacemaker、Web高可用集群、MySQL高可用集群、DRDB、iscsi、gfs2、cLVM等,唯一没有讲解的就是LVS可用,也就是前端高可用,我们这一篇博文主要讲解内容。在说这个之前我们得和大家讨论一个问题,也是好多博友问的问题。Heartbeat、Corosync、Keepalived这三个集群组件我们到底选哪个好,首先我想说明的是,Heartbeat、Corosync是属于同一类型,Keepalived与Heartbeat、Corosync,根本不是同一类型的。Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称VRRP);Heartbeat或Corosync是基于主机或网络服务的高可用方式;简单的说就是,Keepalived的目的是模拟路由器的高可用,Heartbeat或Corosync的目的是实现Service的高可用。所以一般Keepalived是实现前端高可用,常用的前端高可用的组合有,就是我们常见的LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。而Heartbeat或Corosync是实现服务的高可用,常见的组合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 实现Web服务器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 实现MySQL服务器的高可用。总结一下,Keepalived中实现轻量级的高可用,一般用于前端高可用,且不需要共享存储,一般常用于两个节点的高可用。而Heartbeat(或Corosync)一般用于服务的高可用,且需要共享存储,一般用于多节点的高可用。这个问题我们说明白了,又有博友会问了,那heartbaet与corosync我们又应该选择哪个好啊,我想说我们一般用corosync,因为corosync的运行机制更优于heartbeat,就连从heartbeat分离出来的pacemaker都说在以后的开发当中更倾向于corosync,所以现在corosync+pacemaker是最佳组合。但说实话我对于软件没有任何倾向性,所以我把所有的集群软件都和大家说了一下,我认为不管什么软件,只要它能存活下来都有它的特点和应用领域,只有把特定的软件放在特定的位置才能发挥最大的作用,那首先我们得对这个软件有所有了解。学习一种软件的最好方法,就是去查官方文档。好了说了那么多希望大家有所收获,下面我们来说一说keepalived。
二、Keepalived 详解
1.Keepalived 定义
Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。Keepalived是VRRP的完美实现,因此在介绍keepalived之前,先介绍一下VRRP的原理。
2.VRRP 协议简介
在现实的网络环境中,两台需要通信的主机大多数情况下并没有直接的物理连接。对于这样的情况,它们之间路由怎样选择?主机如何选定到达目的主机的下一跳路由,这个问题通常的解决方法有二种:
在主机上使用动态路由协议(RIP、OSPF等)
在主机上配置静态路由
很明显,在主机上配置动态路由是非常不切实际的,因为管理、维护成本以及是否支持等诸多问题。配置静态路由就变得十分流行,但路由器(或者说默认网关default gateway)却经常成为单点故障。VRRP的目的就是为了解决静态路由单点故障问题,VRRP通过一竞选(election)协议来动态的将路由任务交给LAN中虚拟路由器中的某台VRRP路由器。
3.VRRP 工作机制
在一个VRRP虚拟路由器中,有多台物理的VRRP路由器,但是这多台的物理的机器并不能同时工作,而是由一台称为MASTER的负责路由工作,其它的都是BACKUP,MASTER并非一成不变,VRRP让每个VRRP路由器参与竞选,最终获胜的就是MASTER。MASTER拥有一些特权,比如,拥有虚拟路由器的IP地址,我们的主机就是用这个IP地址作为静态路由的。拥有特权的MASTER要负责转发发送给网关地址的包和响应ARP请求。
VRRP通过竞选协议来实现虚拟路由器的功能,所有的协议报文都是通过IP多播(multicast)包(多播地址224.0.0.18)形式发送的。虚拟路由器由VRID(范围0-255)和一组IP地址组成,对外表现为一个周知的MAC地址。所以,在一个虚拟路由 器中,不管谁是MASTER,对外都是相同的MAC和IP(称之为VIP)。客户端主机并不需要因为MASTER的改变而修改自己的路由配置,对客户端来说,这种主从的切换是透明的。
在一个虚拟路由器中,只有作为MASTER的VRRP路由器会一直发送VRRP通告信息(VRRPAdvertisement message),BACKUP不会抢占MASTER,除非它的优先级(priority)更高。当MASTER不可用时(BACKUP收不到通告信息), 多台BACKUP中优先级最高的这台会被抢占为MASTER。这种抢占是非常快速的(<1s),以保证服务的连续性。由于安全性考虑,VRRP包使用了加密协议进行加密。
4.VRRP 工作流程
(1).初始化:
路由器启动时,如果路由器的优先级是255(最高优先级,路由器拥有路由器地址),要发送VRRP通告信息,并发送广播ARP信息通告路由器IP地址对应的MAC地址为路由虚拟MAC,设置通告信息定时器准备定时发送VRRP通告信息,转为MASTER状态;否则进入BACKUP状态,设置定时器检查定时检查是否收到MASTER的通告信息。
(2).Master
设置定时通告定时器;
用VRRP虚拟MAC地址响应路由器IP地址的ARP请求;
转发目的MAC是VRRP虚拟MAC的数据包;
如果是虚拟路由器IP的拥有者,将接受目的地址是虚拟路由器IP的数据包,否则丢弃;
当收到shutdown的事件时删除定时通告定时器,发送优先权级为0的通告包,转初始化状态;
如果定时通告定时器超时时,发送VRRP通告信息;
收到VRRP通告信息时,如果优先权为0,发送VRRP通告信息;否则判断数据的优先级是否高于本机,或相等而且实际IP地址大于本地实际IP,设置定时通告定时器,复位主机超时定时器,转BACKUP状态;否则的话,丢弃该通告包;
(3).Backup
设置主机超时定时器;
不能响应针对虚拟路由器IP的ARP请求信息;
丢弃所有目的MAC地址是虚拟路由器MAC地址的数据包;
不接受目的是虚拟路由器IP的所有数据包;
当收到shutdown的事件时删除主机超时定时器,转初始化状态;
主机超时定时器超时的时候,发送VRRP通告信息,广播ARP地址信息,转MASTER状态;
收到VRRP通告信息时,如果优先权为0,表示进入MASTER选举;否则判断数据的优先级是否高于本机,如果高的话承认MASTER有效,复位主机超时定时器;否则的话,丢弃该通告包;
5.ARP查询处理
当内部主机通过ARP查询虚拟路由器IP地址对应的MAC地址时,MASTER路由器回复的MAC地址为虚拟的VRRP的MAC地址,而不是实际网卡的 MAC地址,这样在路由器切换时让内网机器觉察不到;而在路由器重新启动时,不能主动发送本机网卡的实际MAC地址。如果虚拟路由器开启的ARP代理 (proxy_arp)功能,代理的ARP回应也回应VRRP虚拟MAC地址;好了VRRP的简单讲解就到这里,我们下来讲解一下Keepalived的案例。
三、环境准备
1.操作系统
CentOS 6.4 X86_64
2.软件版本
ipvsadm.x86_64 0:1.25-10.el6
keepalived.x86_64 0:1.2.7-3.el6
httpd-2.2.15-29.el6.centos.x86_64
3.实验拓扑
4.时间同步
node1:
1
|
[root@node1 ~] # ntpdate 202.120.2.101 |
node2:
1
|
[root@node2 ~] # ntpdate 202.120.2.101 |
master:
1
|
[root@master ~] # ntpdate 202.120.2.101 |
slave:
1
|
[root@slave ~] # ntpdate 202.120.2.101 |
5.主机名互相解析
node1:
1
2
3
4
5
|
[root@node1 ~] # cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.18.201 node1. test .com node1 192.168.18.202 node2. test .com node2 |
node2:
1
2
3
4
5
|
[root@node2 ~] # cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.18.201 node1. test .com node1 192.168.18.202 node2. test .com node2 |
6.安装yum源
node1:
1
2
|
[root@node1 ~] # rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@node1 ~] # rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm |
node2:
1
2
|
[root@node2 ~] # rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@node2 ~] # rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm |
master:
1
2
|
[root@master ~] # rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@master ~] # rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm |
slave:
1
2
|
[root@slave ~] # rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@slave ~] # rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm |
四、LVS+Keepalived 实现高可用的前端负载均衡器
node1:
1.安装httpd
1
|
[root@node1 ~] # yum install -y httpd |
2.配置httpd
1
2
|
[root@node1 ~] # vim /var/www/html/index.html <h1>RS1. test .com< /h1 > |
3.启动httpd
1
|
[root@node1 ~] # service httpd start |
4.测试
5.设置开机自启动
1
2
3
|
[root@node1 ~] # chkconfig httpd on [root@node1 ~] # chkconfig httpd --list httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 |
6.配置node1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
[root@node1 ~] # mkdir src [root@node1 ~] # cd src/ [root@node1 src] # vim realserver.sh #!/bin/bash # # Script to start LVS DR real server. # description: LVS DR real server # . /etc/rc .d /init .d /functions VIP=192.168.18.200 #修改你的VIP host=` /bin/hostname ` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 down echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce ;; status) # Status of LVS-DR real server. islothere=` /sbin/ifconfig lo:0 | grep $VIP` isrothere=` netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ]; then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac [root@node1 src] # chmod +x realserver.sh [root@node1 src] # ll 总用量 4 -rwxr-xr-x 1 root root 1485 8月 22 10:18 realserver.sh [root@node1 src] # ./realserver.sh start |
7.查看配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[root@node1 src] # ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:11:92:E4 inet addr:192.168.18.201 Bcast:192.168.18.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe11:92e4 /64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:115061 errors:0 dropped:0 overruns:0 frame:0 TX packets:14979 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:43448483 (41.4 MiB) TX bytes:1224926 (1.1 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1 /128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:2 errors:0 dropped:0 overruns:0 frame:0 TX packets:2 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:168 (168.0 b) TX bytes:168 (168.0 b) lo:0 Link encap:Local Loopback inet addr:192.168.18.200 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436 Metric:1 [root@node1 src] # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo 192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0 [root@node1 src] # cat /proc/sys/net/ipv4/conf/lo/arp_ignore 1 [root@node1 src] # cat /proc/sys/net/ipv4/conf/lo/arp_announce 2 [root@node1 src] # cat /proc/sys/net/ipv4/conf/all/arp_ignore 1 [root@node1 src] # cat /proc/sys/net/ipv4/conf/all/arp_announce 2 |
好了,node1到这里基本配置完成,下面我们来配置node2。
node2:
1.安装httpd
1
|
[root@node2 ~] # yum install -y httpd |
2.配置httpd
1
2
|
[root@node2 ~] # vim /var/www/html/index.html <h1>RS2. test .com< /h1 > |
3.启动httpd
1
|
[root@node2 ~] # service httpd start |
4.测试
5.设置开机自启动
1
2
3
|
[root@node2 ~] # chkconfig httpd on [root@node2 ~] # chkconfig httpd --list httpd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 |
6.配置node2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
[root@node2 ~] # mkdir src [root@node2 ~] # cd src/ [root@node2 src] # vim realserver.sh #!/bin/bash # # Script to start LVS DR real server. # description: LVS DR real server # . /etc/rc .d /init .d /functions VIP=192.168.18.200 host=` /bin/hostname ` case "$1" in start) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo up echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0 ;; stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 down echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce ;; status) # Status of LVS-DR real server. islothere=` /sbin/ifconfig lo:0 | grep $VIP` isrothere=` netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ]; then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac [root@node2 src] # chmod +x realserver.sh [root@node2 src] # ./realserver.sh start |
7.查看配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[root@node2 src] # ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B8:DF:6A inet addr:192.168.18.202 Bcast:192.168.18.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb8:df6a /64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:110545 errors:0 dropped:0 overruns:0 frame:0 TX packets:10461 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:32853789 (31.3 MiB) TX bytes:889109 (868.2 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1 /128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:6 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:616 (616.0 b) TX bytes:616 (616.0 b) lo:0 Link encap:Local Loopback inet addr:192.168.18.200 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:16436 Metric:1 [root@node2 src] # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo 192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0 [root@node2 src] # cat /proc/sys/net/ipv4/conf/lo/arp_ignore 1 [root@node2 src] # cat /proc/sys/net/ipv4/conf/lo/arp_announce 2 [root@node2 src] # cat /proc/sys/net/ipv4/conf/all/arp_ignore 1 [root@node2 src] # cat /proc/sys/net/ipv4/conf/all/arp_announce 2 |
好了,到这里node2也基本配置完成。下面我们来配置master与slave。
masterg与slave:
1.安装keepalived与ipvsadm
1
2
|
[root@master ~] # yum install -y keepalived ipvsadm [root@slave ~] # yum install -y keepalived ipvsadm |
2.修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@master ~] # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com #配置管理员邮箱 } notification_email_from root #配置发件人 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 priority 101 #配置优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 #配置虚拟IP地址 } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 #persistence_timeout 50 protocol TCP real_server 192.168.18.201 80 { #配置realaserver weight 1 HTTP_GET { #监控配置 url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } } |
3.将配置文件同步到slave
1
|
[root@master ~] # scp /etc/keepalived/keepalived.conf root@192.168.18.207:/etc/keepalived/ |
4.简单修改一下slave配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@slave keepalived] # cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP #修改为BACKUP interface eth0 virtual_router_id 51 priority 100 #修改优先级 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 #persistence_timeout 50 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } } |
5.启动master与slave的keepalived服务
1
2
3
4
|
[root@master ~] # service keepalived start 正在启动 keepalived: [确定] [root@slave ~] # service keepalived start 正在启动 keepalived: [确定] |
6.查看一下LVS状态
1
2
3
4
5
6
7
|
[root@master ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.18.200:80 rr -> 192.168.18.201:80 Route 1 0 0 -> 192.168.18.202:80 Route 1 0 0 |
7.测试
8.模拟故障
(1).停止一下node1
1
2
|
[root@node1 src] # service httpd stop 停止 httpd: [确定] |
(2).查看一下的lvs
1
2
3
4
5
6
|
[root@master ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.18.200:80 rr -> 192.168.18.202:80 Route 1 0 0 |
(3).测试一下
(4).查看一下邮件
(5).重新启动一下node1
1
2
|
[root@node1 src] # service httpd start 正在启动 httpd: [确定] |
(6).再查看一下lvs状态
1
2
3
4
5
6
7
|
[root@master ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.18.200:80 rr -> 192.168.18.201:80 Route 1 0 0 -> 192.168.18.202:80 Route 1 0 0 |
(7).再查看一下邮件
(8).关闭master上keepalived
1
2
3
4
5
6
|
[root@master ~] # service keepalived stop 停止 keepalived: [确定] [root@master ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn |
(9).查看一下slave状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@slave ~] # 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:f9:e6:26 brd ff:ff:ff:ff:ff:ff inet 192.168.18.207 /24 brd 192.168.18.255 scope global eth0 inet 192.168.18.200 /32 scope global eth0 inet6 fe80::20c:29ff:fef9:e626 /64 scope link valid_lft forever preferred_lft forever [root@slave ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.18.200:80 rr -> 192.168.18.201:80 Route 1 0 0 -> 192.168.18.202:80 Route 1 0 0 |
(10).再次测试一下
注,大家可以看到,经过上面的演示我们现在LVS的高可用即前端负载均衡的高可用,同时实现对后端realserver监控,也实现后端realserver宕机时会给管理员发送邮件。但还有几个问题我们还没有解决,问题如下:
所有realserver都down机,怎么处理?是不是用户就没法打开,还是提供一下维护页面。
怎么完成维护模式keepalived切换?
如何在keepalived故障时,发送警告邮件给指定的管理员?
9.所有realserver都down机,怎么处理?
问题:在集群中如果所有real server全部宕机了,客户端访问时就会出现错误页面,这样是很不友好的,我们得提供一个维护页面来提醒用户,服务器正在维护,什么时间可以访问等,下面我们就来解决一下这个问题。解决方案有两种,一种是提供一台备用的real server当所有的服务器宕机时,提供维护页面,但这样做有点浪费服务器。另一种就是在负载均衡器上提供维护页面,这样是比较靠谱的,也比较常用。下面我们就来具体操作一下。
(1).master与slave安装上httpd
1
2
|
[root@master ~] # yum install -y httpd [root@slave ~] # yum install -y httpd |
(2).配置维护页面
1
2
3
4
|
[root@master ~] # vim /var/www/html/index.html Website is currently under maintenance, please come back later! [root@slave ~] # vim /var/www/html/index.html Website is currently under maintenance, please come back later! |
(3).启动httpd服务并测试
1
2
3
4
|
[root@master ~] # service httpd start 正在启动 httpd: [确定] [root@slave ~] # service httpd start 正在启动 httpd: [确定] |
(4).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@master ~] # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root 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 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 #增加一行sorry_server } |
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[root@slave ~] # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root 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 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 #增加一行sorry_server } |
(5).关闭所有的real server并重新启动一下master与slave的keepalived
1
2
3
4
5
6
7
8
9
10
|
[root@node1 ~] # service httpd stop 停止 httpd: [确定] [root@node2 ~] # service httpd stop 停止 httpd: [确定] [root@master ~] # service keepalived restart 停止 keepalived: [确定] 正在启动 keepalived: [确定] [root@slave ~] # service keepalived restart 停止 keepalived: [确定] 正在启动 keepalived: [确定] |
(6).查看一下lvs
1
2
3
4
5
6
|
[root@master ~] # ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.18.200:80 rr -> 127.0.0.1:80 Local 1 0 0 |
(7).测试
注,sorry_server测试成功,下面我们继续。
10.怎么完成维护模式keepalived切换?
问题:我们一般进行主从切换测试时都是关闭keepalived或关闭网卡接口,有没有一种方法能实现在不关闭keepalived下或网卡接口来实现维护呢?方法肯定是有的,在keepalived新版本中,支持脚本vrrp_srcipt,具体如何使用大家可以man keepalived.conf查看。下面我们来演示一下具体怎么实现。
(1).定义脚本
1
2
3
4
5
6
7
|
vrrp_srcipt chk_schedown { script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" interval 1 #监控间隔 weight -5 #减小优先级 fall 2 #监控失败次数 rise 1 #监控成功次数 } |
(2).执行脚本
1
2
3
|
track_script { chk_schedown #执行chk_schedown脚本 } |
(3).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
[root@master ~] # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_schedown { #定义vrrp执行脚本 script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" #查看是否有down文件,有就进入维护模式 interval 1 #监控间隔时间 weight -5 #降低优先级 fall 2 #失败次数 rise 1 #成功数次 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } track_script { #执行脚本 chk_schedown } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 } |
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
[root@slave ~] # cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_schedown { script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" interval 1 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } track_script { chk_schedown } } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 |
(4).测试
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[root@master keepalived] # touch down #新建一下down文件 [root@master keepalived] # ll 总用量 4 -rw-r--r-- 1 root root 0 8月 22 13:39 down -rw-r--r-- 1 root root 1317 8月 22 13:35 keepalived.conf [root@master keepalived] # tail -f /var/log/messages #查看一下日志 Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering MASTER STATE Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) setting protocol VIPs. Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.18.200 Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Received higher prio advert Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering BACKUP STATE Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) removing protocol VIPs. Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 added Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 removed Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent. Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent. ^C [root@master keepalived] # ip add show #查看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:4b:a1:85 brd ff:ff:ff:ff:ff:ff inet 192.168.18.208 /24 brd 192.168.18.255 scope global eth0 inet6 fe80::20c:29ff:fe4b:a185 /64 scope link valid_lft forever preferred_lft forever |
slave:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@slave ~] # ip addr show #查看一下VIP已转移到slave上 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:f9:e6:26 brd ff:ff:ff:ff:ff:ff inet 192.168.18.207 /24 brd 192.168.18.255 scope global eth0 inet 192.168.18.200 /32 scope global eth0 inet6 fe80::20c:29ff:fef9:e626 /64 scope link valid_lft forever preferred_lft forever |
好了,自写监测脚本,完成维护模式切换,到这里就演示成功,下面我们来解决最后一个问题,就是keepalived主从切换的邮件通告。
11.如何在keepalived故障时(或主备切换时),发送警告邮件给指定的管理员?
(1).keepalived通知脚本进阶示例
下面的脚本可以接受选项,其中
-s, --service SERVICE,...:指定服务脚本名称,当状态切换时可自动启动、重启或关闭此服务;
-a, --address VIP: 指定相关虚拟路由器的VIP地址;
-m, --mode {mm|mb}:指定虚拟路由的模型,mm表示主主,mb表示主备;它们表示相对于同一种服务而方,其VIP的工作类型;
-n, --notify {master|backup|fault}:指定通知的类型,即vrrp角色切换的目标角色;
-h, --help:获取脚本的使用帮助;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/bin/bash # Author: freeloda # description: An example of notify script # Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help contact= '1521076067@163.com' helpflag=0 serviceflag=0 modeflag=0 addressflag=0 notifyflag=0 Usage() { echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>" echo "Usage: notify.sh -h|--help" } ParseOptions() { local I=1; if [ $ # -gt 0 ]; then while [ $I - le $ # ]; do case $1 in -s|--service) [ $ # -lt 2 ] && return 3 serviceflag=1 services=(` echo $2| awk -F "," '{for(i=1;i<=NF;i++) print $i}' `) shift 2 ;; -h|--help) helpflag=1 return 0 shift ;; -a|--address) [ $ # -lt 2 ] && return 3 addressflag=1 vip=$2 shift 2 ;; -m|--mode) [ $ # -lt 2 ] && return 3 mode=$2 shift 2 ;; -n|--notify) [ $ # -lt 2 ] && return 3 notifyflag=1 notify=$2 shift 2 ;; *) echo "Wrong options..." Usage return 7 ;; esac done return 0 fi } #workspace=$(dirname $0) RestartService() { if [ ${ #@} -gt 0 ]; then for I in $@; do if [ -x /etc/rc .d /init .d/$I ]; then /etc/rc .d /init .d/$I restart else echo "$I is not a valid service..." fi done fi } StopService() { if [ ${ #@} -gt 0 ]; then for I in $@; do if [ -x /etc/rc .d /init .d/$I ]; then /etc/rc .d /init .d/$I stop else echo "$I is not a valid service..." fi done fi } Notify() { mailsubject= "`hostname` to be $1: $vip floating" mailbody= "`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1." echo $mailbody | mail -s "$mailsubject" $contact } # Main Function ParseOptions $@ [ $? - ne 0 ] && Usage && exit 5 [ $helpflag - eq 1 ] && Usage && exit 0 if [ $addressflag - ne 1 -o $notifyflag - ne 1 ]; then Usage exit 2 fi mode=${mode:-mb} case $notify in 'master' ) if [ $serviceflag - eq 1 ]; then RestartService ${services[*]} fi Notify master ;; 'backup' ) if [ $serviceflag - eq 1 ]; then if [ "$mode" == 'mb' ]; then StopService ${services[*]} else RestartService ${services[*]} fi fi Notify backup ;; 'fault' ) Notify fault ;; *) Usage exit 4 ;; esac |
(2).在keepalived.conf配置文件中,其调用方法如下所示:
notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200"
notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
(3).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[root@master keepalived] # cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_schedown { script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" interval 1 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } track_script { chk_schedown } #增加以下三行 notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200" notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200" notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200" } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 } |
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[root@slave keepalived] # cat keepalived.conf ! Configuration File for keepalived global_defs { notification_email { 15251076067@163.com } notification_email_from root smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_schedown { script "[ -e /etc/keepalived/down ] && exit 1 || exit 0" interval 1 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.18.200 } track_script { chk_schedown } #增加以下三行 notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200" notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200" notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200" } virtual_server 192.168.18.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 protocol TCP real_server 192.168.18.201 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } real_server 192.168.18.202 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 } } sorry_server 127.0.0.1 80 } |
(4).增加脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
[root@slave keepalived] # pwd /etc/keepalived [root@slave keepalived] # vim notify.sh [root@slave keepalived] # cat notify.sh #!/bin/bash # Author: freeloda # description: An example of notify script # Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help contact= '15251076067@163.com' helpflag=0 serviceflag=0 modeflag=0 addressflag=0 notifyflag=0 Usage() { echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>" echo "Usage: notify.sh -h|--help" } ParseOptions() { local I=1; if [ $ # -gt 0 ]; then while [ $I - le $ # ]; do case $1 in -s|--service) [ $ # -lt 2 ] && return 3 serviceflag=1 services=(` echo $2| awk -F "," '{for(i=1;i<=NF;i++) print $i}' `) shift 2 ;; -h|--help) helpflag=1 return 0 shift ;; -a|--address) [ $ # -lt 2 ] && return 3 addressflag=1 vip=$2 shift 2 ;; -m|--mode) [ $ # -lt 2 ] && return 3 mode=$2 shift 2 ;; -n|--notify) [ $ # -lt 2 ] && return 3 notifyflag=1 notify=$2 shift 2 ;; *) echo "Wrong options..." Usage return 7 ;; esac done return 0 fi } #workspace=$(dirname $0) RestartService() { if [ ${ #@} -gt 0 ]; then for I in $@; do if [ -x /etc/rc .d /init .d/$I ]; then /etc/rc .d /init .d/$I restart else echo "$I is not a valid service..." fi done fi } StopService() { if [ ${ #@} -gt 0 ]; then for I in $@; do if [ -x /etc/rc .d /init .d/$I ]; then /etc/rc .d /init .d/$I stop else echo "$I is not a valid service..." fi done fi } Notify() { mailsubject= "`hostname` to be $1: $vip floating" mailbody= "`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1." echo $mailbody | mail -s "$mailsubject" $contact } # Main Function ParseOptions $@ [ $? - ne 0 ] && Usage && exit 5 [ $helpflag - eq 1 ] && Usage && exit 0 if [ $addressflag - ne 1 -o $notifyflag - ne 1 ]; then Usage exit 2 fi mode=${mode:-mb} case $notify in 'master' ) if [ $serviceflag - eq 1 ]; then RestartService ${services[*]} fi Notify master ;; 'backup' ) if [ $serviceflag - eq 1 ]; then if [ "$mode" == 'mb' ]; then StopService ${services[*]} else RestartService ${services[*]} fi fi Notify backup ;; 'fault' ) Notify fault ;; *) Usage exit 4 ;; esac |
(5).给脚本增加执行权限
1
|
[root@slave keepalived] # chmod +x notify.sh |
(6).将master上脚本复制到slave上
1
|
[root@slave keepalived] # scp -p notify.sh root@192.168.18.207:/etc/keepalived/ |
(7).测试一下脚本
1
2
3
4
5
6
7
|
[root@slave keepalived] # ./notify.sh -h Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}> Usage: notify.sh -h|--help [root@slave keepalived] # ./notify.sh --help Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}> Usage: notify.sh -h|--help [root@slave keepalived] # ./notify.sh -m mb -a 1.1.1.1 -n master |
(8).查看一下邮件
注,大家可以看到成功收到邮件,测试成功。在模拟故障时先重启一下keepalived服务。
(9).模拟故障
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
[root@master keepalived] # ip addr show #查看一下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:4b:a1:85 brd ff:ff:ff:ff:ff:ff inet 192.168.18.208 /24 brd 192.168.18.255 scope global eth0 inet 192.168.18.200 /32 scope global eth0 inet6 fe80::20c:29ff:fe4b:a185 /64 scope link valid_lft forever preferred_lft forever [root@master keepalived] # touch down #进入维护模式 [root@master keepalived] # ll 总用量 8 -rw-r--r-- 1 root root 0 8月 22 14:39 down -rw-r--r-- 1 root root 1543 8月 22 14:04 keepalived.conf -rwxr-xr-x 1 root root 2516 8月 22 14:15 notify.sh [root@master keepalived] # ip addr show #再次查看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:4b:a1:85 brd ff:ff:ff:ff:ff:ff inet 192.168.18.208 /24 brd 192.168.18.255 scope global eth0 inet6 fe80::20c:29ff:fe4b:a185 /64 scope link valid_lft forever preferred_lft forever [root@slave keepalived] # ip addr show #大家可以看到VIP成功移动到slave上 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:f9:e6:26 brd ff:ff:ff:ff:ff:ff inet 192.168.18.207 /24 brd 192.168.18.255 scope global eth0 inet 192.168.18.200 /32 scope global eth0 inet6 fe80::20c:29ff:fef9:e626 /64 scope link valid_lft forever preferred_lft forever |
(10).查看一下邮件
注,大家可以看到,主备切换时,会发送邮件报警,好了到这里所有演示全部完成。希望大家有所收获^_^……
Linux 高可用(HA)集群之keepalived详解的更多相关文章
- corosync+pacemaker实现高可用(HA)集群
corosync+pacemaker实现高可用(HA)集群(一) 重要概念 在准备部署HA集群前,需要对其涉及的大量的概念有一个初步的了解,这样在实际部署配置时,才不至于不知所云 资源.服务与 ...
- heartbeat+nginx搭建高可用HA集群
前言: HA即(high available)高可用,又被叫做双机热备,用于关键性业务.简单理解就是,有2台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至B机器 ...
- HA集群基本概念详解
一.高可用集群的定义 二.高可用集群的衡量标准 三.高可用集群的层次结构 四.高可用集群的分类 五.高可用集群常用软件 六.共享存储 七.集群文件系统与集群LVM 八.高可用集群的工作原理 一.高可用 ...
- Keepalived+Nginx+Tomcat 实现高可用Web集群
https://www.jianshu.com/p/bc34f9101c5e Keepalived+Nginx+Tomcat 实现高可用Web集群 0.3912018.01.08 20:28:59字数 ...
- 搭建高可用kubernetes集群(keepalived+haproxy)
序 由于单master节点的kubernetes集群,存在master节点异常之后无法继续使用的缺陷.本文参考网管流程搭建一套多master节点负载均衡的kubernetes集群.官网给出了两种拓扑结 ...
- Keepalived之高可用LVS集群
前文我们聊了下keepalived的邮件通知相关配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13645163.html:今天我们来说说keepalive ...
- 企业运维实践-还不会部署高可用的kubernetes集群?使用kubeadm方式安装高可用k8s集群v1.23.7
关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 文章目录: 0x00 前言简述 ...
- 高可用Kubernetes集群-16. ansible快速部署
说明 本文档指导采用二进制包的方式快速部署高可用kubernetes集群. 脚本托管:k8s-ansible(持续更新) 参考:高可用kubernetes集群 组件版本 组件 版本 备注 centos ...
- 构建高可用ZooKeeper集群
ZooKeeper 是 Apache 的一个顶级项目,为分布式应用提供高效.高可用的分布式协调服务,提供了诸如数据发布/订阅.负载均衡.命名服务.分布式协调/通知和分布式锁等分布式基础服务.由于 Zo ...
随机推荐
- YII数据库增删查改操作
初学YII, 整理了一些YII数据库的相关操作, 共同学习,共同进步. 一.查询数据集合 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll ...
- iOS使用阿里云OSS对象存储 (SDK 2.1.1)
最近项目中用到了阿里云OSS对象存储,用来存储APP中图片.音频等一些数据.但坑爹的阿里云居然在11月20日将SDK版本更新到了2.1.1,然而网上给出的教程都是1.*版本的(针对iOS),两个版本所 ...
- C# static 干货全解析
讲解顺序 背景 静态字段 静态函数 静态方法 疑问解答 背景 static来源 在编写类的时候,有时候需要类里面的某个成员具有唯一性,也就是,对所有的对象都保持只有一个的状态.比如创建个人信息,我们都 ...
- nginx配置无效的问题
今天修改nginx的一个配置文件,却怎么都没效果,发现是启动nginx指定的配置文件不一样. #ps aux|grep nginx root 17672 0.0 0.0 45856 2 ...
- Bootstrap_排版_代码
不管使用哪种代码风格,在代码中碰到小于号(<)要使用硬编码“<”来替代,大于号(>)使用“>”来替代 一.单行内联代码 <code>:一般是针对于单个单词或单个句子 ...
- php中const定义常量
const 常量 1.在定义时必须被初始值,2.前面不加任何修饰符3.变量名字母一般都大写4.常量可以被子类继承5.一个常量是属于一个类的,而不是某个对象的 作用:当某些值是固定不变的,就用const ...
- python+flask+mongodb+whoosh实现自己的搜索引擎(一):目录
python+flask+jieba+mongodb+whoosh实现自己的搜索引擎 一.目录 二.基于python的爬虫 三.网页去燥,URL去重 四.基于mongodb的数据存储 五.基于whoo ...
- MYSQL 存储过程1、SQL存储过程的基础知识
在深入理解MySq之前,我们先理下一些简单的问题 Q:什么是存储过程?(stored procedure) A:是一段写好的SQL代码,特别的就是它是存在数据库的目录里.所以外部程序可以直接调用数据库 ...
- JQuery整体大纲
今天公司放假,闲的无聊,就总结了一套JQuery的笔记,我感觉更像是大纲,在这里跟大家分享一下,这是我的成果: 这个就是我的劳动成果了,说实话真是不容易,为了做这个东西,翻阅了很多以前做过的笔记,发现 ...
- ExtJS5_主界面上加入顶部和底部区域
为主界面加一个顶部区域和底部区域.一个管理系统的界面可以粗分为顶部标题部分.中间数据展示和处理的部分.底部备注和状态部分. 在增加这二个区域之前,我们先在MainModel.js中加入一些数据. Ex ...