from:http://www.tuicool.com/articles/jYnQraB
Keepalived Principle and Practical Essay
What is Keepalived? According to its name, keepalived means that keeping alive in the network means keeping online, which is called high availability or hot standby to prevent single point failure (single point failure means that once a failure occurs at a certain point, it will lead to the entire system architecture. When it comes to keepalived, a protocol that has to be said is the VRRP protocol. It can be said that this protocol is the basis of the keepalived implementation. First, let's take a look at the VRRP protocol.
Note that you must be patient enough for operation and maintenance. If you don't understand the protocol, it is difficult to thoroughly grasp keepalived.
A VRRP protocol
VRRP protocol
Friends who have studied networking know that the network design must take into account redundancy and disaster recovery, including line redundancy, equipment redundancy, etc. to prevent a single point of failure in the network. It is particularly important to implement redundancy at routers or Layer 3 switches. In the network There is a protocol in it to do this. This protocol is the VRRP protocol. Keepalived is to use the VRRP protocol to achieve high availability (HA).
There is an article on the VRRP protocol that is very well written. You can read it directly here (remember to read it carefully, it is basically based on this)
Post address http://bbs.ywlm.net/thread-790-1-1.html
Just use the server as a router
In the " VRRP Protocol ", it is mentioned that the ID of the virtual router, that is, the VRID, is more important here.
keepalived fully complies with the VRRP protocol including election mechanism and more
Two Keepalived principle
Keepalived Principle
Keepalived is also a modular design with different modules complex and different functions. Below are the components of keepalived
core check vrrp libipfwc libipvs-2.4 libipvs-2.6
core is the core of keepalived, the complex main process starts and maintains the loading and parsing of global configuration files, etc.
check负责healthchecker(健康检查)包括了各种健康检查方式以及对应的配置的解析包括LVS的配置解析
vrrpVRRPD子进程VRRPD子进程就是来实现VRRP协议的
libipfwciptables(ipchains)库配置LVS会用到
libipvs*配置LVS会用到
注意keepalived和LVS完全是两码事只不过他们各负其责相互配合而已
keepalived启动后会有三个进程
父进程内存管理子进程管理等等
子进程VRRP子进程
子进程healthchecker子进程
有图可知两个子进程都被系统WatchDog看管两个子进程各自复杂自己的事healthchecker子进程复杂检查各自服务器的健康程度例如HTTPLVS等等如果healthchecker子进程 检查到MASTER上服务不可用了就会通知本机上的兄弟 VRRP子进程 让他删除通告并且去掉虚拟IP转换为BACKUP状态
三Keepalived配置文件详解
keepalived配置详解
keepalived有三类配置区域(姑且就叫区域吧)注意不是三种配置文件是一个配置文件里面三种不同类别的配置区域
全局配置(Global Configuration)
VRRPD配置
一全局配置
全局配置又包括两个子配置
全局定义(global definition)
静态路由配置(static ipaddress/routes)
1全局定义(global definition)配置范例
global_defs { notification_email { admin@example.com } notification_email_from admin@example.com smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id node1 }
全局配置解析
global_defs全局配置标识表面这个区域{}是全局配置
notification_email { admin@example.com admin@ywlm.net }
表示keepalived在发生诸如切换操作时需要发送email通知以及email发送给哪些邮件地址邮件地址可以多个每行一个
notification_email_from [email protected]
表示发送通知邮件时邮件源地址是谁
smtp_server 127.0.0.1
表示发送email时使用的smtp服务器地址这里可以用本地的sendmail来实现
smtp_connect_timeout 30
连接smtp连接超时时间
router_id node1
机器标识
2静态地址和路由配置范例
notification_email { admin@example.com admin@ywlm.net }
这里实际上和系统里面命令配置IP地址和路由一样例如
192.168.1.1/24 brd + dev eth0 scope global 相当于: ip addr add 192.168.1.1/24 brd + dev eth0 scope global
就是给eth0配置IP地址
路由同理
一般这个区域不需要配置
这里实际上就是给服务器配置真实的IP地址和路由的在复杂的环境下可能需要配置一般不会用这个来配置 我们可以直接用vi /etc/sysconfig/network-script/ifcfg-eth1来配置切记这里可不是VIP哦不要搞混淆了切记切记
二VRRPD配置
VRRPD配置包括三个类
VRRP同步组(synchroization group)
VRRP实例(VRRP Instance)
VRRP脚本
1VRRP同步组(synchroization group)配置范例
vrrp_sync_group VG_1 { group { http mysql } notify_master /path/to/to_master.sh notify_backup /path_to/to_backup.sh notify_fault "/path/fault.sh VG_1" notify /path/to/notify.sh smtp_alert }
其中
group {
http
mysql
}
http和mysql是实例名和下面的实例名一致
notify_master /path/to/to_master.sh表示当切换到master状态时要执行的脚本 notify_backup /path_to/to_backup.sh表示当切换到backup状态时要执行的脚本 notify_fault "/path/fault.sh VG_1"
notify /path/to/notify.sh
smtp alter表示切换时给global defs中定义的邮件地址发送右键通知
2VRRP实例(instance)配置范例
vrrp_instance http { state MASTER interface eth0 dont_track_primary track_interface { eth0 eth1 } mcast_src_ip <IPADDR> garp_master_delay 10 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS autp_pass 1234 } virtual_ipaddress { #<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPT> label <LABEL> 192.168.200.17/24 dev eth1 192.168.200.18/24 dev eth2 label eth2:1 } virtual_routes { # src <IPADDR> [to] <IPADDR>/<MASK> via|gw <IPADDR> dev <STRING> scope <SCOPE> tab src 192.168.100.1 to 192.168.109.0/24 via 192.168.200.254 dev eth1 192.168.110.0/24 via 192.168.200.254 dev eth1 192.168.111.0/24 dev eth2 192.168.112.0/24 via 192.168.100.254 } nopreempt preemtp_delay 300 debug }
state state指定 instance(Initial)的初始状态就是说在配置好后这台 服务器的初始状态就是这里指定的但这里指定的不算还是得要通过竞选通过优先级来确定里如果这里设置为master但如若他的优先级不及另外一台 那么这台在发送通告时会发送自己的优先级另外一台发现优先级不如自己的高那么他会就回抢占为master
interface 实例绑定的网卡因为在配置虚拟IP的时候必须是在已有的网卡上添加的
dont track primary 忽略VRRP的interface错误
track interface 跟踪接口设置额外的监控里面任意一块网卡出现问题都会进入故障(FAULT)状态例如用nginx做均衡器的时候内网必须正常工作如果内网出问题了这个均衡器也就无法运作了所以必须对内外网同时做健康检查
mcast src ip 发送多播数据包时的源IP地址这里注意了这里实际上就是在那个地址上发送VRRP通告这个非常重要一定要选择稳定的网卡端口来发送这里相当于heartbeat的心跳端口如果没有设置那么就用默认的绑定的网卡的IP也就是interface指定的IP地址
garp master delay 在切换到master状态后延迟进行免费的ARP(gratuitous ARP)请求
virtual router id 这里设置VRID这里非常重要相同的VRID为一个组他将决定多播的MAC地址
priority 100 设置本节点的优先级优先级高的为master
advert int 检查间隔默认为1秒
virtual ipaddress 这里设置的就是VIP也就是虚拟IP地址他随着state的变化而增加删除当state为master的时候就添加当state为backup的时候删除这里主要是有优先级来决定的和state设置的值没有多大关系这里可以设置多个IP地址
virtual routes 原理和virtual ipaddress一样只不过这里是增加和删除路由
lvs sync daemon interface lvs syncd绑定的网卡
authentication 这里设置认证
auth type 认证方式可以是PASS或AH两种认证方式
auth pass 认证密码
nopreempt 设置不抢占这里只能设置在state为backup的节点上而且这个节点的优先级必须别另外的高
preempt delay 抢占延迟
debug debug级别
notify master 和sync group这里设置的含义一样可以单独设置例如不同的实例通知不同的管理人员http实例发给网站管理员mysql的就发邮件给DBA
3VRRP脚本
vrrp_script check_running { script "/usr/local/bin/check_running" interval 10 weight 10 } vrrp_instance http { state BACKUP smtp_alert interface eth0 virtual_router_id 101 priority 90 advert_int 3 authentication { auth_type PASS auth_pass whatever } virtual_ipaddress { 1.1.1.1 } track_script { check_running weight 20 } }
首先在vrrp_script区域定义脚本名字和脚本执行的间隔和脚本执行的优先级变更
vrrp_script check_running { script "/usr/local/bin/check_running" interval 10 #脚本执行间隔 weight 10 #脚本结果导致的优先级变更10表示优先级+10-10则表示优先级-10 }
然后在 实例 ( vrrp_instance )里面引用有点类似脚本里面的函数引用一样先定义后引用函数名
track_script { check_running weight 20 }
注意VRRP脚本(vrrp_script)和VRRP实例( vrrp_instance )属于同一个级别
LVS配置
如果你没有配置LVS+keepalived那么无需配置这段区域里如果你用的是nginx来代替LVS这无限配置这款这里的LVS配置是专门为keepalived+LVS集成准备的。
注意了这里LVS配置并不是指真的安装LVS然后用ipvsadm来配置他而是用keepalived的配置文件来代替ipvsadm来配置LVS这样会方便很多一个配置文件搞定这些维护方便配置方便是也
这里LVS配置也有两个配置
一个是虚拟主机组配置
一个是虚拟主机配置
1虚拟主机组配置文件详解
这个配置是可选的根据需求来配置吧这里配置主要是为了让一台realserver上的某个服务可以属于多个Virtual Server并且只做一次健康检查
virtual_server_group <STRING> {
# VIP port
<IPADDR> <PORT>
<IPADDR> <PORT>
fwmark <INT>
}
2虚拟主机配置
virtual server可以以下面三种的任意一种来配置
1. virtual server IP port
2. virtual server fwmark int
3. virtual server group string
下面以第一种比较常用的方式来配详细解说一下
virtual_server 192.168.1.2 80 { #设置一个virtual server: VIP:Vport
delay_loop 3 # service polling的delay时间即服务轮询的时间间隔
lb_algo rr|wrr|lc|wlc|lblc|sh|dh #LVS调度算法
lb_kind NAT|DR|TUN #LVS集群模式
persistence_timeout 120 #会话保持时间秒为单位即以用户在120秒内被分配到同一个后端realserver
persistence_granularity <NETMASK> #LVS会话保持粒度ipvsadm中的-M参数默认是0xffffffff即每个客户端都做会话保持
protocol TCP #健康检查用的是TCP还是UDP
ha_suspend #suspendhealthchecker’s activity
virtualhost <string> #HTTP_GET做健康检查时检查的web服务器的虚拟主机即host头
sorry_server <IPADDR> <PORT> #备用机就是当所有后端realserver节点都不可用时就用这里设置的也就是临时把所有的请求都发送到这里啦
real_server <IPADDR> <PORT> #后端真实节点主机的权重等设置主要后端有几台这里就要设置几个
{
weight 1 #给每台的权重0表示失效(不知给他转发请求知道他恢复正常)默认是1
inhibit_on_failure #表示在节点失败后把他权重设置成0而不是冲IPVS中删除
notify_up <STRING> | <QUOTED-STRING> #检查服务器正常(UP)后要执行的脚本
notify_down <STRING> | <QUOTED-STRING> #检查服务器失败(down)后要执行的脚本
HTTP_GET #健康检查方式
{
url { #要坚持的URL可以有多个
path / #具体路径
digest <STRING>
status_code 200 #返回状态码
}
connect_port 80 #监控检查的端口
bindto <IPADD> #健康检查的IP地址
connect_timeout 3 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 2 #重连间隔
} # END OF HTTP_GET|SSL_GET
#下面是常用的健康检查方式健康检查方式一共有HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK这些
#TCP方式
TCP_CHECK {
connect_port 80
bindto 192.168.1.1
connect_timeout 4
} # TCP_CHECK
# SMTP方式 这个可以用来给邮件服务器做集群
SMTP_CHECK
host {
connect_ip <IP ADDRESS>
connect_port <PORT> #默认检查25端口
14 KEEPALIVED
bindto <IP ADDRESS>
}
connect_timeout <INTEGER>
retry <INTEGER>
delay_before_retry <INTEGER>
# "smtp HELO"|・-ê§à"
helo_name <STRING>|<QUOTED-STRING>
} #SMTP_CHECK
#MISC方式 这个可以用来检查很多服务器只需要自己会些脚本即可
MISC_CHECK
{
misc_path <STRING>|<QUOTED-STRING> #外部程序或脚本
misc_timeout <INT> #脚本或程序执行超时时间
misc_dynamic #这个就很好用了可以非常精确的来调整权重是后端每天服务器的压力都能均衡调配这个主要是通过执行的程序或脚本返回的状态代码来动态调整 weight值使权重根据真实的后端压力来适当调整不过这需要有过硬的脚本功夫才行哦
#返回0健康检查没问题不修改权重
#返回1健康检查失败权重设置为0
#返回2-255健康检查没问题但是权重却要根据返回代码修改为 返回码-2 例如如果程序或脚本执行后返回的代码为200#那么权重这回被修改为 200-2
}
} # Realserver
} # Virtual Server
配置文件到此就讲完了下面是一份未加备注的完整配置文件
global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id node1 } notification_email { [email protected] [email protected] } static_ipaddress { 192.168.1.1/24 brd + dev eth0 scope global 192.168.1.2/24 brd + dev eth1 scope global } static_routes { src $SRC_IP to $DST_IP dev $SRC_DEVICE src $SRC_IP to $DST_IP via $GW dev $SRC_DEVICE } vrrp_sync_group VG_1 { group { http mysql } notify_master /path/to/to_master.sh notify_backup /path_to/to_backup.sh notify_fault "/path/fault.sh VG_1" notify /path/to/notify.sh smtp_alert } group { http mysql } vrrp_script check_running { script "/usr/local/bin/check_running" interval 10 weight 10 } vrrp_instance http { state MASTER interface eth0 dont_track_primary track_interface { eth0 eth1 } mcast_src_ip <IPADDR> garp_master_delay 10 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS autp_pass 1234 } virtual_ipaddress { #<IPADDR>/<MASK> brd <IPADDR> dev <STRING> scope <SCOPT> label <LABEL> 192.168.200.17/24 dev eth1 192.168.200.18/24 dev eth2 label eth2:1 } virtual_routes { # src <IPADDR> [to] <IPADDR>/<MASK> via|gw <IPADDR> dev <STRING> scope <SCOPE> tab src 192.168.100.1 to 192.168.109.0/24 via 192.168.200.254 dev eth1 192.168.110.0/24 via 192.168.200.254 dev eth1 192.168.111.0/24 dev eth2 192.168.112.0/24 via 192.168.100.254 } track_script { check_running weight 20 } nopreempt preemtp_delay 300 debug } virtual_server_group <STRING> { # VIP port <IPADDR> <PORT> <IPADDR> <PORT> fwmark <INT> } virtual_server 192.168.1.2 80 { delay_loop 3 lb_algo rr|wrr|lc|wlc|lblc|sh|dh lb_kind NAT|DR|TUN persistence_timeout 120 persistence_granularity <NETMASK> protocol TCP ha_suspend virtualhost <string> sorry_server <IPADDR> <PORT> real_server <IPADDR> <PORT> { weight 1 inhibit_on_failure notify_up <STRING> | <QUOTED-STRING> notify_down <STRING> | <QUOTED-STRING> #HTTP_GET方式 HTTP_GET | SSL_GET { url { path / digest <STRING> status_code 200 } connect_port 80 bindto <IPADD> connect_timeout 3 nb_get_retry 3 delay_before_retry 2 } } }
注意这里仅仅是罗列并不是可用的配置文件。里面需要根据自己的时间情况稍加配置才能用
1
2
4
3
5
6