基于keepalived实现web服务的高可用
这是在完成上述实验后在HA1与HA2主机实现web服务的高可用

关闭node1与node2的keepalive
#service keepalived stop
在node1与node2上安装web服务
#yum -y install httpd
启动web服务后,分别准备web页面
# echo "node1.magedu.com" >/var/www/html/index.html
# echo "node2.magedu.com" >/var/www/html/index.html
访问web进行测试


配置文件准备与配置,此rpm制作(马哥专利的哦)时已经提供了(rpm包已经上传到附件了)
# cp keepalived.conf.haproxy_examplekeepalived.conf
# vim keepalived.conf
修改内容
定义一下服务脚本名称httpd(整个配置文件就贴出来了)
|
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
|
! Configuration File for keepalivedglobal_defs {notification_email {linuxedu@foxmail.commageedu@126.com} notification_email_from kanotify@magedu.com! Configuration File for keepalivedglobal_defs {notification_email {linuxedu@foxmail.commageedu@126.com}notification_email_from kanotify@magedu.comsmtp_connect_timeout 3smtp_server 127.0.0.1router_id LVS_DEVEL}vrrp_script chk_httpd {script "killall -0 httpd"interval 2# check every 2 secondsweight -2# if failed, decrease 2 of the priorityfall 2# require 2 failures for failuresrise 1# require 1 sucesses for ok}vrrp_script chk_schedown {script "[[ -f /etc/keepalived/down ]]&& exit 1 || exit 0"interval 2weight -2}vrrp_instance VI_1 {interface eth0# interface for inside_network, bound byvrrpstate MASTER# Initial state, MASTER|BACKUP# As soon as the other machine(s) come up,# an election will be held and the machine# with the highest "priority"will become MASTER.# So the entry here doesn't matter a wholelot.priority 101# for electing MASTER, highest prioritywins.# to be MASTER, make 50 more than other machines.virtual_router_id 51# arbitary unique number 0..255# used to differentiate multiple instancesof vrrpd# running on the same NIC (and hence samesocket).garp_master_delay 1authentication {auth_typePASSauth_pass password}track_interface {eth0}# optional, monitor these as well.# go to FAULT state if any of these godown.virtual_ipaddress {172.16.51.70/16 dev eth0 label eth0:0}#addresses add|del on change to MASTER, toBACKUP.#With the same entries on other machines,#the opposite transition will be occuring.}track_interface {eth0}# optional, monitor these as well.# go to FAULT state if any of these godown.virtual_ipaddress {172.16.51.70/16 dev eth0 label eth0:0}#addresses add|del on change to MASTER, toBACKUP.#With the same entries on other machines,#the opposite transition will be occuring.#<IPADDR>/<MASK> brd<IPADDR> dev <STRING> scope <SCOPE> label <LABEL>track_script {chk_httpd chk_schedown}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 {# interface eth0# state MASTER # BACKUP for slaverouters# priority 101 # 100 for BACKUP# virtual_router_id 52# garp_master_delay 1## authentication {# auth_typePASS# auth_pass password# }# track_interface {# eth0# }# virtual_ipaddress {# 172.16.100.2/16 dev eth0 label eth0:1# }# track_script {# chk_haproxy# chk_mantaince_down# }## notify_master "/etc/keepalived/notify.sh master eth0:1"# notify_backup "/etc/keepalived/notify.sh backup eth0:1"# notify_fault "/etc/keepalived/notify.sh fault eth0:1"#} |
复制配置文件到node2主机
# scp /etc/keepalived/keepalived.confnode2:/etc/keepalived/
复制后需要修改内容如下
state BACKUP
priority 100
启动keepalived服务

查看MASTER(node1)上主机定义的VIP(eth0别名IP)是否成功

访问虚拟IP地址

主备转换测试
# cd /etc/keepalived/
# touch down
在备用节点node2上查看
访问虚拟IP

现在把down文件删除,MASTER节点会夺回资源,因为在node1上的优先级(101)比nod2上的优先级(100)高
