利用keepalived的实现LVS的高可用和web高可用
利用keepalived的实现LVS的高可用
LVS+Keepalived的简介
LVS
LVS是Linux VirtualServer的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。主要实现的是服务的负载均衡能力。
Keepalived
Keepalived是一个用C写的路由软件,主要的目标是提供简单的设施和为强大的负载集群(ipvs)提供高可用性。Keepalived在这里的主要作用是做RealServer的健康状态检查以及LVS的故障转移。
VRRP介绍:
VRRP(虚拟路由冗余协议)是一种路由容错协议,也叫做备份路由协议,它可以把一个虚拟路由器的责任动态分配到局域网上的 VRRP 路由器中的一台。Vrrp中存在着一种选举机制,用以选出提供服务的路由即主控路由,其他的则成了备份路由。当主控路由失效后,备份路由中会重新选举出一个主控路由,来继续工作,来保障不间断服务。简单来说就是一个就是实现路由的高可用性。
Vrrp路由器有三种状态Initialize、 Master、 Backup 。
Inittialize
即初始化状态,是在选举开始之前的状态
Backup
即备份机状态,监控主控机状态,并准备随时接替,无缝链接
1)不对目标地址为VIP的arp包回应
2)丢弃目标MAC为虚拟路由器MAC的包
3)丢弃目标IP是VIP的包
4)如果规定时间内没有接收到master的vrrp通告,将会发出通告,重新选举选举时,会考虑到优先级,IP地址打下,还有一个特殊选项即nopreempt(下面有讲解)
Master
即主控机状态,是真实转发目标IP为VIP的包的路由
1)必须对目标IP为VIP的ARP请求处理
2)必须对目标MAC为虚拟路由器的MAC处理
3)必须接受和VIP有关的数据包
4)当出现故障的时候,会先取消adver_timer,发出一个宣称自己的优先级降为0的通告,进入初始化状态。具体内容看RFC文档。
LVS+keepalived的实现框架
http://blog.运维网.com/attachment/201305/235327474.pnghttp://blog.运维网.com/e/u/themes/default/images/spacer.gif
IP配置信息:
LVS-DR-Master 172.16.11.4
LVS-DR-Backup 172.16.11.5
LVS-DR-VIP 172.16.11.1
WEB1-Realserver172.16.11.2
WEB2-RealServer172.16.11.3
实现过程:
一、WEB层
Realserver1
1、安装httpd服务
# yun install httpd -y
2、Realserver配置
# echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
# echo 1 >/proc/sys/net/ipv4/conf/eth0/arp_ignore
# echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
# echo 2 >/proc/sys/net/ipv4/conf/eth0/ arp_announce
# ifconfig lo:0172.16.11.1 broadcast 172.146.11.1 netmask 255.255.255.255
# route add –host 172.16.11.1 dev lo:0
3、启动httpd测试WEB服务
http://blog.运维网.com/attachment/201305/235359226.png
Realserver配置同Realserver1
http://blog.运维网.com/attachment/201305/235432723.png
二、负载均衡层
1、安装ipvsadm和keepalived(HA1和HA2都安装,这里只以HA1为例)
# yum installipvsadm –y
# yum--nogpgcheck localinstall keepalived-1.2.7-5.i386.rpm
2、配置keepalived
# cd/etc/keepalived/
# vimkeepalived.conf
修改后的内容如下
! ConfigurationFile for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
state MASTER
interface eth0
virtual_router_id 57
priority 101
advert_int 1
authentication {
auth_typePASS
auth_pass password
}
virtual_ipaddress {
172.16.11.1
}
}
virtual_server172.16.11.1 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
nat_mask 255.255.0.0
protocol TCP
real_server 172.16.11.2 80 {
weight 1
TCP_CHECK
{
connect_port 80
bindto 172.16.11.2
connect_timeout 2
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
real_server 172.16.11.3 80 {
weight 1
TCP_CHECK
{
connect_port 80
bindto 172.16.11.3
connect_timeout 2
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
更改后复制到HA2上一份,
3、修改HA2上的配置文件,修改部分如下
state BACKUP
priority 100
4、启动keepalived服务(先启动主后启动从)
# servicekeepalived start
5、查看虚拟路由ip是否已添加上
# ip addr show
HA1:
http://blog.运维网.com/attachment/201305/235450761.png
HA2:
http://blog.运维网.com/attachment/201305/235516895.png
6、查看LVS服务运行
# ipvsadm –L –n
http://blog.运维网.com/attachment/201305/235526152.png
7、测试web的服务(记得启动httpd服务)
http://blog.运维网.com/attachment/201305/235542839.png
刷新
http://blog.运维网.com/attachment/201305/235556522.png
8、HA1停止服务,查看HA2是否启动
HA1:
http://blog.运维网.com/attachment/201305/235609122.png
HA2:
http://blog.运维网.com/attachment/201305/235625768.png
基于keepalived实现web的高可用
http://blog.运维网.com/attachment/201305/235653213.png
一、在HA1和HA2上安装web服务并提供测试页面测试
# yum installhttpd –y
提供测试页面,启动服务测试
HA1:
http://blog.运维网.com/attachment/201305/235707500.png
http://blog.运维网.com/attachment/201305/235734526.png
HA2:
http://blog.运维网.com/attachment/201305/235746593.png
http://blog.运维网.com/attachment/201305/235758469.png
二、在HA1和HA2(上面keepalived+LVS的机器)修改keepalived的配置文件,(HA1上的state MASTER和priority 101 在HA1上全部换成state BACKUP和priority 100)
!Configuration File forkeepalived
global_defs {
notification_email{
linuxedu@foxmail.com
mageedu@126.com
}
notification_email_fromkanotify@magedu.com
!Configuration File forkeepalived
global_defs{
notification_email{
linuxedu@foxmail.com
mageedu@126.com
}
notification_email_fromkanotify@magedu.com
smtp_connect_timeout 3
smtp_server 127.0.0.1
router_idLVS_DEVEL
}
vrrp_scriptchk_httpd {
script "killall-0 httpd"
interval 2
#check every 2seconds
weight-2
# iffailed, decrease 2of the priority
fall 2
#require 2failures forfailures
rise 1
#require 1sucesses forok
}
vrrp_scriptchk_schedown {
script "[[-f /etc/keepalived/down ]]&& exit 1 || exit 0"
interval 2
weight-2
}
vrrp_instanceVI_1 {
interfaceeth0
# interfaceforinside_network, bound byvrrp
stateMASTER
#Initial state, MASTER|BACKUP
# Assoon asthe othermachine(s) come up,
# anelection will be held and the machine
# withthe highest "priority"willbecome MASTER.
# Sothe entry here doesn't matter a wholelot.
priority 101
# forelecting MASTER, highest prioritywins.
# tobe MASTER, make 50more 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 1
authentication{
auth_typePASS
auth_passpassword
}
track_interface{
eth0
}
#optional, monitor these aswell.
# goto FAULT state ifany of these godown.
virtual_ipaddress{
172.16.11.6
}
#addressesadd|del on change to MASTER, toBACKUP.
#Withthe same entries on other machines,
#theopposite transition will be occuring.
}
track_interface{
eth0
}
#optional, monitor these aswell.
# goto FAULT state ifany of these godown.
virtual_ipaddress{
172.16.11.6
}
#addressesadd|del on change to MASTER, toBACKUP.
#Withthe same entries on other machines,
#theopposite transition will be occuring.
#/brd devscopelabel
track_script{
chk_httpd
chk_schedown
}
notify_master"/etc/keepalived/notify.shmaster"
notify_backup"/etc/keepalived/notify.shbackup"
notify_fault"/etc/keepalived/notify.shfault"
}
#vrrp_instanceVI_2 {
# interfaceeth0
#state MASTER# BACKUP forslaverouters
#priority 101# 100forBACKUP
#virtual_router_id 52
#garp_master_delay 1
#
#authentication {
# auth_typePASS
# auth_pass password
#}
#track_interface {
# eth0
#}
#virtual_ipaddress {
# 172.16.11.6/16
#}
#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"
#
}
三、测试
启动keepalived服务
http://blog.运维网.com/attachment/201305/235813765.png
查看服务启动的虚拟ip
主节点:
http://blog.运维网.com/attachment/201305/235829712.png
备节点:
http://blog.运维网.com/attachment/201305/235844893.png
访问测试页面,看web的
http://blog.运维网.com/attachment/201305/235901998.png
四、手动节点转移
# touch /etc/keepalived/down
查看节点转移情况:
主节点:
http://blog.运维网.com/attachment/201305/235913884.png
备节点:
http://blog.运维网.com/attachment/201305/235923985.png
测试页面:
http://blog.运维网.com/attachment/201305/235938440.png
节点切换成功
页:
[1]