四、利用keepalived实现web服务的高可用:
此过程的实现只需两台虚拟机HA1、HA2
1、HA1的配置
[root@node1 ~]# service keepalived stop
[root@node1 ~]# yum -y install httpd
[root@node1 ~]# vim /var/www/html/index.html
[root@node1 keepalived]# service httpd start
Starting httpd: [ OK ]
在物理主机上访问172.16.66.6
2、HA2:
[root@node1 ~]# service keepalived stop 停止keepalived服务
[root@node2 ~]# yum -y install httpd 安装httpd
[root@node2 keepalived]# vim /var/www/html/index.html
[root@node2 keepalived]# service httpd start
Starting httpd: [ OK ]
在物理主机上访问172.16.66.7
3、编辑节点1的keepalived的配置文件并提供相应的脚本后启动服务
[root@node1 ~]# cd /etc/keepalived/
[root@node1 keepalived]# cp keepalived.conf.haproxy_example keepalived.conf
cp: overwrite `keepalived.conf'? yes
分别修改两节点的配置文件并重启服务:
HA1:
[root@node1 keepalived]# vim keepalived.conf 脚本内容如下:
! Configuration File for keepalived
global_defs {
notification_email {
linuxedu@foxmail.com
mageedu@126.com
}
notification_email_from kanotify@magedu.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_id LVS_DEVEL
}
vrrp_script chk_httpd {
script "killall -0 haproxy"
interval 2
# check every 2 seconds
weight -2
# if failed, decrease 2 of the priority
fall 2
# require 2 failures for failures
rise 1
# require 1 sucesses for ok
}
vrrp_script chk_schedown {
script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
interval 2
weight -2
}
vrrp_instance VI_1 {
interface eth0
# interface for inside_network, bound by vrrp
state 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 whole lot.
priority 101
# for electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines.
virtual_router_id 51
# arbitary unique number 0..255
# used to differentiate multiple instances of vrrpd
# running on the same NIC (and hence same socket).
garp_master_delay 1
authentication {
auth_type PASS
auth_pass password
}
track_interface {
eth0
}
# optional, monitor these as well.
# go to FAULT state if any of these go down.
virtual_ipaddress {
172.16.66.1/16 dev eth0 label eth0:0
}
#addresses add|del on change to MASTER, to BACKUP.
#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_haproxy
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 slave routers
# priority 101 # 100 for BACKUP
# virtual_router_id 79
# garp_master_delay 1
#
# authentication {
# auth_type PASS
# auth_pass password
# }
# track_interface {
# eth0
# }
# virtual_ipaddress {
# 172.16.66.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"
#}
复制配置文件内容到节点2上
[root@node1 keepalived]# service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
HA2的配置:
[root@node1 keepalived]# vim keepalived.conf 修改如下内容
vrrp_instance VI_1 {
interface eth0
# interface for inside_network, bound by vrrp
state BACKUP
# 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 whole lot.
priority 100
# for electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines.
[root@node2 keepalived]# service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
4、模拟master出现故障
首先停掉主服务器的web服务,然后查看IP是否漂移到了从服务器上
[root@node1 keepalived]# service httpd stop
Stopping httpd: [ OK ]
主服务器上IP显示为:
从服务器上IP显示为
测试:在物理主机上访问:172.16.66.1